Skip to main content
Version: v0.5.x

Cheqd Did Module

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

info

This section assumes that

  1. You have set-up your develoment environment.
  2. 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

  1. method*: cheqd
  2. secret
  3. options*
  4. 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

  1. did*
  2. didDocument*: The updated DID Document
  3. options
  4. 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

  1. did*
  2. 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

  1. did*
  2. name*
  3. resourceType*
  4. id*
  5. data* : Text, Base64 string or an object
  6. alsoKnownAs
  7. 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 and resourceType

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:

  1. Ed25519VerificationKey2020
  2. Ed25519VerificationKey2018
  3. JsonWebKey2020
verificationMethod.privateKey

Type: string


options.methodSpecificIdAlgo

Specifies what type of method specific identifier is needed for your DID

Type: string

Default: uuid

Members:

  1. uuid
  2. base58btc

options.network*

Specifies the cheqd network name to be published

Type: string

Default: testnet

Members:

  1. testnet
  2. mainnet

options.versionId

Specifies the version of the DID Document to be published

Type: string