0.1.5 • Published 9 months ago

@bloomprotocol/vc v0.1.5

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
9 months ago

VC

TypeScript types, JSON schemas, and signing and verifying functions for Verifiable Credentials and Presentations.

Installation

npm i @bloomprotocol/vc

OR

yarn add @bloomprotocol/vc

Usage

Signing

signVC

signVC takes a single parameter with the following properties:

NameDescriptionType
unsignedThe unsigned VC to be signedOmit<VC, 'proof'>
suiteSigning suite that is compatible with jsonld-signaturesClass that is compatible with LinkedDataSignature
documentLoaderFunction that resolves context URLs and DIDs(url: string) => Promise<{document: null \| Record<string, unknown>, documentUrl: string}> \| {document: null \| Record<string, unknown>, documentUrl: string}
proofPurposeOptionsAdditional options to pass to the proof purpose classundefined \| Record<string, unknown>
import { signVC, VC } from '@bloomprotocol/vc'

const { Ed25519VerificationKey2020 } = require('@digitalbazaar/ed25519-verification-key-2020')
const { Ed25519Signature2020 } = require('@digitalbazaar/ed25519-signature-2020')

const unsignedVC: Omit<VC, 'proof'> = {
  '@context': ['https://www.w3.org/2018/credentials/v1', 'https://w3id.org/security/suites/ed25519-2020/v1'],
  id: 'urn:uuid:9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
  type: ['VerifiableCredential'],
  issuanceDate: new Date().toISOString(),
  issuer: 'did:example:issuer',
  credentialSubject: {},
}

const suite = new Ed25519Signature2020({
  key: new Ed25519VerificationKey2020(keyPair),
})

const vc = await signVC({
  unsigned: unsigngedVC,
  suite,
  documentLoader: (url: string) => {
    //...
  },
})

signVP

signVP takes a single parameter with the following properties:

NameDescriptionType
unsignedThe unsigned VP to be signedOmit<VP, 'proof'>
suiteSigning suite that is compatible with jsonld-signaturesClass that is compatible with LinkedDataSignature
documentLoaderFunction that resolves context URLs and DIDs(url: string) => Promise<{document: null \| Record<string, unknown>, documentUrl: string}> \| {document: null \| Record<string, unknown>, documentUrl: string}
proofPurposeOptionsAdditional options to pass to the proof purpose class{challenge: string, domain: string, [k: string]?: unknown}
import { signVC, VC } from '@bloomprotocol/vc'

const { Ed25519VerificationKey2020 } = require('@digitalbazaar/ed25519-verification-key-2020')
const { Ed25519Signature2020 } = require('@digitalbazaar/ed25519-signature-2020')

const unsignedVP: Omit<VP, 'proof'> = {
  '@context': ['https://www.w3.org/2018/credentials/v1', 'https://w3id.org/security/suites/ed25519-2020/v1'],
  id: 'urn:uuid:9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
  type: ['VerifiablePresentation'],
  holder: 'did:example:holder',
  verifiableCredential: [vc]
}

const suite = new Ed25519Signature2020({
  key: new Ed25519VerificationKey2020(keyPair),
})

const vp = await signVP({
  unsigned: unsigngedVP,
  suite,
  proofPurposeOptions: {
    challenge: '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
    domain: 'https://example.com'
  },
  documentLoader: (url: string) => {
    //...
  },
})

Verifying

verifyVC

verifyVC takes a single parameter with the following properties:

NameDescriptionTypeRequiredDefault
vcThe VC to be verifiedunknownYes-
getSuiteFunction that returns a signing suite that is compatible with jsonld-signatures(options: { verificationMethod: string, controller: string, proofType: string }) => any \| Promise<any>Yes-
documentLoaderFunction that resolves context URLs and DIDs(url: string) => Promise<{document: null \| Record<string, unknown>, documentUrl: string}> \| {document: null \| Record<string, unknown>, documentUrl: string}Yes-
getProofPurposeOptionsFunction that returns additional options to pass to the proof purpose class(options: { proofPurpose: string, verificationMethod: string, controller: string }) => Record<string, unknown> \| Promise<Record<string, unknown>>No-
schemaCustom json schema to validate the vc againstJSONSchemaNovcSchema
ajvCustom Ajv instance to use when validating the vcAjvNo-
schemaKeyCustom key to use for Ajv caching of schemasstringNo"vcSchema"

On success verifyVC returns:

type VerifyVCResponseSuccess<VCType extends VC> = {
  success: true
  vc: VCType
}

And on failure returns:

type VerifyVCResposeFailure = {
  success: false
  schemaErrors?: ErrorObject[]
  proofErrors?: ProofError[]
  issuanceErrors?: IssuanceError[]
}
  • schemaErrors are errors returned from Ajv
  • proofErrors are errors returned from jsonld-signatures
  • issuanceErrors are errors about the VC's issuance and expiration date

verifyVP

verifyVP takes a single parameter with the following properties:

NameDescriptionTypeRequiredDefault
vpThe VP to be verifiedunknownYes-
getSuiteFunction that returns a signing suite that is compatible with jsonld-signatures(options: { verificationMethod: string, controller: string, proofType: string }) => any \| Promise<any>Yes-
documentLoaderFunction that resolves context URLs and DIDs(url: string) => Promise<{document: null \| Record<string, unknown>, documentUrl: string}> \| {document: null \| Record<string, unknown>, documentUrl: string}Yes-
getProofPurposeOptionsFunction that returns additional options to pass to the proof purpose class(options: { proofPurpose: string, verificationMethod: string, controller: string }) => Record<string, unknown> \| Promise<Record<string, unknown>>No-
schemaCustom json schema to validate the vp againstJSONSchemaNovpSchema
ajvCustom Ajv instance to use when validating the vpAjvNo-
schemaKeyCustom key to use for Ajv caching of schemasstringNo"vpSchema"

On success verifyVC returns:

type VerifyVPResponseSuccess<VPType extends VP> = {
  success: true
  vp: VPType
}

And on failure returns:

type VerifyVPResposeFailure = {
  success: false
  schemaErrors?: ErrorObject[]
  proofErrors?: ProofError[]
  credentialProofErrors?: {id: string, errors: ProofError[]}[]
  credentialiIssuanceErrors?: {id: string, errors: IssuanceError[]}[]
}
  • schemaErrors are errors returned from Ajv
  • proofErrors are errors returned from jsonld-signatures for the VP
  • credentialProofErrors are errors returned from jsonld-signatures for the VCs within the VP
  • credentialIssuanceErrors are errors about the issuance and expiration date for the VCs within the VP