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

66 lines (65 loc) • 4.17 kB
import { CredoError } from "../../../../error/CredoError.mjs"; import { ClassValidationError } from "../../../../error/ClassValidationError.mjs"; import "../../../../error/index.mjs"; import { JsonTransformer } from "../../../../utils/JsonTransformer.mjs"; import "../../../../utils/index.mjs"; import { Jwt } from "../../../../crypto/jose/jwt/Jwt.mjs"; import { W3cV2DataIntegrityVerifiablePresentation } from "../../data-integrity/W3cV2DataIntegrityVerifiablePresentation.mjs"; import { W3cV2Presentation, isEmbeddedDataIntegrityPresentationEntry } from "./W3cV2Presentation.mjs"; import { W3cV2JwtVerifiablePresentation } from "../../jwt-vc/W3cV2JwtVerifiablePresentation.mjs"; import { W3cV2SdJwtVerifiablePresentation } from "../../sd-jwt-vc/W3cV2SdJwtVerifiablePresentation.mjs"; import "../../data-integrity/index.mjs"; import { presentationFromDataUri } from "./W3cV2EnvelopedVerifiablePresentationCodec.mjs"; import { Transform, TransformationType } from "class-transformer"; import { ValidationError } from "class-validator"; //#region src/modules/vc/models/presentation/W3cV2VerifiablePresentation.ts const decodeW3cV2VerifiablePresentation = (value) => { try { if (typeof value !== "string") throw new CredoError("Expected a plain string encoding"); const trimmedValue = value.trim(); try { const parsedJson = JSON.parse(trimmedValue); if (isEmbeddedDataIntegrityPresentationEntry(parsedJson)) return new W3cV2DataIntegrityVerifiablePresentation({ securedPresentation: parsedJson, resolvedPresentation: JsonTransformer.fromJSON(parsedJson, W3cV2Presentation, { validate: false }) }); } catch {} if (trimmedValue.startsWith("data:")) return presentationFromDataUri(trimmedValue); const issuerSignedCompact = trimmedValue.split("~")[0]; if (issuerSignedCompact !== trimmedValue) { if (!Jwt.format.test(issuerSignedCompact)) throw new CredoError("Unsupported presentation string encoding. Expected compact 'vp+sd-jwt'."); const typ = Jwt.fromSerializedJwt(issuerSignedCompact).header.typ; if (typ === "vc+sd-jwt" || typ === "dc+sd-jwt") throw new CredoError("Value is a W3C SD-JWT VC, but a W3C SD-JWT VP was expected"); if (typ && typ !== "vp+sd-jwt") throw new CredoError(`Unsupported W3C SD-JWT VP typ '${typ}'. Expected 'vp+sd-jwt'.`); return W3cV2SdJwtVerifiablePresentation.fromCompact(trimmedValue); } if (!Jwt.format.test(trimmedValue)) throw new CredoError("Unsupported presentation string encoding. Expected compact 'vp+jwt'."); const typ = Jwt.fromSerializedJwt(trimmedValue).header.typ; if (typ === "vc+jwt" || typ === "dc+jwt") throw new CredoError("Value is a W3C VC JWT, but a W3C VP JWT was expected"); if (typ === "vp+sd-jwt") throw new CredoError("Value has typ 'vp+sd-jwt' but is missing SD-JWT disclosures"); if (typ && typ !== "vp+jwt") throw new CredoError(`Unsupported W3C VP JWT typ '${typ}'. Expected 'vp+jwt'.`); return W3cV2JwtVerifiablePresentation.fromCompact(trimmedValue); } catch (error) { if (error instanceof ValidationError || error instanceof ClassValidationError) throw error; throw new CredoError(`Value '${value}' is not a valid W3cV2VerifiablePresentation. ${error.message}`); } }; const encodePresentation = (value) => { if (value instanceof W3cV2JwtVerifiablePresentation) return value.encoded; if (value instanceof W3cV2SdJwtVerifiablePresentation) return value.encoded; if (value instanceof W3cV2DataIntegrityVerifiablePresentation) return value.encoded; throw new CredoError(`Value '${value}' is not a valid W3cV2VerifiablePresentation`); }; function W3cV2VerifiablePresentationTransformer() { return Transform(({ value, type }) => { if (type === TransformationType.PLAIN_TO_CLASS) return Array.isArray(value) ? value.map(decodeW3cV2VerifiablePresentation) : decodeW3cV2VerifiablePresentation(value); if (type === TransformationType.CLASS_TO_PLAIN) { if (Array.isArray(value)) return value.map(encodePresentation); return encodePresentation(value); } return value; }); } //#endregion export { W3cV2VerifiablePresentationTransformer, decodeW3cV2VerifiablePresentation }; //# sourceMappingURL=W3cV2VerifiablePresentation.mjs.map