Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

did-jwt-vc

Create and verify W3C Verifiable Credentials and Presentations in JWT format

readme

npm npm codecov

did-jwt-vc

Create and verify W3C Verifiable Credentials and Presentations in JWT format

Installation

npm install did-jwt-vc

Usage

Creating JWTs

Prerequisites

Create an Issuer object to sign JWTs using, for example ethr-did

import { EthrDID } from 'ethr-did'
import { Issuer } from 'did-jwt-vc'

const issuer = new EthrDID({
  identifier: '0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198',
  privateKey: 'd8b595680851765f38ea5405129244ba3cbad84467d190859f4c8b20c1ff6c75'
}) as Issuer

The Issuer object must contain a did attribute, an alg property that is used in the JWT header and a signer function to generate the signature.

Creating a Verifiable Credential

Specify a payload matching the CredentialPayload or JwtCredentialPayload interfaces. Create a JWT by signing it with the previously configured issuer using the createVerifiableCredentialJwt function:

import { JwtCredentialPayload, createVerifiableCredentialJwt } from 'did-jwt-vc'

const vcPayload: JwtCredentialPayload = {
  sub: 'did:ethr:0x435df3eda57154cf8cf7926079881f2912f54db4',
  nbf: 1562950282,
  vc: {
    '@context': ['https://www.w3.org/2018/credentials/v1'],
    type: ['VerifiableCredential'],
    credentialSubject: {
      degree: {
        type: 'BachelorDegree',
        name: 'Baccalauréat en musiques numériques'
      }
    }
  }
}

const vcJwt = await createVerifiableCredentialJwt(vcPayload, issuer)
console.log(vcJwt)
// eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQi...0CQmqB14NnN5XxD0d_glLRs1Myc_LBJjnuNwE

Creating a Verifiable Presentation

Specify a payload matching the PresentationPayload or JwtPresentationPayload interfaces, including the VC JWTs to be presented in the vp.verifiableCredential array. Create a JWT by signing it with the previously configured issuer using the createVerifiablePresentationJwt function:

import { JwtPresentationPayload, createVerifiablePresentationJwt } from 'did-jwt-vc'

const vpPayload: JwtPresentationPayload = {
  vp: {
    '@context': ['https://www.w3.org/2018/credentials/v1'],
    type: ['VerifiablePresentation'],
    verifiableCredential: [vcJwt]
  }
}

const vpJwt = await createVerifiablePresentationJwt(vpPayload, issuer)
console.log(vpJwt)
// eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1ODI1NDc...JNMUzZ6naacuWNGdZGuU0ZDwmgpUMUqIzMqFFRmge0R8QA

Verifying JWTs

Prerequisites

Create a Resolver using did-resolver and register the ethr-did-resolver. When verifying a JWT signed by a DID, it is necessary to resolve its DID Document to check for keys that can validate the signature.

import { Resolver } from 'did-resolver'
import { getResolver } from 'ethr-did-resolver'

// see also https://github.com/decentralized-identity/ethr-did-resolver#multi-network-configuration
const providerConfig = {
  rpcUrl: 'https://mainnet.infura.io/v3/<YOUR infura.io PROJECT ID>',
  registry: '0xdca7ef03e98e0dc2b855be647c39abe984fcf21b'
}
const resolver = new Resolver(getResolver(providerConfig))

Verifying a Verifiable Credential

Pass in a VC JWT along with the resolver to verify using the verifyCredential function:

import { verifyCredential } from 'did-jwt-vc'

const verifiedVC = await verifyCredential(vcJwt, resolver)
console.log(verifiedVC)
/*
{
  "payload": {
    // the original payload of the signed credential
  },
  "doc": {
    // the DID document of the credential issuer (as returned by the `resolver`)
  },
  "issuer": "did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198", //the credential issuer
  "signer": {
    //the publicKey entry of the `doc` that has signed the credential
  },
  "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjY...Sx3Y2IdWaUpatJQA", // the original credential

  //parsed payload aligned to the W3C data model
  "verifiableCredential": {
    "@context": [Array],
    "type": [ "VerifiableCredential", "UniversityDegreeCredential" ],
    "issuer": {
      "id": "did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198"
    },
    "issuanceDate": "2019-07-12T16:51:22.000Z",
    "credentialSubject": {
      "id": "did:ethr:0x435df3eda57154cf8cf7926079881f2912f54db4"
      "degree": {
        "type": "BachelorDegree",
        "name": "Baccalauréat en musiques numériques"
      },
    },
    "proof": {
      //  proof type for internal use, NOT a registered vc-data-model type
      "type": "JwtProof2020",
      "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjY...Sx3Y2IdWaUpatJQA"
    }
  }
}
*/

Verifying a Verifiable Presentation

Pass in a VP JWT along with the resolver to verify using the verifyPresentation function:

import { verifyPresentation } from 'did-jwt-vc'

const verifiedVP = await verifyPresentation(vpJwt, resolver)
console.log(verifiedVP)
/*
{
  //original JWT payload
  payload: {
    iat: 1568045263,
    vp: {
      '@context': [Array],
      type: ['VerifiablePresentation'],
      verifiableCredential: [
        'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjY5...lpNm51cqSx3Y2IdWaUpatJQA'
      ]
    },
    iss: 'did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198'
  },

  doc: {
    // the DID document of the presentation issuer (as returned by the `resolver`)
  },

  signer: {
    //the publicKey entry of the `doc` that has signed the presentation
  },

  issuer: 'did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198',

  jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjgwNDUyNjMsInZwIjp7...ViNNCvoTQ-swSHwbELW7-EGPAcHLOMiIwE',

  // parsed payload aligned to the W3C data model
  verifiablePresentation: {
    verifiableCredential: [
      {
        iat: 1566923269,
        credentialSubject: {
          degree: { type: 'BachelorDegree', name: 'Baccalauréat en musiques numériques' },
          id: 'did:ethr:0x435df3eda57154cf8cf7926079881f2912f54db4'
        },
        issuer: { id: 'did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198' },
        type: ['VerifiableCredential', 'UniversityDegreeCredential'],
        '@context': [Array],
        issuanceDate: '2019-07-12T16:51:22.000Z',
        proof: {
          type: 'JwtProof2020',
          jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjY5...lpNm51cqSx3Y2IdWaUpatJQA'
        }
      }
    ],
    holder: 'did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198',
    type: ['VerifiablePresentation'],
    '@context': [Array],
    issuanceDate: '2019-09-09T16:07:43.000Z',
    proof: {
      // proof type for internal use, NOT a registered W3C vc-data-model proof type
      type: 'JwtProof2020',
      jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjgwNDUyNjMsInZwI...ViNNCvoTQ-swSHwbELW7-EGPAcHLOMiIwE'
    }
  }
}
*/

Notes on verification and proof properties

The result of the verification methods, when successful, also conveniently contain the decoded and parsed payloads, in a format that closely matches the W3C data model for verifiable credentials and presentations. This makes it easier to work with both credential encodings in the same system. This parsed payload also shows a proof property that lists the full JWT credential or presentation.

The JwtProof2020 is a synthetic proof type, usable for differentiating credentials by type. It is not a registered W3C VC Data Model algorithm and should not be treated as such.

Also note that the @context fields that appear in this parsed payload are the same as the ones in the incoming JWT. This means that the parsed payload will probably not be suitable for an LD-processor.

Please see #54 for more information.

changelog

4.0.12 (2025-04-25)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.14 (315da9a)

4.0.11 (2025-04-23)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.13 (bce1e35)

4.0.10 (2025-04-22)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.12 (2045b6c)

4.0.9 (2025-04-15)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.11 (2f43370)

4.0.8 (2025-04-15)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.10 (63c2c20)

4.0.7 (2025-03-19)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.9 (b58b7a8)

4.0.6 (2025-01-28)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.8 (#152) (80b7ff7)

4.0.5 (2025-01-19)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.7 (#151) (bb4f6e1)

4.0.4 (2024-03-26)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.4 (c41d3a4)

4.0.3 (2024-03-20)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.3 (3b83ac5)

4.0.2 (2024-03-14)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.2 (a5ed5a4)

4.0.1 (2024-02-28)

Bug Fixes

  • deps: Update dependency did-jwt to v8.0.1 (a091e1a)

4.0.0 (2024-01-18)

Bug Fixes

  • deps: Update dependency did-jwt to v8 (#146) (7f3caaa)

BREAKING CHANGES

  • deps: the updated did-jwt library includes some breaking changes so we are bumping the major version here too for safety

3.2.15 (2023-12-12)

Bug Fixes

  • deps: Update dependency did-jwt to v7.4.6 (b1880eb)

3.2.14 (2023-11-01)

Bug Fixes

3.2.13 (2023-10-25)

Bug Fixes

  • deps: Update dependency did-jwt to v7.4.4 (053d735)

3.2.12 (2023-10-23)

Bug Fixes

  • deps: Update dependency did-jwt to v7.4.3 (448b295)

3.2.11 (2023-10-03)

Bug Fixes

  • deps: Update dependency did-jwt to v7.4.2 (b696345)

3.2.10 (2023-09-27)

Bug Fixes

  • deps: Update dependency did-jwt to v7.4.1 (#140) (65bc254)

3.2.9 (2023-09-24)

Bug Fixes

  • deps: Update dependency did-jwt to v7.3.0 (b46ea30)

3.2.8 (2023-09-18)

Bug Fixes

  • deps: Update dependency did-jwt to v7.2.8 (49819ee)

3.2.7 (2023-09-04)

Bug Fixes

  • deps: Update dependency did-jwt to v7.2.7 (920aa69)

3.2.6 (2023-08-24)

Bug Fixes

  • deps: Update dependency did-jwt to v7.2.6 (61964e9)

3.2.5 (2023-08-04)

Bug Fixes

  • deps: Update dependency did-jwt to v7.2.5 (cbf34d2)

3.2.4 (2023-06-27)

Bug Fixes

  • deps: Update dependency did-jwt to v7.2.4 (0591be0)

3.2.3 (2023-06-08)

Bug Fixes

  • deps: Update dependency did-jwt to v7.2.2 (3e48606)

3.2.2 (2023-06-04)

Bug Fixes

  • deps: Update dependency did-jwt to v7.2.1 (f0275a2)

3.2.1 (2023-05-18)

Bug Fixes

  • deps: Update dependency did-jwt to v7.2.0 (ddf3914)

3.2.0 (2023-05-09)

Features

  • support credentials using ConditionalProof2022 (#133) (6cc9bed)

3.1.4 (2023-05-04)

Bug Fixes

  • deps: Update dependency did-jwt to v7 (1e81dea)

3.1.3 (2023-04-03)

Bug Fixes

  • deps: Update dependency did-jwt to v6.11.6 (de29a5c)

3.1.2 (2023-03-16)

Bug Fixes

  • deps: Update did-dependencies (8255a50)

3.1.1 (2023-02-10)

Bug Fixes

3.1.0 (2022-08-19)

Features

3.0.1 (2022-08-18)

Bug Fixes

3.0.0 (2022-08-18)

Features

BREAKING CHANGES

  • The error messages are now prefixed with recognizable error codes. This is a breaking change if you were checking the exact error messages being returned before. Otherwise, it is safe to upgrade without issues.

2.1.14 (2022-08-03)

Bug Fixes

  • deps: update did-resolver and did-jwt dependencies (#117) (d07dd45)

2.1.13 (2022-06-29)

Bug Fixes

2.1.12 (2022-06-06)

Bug Fixes

  • ci: groom the build scripts and dependencies (#114) (381d973)

2.1.11 (2022-05-22)

Bug Fixes

2.1.10 (2022-05-20)

Bug Fixes

2.1.9 (2022-01-13)

Bug Fixes

  • deps: bump dependencies to latest (7cb7a88)

2.1.8 (2021-11-23)

Bug Fixes

  • normalizeCredential overwrite of "evidence" when "vc" is missing (#86) (0039298)

2.1.7 (2021-08-31)

Bug Fixes

  • forward JWT options when creating a VC or VP (#90) (13cea08), closes #89

2.1.6 (2021-07-22)

Bug Fixes

  • build: revert back to microbundle and module output (d28729a), closes #84

2.1.5 (2021-07-21)

Bug Fixes

  • build: revert to tsc for compilation (#85) (b7dedea), closes #84

2.1.4 (2021-07-13)

Bug Fixes

2.1.3 (2021-06-16)

Bug Fixes

  • w3c additional props should not be overwritten inside JWT (#78) (d2a5c44)

2.1.2 (2021-05-28)

Bug Fixes

  • mapp additional W3C spec fields to and from JWT VC (#75) (e0482dc), closes #73

2.1.1 (2021-05-18)

Bug Fixes

2.1.0 (2021-04-08)

Features

  • add ability to add to the JWT header of the VC/VP (#71) (79d2d76), closes #69

2.0.2 (2021-03-26)

Bug Fixes

  • deps: use Resolvable type from did-resolver (e731e7e)

2.0.1 (2021-03-25)

Bug Fixes

2.0.0 (2021-03-11)

Bug Fixes

BREAKING CHANGES

  • deps: the type of Resolver used for verification has been upgraded to the latest spec and no longer returns just the DID Document

1.2.0 (2021-03-11)

Features

  • add option to keep original fields when transforming JWT<->JOSE payload formats (#63) (cf59b6f), closes #62 #22

1.1.0 (2021-02-24)

Features

  • support challenge & domain in Presentation creation and verification (#61) (3a75c47), closes #60 #22

1.0.7 (2021-01-18)

Bug Fixes

1.0.6 (2020-08-18)

Bug Fixes

1.0.5 (2020-08-18)

Bug Fixes

  • set credentialSubject.id as optional (#45) (c31ee17)

1.0.4 (2020-07-20)

Bug Fixes

1.0.3 (2020-07-02)

Bug Fixes

  • stop input from being mutated by converter methods (#41) (346e6f7)

1.0.2 (2020-06-29)

Bug Fixes

1.0.1 (2020-06-26)

Bug Fixes

  • build: Use tsc instead of microbundle (#38) (f75a967)

1.0.0 (2020-06-25)

Bug Fixes

  • build: add babel plugin to fix microbundle 0.12 build error (e14c2aa)
  • use ES256K as the default JWT algorithm (a097c69)

Code Refactoring

  • rename creation and validation methods to reflect JWT target (829956f)
  • rename credential validation methods (2bb2e5a)
  • rename existing payload types to reflect JWT target (af74207)

Features

  • add a method to convert a credential payload from W3C to JWT (f7e86f0)
  • add a normalizer method to an unambiguous Verifiable<Credential> (ffbd67f)
  • add methods to convert to unambiguous Verifiable<Presentation> and JWTPresentationPayload (1721e4a)
  • define W3C compatible data types for credentials and presentations (adb27e9)
  • homogenize verifyCredential()/verifyPresentation() API (e9fbb99)
  • homogenize createCredentialJwt/PresentationJwt API (3999382)

BREAKING CHANGES

  • removed Verifiable from the credential validation methods since the parameter is only the payload validateJwtVerifiableCredentialPayload -> validateJwtCredentialPayload validateVerifiableCredentialPayload -> validateCredentialPayload
  • renamed createPresentationJWT to createVerifiablePresentationAJwt
  • the following methods have been renamed: createVerifiableCredential -> createVerifiableCredentialJwt createPresentation -> createPresentationJwt validateVerifiableCredentialAttributes -> validateJwtVerifiableCredentialPayload validatePresentationAttributes -> validateJwtPresentationPayload

Also exporting the JWT type which maps to string

  • the following interface definitions have been renamed: VerifiableCredentialPayload -> JwtVerifiableCredentialPayload PresentationPayload -> JwtPresentationPayload

0.2.0 (2020-04-30)

Features

  • remove explicit declaration of the nullable credentialStatus (078ba82)

0.1.6 (2020-04-28)

Bug Fixes