VC.js

vc.js is a Javascript library that implements W3C standards for verifiable data

Install

The vc.js library can be installed with the following command

npm i @transmute/vc.js --save

Verifiable Credentials

The spec for the Verifiable Credential Data model can be found here: https://www.w3.org/TR/vc-data-model/. vc.js supports issuing and verifying credentials. Code examples can be found in the following sections.

Issue a Credential

The act of issuing a credential is creating a proof attribute that gets added to the credential as an attribute. The proof contains a signature from the provided key which can then later be verified.

The following code snippet provides an example of how to issue a credential.

import {
  JsonWebKey,
  JsonWebSignature,
  JsonWebKey2020,
} from "@transmute/json-web-signature";
import { documentLoader } from "path/somewhere";
import { verifiable } from "@transmute/vc.js";

const key = {
  id: "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr#z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
  type: "JsonWebKey2020",
  controller: "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
  publicKeyJwk: {
    kty: "OKP",
    crv: "Ed25519",
    x: "ijtvFnowiumYMcYVbaz6p64Oz6bXwe2V_9IlCgDR_38",
  },
  privateKeyJwk: {
    kty: "OKP",
    crv: "Ed25519",
    x: "ijtvFnowiumYMcYVbaz6p64Oz6bXwe2V_9IlCgDR_38",
    d: "ZrHpIW1JBb-sK2-wzKV0mQjbxpnxjUCu151QZ9_F_Vs",
  },
};

const credential = {
  "@context": [
    "https://www.w3.org/2018/credentials/v1",
    "https://w3id.org/security/suites/jws-2020/v1",
  ],
  id: "http://example.edu/credentials/3732",
  type: ["VerifiableCredential"],
  issuer: {
    id: "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
  },
  issuanceDate: "2010-01-01T19:23:24Z",
  credentialSubject: {
    id: "did:example:ebfeb1f712ebc6f1c276e12ec21",
  },
};

const result = await verifiable.credential.create({
  credential,
  format: ["vc", "vc-jwt"],
  documentLoader: documentLoader,
  suite: new JsonWebSignature({
    key: await JsonWebKey.from(key as JsonWebKey2020),
  }),
});

Verify a Credential

A credential can be verified by checking the hash of the credential with the public key component of the private key used to sign the credential. The following snippet provides the code for how to do this.

import {
  JsonWebKey,
  JsonWebSignature,
  JsonWebKey2020,
} from "@transmute/json-web-signature";
import { documentLoader } from "path/somewhere";
import { verifiable } from "@transmute/vc.js";
import { credential } from "path/somewhere";

const result = await verifiable.credential.verify({
  credential,
  format: ["vc", "vc-jwt"],
  documentLoader: documentLoader,
  suite: [new JsonWebSignature()],
});

Verifiable Presentation

A verifiable presentation is a way to exchange verifiable credentials from a holder to a verifier over an API.

Spec: https://www.w3.org/TR/vc-data-model/#presentations

More information with respect to verifiable presentations can be found with their exchange specs.

Issue a Verifiable Presentation

A verifiable presentation can be signed by the holder. The following is a code snippet for how to sign a verifiable presentations.

import {
  JsonWebKey,
  JsonWebSignature,
  JsonWebKey2020,
} from "@transmute/json-web-signature";
import { documentLoader } from "path/somewhere";
import { key } from "path/somewhere";
import { verifiable } from "@transmute/vc.js";

const presentation = {
  "@context": [
    "https://www.w3.org/2018/credentials/v1",
    "https://w3id.org/security/suites/jws-2020/v1",
  ],
  type: ["VerifiablePresentation"],
  holder: {
    id: key.controller,
  },
};

const result = await verifiable.presentation.create({
  presentation,
  format: ["vp", "vp-jwt"],
  documentLoader: documentLoader,
  challenge: "123", // this is supplied by the verifier / presentation recipient
  suite: new JsonWebSignature({
    key: await JsonWebKey.from(fixtures.key as JsonWebKey2020),
  }),
});

Verify a presentation

The following code snippet provides how to

import {
  JsonWebKey,
  JsonWebSignature,
  JsonWebKey2020,
} from "@transmute/json-web-signature";
import { documentLoader } from "path/somewhere";
import { verifiable } from "@transmute/vc.js";
import { presentation } from "path/somewhere";

const result = await verifiable.presentation.verify({
  presentation,
  format: ["vp", "vp-jwt"],
  documentLoader: documentLoader,
  challenge: "123", // this is supplied by the verifier / presentation recipient
  suite: new JsonWebSignature(),
});

Last updated