项目作者: decentralized-identity

项目描述 :
Create and verify W3C Verifiable Credentials and Presentations in JWT format
高级语言: TypeScript
项目地址: git://github.com/decentralized-identity/did-jwt-vc.git
创建时间: 2019-07-22T20:20:06Z
项目社区:https://github.com/decentralized-identity/did-jwt-vc

开源协议:Apache License 2.0

下载


npm
npm
codecov

did-jwt-vc

Create and verify W3C Verifiable Credentials and Presentations in JWT format

Installation

  1. npm install did-jwt-vc

Usage

Creating JWTs

Prerequisites

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

  1. import { EthrDID } from 'ethr-did'
  2. import { Issuer } from 'did-jwt-vc'
  3. const issuer = new EthrDID({
  4. identifier: '0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198',
  5. privateKey: 'd8b595680851765f38ea5405129244ba3cbad84467d190859f4c8b20c1ff6c75'
  6. }) 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:

  1. import { JwtCredentialPayload, createVerifiableCredentialJwt } from 'did-jwt-vc'
  2. const vcPayload: JwtCredentialPayload = {
  3. sub: 'did:ethr:0x435df3eda57154cf8cf7926079881f2912f54db4',
  4. nbf: 1562950282,
  5. vc: {
  6. '@context': ['https://www.w3.org/2018/credentials/v1'],
  7. type: ['VerifiableCredential'],
  8. credentialSubject: {
  9. degree: {
  10. type: 'BachelorDegree',
  11. name: 'Baccalauréat en musiques numériques'
  12. }
  13. }
  14. }
  15. }
  16. const vcJwt = await createVerifiableCredentialJwt(vcPayload, issuer)
  17. console.log(vcJwt)
  18. // 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:

  1. import { JwtPresentationPayload, createVerifiablePresentationJwt } from 'did-jwt-vc'
  2. const vpPayload: JwtPresentationPayload = {
  3. vp: {
  4. '@context': ['https://www.w3.org/2018/credentials/v1'],
  5. type: ['VerifiablePresentation'],
  6. verifiableCredential: [vcJwt]
  7. }
  8. }
  9. const vpJwt = await createVerifiablePresentationJwt(vpPayload, issuer)
  10. console.log(vpJwt)
  11. // 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.

  1. import { Resolver } from 'did-resolver'
  2. import { getResolver } from 'ethr-did-resolver'
  3. // see also https://github.com/decentralized-identity/ethr-did-resolver#multi-network-configuration
  4. const providerConfig = {
  5. rpcUrl: 'https://mainnet.infura.io/v3/<YOUR infura.io PROJECT ID>',
  6. registry: '0xdca7ef03e98e0dc2b855be647c39abe984fcf21b'
  7. }
  8. 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:

  1. import { verifyCredential } from 'did-jwt-vc'
  2. const verifiedVC = await verifyCredential(vcJwt, resolver)
  3. console.log(verifiedVC)
  4. /*
  5. {
  6. "payload": {
  7. // the original payload of the signed credential
  8. },
  9. "doc": {
  10. // the DID document of the credential issuer (as returned by the `resolver`)
  11. },
  12. "issuer": "did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198", //the credential issuer
  13. "signer": {
  14. //the publicKey entry of the `doc` that has signed the credential
  15. },
  16. "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjY...Sx3Y2IdWaUpatJQA", // the original credential
  17. //parsed payload aligned to the W3C data model
  18. "verifiableCredential": {
  19. "@context": [Array],
  20. "type": [ "VerifiableCredential", "UniversityDegreeCredential" ],
  21. "issuer": {
  22. "id": "did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198"
  23. },
  24. "issuanceDate": "2019-07-12T16:51:22.000Z",
  25. "credentialSubject": {
  26. "id": "did:ethr:0x435df3eda57154cf8cf7926079881f2912f54db4"
  27. "degree": {
  28. "type": "BachelorDegree",
  29. "name": "Baccalauréat en musiques numériques"
  30. },
  31. },
  32. "proof": {
  33. // proof type for internal use, NOT a registered vc-data-model type
  34. "type": "JwtProof2020",
  35. "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjY...Sx3Y2IdWaUpatJQA"
  36. }
  37. }
  38. }
  39. */

Verifying a Verifiable Presentation

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

  1. import { verifyPresentation } from 'did-jwt-vc'
  2. const verifiedVP = await verifyPresentation(vpJwt, resolver)
  3. console.log(verifiedVP)
  4. /*
  5. {
  6. //original JWT payload
  7. payload: {
  8. iat: 1568045263,
  9. vp: {
  10. '@context': [Array],
  11. type: ['VerifiablePresentation'],
  12. verifiableCredential: [
  13. 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjY5...lpNm51cqSx3Y2IdWaUpatJQA'
  14. ]
  15. },
  16. iss: 'did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198'
  17. },
  18. doc: {
  19. // the DID document of the presentation issuer (as returned by the `resolver`)
  20. },
  21. signer: {
  22. //the publicKey entry of the `doc` that has signed the presentation
  23. },
  24. issuer: 'did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198',
  25. jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjgwNDUyNjMsInZwIjp7...ViNNCvoTQ-swSHwbELW7-EGPAcHLOMiIwE',
  26. // parsed payload aligned to the W3C data model
  27. verifiablePresentation: {
  28. verifiableCredential: [
  29. {
  30. iat: 1566923269,
  31. credentialSubject: {
  32. degree: { type: 'BachelorDegree', name: 'Baccalauréat en musiques numériques' },
  33. id: 'did:ethr:0x435df3eda57154cf8cf7926079881f2912f54db4'
  34. },
  35. issuer: { id: 'did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198' },
  36. type: ['VerifiableCredential', 'UniversityDegreeCredential'],
  37. '@context': [Array],
  38. issuanceDate: '2019-07-12T16:51:22.000Z',
  39. proof: {
  40. type: 'JwtProof2020',
  41. jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjY5...lpNm51cqSx3Y2IdWaUpatJQA'
  42. }
  43. }
  44. ],
  45. holder: 'did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198',
  46. type: ['VerifiablePresentation'],
  47. '@context': [Array],
  48. issuanceDate: '2019-09-09T16:07:43.000Z',
  49. proof: {
  50. // proof type for internal use, NOT a registered W3C vc-data-model proof type
  51. type: 'JwtProof2020',
  52. jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1NjgwNDUyNjMsInZwI...ViNNCvoTQ-swSHwbELW7-EGPAcHLOMiIwE'
  53. }
  54. }
  55. }
  56. */

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.