{"version":3,"file":"presentationSelection.mjs","names":[],"sources":["../../../../src/modules/dif-presentation-exchange/utils/presentationSelection.ts"],"sourcesContent":["import { JSONPath } from '@astronautlabs/jsonpath'\nimport { CredoError } from '../../../error'\nimport type { SingleOrArray } from '../../../types'\nimport { MdocDeviceResponse } from '../../mdoc'\nimport type { W3cJsonLdVerifiableCredential, W3cJwtVerifiableCredential } from '../../vc'\nimport {\n  ClaimFormat,\n  W3cJsonLdVerifiablePresentation,\n  W3cJwtVerifiablePresentation,\n  W3cV2JwtVerifiablePresentation,\n} from '../../vc'\nimport { W3cV2SdJwtVerifiablePresentation } from '../../vc/sd-jwt-vc'\nimport type {\n  DifPresentationExchangeDefinition,\n  DifPresentationExchangeSubmission,\n  VerifiablePresentation,\n} from '../models'\n\nexport type DifPexPresentationWithDescriptor = ReturnType<\n  typeof extractPresentationsWithDescriptorsFromSubmission\n>[number]\nexport function extractPresentationsWithDescriptorsFromSubmission(\n  presentations: SingleOrArray<VerifiablePresentation>,\n  submission: DifPresentationExchangeSubmission,\n  definition: DifPresentationExchangeDefinition\n) {\n  return submission.descriptor_map.map((descriptor) => {\n    const [presentation] = JSONPath.query(presentations, descriptor.path) as [VerifiablePresentation | undefined]\n    const inputDescriptor = definition.input_descriptors.find(({ id }) => id === descriptor.id)\n\n    if (!presentation) {\n      throw new CredoError(\n        `Unable to extract presentation at path '${descriptor.path}' for submission descriptor '${descriptor.id}'`\n      )\n    }\n\n    if (!inputDescriptor) {\n      throw new Error(\n        `Unable to extract input descriptor '${descriptor.id}' from definition '${definition.id}' for submission '${submission.id}'`\n      )\n    }\n\n    if (presentation instanceof MdocDeviceResponse) {\n      const document = presentation.deviceResponse.documents?.find((document) => document.docType === descriptor.id)\n      if (!document) {\n        throw new Error(\n          `Unable to extract mdoc document with doctype '${descriptor.id}' from mdoc device response for submission '${submission.id}'.`\n        )\n      }\n\n      return {\n        claimFormat: ClaimFormat.MsoMdoc,\n        descriptor,\n        presentation,\n        credential: document,\n        inputDescriptor,\n      } as const\n    }\n    if (\n      presentation instanceof W3cJwtVerifiablePresentation ||\n      presentation instanceof W3cJsonLdVerifiablePresentation\n    ) {\n      if (!descriptor.path_nested) {\n        throw new Error(\n          `Submission descriptor '${descriptor.id}' for submission '${submission.id}' has no 'path_nested' but presentation is format '${presentation.claimFormat}'`\n        )\n      }\n\n      const [verifiableCredential] = JSONPath.query(\n        // Path is `$.vp.verifiableCredential[]` in case of jwt vp\n        presentation.claimFormat === ClaimFormat.JwtVp ? { vp: presentation } : presentation,\n        descriptor.path_nested.path\n      ) as [W3cJwtVerifiableCredential | W3cJsonLdVerifiableCredential | undefined]\n\n      if (!verifiableCredential) {\n        throw new CredoError(\n          `Unable to extract credential at path '${descriptor.path_nested.path}' from presentation at path '${descriptor.path}' for submission descriptor '${descriptor.id}'`\n        )\n      }\n\n      return {\n        claimFormat: presentation.claimFormat,\n        descriptor,\n        presentation,\n        credential: verifiableCredential,\n        inputDescriptor,\n      } as const\n    }\n    if (\n      presentation instanceof W3cV2JwtVerifiablePresentation ||\n      presentation instanceof W3cV2SdJwtVerifiablePresentation\n    ) {\n      throw new CredoError(\n        `Presentation format '${presentation.claimFormat}' is not supported with DIF Presentation Exchange`\n      )\n    }\n    return {\n      claimFormat: ClaimFormat.SdJwtDc,\n      descriptor,\n      presentation,\n      credential: presentation,\n      inputDescriptor,\n    } as const\n  })\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAqBA,SAAgB,kDACd,eACA,YACA,YACA;AACA,QAAO,WAAW,eAAe,KAAK,eAAe;EACnD,MAAM,CAAC,gBAAgB,SAAS,MAAM,eAAe,WAAW,KAAK;EACrE,MAAM,kBAAkB,WAAW,kBAAkB,MAAM,EAAE,SAAS,OAAO,WAAW,GAAG;AAE3F,MAAI,CAAC,aACH,OAAM,IAAI,WACR,2CAA2C,WAAW,KAAK,+BAA+B,WAAW,GAAG,GACzG;AAGH,MAAI,CAAC,gBACH,OAAM,IAAI,MACR,uCAAuC,WAAW,GAAG,qBAAq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