Skip to main content
Version: v0.5.x

Hedera Did Module

In this tutorial we will see how to use the Hedera modules in detail.

info

This section assumes that

  1. You have set-up your develoment environment.
  2. You have setup the Hedera module setup hedera

DID Module

The Hedera DID module facilitates the Create, Read, Update, and Delete (CRUD) operations for did:hedera identifiers. To learn more about did:hedera, please refer to the specification

Create DID

The DID can be created in two different ways

Parameters

{
method: 'hedera'
options?: {
network?: HederaNetwork | string
}
secret?: {
rootKeyId?: string
keys?: DidDocumentKey[]
},
didDocument?: DidDocument
}
  1. method: hedera - (required) DidMethod identifier
  2. options - Target network for did document creation. Can be optional if used the single Hedera network, is required if plugin configured for using several networks. Can be equal "mainnet", "testnet", "previewnet", "local-node" or custom network name, defined when the Hedera module was initialized.
  3. secret - (optional) A KMS keyset for using. Keys are required for signing Hedera transaction.
  4. didDocument - (optional) Did document for initializing
Option 1

Provide a DID Document payload according to the w3c did core specification in the request body. This is possible when the keys corresponding to the verification methods provided in the DID Document are already created in the wallet

// Create a key pair
const key = await agent.wallet.createKey({
keyType: KeyType.Ed25519,
})
// Create a DID
await agent.dids.create<HederaDidCreateOptions>({
method: 'hedera',
options: { network: 'testnet' },
didDocument: new DidDocument({
id: 'did:hedera:testnet:44eesExqdsUvLZ35FpnBPErqRGRnYbzzyG3wgCCYxkmq_0.0.6226170',
verificationMethod: [
{
id: '#key-1',
type: 'Ed25519VerificationKey2020',
controller: 'did:hedera:testnet:44eesExqdsUvLZ35FpnBPErqRGRnYbzzyG3wgCCYxkmq_0.0.6226170',
publicKeyMultibase: key.fingerprint,
},
],
}),
})
Option 2

If a DID Document is not passed to the registrar, it requires the secret parameter with a verificationMethod to construct the DID Document.

await agent.dids.create<HederaDidCreateOptions>({
method: 'hedera',
options: { network: 'testnet' },
})

Update DID

To update a DID Document, fetch the body of the DID Document you want to change from the DID Resolver, make the relevant updates and pass it as the parameter

Parameters

{
did: string
secret: {
keys: DidDocumentKey[]
}
didDocumentOperation: DidDocumentOperation
didDocument: DidDocument | Partial<DidDocument>
}
  1. did - (required) Did identifier. Target Hedera network will be defined by did automatically.
  2. secret - (required) A KMS keyset for using. Keys are required for signing Hedera transaction.
  3. didDocumentOperation - Update did document action. Can be equal 'setDidDocument', 'addToDidDocument' or 'removeFromDidDocument'
  4. didDocument - (required) Did document properties for applying to the changing document

Example

// Updates DID Document with adding a service
const didUpdateResult1 = await agent.dids.update({
did: 'did:hedera:testnet:44eesExqdsUvLZ35FpnBPErqRGRnYbzzyG3wgCCYxkmq_0.0.6226170',
didDocumentOperation: 'addToDidDocument',
didDocument: {
service: [
new DidDocumentService({
id: '#service-1',
type: 'rand',
serviceEndpoint: 'https://rand.in',
}),
],
},
})
// Updates DID Document with removing a service
const didUpdateResult2 = await agent.dids.update({
did: 'did:hedera:testnet:44eesExqdsUvLZ35FpnBPErqRGRnYbzzyG3wgCCYxkmq_0.0.6226170',
didDocumentOperation: 'removeFromDidDocument',
didDocument: {
service: [
new DidDocumentService({
id: '#service-1',
type: 'rand',
serviceEndpoint: 'https://rand.in',
}),
],
},
})

Deactivate DID

A DID can be deactivated, it can still be resolved

Parameters

{
did: string
}
  1. did - (required) Did identifier. Target Hedera network will be defined by did automatically.

Example

const didDectivateResult = await agent.dids.deactivate({
did: 'did:hedera:testnet:44eesExqdsUvLZ35FpnBPErqRGRnYbzzyG3wgCCYxkmq_0.0.6226170',
})