Cheqd Did Module
In this tutorial we will see how to use the cheqd modules in detail
This section assumes that
- You have set-up your develoment environment.
- You have setup the cheqd module setup cheqd
DID Module
The cheqd DID module facilitates the Create, Read, Update, and Delete (CRUD) operations for did:cheqd identifiers. To learn more about "did:cheqd," please refer to the specification
Create DID
The DID can be created in two different ways
Parameters
method
*:cheqd
secret
options
*didDocument
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,
})
// encode public key according to the verification method
const ed25519PublicKeyBase58 = key.publicKeyBase58
// Create a DID
await agent.dids.create<CheqdDidCreateOptions>({
method: 'cheqd',
secret: {},
options: {},
didDocument: new DidDocument({
id: 'did:cheqd:testnet:92874297-d824-40ea-8ae5-364a1ec9237d',
controller: ['did:cheqd:testnet:92874297-d824-40ea-8ae5-364a1ec9237d'],
verificationMethod: [
{
id: 'did:cheqd:testnet:92874297-d824-40ea-8ae5-364a1ec9237d#key-1',
type: 'Ed25519VerificationKey2018',
controller: 'did:cheqd:testnet:92874297-d824-40ea-8ae5-364a1ec9237d',
publicKeyBase58: ed25519PublicKeyBase58,
},
],
authentication: ['did:cheqd:testnet:92874297-d824-40ea-8ae5-364a1ec9237d#key-1'],
}),
})
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({
method: 'cheqd',
// the secret contains a the verification method type and id
secret: {
verificationMethod: {
id: 'key-1',
type: 'Ed25519VerificationKey2020',
},
},
// an optional methodSpecificIdAlgo parameter
options: {
network: 'testnet',
methodSpecificIdAlgo: 'uuid',
},
})
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
*didDocument
*: The updated DID Documentoptions
secret
await agent.dids.update({
did: 'did:cheqd:testnet:b84817b8-43ee-4483-98c5-f03760816411',
// Updates DID Document with an additional verification method if provided
secret: {
verificationMethod: {
id: 'key-2',
type: 'JsonWebKey2020',
},
},
didDocument: {
id: 'did:cheqd:testnet:b84817b8-43ee-4483-98c5-f03760816411',
controller: ['did:cheqd:testnet:b84817b8-43ee-4483-98c5-f03760816411'],
verificationMethod: [
{
id: 'did:cheqd:testnet:b84817b8-43ee-4483-98c5-f03760816411#key-1',
type: 'Ed25519VerificationKey2020',
controller: 'did:cheqd:testnet:b84817b8-43ee-4483-98c5-f03760816411',
publicKeyMultibase: 'z6MknkzLUEP5cxqqsaysNMWoh8NJRb3YsowTCj2D6yhwyEdj',
},
],
authentication: ['did:cheqd:testnet:b84817b8-43ee-4483-98c5-f03760816411#key-1'],
// updates did document with a service block
service: [
new DidDocumentService({
id: 'did:cheqd:testnet:b84817b8-43ee-4483-98c5-f03760816411#rand',
type: 'rand',
serviceEndpoint: 'https://rand.in',
}),
],
},
})
Deactivate DID
A DID can be deactivated, it can still be resolved
Parameters
did
*options
await agent.dids.deactivate({
did: 'did:cheqd:testnet:b84817b8-43ee-4483-98c5-f03760816411',
// an optional versionId parameter
options: {
versionId: '3.0',
},
})
Create Resource
Using the createResource cheqd api agents are able to create custom DID-Linked Resources, including:
- Trust Registries
- Status Lists
- Logos associated with DIDs
- Governance files
Parameters
did
*name
*resourceType
*id
*data
* : Text, Base64 string or an objectalsoKnownAs
version
await agent.modules.cheqd.createResource('did:cheqd:testnet:92874297-d824-40ea-8ae5-364a1ec9237d', {
name: 'resourceName',
resourceType: 'resourceType',
id: '6de33634-6439-4e46-aa3f-bfe03606b000',
version: '1.0',
data: {
name: 'name',
age: 18,
},
})
Resolve Resource
The resolveResource function in the cheqd API allows agents to resolve DID-Linked Resources in two ways:
- By
resourceId
- By
resourceName
andresourceType
Additionally, both conventions support the following query parameters:
- To fetch a specific resource version:
resourceUrl?version=<versionId>
- To fetch the closest resource version for a given time:
resourceUrl?resourceVersionTime=<epoch time>
Using resourceId
await agent.modules.cheqd.resolveResource(
'did:cheqd:testnet:92874297-d824-40ea-8ae5-364a1ec9237d/resources/6de33634-6439-4e46-aa3f-bfe03606b000'
)
Using resourceName
and resourceType
await agent.modules.cheqd.resolveResource(
'did:cheqd:testnet:92874297-d824-40ea-8ae5-364a1ec9237d?resourceName=resourceName&resourceType=resourceType'
)
Types
secret.verificationMethod
Type: verificationMethod
verificationMethod.id
*
Type: string
verificationMethod.type
*
Type: string
Default: Ed25519VerificationKey2020
Members:
Ed25519VerificationKey2020
Ed25519VerificationKey2018
JsonWebKey2020
verificationMethod.privateKey
Type: string
options.methodSpecificIdAlgo
Specifies what type of method specific identifier is needed for your DID
Type: string
Default: uuid
Members:
uuid
base58btc
options.network
*
Specifies the cheqd network name to be published
Type: string
Default: testnet
Members:
testnet
mainnet
options.versionId
Specifies the version of the DID Document to be published
Type: string