toip.tools

Guide ยท @toip-tools/trust-core

Create a did:key and sign a JSON document in TypeScript

Generate an Ed25519 did:key, sign a JSON document with a Data Integrity proof, and verify it from the DID alone.

A did:key is a decentralized identifier that is its public key, written out. There is nothing to register and nothing to look up: anyone holding the DID can check your signatures. That makes it the quickest way to give a person, device or service a verifiable identity.

This program creates one from 32 random bytes, signs a small JSON document, and verifies the signature. It then changes one field to show that verification fails.

Before you start

You need Node 22.13 or later. In a new folder, install the package and tsx, a TypeScript runner:

ShellTerminal
mkdir did-key-sign-document && cd did-key-sign-document
npm init -y && npm pkg set type=module
npm install @toip-tools/trust-core tsx

The code

Save this as did-key-sign-document.ts. It is the complete program; the site's checks run it on every build.

TypeScriptdid-key-sign-document.ts
// Create a did:key identity, sign a JSON document with it, and verify the signature from the DID alone.
import { SystemClock, identityFromSeed, resolveDidKey, signDocument, verifyDocument } from "@toip-tools/trust-core";

const clock = new SystemClock();

// A did:key identity is 32 random bytes. Keep the seed secret: whoever holds it can sign as you.
const seed = crypto.getRandomValues(new Uint8Array(32));
const me = identityFromSeed(seed);
console.log("DID:", me.did);

// The DID encodes the Ed25519 public key, so anyone can recover it without a registry or network call.
const { signingKey } = resolveDidKey(me.did);
console.log("Public key from the DID:", Buffer.from(signingKey).toString("hex"));

// signDocument adds a W3C Data Integrity proof (eddsa-jcs-2022). The document's issuer must be the signer's DID.
interface Receipt { issuer: string; kind: string; item: string; cents: number; proof?: { type: string; cryptosuite: string } }
const receipt = signDocument<Receipt>({ issuer: me.did, kind: "receipt", item: "bike tune-up", cents: 4500 }, me, clock);
console.log("Proof:", receipt.proof?.type, receipt.proof?.cryptosuite);

// Anyone can verify it: the proof names the key, and the key is in the DID.
const check = await verifyDocument(receipt, clock);
console.log("Valid:", check.valid);

// Change one field and the signature no longer matches.
const altered = await verifyDocument({ ...receipt, cents: 450 }, clock);
console.log("Valid after changing the price:", altered.valid, `(${altered.failure})`);

Run it:

ShellTerminal
npx tsx did-key-sign-document.ts
OutputRecorded output
DID: did:key:z6MkoMDtsgSyN7GBcCQN5UmnNDe3tP57hKu3B9m4J8m7TsSh
Public key from the DID: 842d44221b776a19de0d74b4e41d6aa69a7753f16c3f53c89037ab0f3c724e0a
Proof: DataIntegrityProof eddsa-jcs-2022
Valid: true
Valid after changing the price: false (signatureInvalid)

Keys are random, so identifiers and sizes differ on each run. From a clone of the repository, run npx tsx --tsconfig site/tsconfig.json site/examples/guides/did-key-sign-document.ts, or read it in site/examples/guides/did-key-sign-document.ts.

How it works

  • identityFromSeed turns a 32-byte seed into an Ed25519 key pair, its did:key identifier, and a derived X25519 key for encryption. The seed is the secret: store it as you would a password hash's key, not in source code.
  • resolveDidKey decodes the public key from the DID itself, with no network access. This is what a verifier does.
  • signDocument adds a W3C Data Integrity proof with the eddsa-jcs-2022 cryptosuite: the document is canonicalized with JCS (RFC 8785) and signed with Ed25519. The document's issuer must be the signer's DID; the proof's verificationMethod points at the key inside that DID.
  • verifyDocument recomputes the canonical form and checks the signature, allowing five minutes of clock skew on the proof's creation time. It returns { valid, failure } rather than throwing.

Limits and production notes

  • A did:key can't rotate its key. If the seed leaks, you need a new identifier. For identifiers that outlive their keys, see the KERI audit log guide.
  • Only Ed25519 did:key identifiers are supported. There is no resolver for other DID methods.
  • seedFrom(label) hashes a label into a seed. It is for repeatable demos and tests: a guessable label gives a guessable key.