Hedera Did Module
In this tutorial we will see how to use the Hedera modules in detail.
This section assumes that
- You have set-up your develoment environment.
- 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
}
method:hedera- (required) DidMethod identifieroptions- 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.secret- (optional) A KMS keyset for using. Keys are required for signing Hedera transaction.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>
}
did- (required) Did identifier. Target Hedera network will be defined bydidautomatically.secret- (required) A KMS keyset for using. Keys are required for signing Hedera transaction.didDocumentOperation- Update did document action. Can be equal 'setDidDocument', 'addToDidDocument' or 'removeFromDidDocument'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
}
did- (required) Did identifier. Target Hedera network will be defined bydidautomatically.
Example
const didDectivateResult = await agent.dids.deactivate({
did: 'did:hedera:testnet:44eesExqdsUvLZ35FpnBPErqRGRnYbzzyG3wgCCYxkmq_0.0.6226170',
})