did

In this tutorial, you will learn how to generate DIDs by generating randomized mnemonics and uploading the DID documents to the VDR via the interface.

Complete code example: zkid-sdk-example /src/did/createDID.ts

Tutorial

  1. Initialize @noble cryptographic library and wasm;

await initCrypto();
  1. Generate random mnemonics;

const mnemonic = mnemonicGenerate(12);
  1. Generate DID through mnemonics;

const keyring = new Keyring();
const did = keys.fromMnemonic(keyring, mnemonic, "ecdsa");

In the process of generating DIDs from mnemonic, the controller key and the key pairs of two different verification methods need to be managed by keyring structure.

  1. Upload DID Document to VDR;

const doc = await did.getPublish();
await registerDidDoc(doc);

// src/utils/didHelpers.ts
export async function registerDidDoc(
  didDoc: DidDocument,
  url = process.env.BASE_URL
) {
  const res = await axios.post(`${url}/did`, { didDocument: didDoc });

  if (res.data.code === 200) {
    console.log(`Success: DID Document Registerd (${didDoc.controller})`);
  } else {
    console.log(`ERROR: ${res.data.message}`);
  }
}

Uploading a DID document to the VDR requires two steps. First of all, you need to use your own controller key to sign the DID document, and the signature-related information will be appended to the back of the DID document structure, and then initiate a request to the RESTful API interface to send the structure to the server for storage and processing, which will be deposited into the database with Arweave.

Well, through the above steps, you will have mastered how to generate a DID and store it into VDR.

Last updated