{"version":3,"file":"W3cV2EnvelopedVerifiablePresentation.mjs","names":[],"sources":["../../../../../src/modules/vc/models/presentation/W3cV2EnvelopedVerifiablePresentation.ts"],"sourcesContent":["import { Exclude, Expose } from 'class-transformer'\nimport { buildMessage, IsDataURI, ValidateBy, type ValidationOptions } from 'class-validator'\nimport { CredoError } from '../../../../error'\nimport type { JsonObject, SingleOrArray } from '../../../../types'\nimport {\n  CREDENTIALS_CONTEXT_V2_URL,\n  ENVELOPED_VERIFIABLE_CREDENTIAL_TYPE,\n  ENVELOPED_VERIFIABLE_PRESENTATION_TYPE,\n} from '../../constants'\nimport { W3cV2JwtVerifiablePresentation } from '../../jwt-vc'\nimport { W3cV2SdJwtVerifiablePresentation } from '../../sd-jwt-vc'\nimport { IsCredentialJsonLdContext } from '../../validators'\nimport { ClaimFormat } from '../ClaimFormat'\nimport { W3cV2Presentation } from './W3cV2Presentation'\n\nexport interface W3cV2EnvelopedVerifiablePresentationOptions {\n  id: string\n  context?: string | Array<string | JsonObject>\n  type?: SingleOrArray<string>\n}\n\nexport class W3cV2EnvelopedVerifiablePresentation {\n  @Exclude()\n  private _envelopedPresentation?: W3cV2SdJwtVerifiablePresentation | W3cV2JwtVerifiablePresentation\n\n  public constructor(options: W3cV2EnvelopedVerifiablePresentationOptions) {\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._envelopedPresentation = presentationFromDataUri(options.id)\n    }\n  }\n\n  public static fromVerifiablePresentation(\n    presentation: W3cV2SdJwtVerifiablePresentation | W3cV2JwtVerifiablePresentation\n  ): W3cV2EnvelopedVerifiablePresentation {\n    return new W3cV2EnvelopedVerifiablePresentation({\n      id: presentationToDataUri(presentation),\n      context: CREDENTIALS_CONTEXT_V2_URL,\n      type: ENVELOPED_VERIFIABLE_PRESENTATION_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  @IsEnvelopedVerifiablePresentationType()\n  public type!: SingleOrArray<string>\n\n  /**\n   * Gets the enveloped presentation.\n   */\n  public get envelopedPresentation() {\n    if (!this._envelopedPresentation) {\n      this._envelopedPresentation = presentationFromDataUri(this.id)\n    }\n\n    return this._envelopedPresentation\n  }\n\n  /**\n   * Resolved presentation is the fully resolved {@link W3cV2Presentation} instance.\n   */\n  public get resolvedPresentation(): W3cV2Presentation {\n    return this.envelopedPresentation.resolvedPresentation\n  }\n\n  /**\n   * The {@link ClaimFormat} of the enveloped presentation.\n   */\n  public get claimFormat(): ClaimFormat.SdJwtW3cVp | ClaimFormat.JwtW3cVp {\n    return this.envelopedPresentation.claimFormat\n  }\n}\n\nexport function IsEnvelopedVerifiablePresentationType(validationOptions?: ValidationOptions): PropertyDecorator {\n  return ValidateBy(\n    {\n      name: 'IsEnvelopedVerifiablePresentationType',\n      validator: {\n        validate: (value): boolean => {\n          return Array.isArray(value)\n            ? value.includes(ENVELOPED_VERIFIABLE_PRESENTATION_TYPE)\n            : value === ENVELOPED_VERIFIABLE_PRESENTATION_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_PRESENTATION_TYPE}\"`,\n          validationOptions\n        ),\n      },\n    },\n    validationOptions\n  )\n}\n\nfunction presentationFromDataUri(uri: string) {\n  if (!uri.startsWith('data:')) {\n    throw new CredoError('Invalid Enveloped Verifiable Presentation: \"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 Presentation: \"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/vp+sd-jwt':\n      return W3cV2SdJwtVerifiablePresentation.fromCompact(data)\n\n    case 'application/vp+jwt':\n      return W3cV2JwtVerifiablePresentation.fromCompact(data)\n\n    default:\n      throw new CredoError(`Unsupported Enveloped Verifiable Presentation: ${mimetype} not recognized`)\n  }\n}\n\nfunction presentationToDataUri(presentation: W3cV2SdJwtVerifiablePresentation | W3cV2JwtVerifiablePresentation) {\n  if (presentation instanceof W3cV2SdJwtVerifiablePresentation) {\n    return `data:application/vp+sd-jwt,${presentation.encoded}`\n  }\n\n  if (presentation instanceof W3cV2JwtVerifiablePresentation) {\n    return `data:application/vp+jwt,${presentation.encoded}`\n  }\n\n  throw new CredoError('Unsupported Verifiable Presentation instance')\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAqBA,IAAa,uCAAb,MAAa,qCAAqC;CAIhD,AAAO,YAAY,SAAsD;AACvE,MAAI,SAAS;AACX,QAAK,UAAU,QAAQ,WAAW;AAClC,QAAK,KAAK,QAAQ;AAClB,QAAK,OAAO,QAAQ,QAAQ;AAC5B,QAAK,yBAAyB,wBAAwB,QAAQ,GAAG;;;CAIrE,OAAc,2BACZ,cACsC;AACtC,SAAO,IAAI,qCAAqC;GAC9C,IAAI,sBAAsB,aAAa;GACvC,SAAS;GACT,MAAM;GACP,CAAC;;;;;CAgBJ,IAAW,wBAAwB;AACjC,MAAI,CAAC,KAAK,uBACR,MAAK,yBAAyB,wBAAwB,KAAK,GAAG;AAGhE,SAAO,KAAK;;;;;CAMd,IAAW,uBAA0C;AACnD,SAAO,KAAK,sBAAsB;;;;;CAMpC,IAAW,cAA6D;AACtE,SAAO,KAAK,sBAAsB;;;YAtDnC,SAAS;;CAsBT,OAAO,EAAE,MAAM,YAAY,CAAC;CAC5B,0BAA0B;EAAE,aAAa;EAAM,mBAAmB;EAA4B,CAAC;;;YAG/F,WAAW;YAGX,uCAAuC;AA6B1C,SAAgB,sCAAsC,mBA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