DID
Time to make a light DID using the previously created account for the Claimer. Since a light DID is not registered on the blockchain, you don't need funds for creating one. Remember light DIDs can:
- Sign attestation requests and presentation with the authentication keys
- Encrypt messages with the encryption keys
Take a look at our DID documentation to learn more about DIDs and the difference between their light and full versions.
Generate Keysโ
Similar to the Attester, the Claimer must set up the DID keys.
- Typescript
- Javascript
import * as Kilt from '@kiltprotocol/sdk-js'
import {
blake2AsU8a,
keyExtractPath,
keyFromPath,
mnemonicGenerate,
mnemonicToMiniSecret,
sr25519PairFromSeed
} from '@polkadot/util-crypto'
import { generateAccount } from './generateAccount'
function generateKeyAgreement(mnemonic: string) {
const secretKeyPair = sr25519PairFromSeed(mnemonicToMiniSecret(mnemonic))
const { path } = keyExtractPath('//did//keyAgreement//0')
const { secretKey } = keyFromPath(secretKeyPair, path, 'sr25519')
return Kilt.Utils.Crypto.makeEncryptionKeypairFromSeed(blake2AsU8a(secretKey))
}
export function generateKeypairs(mnemonic = mnemonicGenerate()) {
const { account } = generateAccount(mnemonic)
const authentication = {
...account.derive('//did//0'),
type: 'sr25519'
} as Kilt.KiltKeyringPair
const keyAgreement = generateKeyAgreement(mnemonic)
return {
authentication: authentication,
keyAgreement: keyAgreement
}
}
import * as Kilt from '@kiltprotocol/sdk-js'
import {
blake2AsU8a,
keyExtractPath,
keyFromPath,
mnemonicGenerate,
mnemonicToMiniSecret,
sr25519PairFromSeed,
} from '@polkadot/util-crypto'
import { generateAccount } from './generateAccount'
function generateKeyAgreement(mnemonic) {
const secretKeyPair = sr25519PairFromSeed(mnemonicToMiniSecret(mnemonic))
const { path } = keyExtractPath('//did//keyAgreement//0')
const { secretKey } = keyFromPath(secretKeyPair, path, 'sr25519')
return Kilt.Utils.Crypto.makeEncryptionKeypairFromSeed(blake2AsU8a(secretKey))
}
export function generateKeypairs(mnemonic = mnemonicGenerate()) {
const { account } = generateAccount(mnemonic)
const authentication = {
...account.derive('//did//0'),
type: 'sr25519',
}
const keyAgreement = generateKeyAgreement(mnemonic)
return {
authentication: authentication,
keyAgreement: keyAgreement,
}
}
The Claimer only needs an authentication key and an encryption key. Here the keys are both derived from the same seed, but they could also have two different seeds.
Generate Light DIDโ
Once our keypairs
are generated we can create our light DID.
Because it's off-chain we can just create the DID object every time, we don't need to resolve them before using it.
But we'll still accept the mnemonic
and prompt to save it in .env
for our reference.
- Typescript
- Javascript
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
}
})()
}
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) {
const { authentication, keyAgreement } = generateKeypairs(mnemonic)
return Kilt.Did.createLightDidDocument({
authentication: [authentication],
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
}
})()
}
After everything is initialized, we create a mnemonic that will be used to create the light DID.
As you may have noticed the Claimer doesn't have an account
.
The Claimer doesn't need to hold funds and also doesn't need a blockchain account.
Runโ
- Typescript
- Javascript
yarn ts-node ./claimer/generateLightDid.ts
node ./claimer/generateLightDid.js
Your output will provide you with CLAIMER_DID_MNEMONIC
.
Be sure to save it in your .env
file, it should now look similar to this.
WSS_ADDRESS=wss://peregrine.kilt.io
ATTESTER_ACCOUNT_MNEMONIC="warrior icon use cry...
ATTESTER_ACCOUNT_ADDRESS=4ohMvUHsyeDhMVZF...
ATTESTER_DID_MNEMONIC="beyond large galaxy...
CLAIMER_DID_MNEMONIC="danger awkward wrestle snap...
Well done - You've successfully generated a light DID!