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 • 5.03 kB
{"version":3,"file":"W3cV2DataIntegrityVerifiablePresentation.mjs","names":[],"sources":["../../../../src/modules/vc/data-integrity/W3cV2DataIntegrityVerifiablePresentation.ts"],"sourcesContent":["import { CredoError } from '../../../error'\nimport type { JsonObject, SingleOrArray } from '../../../types'\nimport { MessageValidator } from '../../../utils'\nimport { ClaimFormat } from '../models/ClaimFormat'\nimport { W3cV2DataIntegrityVerifiableCredential } from './W3cV2DataIntegrityVerifiableCredential'\n\nexport type W3cV2DataIntegritySecuredPresentation = Record<string, unknown> & { proof: unknown }\n\ntype W3cV2DataIntegrityPresentationCredentialObjectEntry = {\n id?: string\n type?: SingleOrArray<string>\n proof?: unknown\n verifiableCredential?: SingleOrArray<W3cV2DataIntegrityPresentationCredentialEntry>\n}\n\nexport type W3cV2DataIntegrityPresentationCredentialEntry =\n | W3cV2DataIntegrityVerifiableCredential\n | W3cV2DataIntegrityVerifiablePresentation\n | W3cV2DataIntegrityPresentationCredentialObjectEntry\n | string\n\nexport type W3cV2DataIntegrityResolvedPresentation = {\n context?: Array<string | JsonObject>\n type?: SingleOrArray<string>\n holder?: string | { id: string; [property: string]: unknown }\n holderId?: string\n verifiableCredential?: SingleOrArray<W3cV2DataIntegrityPresentationCredentialEntry>\n}\n\nexport interface W3cV2DataIntegrityVerifiablePresentationOptions {\n securedPresentation: W3cV2DataIntegritySecuredPresentation\n resolvedPresentation: W3cV2DataIntegrityResolvedPresentation\n}\n\n/**\n * Represents a Verifiable Presentation secured with Data Integrity proof(s).\n *\n * @see https://www.w3.org/TR/vc-data-integrity/\n */\nexport class W3cV2DataIntegrityVerifiablePresentation {\n public constructor(options: W3cV2DataIntegrityVerifiablePresentationOptions) {\n this.securedPresentation = options.securedPresentation\n this.resolvedPresentation = options.resolvedPresentation\n\n // Validates the presentation structure and proof presence\n this.validate()\n }\n\n public static fromObject(\n presentation: W3cV2DataIntegritySecuredPresentation,\n resolvedPresentation: W3cV2DataIntegrityResolvedPresentation\n ) {\n return new W3cV2DataIntegrityVerifiablePresentation({\n securedPresentation: presentation,\n resolvedPresentation,\n })\n }\n\n /**\n * The original presentation object with embedded Data Integrity proof(s).\n */\n public readonly securedPresentation: W3cV2DataIntegritySecuredPresentation\n\n /**\n * Resolved presentation is the parsed VP object used for traversal and validation.\n */\n public readonly resolvedPresentation: W3cV2DataIntegrityResolvedPresentation\n\n /**\n * The JSON representation of this presentation.\n */\n public get encoded() {\n return JSON.stringify(this.securedPresentation)\n }\n\n /**\n * The {@link ClaimFormat} of the presentation.\n *\n * For W3C VP Data Integrity presentations this is always `di_vp`.\n */\n public get claimFormat(): ClaimFormat.DiVp {\n return ClaimFormat.DiVp\n }\n\n /**\n * Validates the presentation and proof structure.\n */\n public validate() {\n // Validate the resolved presentation according to the data model\n MessageValidator.validateSync(this.resolvedPresentation)\n\n // Validate that proof field exists and is properly structured\n const proof = this.securedPresentation.proof\n if (!proof) {\n throw new CredoError('The provided presentation does not have a proof field.')\n }\n\n // Proof should be either a single proof object or an array of proofs\n if (typeof proof !== 'object') {\n throw new CredoError('The proof field must be an object or array of objects.')\n }\n\n if (\n !Array.isArray(proof) &&\n typeof proof === 'object' &&\n (!('type' in proof) || proof.type !== 'DataIntegrityProof')\n ) {\n throw new CredoError('The proof must have type \"DataIntegrityProof\".')\n }\n\n if (Array.isArray(proof)) {\n for (const p of proof) {\n if (typeof p !== 'object' || !('type' in p) || p.type !== 'DataIntegrityProof') {\n throw new CredoError('All proofs in the proof array must have type \"DataIntegrityProof\".')\n }\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;AAuCA,IAAa,2CAAb,MAAa,yCAAyC;CACpD,YAAmB,SAA0D;EAC3E,KAAK,sBAAsB,QAAQ;EACnC,KAAK,uBAAuB,QAAQ;EAGpC,KAAK,SAAS;CAChB;CAEA,OAAc,WACZ,cACA,sBACA;EACA,OAAO,IAAI,yCAAyC;GAClD,qBAAqB;GACrB;EACF,CAAC;CACH;;;;CAeA,IAAW,UAAU;EACnB,OAAO,KAAK,UAAU,KAAK,mBAAmB;CAChD;;;;;;CAOA,IAAW,cAAgC;EACzC,OAAA;CACF;;;;CAKA,WAAkB;EAEhB,iBAAiB,aAAa,KAAK,oBAAoB;EAGvD,MAAM,QAAQ,KAAK,oBAAoB;EACvC,IAAI,CAAC,OACH,MAAM,IAAI,WAAW,wDAAwD;EAI/E,IAAI,OAAO,UAAU,UACnB,MAAM,IAAI,WAAW,wDAAwD;EAG/E,IACE,CAAC,MAAM,QAAQ,KAAK,KACpB,OAAO,UAAU,aAChB,EAAE,UAAU,UAAU,MAAM,SAAS,uBAEtC,MAAM,IAAI,WAAW,kDAAgD;EAGvE,IAAI,MAAM,QAAQ,KAAK,GAChB;QAAA,MAAM,KAAK,OACd,IAAI,OAAO,MAAM,YAAY,EAAE,UAAU,MAAM,EAAE,SAAS,sBACxD,MAAM,IAAI,WAAW,sEAAoE;EAAA;CAIjG;AACF"}