Skip to main content

Create a Full DID

The following is an example of how to create and write on the blockchain a full DID that specifies only an authentication key.

import * as Kilt from '@kiltprotocol/sdk-js'

export async function createSimpleFullDid(
submitterAccount: Kilt.KiltKeyringPair,
{
authentication
}: {
authentication: Kilt.NewDidVerificationKey
},
signCallback: Kilt.Did.GetStoreTxSignCallback
): Promise<Kilt.DidDocument> {
const api = Kilt.ConfigService.get('api')

// Generate the DID-signed creation tx and submit it to the blockchain with the specified account.
// The submitter account parameter, ensures that only an entity authorized by the DID subject
// can submit the tx to the KILT blockchain.
const fullDidCreationTx = await Kilt.Did.getStoreTx(
{
authentication: [authentication]
},
submitterAccount.address,
signCallback
)

await Kilt.Blockchain.signAndSubmitTx(fullDidCreationTx, submitterAccount)

// The new information is fetched from the blockchain and returned.
const fullDid = Kilt.Did.getFullDidUriFromKey(authentication)
const encodedUpdatedDidDetails = await api.call.did.query(
Kilt.Did.toChain(fullDid)
)
return Kilt.Did.linkedInfoFromChain(encodedUpdatedDidDetails).document
}

If additional keys or services are to be specified, they can be passed as parameters to the creation transaction.

import * as Kilt from '@kiltprotocol/sdk-js'

export async function createCompleteFullDid(
submitterAccount: Kilt.KiltKeyringPair,
{
authentication,
keyAgreement,
assertionMethod,
capabilityDelegation
}: {
authentication: Kilt.NewDidVerificationKey
keyAgreement: Kilt.NewDidEncryptionKey
assertionMethod: Kilt.NewDidVerificationKey
capabilityDelegation: Kilt.NewDidVerificationKey
},
signCallback: Kilt.SignExtrinsicCallback
): Promise<Kilt.DidDocument> {
const api = Kilt.ConfigService.get('api')

const fullDidCreationTx = await Kilt.Did.getStoreTx(
{
authentication: [authentication],
keyAgreement: [keyAgreement],
assertionMethod: [assertionMethod],
capabilityDelegation: [capabilityDelegation],
// Example service.
service: [
{
id: '#my-service',
type: ['service-type'],
serviceEndpoint: ['https://www.example.com']
}
]
},
submitterAccount.address,
signCallback
)

await Kilt.Blockchain.signAndSubmitTx(fullDidCreationTx, submitterAccount)

// The new information is fetched from the blockchain and returned.
const fullDid = Kilt.Did.getFullDidUriFromKey(authentication)
const encodedUpdatedDidDetails = await api.call.did.query(
Kilt.Did.toChain(fullDid)
)
return Kilt.Did.linkedInfoFromChain(encodedUpdatedDidDetails).document
}

Upgrade a Light DID to a Full DID

Another way to obtain a full DID is by upgrading a previously-created light DID. KILT supports this operation in a way that does not invalidate any credentials that had been issued to the light DID before being upgraded.

The following code shows how to migrate a light DID to a full DID. Credentials, presentations, and verifications remain unchanged and remain valid.

import * as Kilt from '@kiltprotocol/sdk-js'

export async function migrateLightDid(
lightDid: Kilt.DidDocument,
submitterAccount: Kilt.KiltKeyringPair,
signCallback: Kilt.SignExtrinsicCallback
): Promise<Kilt.DidDocument> {
const api = Kilt.ConfigService.get('api')

// Generate the DID migration tx.
const migrationTx = await Kilt.Did.getStoreTx(
lightDid,
submitterAccount.address,
signCallback
)

// The tx can then be submitted by the authorized account as usual.
await Kilt.Blockchain.signAndSubmitTx(migrationTx, submitterAccount)

// The new information is fetched from the blockchain and returned.
const migratedFullDidUri = Kilt.Did.getFullDidUri(lightDid.uri)
const encodedUpdatedDidDetails = await api.call.did.query(
Kilt.Did.toChain(migratedFullDidUri)
)
return Kilt.Did.linkedInfoFromChain(encodedUpdatedDidDetails).document
}