UNPKG

@credo-ts/core

Version:

<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1

1 lines • 3.7 kB
{"version":3,"file":"W3cV2SdJwtVerifiableCredential.mjs","names":[],"sources":["../../../../src/modules/vc/sd-jwt-vc/W3cV2SdJwtVerifiableCredential.ts"],"sourcesContent":["import { CredoError } from '../../../error'\nimport { JsonTransformer, MessageValidator } from '../../../utils'\nimport { ClaimFormat } from '../models/ClaimFormat'\nimport { W3cV2Credential } from '../models/credential/W3cV2Credential'\nimport { decodeSdJwt, type W3cV2SdJwt } from './W3cV2SdJwt'\n\nexport interface W3cV2SdJwtVerifiableCredentialOptions {\n sdJwt: W3cV2SdJwt<ClaimFormat.SdJwtW3cVc>\n}\n\n/**\n * Represents a Verifiable Credential encoded as a SD-JWT.\n *\n * @see https://www.w3.org/TR/vc-jose-cose/#securing-with-sd-jwt\n */\nexport class W3cV2SdJwtVerifiableCredential {\n public constructor(options: W3cV2SdJwtVerifiableCredentialOptions) {\n this.sdJwt = options.sdJwt\n this.resolvedCredential = JsonTransformer.fromJSON(options.sdJwt.prettyClaims, W3cV2Credential, { validate: false })\n\n // Validates the SD-JWT and resolved credential\n this.validate()\n }\n\n public static fromCompact(compact: string) {\n const sdJwt = decodeSdJwt(compact, ClaimFormat.SdJwtW3cVc)\n\n return new W3cV2SdJwtVerifiableCredential({\n sdJwt,\n })\n }\n\n /**\n * The original SD-JWT.\n */\n public readonly sdJwt: W3cV2SdJwt<ClaimFormat.SdJwtW3cVc>\n\n /**\n * Resolved credential is the fully resolved {@link W3cV2Credential} instance.\n */\n public readonly resolvedCredential: W3cV2Credential\n\n /**\n * The encoded version of this credential.\n */\n public get encoded() {\n return this.sdJwt.compact\n }\n\n /**\n * The {@link ClaimFormat} of the credential.\n *\n * For W3C VC SD-JWT credentials this is always `vc+sd-jwt`.\n */\n public get claimFormat(): ClaimFormat.SdJwtW3cVc {\n return ClaimFormat.SdJwtW3cVc\n }\n\n /**\n * Validates the SD-JWT and the resolved credential.\n */\n public validate() {\n // Validate the resolved credential according to the data model\n MessageValidator.validateSync(this.resolvedCredential)\n\n // JWT validations to ensure compliance to the specification\n const sdJwt = this.sdJwt\n const header = sdJwt.header\n const payload = sdJwt.prettyClaims\n\n if (header.alg === 'none') {\n throw new CredoError(`The provided W3C VC SD-JWT uses 'alg'='none', which is not allowed.`)\n }\n\n if ('typ' in header && header.typ !== 'vc+sd-jwt') {\n throw new CredoError(`The provided W3C VC SD-JWT does not have the correct 'typ' header.`)\n }\n\n if ('cty' in header && header.cty !== 'vc') {\n throw new CredoError(`The provided W3C VC SD-JWT does not have the correct 'cty' header.`)\n }\n\n if (payload.iss) {\n if (this.resolvedCredential.issuerId !== payload.iss) {\n throw new CredoError(`The provided W3C VC SD-JWT has both 'iss' and 'issuer' claims, but they differ.`)\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAeA,IAAa,iCAAb,MAAa,+BAA+B;CAC1C,YAAmB,SAAgD;EACjE,KAAK,QAAQ,QAAQ;EACrB,KAAK,qBAAqB,gBAAgB,SAAS,QAAQ,MAAM,cAAc,iBAAiB,EAAE,UAAU,MAAM,CAAC;EAGnH,KAAK,SAAS;CAChB;CAEA,OAAc,YAAY,SAAiB;EACzC,MAAM,QAAQ,YAAY,SAAA,WAA+B;EAEzD,OAAO,IAAI,+BAA+B,EACxC,MACF,CAAC;CACH;;;;CAeA,IAAW,UAAU;EACnB,OAAO,KAAK,MAAM;CACpB;;;;;;CAOA,IAAW,cAAsC;EAC/C,OAAA;CACF;;;;CAKA,WAAkB;EAEhB,iBAAiB,aAAa,KAAK,kBAAkB;EAGrD,MAAM,QAAQ,KAAK;EACnB,MAAM,SAAS,MAAM;EACrB,MAAM,UAAU,MAAM;EAEtB,IAAI,OAAO,QAAQ,QACjB,MAAM,IAAI,WAAW,qEAAqE;EAG5F,IAAI,SAAS,UAAU,OAAO,QAAQ,aACpC,MAAM,IAAI,WAAW,oEAAoE;EAG3F,IAAI,SAAS,UAAU,OAAO,QAAQ,MACpC,MAAM,IAAI,WAAW,oEAAoE;EAG3F,IAAI,QAAQ,KACN;OAAA,KAAK,mBAAmB,aAAa,QAAQ,KAC/C,MAAM,IAAI,WAAW,iFAAiF;EAAA;CAG5G;AACF"}