@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 • 8.78 kB
Source Map (JSON)
{"version":3,"file":"W3cV2EnvelopedVerifiableCredential.mjs","names":[],"sources":["../../../../../src/modules/vc/models/credential/W3cV2EnvelopedVerifiableCredential.ts"],"sourcesContent":["import { Exclude, Expose, instanceToPlain, plainToInstance, Transform, TransformationType } from 'class-transformer'\nimport { buildMessage, IsDataURI, isInstance, ValidateBy, type ValidationOptions } from 'class-validator'\nimport { CredoError } from '../../../../error'\nimport type { JsonObject, SingleOrArray } from '../../../../types'\nimport { CREDENTIALS_CONTEXT_V2_URL, ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE } from '../../constants'\nimport { W3cV2JwtVerifiableCredential } from '../../jwt-vc/W3cV2JwtVerifiableCredential'\nimport { W3cV2SdJwtVerifiableCredential } from '../../sd-jwt-vc/W3cV2SdJwtVerifiableCredential'\nimport { IsCredentialJsonLdContext } from '../../validators'\nimport { ClaimFormat } from '../ClaimFormat'\nimport { W3cV2Credential } from './W3cV2Credential'\n\nexport interface W3cV2EnvelopedVerifiableCredentialOptions {\n id: string\n context?: string | Array<string | JsonObject>\n type?: SingleOrArray<string>\n}\n\nexport class W3cV2EnvelopedVerifiableCredential {\n @Exclude()\n private _envelopedCredential?: W3cV2SdJwtVerifiableCredential | W3cV2JwtVerifiableCredential\n\n public constructor(options: W3cV2EnvelopedVerifiableCredentialOptions) {\n if (options) {\n this.context = options.context ?? CREDENTIALS_CONTEXT_V2_URL\n this.id = options.id\n this.type = options.type ?? ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE\n this._envelopedCredential = credentialFromDataUri(options.id)\n }\n }\n\n public static fromVerifiableCredential(\n credential: W3cV2SdJwtVerifiableCredential | W3cV2JwtVerifiableCredential\n ): W3cV2EnvelopedVerifiableCredential {\n return new W3cV2EnvelopedVerifiableCredential({\n id: credentialToDataUri(credential),\n context: CREDENTIALS_CONTEXT_V2_URL,\n type: ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE,\n })\n }\n\n @Expose({ name: '@context' })\n @IsCredentialJsonLdContext({ allowString: true, credentialContext: CREDENTIALS_CONTEXT_V2_URL })\n public context!: string | Array<string | JsonObject>\n\n @IsDataURI()\n public id!: string\n\n @IsEnvelopedVerifiableCredentialType()\n public type!: SingleOrArray<string>\n\n /**\n * Gets the enveloped credential.\n */\n public get envelopedCredential() {\n if (!this._envelopedCredential) {\n this._envelopedCredential = credentialFromDataUri(this.id)\n }\n\n return this._envelopedCredential\n }\n\n /**\n * Resolved credential is the fully resolved {@link W3cV2Credential} instance.\n */\n public get resolvedCredential(): W3cV2Credential {\n return this.envelopedCredential.resolvedCredential\n }\n\n /**\n * The {@link ClaimFormat} of the enveloped credential.\n */\n public get claimFormat(): ClaimFormat.SdJwtW3cVc | ClaimFormat.JwtW3cVc {\n return this.envelopedCredential.claimFormat\n }\n}\n\n// Pre-construction shape detector\nexport function isEnvelopedVerifiableCredentialEntry(\n value: unknown\n): value is W3cV2EnvelopedVerifiableCredentialOptions {\n if (!value || typeof value !== 'object' || Array.isArray(value)) return false\n\n const type = (value as { type?: unknown }).type\n const values = Array.isArray(type) ? type : [type]\n\n return values.some((entry) => entry === ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE)\n}\n\nconst jsonToClass = (v: unknown) => {\n if (!v || typeof v !== 'object') {\n throw new CredoError('Invalid plain W3cV2EnvelopedVerifiableCredential')\n }\n\n if (isInstance(v, W3cV2EnvelopedVerifiableCredential)) {\n return v\n }\n\n return plainToInstance(W3cV2EnvelopedVerifiableCredential, v)\n}\n\nconst classToJson = (v: unknown) => {\n return instanceToPlain(v)\n}\n\nexport function W3cV2EnvelopedVerifiableCredentialTransformer() {\n return Transform(({ value, type }: { value: SingleOrArray<unknown>; type: TransformationType }) => {\n if (type === TransformationType.PLAIN_TO_CLASS) {\n return Array.isArray(value) ? value.map(jsonToClass) : jsonToClass(value)\n }\n\n if (type === TransformationType.CLASS_TO_PLAIN) {\n if (Array.isArray(value)) return value.map(classToJson)\n return classToJson(value)\n }\n\n // PLAIN_TO_PLAIN\n return value\n })\n}\n\nexport function IsEnvelopedVerifiableCredentialType(validationOptions?: ValidationOptions): PropertyDecorator {\n return ValidateBy(\n {\n name: 'IsEnvelopedVerifiableCredentialType',\n validator: {\n validate: (value): boolean => {\n return Array.isArray(value)\n ? value.includes(ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE) && value.every((v) => typeof v === 'string')\n : value === ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE\n },\n defaultMessage: buildMessage(\n (eachPrefix) =>\n `${eachPrefix}$property must be a string that equals, or an array of strings which includes \"${ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE}\"`,\n validationOptions\n ),\n },\n },\n validationOptions\n )\n}\n\nfunction credentialFromDataUri(uri: string) {\n if (!uri.startsWith('data:')) {\n throw new CredoError('Invalid Enveloped Verifiable Credential: \"id\" is not a valid data URI')\n }\n\n const mimetypeData = uri.slice(5)\n const commaIndex = mimetypeData.indexOf(',')\n if (commaIndex === -1) {\n throw new CredoError('Invalid Enveloped Verifiable Credential: \"id\" data URI is missing comma separator')\n }\n\n const mimetype = mimetypeData.slice(0, commaIndex)\n const data = mimetypeData.slice(commaIndex + 1)\n\n switch (mimetype) {\n case 'application/vc+sd-jwt':\n return W3cV2SdJwtVerifiableCredential.fromCompact(data)\n\n case 'application/vc+jwt':\n return W3cV2JwtVerifiableCredential.fromCompact(data)\n\n default:\n throw new CredoError(`Unsupported Enveloped Verifiable Credential: ${mimetype} not recognized`)\n }\n}\n\nfunction credentialToDataUri(presentation: W3cV2SdJwtVerifiableCredential | W3cV2JwtVerifiableCredential) {\n if (presentation instanceof W3cV2SdJwtVerifiableCredential) {\n return `data:application/vc+sd-jwt,${presentation.encoded}`\n }\n\n if (presentation instanceof W3cV2JwtVerifiableCredential) {\n return `data:application/vc+jwt,${presentation.encoded}`\n }\n\n throw new CredoError('Unsupported Verifiable Credential instance')\n}\n"],"mappings":";;;;;;;;;;;;AAiBA,IAAa,qCAAb,MAAa,mCAAmC;CAI9C,YAAmB,SAAoD;EACrE,IAAI,SAAS;GACX,KAAK,UAAU,QAAQ,WAAA;GACvB,KAAK,KAAK,QAAQ;GAClB,KAAK,OAAO,QAAQ,QAAA;GACpB,KAAK,uBAAuB,sBAAsB,QAAQ,EAAE;EAC9D;CACF;CAEA,OAAc,yBACZ,YACoC;EACpC,OAAO,IAAI,mCAAmC;GAC5C,IAAI,oBAAoB,UAAU;GAClC,SAAS;GACT,MAAM;EACR,CAAC;CACH;;;;CAeA,IAAW,sBAAsB;EAC/B,IAAI,CAAC,KAAK,sBACR,KAAK,uBAAuB,sBAAsB,KAAK,EAAE;EAG3D,OAAO,KAAK;CACd;;;;CAKA,IAAW,qBAAsC;EAC/C,OAAO,KAAK,oBAAoB;CAClC;;;;CAKA,IAAW,cAA6D;EACtE,OAAO,KAAK,oBAAoB;CAClC;AACF;AAxDG,WAAA,CAAA,QAAQ,GAAA,mBAAA,eAAA,MAAA,CAAA,GAAA,mCAAA,WAAA,wBAAA,KAAA,CAAA;;CAsBR,OAAO,EAAE,MAAM,WAAW,CAAC;CAC3B,0BAA0B;EAAE,aAAa;EAAM,mBAAmB;CAA2B,CAAC;;;AAG9F,WAAA,CAAA,UAAU,GAAA,mBAAA,eAAA,MAAA,CAAA,GAAA,mCAAA,WAAA,MAAA,KAAA,CAAA;AAGV,WAAA,CAAA,oCAAoC,GAAA,mBAAA,eAAA,MAAA,CAAA,GAAA,mCAAA,WAAA,QAAA,KAAA,CAAA;AA8BvC,SAAgB,qCACd,OACoD;CACpD,IAAI,CAAC,SAAS,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG,OAAO;CAExE,MAAM,OAAQ,MAA6B;CAG3C,QAFe,MAAM,QAAQ,IAAI,IAAI,OAAO,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