|
1 | 1 | # SD-JWT-VC plugin for Veramo
|
| 2 | + |
| 3 | +[](https://github.com/cre8/sd-jwt-veramo/actions/workflows/cicd.yml) |
| 4 | + |
| 5 | +[](https://codecov.io/gh/cre8/sd-jwt-veramo) |
| 6 | + |
| 7 | +[](https://badge.fury.io/js/@bcrl%2Fveramo-sd-jwt) |
| 8 | + |
| 9 | +A Veramo plugin to issue, present and verify [SD-JWT-VCs](https://datatracker.ietf.org/doc/draft-ietf-oauth-sd-jwt-vc/) and is using [SD-JWT-JS](https://github.com/openwallet-foundation-labs/sd-jwt-js) library. |
| 10 | + |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```shell |
| 15 | +npm install @bcrl/veramo-sd-jwt |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Detailed examples can be found in the [test file](./src/agent-plugin/sd-jwt-plugin.spec.ts). |
| 21 | + |
| 22 | +```typescript |
| 23 | +const agent = createAgent<ISDJwtPlugin>({ |
| 24 | + plugins: [ |
| 25 | + new SDJwtPlugin({ |
| 26 | + // calculate the digest of the JWT |
| 27 | + hasher: digest, |
| 28 | + // generate a random salt |
| 29 | + saltGenerator: generateSalt, |
| 30 | + // verify the JWT |
| 31 | + verifySignature, |
| 32 | + }), |
| 33 | +}); |
| 34 | +``` |
| 35 | +
|
| 36 | +Instead of implementing the hasher and saltGenerator on your own, you can use the default implementations for each environment: |
| 37 | +```typescript |
| 38 | +// Node.js |
| 39 | +import { digest, generateSalt } from '@sd-jwt/crypto-nodejs'; |
| 40 | + |
| 41 | +// Browser |
| 42 | +import { digest, generateSalt } from '@sd-jwt/crypto-browser'; |
| 43 | +``` |
| 44 | +
|
| 45 | +### Issue a credential |
| 46 | +
|
| 47 | +```typescript |
| 48 | + |
| 49 | + const claims = { |
| 50 | + given_name: 'John', |
| 51 | + family_name: 'Deo', |
| 52 | + |
| 53 | + phone: '+1-202-555-0101', |
| 54 | + address: { |
| 55 | + street_address: '123 Main St', |
| 56 | + locality: 'Anytown', |
| 57 | + region: 'Anystate', |
| 58 | + country: 'US', |
| 59 | + }, |
| 60 | + birthdate: '1940-01-01', |
| 61 | + }; |
| 62 | + |
| 63 | + const disclosureFrame: DisclosureFrame<typeof claims> = { |
| 64 | + _sd: [ |
| 65 | + 'given_name', |
| 66 | + 'family_name', |
| 67 | + 'email', |
| 68 | + 'phone', |
| 69 | + 'address', |
| 70 | + 'birthdate', |
| 71 | + ], |
| 72 | + }; |
| 73 | + |
| 74 | +const credentialPayload: SdJwtVcPayload = { |
| 75 | + ...claims, |
| 76 | + // a did url that is used for the issuance. You need to pass the reference to the key that is used to sign the JWT |
| 77 | + iss: 'did:example:123#key-1', |
| 78 | + iat: new Date().getTime() / 1000, |
| 79 | + // type of the credential |
| 80 | + vct: 'ID-Card', |
| 81 | + // required to perform holder binding. |
| 82 | + cnf: { |
| 83 | + // the public key of the holder encoded as Json Web Key |
| 84 | + jwk, |
| 85 | + }, |
| 86 | + |
| 87 | +}; |
| 88 | +const credential = await agent.createSdJwtVc({ |
| 89 | + credentialPayload, |
| 90 | + disclosureFrame, |
| 91 | +}); |
| 92 | +``` |
| 93 | +
|
| 94 | +### Verify a credential |
| 95 | +The verify function will validate the signature of the SD-JWT-VC, it will not validate a referenced status list. |
| 96 | +
|
| 97 | +```typescript |
| 98 | +const verified = await agent.verifySdJwtVc({ |
| 99 | + credential: credential.credential, |
| 100 | +}); |
| 101 | +``` |
| 102 | +
|
| 103 | +### Create a presentation |
| 104 | +Creates a presentation of a credential. The presentation will only contain the claims that are specified in the presentationKeys. If you want to present all claims, you can pass an empty object. |
| 105 | +
|
| 106 | +To perform a holder binding, The included key in the `cnf` claim must be used. Right now it only supports the `cnf.jwk` approach so you need to pass a JWK during the issuance and not a did url. |
| 107 | +
|
| 108 | +```typescript |
| 109 | +const presentationKeys: PresentationFrame<typeof claims> = { |
| 110 | + given_name: true, |
| 111 | +}; |
| 112 | + |
| 113 | +const presentation = await agent.createSdJwtVcPresentation({ |
| 114 | + presentation: credential.credential, |
| 115 | + presentationKeys, |
| 116 | + kb: { |
| 117 | + payload: { |
| 118 | + aud: '1', |
| 119 | + iat: 1, |
| 120 | + nonce: '342', |
| 121 | + }, |
| 122 | + }, |
| 123 | +}); |
| 124 | +``` |
| 125 | +
|
| 126 | +### Verify a presentation |
| 127 | +Verifies a given presentation. It will validate the signature of the issuer, if all claims are present and optional if the key binding is correct. |
| 128 | +
|
| 129 | +```typescript |
| 130 | +const result = await agent.verifySdJwtVcPresentation({ |
| 131 | + // encoded presentation |
| 132 | + presentation: presentation.presentation, |
| 133 | + // list of required claims |
| 134 | + requiredClaimKeys: ['given_name'], |
| 135 | + // if set to true, the kb will be verified |
| 136 | + kb: true, |
| 137 | +}); |
| 138 | +``` |
| 139 | +
|
| 140 | +## Build |
| 141 | +Packages are managed via `pnpm`. |
| 142 | +```shell |
| 143 | +pnpm run build |
| 144 | +``` |
| 145 | +
|
| 146 | +## Test |
| 147 | +Test are written with `vitest`; |
| 148 | +
|
| 149 | +```shell |
| 150 | +pnpm run test |
| 151 | +``` |
| 152 | +
|
| 153 | +To get the coverage report, run: |
| 154 | +```shell |
| 155 | +pnpm run format |
| 156 | +``` |
| 157 | +
|
| 158 | +## Format |
| 159 | +The code is formatted with `biome`, passing the format and lint check is required to pass the CI. To format the code locally, run |
| 160 | +```shell |
| 161 | +pnpm run format |
| 162 | +``` |
0 commit comments