Skip to main content

DID

This section covers creating a light DID using the account you created for the Claimer.

Since a light DID is not registered on the blockchain, you don't need funds to create one.

info

Remember, light DIDs can do the following:

  • Sign attestation requests and presentation with the authentication keys
  • Encrypt messages with the encryption keys

Read the DID documentation to learn more about DIDs and the difference between their light and full versions.

Generate Keysโ€‹

Like the Attester, the Claimer must also set up the DID keys.

claimer/generateKeypairs.ts
import * as Kilt from '@kiltprotocol/sdk-js'
import { mnemonicGenerate } from '@polkadot/util-crypto'

export function generateKeypairs(mnemonic = mnemonicGenerate()) {
const authentication = Kilt.Utils.Crypto.makeKeypairFromUri(mnemonic)

const keyAgreement = Kilt.Utils.Crypto.makeEncryptionKeypairFromSeed(
Kilt.Utils.Crypto.mnemonicToMiniSecret(mnemonic)
)

return {
authentication: authentication,
keyAgreement: keyAgreement
}
}

The code above is similar to the generateKeyAgreement function used in the Attester section but simpler, as the Claimer only needs an authentication key and an encryption key.

Both the keys are derived from the same seed, but they could also have two different seeds.

Generate Light DIDโ€‹

With the keypairs generated, you can create the light DID. Because it's off-chain you can create the DID object every time, but you still need to save the mnemonic to the .env file with a different variable name.

claimer/generateLightDid.ts
import { config as envConfig } from 'dotenv'

import { mnemonicGenerate } from '@polkadot/util-crypto'

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

import { generateKeypairs } from './generateKeypairs'

export function generateLightDid(mnemonic: string): Kilt.DidDocument {
const { authentication, keyAgreement } = generateKeypairs(mnemonic)
return Kilt.Did.createLightDidDocument({
authentication: [authentication as Kilt.NewLightDidVerificationKey],
keyAgreement: [keyAgreement]
})
}

// Don't execute if this is imported by another file.
if (require.main === module) {
;(async () => {
envConfig()

try {
await Kilt.init()

const mnemonic = mnemonicGenerate()
console.log('\nsave following to .env to continue\n')
console.log(`CLAIMER_DID_MNEMONIC="${mnemonic}"`)
} catch (e) {
console.log('Error while setting up claimer DID')
throw e
}
})()
}

The Claimer doesn't have an account, as the Claimer doesn't need to hold funds.

The generateKeypairs function takes the mnemonic value and generates the authentication and keyAgreement keys.

The createLightDidDocument method takes these two values and generates the light DID.

Runโ€‹

yarn ts-node ./claimer/generateLightDid.ts

Well done - You successfully generated a light DID!