Create DID

Using the walt.id did library you can create & register DIDs based on multiple methods.

Creating a key-based DID (did:key)

import id.walt.did.dids.DidService
import id.walt.did.dids.registrar.dids.DidKeyCreateOptions

suspend fun main() {
    DidService.minimalInit()

    val didKey = DidService.register(DidKeyCreateOptions())
    println("DID Key: " + didKey.did)
}

Creating a Web-based DID (did:web)

Web-based DIDs require a domain that you control.

import id.walt.did.dids.DidService
import id.walt.did.dids.registrar.dids.DidWebCreateOptions

suspend fun main() {
    DidService.minimalInit()

    val didWeb = DidService.register(DidWebCreateOptions("yourdomain.com", "directory/subdirectory"))
    println("DID Web: " + didWeb.did)
}

Creating a JWK-based DID (did:jwk)

import id.walt.did.dids.DidService
import id.walt.did.dids.registrar.dids.DidJwkCreateOptions

suspend fun main() {
    DidService.minimalInit()

    val didJwk = DidService.register(DidJwkCreateOptions())
    println("DID JWK: " + didJwk.did)
}

In the examples above, DidService.register() method is called with the intended DID method specified in DidCreateOptions. The method returns a DidResult that contains the did (the unique identifier) and didDocument (the public information related to the DID).

For did:web, make sure you own the domain and it is configured to serve the DID Document at the appropriate URI.

In did:jwk and did:key methods, cryptographic keys are automatically generated as part of the DID creation. To have more control over the keys used, consider using DidService.registerByKey() and provide your own key.

Example

import id.walt.crypto.keys.LocalKey
import id.walt.crypto.keys.LocalKeyMetadata
import id.walt.crypto.keys.KeyType
import id.walt.did.dids.*
import id.walt.did.dids.DidService

suspend fun main() {
    DidService.minimalInit()

    // Generate a local key with RSA algorithm
    val localKey = LocalKey.generate(KeyType.RSA, LocalKeyMetadata())

    // Create a did:key
    val didKeyOptions = DidCreateOptions("key")
    val didKey = DidService.registerByKey(localKey, didKeyOptions)
    println("DID Key: " + didKey.did)
}