toip.tools

Guide · @toip-tools/trust-core

Issue and verify a W3C verifiable credential in TypeScript

Issue a credential in the W3C VC 2.0 shape with a Data Integrity proof, then check its proof, issuer, subject and validity window.

A verifiable credential is a signed statement by an issuer about a subject: "this DID holds a BSc from this university". The holder keeps it and shows it when asked; the verifier checks it without calling the issuer.

This program issues a degree credential from one did:key to another, prints it, and runs four presentations through a verifier: the real one, the same credential presented by someone else, a copy with the degree changed, and one the graduate signed themselves.

Before you start

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

ShellTerminal
mkdir issue-verify-credential && cd issue-verify-credential
npm init -y && npm pkg set type=module
npm install @toip-tools/trust-core tsx

The code

Save this as issue-verify-credential.ts. It is the complete program; the site's checks run it on every build.

TypeScriptissue-verify-credential.ts
// Issue a W3C verifiable credential (VC Data Model 2.0 shape) and verify it: proof, issuer, subject and validity.
import { SystemClock, identityFromSeed, issueCredential, verifyCredential, type Credential } from "@toip-tools/trust-core";

const clock = new SystemClock();
const random = () => crypto.getRandomValues(new Uint8Array(32));
const university = identityFromSeed(random());
const graduate = identityFromSeed(random());

// The issuer signs a claim about a subject. issueCredential sets @context, id, issuer and validFrom.
const diploma = issueCredential(university, clock, {
  type: ["VerifiableCredential", "DegreeCredential"],
  credentialSubject: { id: graduate.did, degree: "BSc Computer Science", year: 2026 },
  validUntil: new Date(clock.now() + 5 * 365 * 86_400_000).toISOString(),
});
console.log(JSON.stringify({ ...diploma, proof: { ...diploma.proof, proofValue: "…" } }, null, 2));

// A verifier checks the proof and validity window, then decides whether it trusts this issuer for this claim.
const trustedIssuers = new Set([university.did]);
async function accept(vc: Credential, holder: string): Promise<string> {
  const check = await verifyCredential(vc, clock);
  if (!check.ok) return `rejected: ${check.failure}`;
  if (!trustedIssuers.has(vc.issuer)) return "rejected: unknown issuer";
  if (vc.credentialSubject.id !== holder) return "rejected: credential is about someone else";
  return `accepted: ${vc.credentialSubject.degree}`;
}

console.log("\nThe graduate presents it:", await accept(diploma, graduate.did));
console.log("Someone else presents it:", await accept(diploma, identityFromSeed(random()).did));
const inflated = { ...diploma, credentialSubject: { ...diploma.credentialSubject, degree: "PhD Computer Science" } };
console.log("With the degree changed:", await accept(inflated, graduate.did));
const selfIssued = issueCredential(graduate, clock, { type: ["VerifiableCredential", "DegreeCredential"], credentialSubject: { id: graduate.did, degree: "PhD" } });
console.log("Signed by the graduate:", await accept(selfIssued, graduate.did));

Run it:

ShellTerminal
npx tsx issue-verify-credential.ts
OutputRecorded output
{
  "@context": [
    "https://www.w3.org/ns/credentials/v2"
  ],
  "id": "urn:uuid:94282ecf-f355-42be-9290-31d4689f0f12",
  "type": [
    "VerifiableCredential",
    "DegreeCredential"
  ],
  "issuer": "did:key:z6MkmXDyCnbW5QgPYp4TVrGG7NVCVi3Zz951rZ83fS7CRMpb",
  "validFrom": "2026-11-07T09:56:37.441Z",
  "validUntil": "2031-11-06T09:56:37.439Z",
  "credentialSubject": {
    "id": "did:key:z6MkqUzDpEdVRhm2YLwZ3kwEWUWLGMyV73fWA6py1yuVWcx6",
    "degree": "BSc Computer Science",
    "year": 2026
  },
  "proof": {
    "type": "DataIntegrityProof",
    "cryptosuite": "eddsa-jcs-2022",
    "created": "2026-11-07T09:56:37.441Z",
    "verificationMethod": "did:key:z6MkmXDyCnbW5QgPYp4TVrGG7NVCVi3Zz951rZ83fS7CRMpb#z6MkmXDyCnbW5QgPYp4TVrGG7NVCVi3Zz951rZ83fS7CRMpb",
    "proofPurpose": "assertionMethod",
    "proofValue": "…"
  }
}

The graduate presents it: accepted: BSc Computer Science
Someone else presents it: rejected: credential is about someone else
With the degree changed: rejected: signatureInvalid
Signed by the graduate: rejected: unknown issuer

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/issue-verify-credential.ts, or read it in site/examples/guides/issue-verify-credential.ts.

How it works

  • issueCredential fills in @context (https://www.w3.org/ns/credentials/v2), a urn:uuid id, the issuer's DID and validFrom, then signs the whole credential with an eddsa-jcs-2022 Data Integrity proof.
  • verifyCredential checks the proof, that the proof's key belongs to the issuer, and the validFrom/validUntil window. It returns { ok: true } or { ok: false, failure }, where failure is one of signatureInvalid, issuerMismatch, malformedProof, notYetValid or expired.
  • A valid signature only says who made the claim. Deciding whether that issuer is an authority for it is your policy: here, a set of trusted issuer DIDs.
  • Comparing credentialSubject.id with the holder only means something if the holder proved control of that DID. In practice, compare it with the authenticated sender of the message that carried the credential, as the quickstart's step 4 does over TSP.

Limits and production notes

  • Credentials have one issuer and one proof. JSON-LD contexts are not processed or validated, and other proof formats (VC-JWT, SD-JWT, BBS) are not supported.
  • Revocation is a separate check: see Revoke a credential with a status list.