{"version":3,"file":"W3cV2VerifiablePresentation.mjs","names":[],"sources":["../../../../../src/modules/vc/models/presentation/W3cV2VerifiablePresentation.ts"],"sourcesContent":["import { Transform, TransformationType } from 'class-transformer'\nimport { ValidationError } from 'class-validator'\nimport { Jwt } from '../../../../crypto/jose/jwt/Jwt'\nimport { ClassValidationError, CredoError } from '../../../../error'\nimport type { SingleOrArray } from '../../../../types'\nimport { JsonTransformer } from '../../../../utils'\nimport {\n  W3cV2DataIntegrityVerifiablePresentation,\n  type W3cV2DataIntegrityVerifiablePresentationOptions,\n} from '../../data-integrity'\nimport {\n  W3cV2JwtVerifiablePresentation,\n  type W3cV2JwtVerifiablePresentationOptions,\n} from '../../jwt-vc/W3cV2JwtVerifiablePresentation'\nimport {\n  W3cV2SdJwtVerifiablePresentation,\n  type W3cV2SdJwtVerifiablePresentationOptions,\n} from '../../sd-jwt-vc/W3cV2SdJwtVerifiablePresentation'\nimport { ClaimFormat } from '../ClaimFormat'\nimport { presentationFromDataUri } from './W3cV2EnvelopedVerifiablePresentationCodec'\nimport { isEmbeddedDataIntegrityPresentationEntry, W3cV2Presentation } from './W3cV2Presentation'\n\nexport const decodeW3cV2VerifiablePresentation = (value: unknown) => {\n  try {\n    if (typeof value !== 'string') {\n      throw new CredoError('Expected a plain string encoding')\n    }\n\n    const trimmedValue = value.trim()\n\n    try {\n      const parsedJson = JSON.parse(trimmedValue)\n      if (isEmbeddedDataIntegrityPresentationEntry(parsedJson)) {\n        return new W3cV2DataIntegrityVerifiablePresentation({\n          securedPresentation: parsedJson,\n          resolvedPresentation: JsonTransformer.fromJSON(parsedJson, W3cV2Presentation, { validate: false }),\n        })\n      }\n    } catch {\n      // Not JSON; continue with envelope/compact encodings.\n    }\n\n    if (trimmedValue.startsWith('data:')) return presentationFromDataUri(trimmedValue)\n\n    const issuerSignedCompact = trimmedValue.split('~')[0]\n    if (issuerSignedCompact !== trimmedValue) {\n      if (!Jwt.format.test(issuerSignedCompact)) {\n        throw new CredoError(\"Unsupported presentation string encoding. Expected compact 'vp+sd-jwt'.\")\n      }\n\n      const typ = Jwt.fromSerializedJwt(issuerSignedCompact).header.typ\n\n      if (typ === 'vc+sd-jwt' || typ === 'dc+sd-jwt') {\n        throw new CredoError('Value is a W3C SD-JWT VC, but a W3C SD-JWT VP was expected')\n      }\n\n      if (typ && typ !== 'vp+sd-jwt') {\n        throw new CredoError(`Unsupported W3C SD-JWT VP typ '${typ}'. Expected 'vp+sd-jwt'.`)\n      }\n\n      return W3cV2SdJwtVerifiablePresentation.fromCompact(trimmedValue)\n    }\n\n    if (!Jwt.format.test(trimmedValue)) {\n      throw new CredoError(\"Unsupported presentation string encoding. Expected compact 'vp+jwt'.\")\n    }\n\n    const typ = Jwt.fromSerializedJwt(trimmedValue).header.typ\n\n    if (typ === 'vc+jwt' || typ === 'dc+jwt') {\n      throw new CredoError('Value is a W3C VC JWT, but a W3C VP JWT was expected')\n    }\n\n    if (typ === 'vp+sd-jwt') {\n      throw new CredoError(\"Value has typ 'vp+sd-jwt' but is missing SD-JWT disclosures\")\n    }\n\n    if (typ && typ !== 'vp+jwt') {\n      throw new CredoError(`Unsupported W3C VP JWT typ '${typ}'. Expected 'vp+jwt'.`)\n    }\n\n    return W3cV2JwtVerifiablePresentation.fromCompact(trimmedValue)\n  } catch (error) {\n    if (error instanceof ValidationError || error instanceof ClassValidationError) {\n      throw error\n    }\n\n    throw new CredoError(`Value '${value}' is not a valid W3cV2VerifiablePresentation. ${error.message}`)\n  }\n}\n\nconst encodePresentation = (value: unknown) => {\n  if (value instanceof W3cV2JwtVerifiablePresentation) {\n    return value.encoded\n  }\n\n  if (value instanceof W3cV2SdJwtVerifiablePresentation) {\n    return value.encoded\n  }\n\n  if (value instanceof W3cV2DataIntegrityVerifiablePresentation) {\n    return value.encoded\n  }\n\n  throw new CredoError(`Value '${value}' is not a valid W3cV2VerifiablePresentation`)\n}\n\nexport function W3cV2VerifiablePresentationTransformer() {\n  return Transform(({ value, type }: { value: SingleOrArray<unknown>; type: TransformationType }) => {\n    if (type === TransformationType.PLAIN_TO_CLASS) {\n      return Array.isArray(value)\n        ? value.map(decodeW3cV2VerifiablePresentation)\n        : decodeW3cV2VerifiablePresentation(value)\n    }\n\n    if (type === TransformationType.CLASS_TO_PLAIN) {\n      if (Array.isArray(value)) return value.map(encodePresentation)\n      return encodePresentation(value)\n    }\n\n    return value\n  })\n}\n\n/**\n * A Secured W3C Verifiable Presentation (VP) as defined in the W3C VC Data Model 2.0\n * and secured according to VC-JOSE-COSE and VC Data Integrity specifications.\n *\n * It can be one of:\n * - A Verifiable Presentation encoded as a JWT.\n * - A Verifiable Presentation encoded as a SD-JWT.\n * - A Verifiable Presentation with embedded Data Integrity proof(s).\n *\n * This can be further enveloped using a {@link W3cV2EnvelopedVerifiablePresentation}.\n *\n * @see https://www.w3.org/TR/vc-data-model-2.0/\n * @see https://www.w3.org/TR/vc-jose-cose/\n * @see https://www.w3.org/TR/vc-data-integrity/\n *\n */\nexport type W3cV2VerifiablePresentation<\n  Format extends ClaimFormat.JwtW3cVp | ClaimFormat.SdJwtW3cVp | ClaimFormat.DiVp | unknown = unknown,\n> = Format extends ClaimFormat.JwtW3cVp\n  ? W3cV2JwtVerifiablePresentation\n  : Format extends ClaimFormat.SdJwtW3cVp\n    ? W3cV2SdJwtVerifiablePresentation\n    : Format extends ClaimFormat.DiVp\n      ? W3cV2DataIntegrityVerifiablePresentation\n      : W3cV2SdJwtVerifiablePresentation | W3cV2JwtVerifiablePresentation | W3cV2DataIntegrityVerifiablePresentation\n\nexport type W3cV2VerifiablePresentationOptions<\n  Format extends ClaimFormat.JwtW3cVp | ClaimFormat.SdJwtW3cVp | ClaimFormat.DiVp | unknown = unknown,\n> = Format extends ClaimFormat.JwtW3cVp\n  ? W3cV2JwtVerifiablePresentationOptions\n  : Format extends ClaimFormat.SdJwtW3cVp\n    ? W3cV2SdJwtVerifiablePresentationOptions\n    : Format extends ClaimFormat.DiVp\n      ? W3cV2DataIntegrityVerifiablePresentationOptions\n      :\n          | W3cV2SdJwtVerifiablePresentationOptions\n          | W3cV2JwtVerifiablePresentationOptions\n          | W3cV2DataIntegrityVerifiablePresentationOptions\n"],"mappings":";;;;;;;;;;;;;;;AAsBA,MAAa,qCAAqC,UAAmB;CACnE,IAAI;EACF,IAAI,OAAO,UAAU,UACnB,MAAM,IAAI,WAAW,kCAAkC;EAGzD,MAAM,eAAe,MAAM,KAAK;EAEhC,IAAI;GACF,MAAM,aAAa,KAAK,MAAM,YAAY;GAC1C,IAAI,yCAAyC,UAAU,GACrD,OAAO,IAAI,yCAAyC;IAClD,qBAAqB;IACrB,sBAAsB,gBAAgB,SAAS,YAAY,mBAAmB,EAAE,UAAU,MAAM,CAAC;GACnG,CAAC;EAEL,QAAQ,CAER;EAEA,IAAI,aAAa,WAAW,OAAO,GAAG,OAAO,wBAAwB,YAAY;EAEjF,MAAM,sBAAsB,aAAa,MAAM,GAAG,CAAC,CAAC;EACpD,IAAI,wBAAwB,cAAc;GACxC,IAAI,CAAC,IAAI,OAAO,KAAK,mBAAmB,GACtC,MAAM,IAAI,WAAW,yEAAyE;GAGhG,MAAM,MAAM,IAAI,kBAAkB,mBAAmB,CAAC,CAAC,OAAO;GAE9D,IAAI,QAAQ,eAAe,QAAQ,aACjC,MAAM,IAAI,WAAW,4DAA4D;GAGnF,IAAI,OAAO,QAAQ,aACjB,MAAM,IAAI,WAAW,kCAAkC,IAAI,yBAAyB;GAGtF,OAAO,iCAAiC,YAAY,YAAY;EAClE;EAEA,IAAI,CAAC,IAAI,OAAO,KAAK,YAAY,GAC/B,MAAM,IAAI,WAAW,sEAAsE;EAG7F,MAAM,MAAM,IAAI,kBAAkB,YAAY,CAAC,CAAC,OAAO;EAEvD,IAAI,QAAQ,YAAY,QAAQ,UAC9B,MAAM,IAAI,WAAW,sDAAsD;EAG7E,IAAI,QAAQ,aACV,MAAM,IAAI,WAAW,6DAA6D;EAGpF,IAAI,OAAO,QAAQ,UACjB,MAAM,IAAI,WAAW,+BAA+B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