{"version":3,"file":"presentationsToCreate.mjs","names":[],"sources":["../../../../src/modules/dif-presentation-exchange/utils/presentationsToCreate.ts"],"sourcesContent":["import type { JsonObject } from '../../../types'\nimport { MdocRecord } from '../../mdoc'\nimport { SdJwtVcRecord } from '../../sd-jwt-vc'\nimport { ClaimFormat, W3cCredentialRecord } from '../../vc'\nimport type { DifPexInputDescriptorToCredentials } from '../models'\n\n//  - the credentials included in the presentation\nexport interface SdJwtVcPresentationToCreate {\n  claimFormat: ClaimFormat.SdJwtDc\n  subjectIds: [] // subject is included in the cnf of the sd-jwt and automatically extracted by PEX\n  verifiableCredentials: [\n    {\n      credential: SdJwtVcRecord\n      inputDescriptorId: string\n\n      /**\n       * Additional payload to include in the Key Binding JWT\n       */\n      additionalPayload?: JsonObject\n    },\n  ] // only one credential supported for SD-JWT-VC\n}\n\nexport interface JwtVpPresentationToCreate {\n  claimFormat: ClaimFormat.JwtVp\n  subjectIds: [string] // only one subject id supported for JWT VP\n  verifiableCredentials: Array<{\n    credential: W3cCredentialRecord\n    inputDescriptorId: string\n  }> // multiple credentials supported for JWT VP\n}\n\nexport interface LdpVpPresentationToCreate {\n  claimFormat: ClaimFormat.LdpVp\n  // NOTE: we only support one subject id at the moment as we don't have proper\n  // support yet for adding multiple proofs to an LDP-VP\n  subjectIds: undefined | [string]\n  verifiableCredentials: Array<{\n    credential: W3cCredentialRecord\n    inputDescriptorId: string\n  }> // multiple credentials supported for LDP VP\n}\n\nexport interface MdocPresentationToCreate {\n  claimFormat: ClaimFormat.MsoMdoc\n  subjectIds: []\n  verifiableCredentials: [\n    {\n      credential: MdocRecord\n      inputDescriptorId: string\n    },\n  ] // only one credential supported for MDOC\n}\n\nexport type PresentationToCreate =\n  | SdJwtVcPresentationToCreate\n  | JwtVpPresentationToCreate\n  | LdpVpPresentationToCreate\n  | MdocPresentationToCreate\n\n// FIXME: we should extract supported format form top-level presentation definition, and input_descriptor as well\n// to make sure the presentation we are going to create is a presentation format supported by the verifier.\n// In addition we should allow to pass an override 'format' object, as specification like OID4VP do not use the\n// PD formats, but define their own.\nexport function getPresentationsToCreate(credentialsForInputDescriptor: DifPexInputDescriptorToCredentials) {\n  const presentationsToCreate: Array<PresentationToCreate> = []\n\n  // We map all credentials for a input descriptor to the different subject ids. Each subjectId will need\n  // to create a separate proof (either on the same presentation or if not allowed by proof format on separate)\n  // presentations\n  for (const [inputDescriptorId, credentials] of Object.entries(credentialsForInputDescriptor)) {\n    for (const credential of credentials) {\n      if (credential.claimFormat === ClaimFormat.SdJwtDc) {\n        // SD-JWT-VC always needs it's own presentation\n        presentationsToCreate.push({\n          claimFormat: ClaimFormat.SdJwtDc,\n          subjectIds: [],\n          verifiableCredentials: [\n            {\n              inputDescriptorId,\n              credential: credential.credentialRecord,\n              additionalPayload: credential.additionalPayload,\n            },\n          ],\n        })\n      } else if (credential.credentialRecord instanceof MdocRecord) {\n        presentationsToCreate.push({\n          claimFormat: ClaimFormat.MsoMdoc,\n          verifiableCredentials: [{ inputDescriptorId, credential: credential.credentialRecord }],\n          subjectIds: [],\n        })\n      } else {\n        const subjectId = credential.credentialRecord.firstCredential.credentialSubjectIds[0]\n\n        // NOTE: we only support one subjectId per VP -- once we have proper support\n        // for multiple proofs on an LDP-VP we can add multiple subjectIds to a single VP for LDP-vp only\n        const expectedClaimFormat =\n          credential.credentialRecord.firstCredential.claimFormat === ClaimFormat.LdpVc\n            ? ClaimFormat.LdpVp\n            : ClaimFormat.JwtVp\n\n        const matchingClaimFormatAndSubject = presentationsToCreate.find(\n          (p): p is JwtVpPresentationToCreate =>\n            p.claimFormat === expectedClaimFormat && Boolean(p.subjectIds?.includes(subjectId))\n        )\n\n        if (matchingClaimFormatAndSubject) {\n          matchingClaimFormatAndSubject.verifiableCredentials.push({\n            inputDescriptorId,\n            credential: credential.credentialRecord,\n          })\n        } else {\n          presentationsToCreate.push({\n            claimFormat: expectedClaimFormat,\n            subjectIds: [subjectId],\n            verifiableCredentials: [{ credential: credential.credentialRecord, inputDescriptorId }],\n          })\n        }\n      }\n    }\n  }\n\n  return presentationsToCreate\n}\n"],"mappings":";;;;;;;;;AAgEA,SAAgB,yBAAyB,+BAAmE;CAC1G,MAAM,wBAAqD,EAAE;AAK7D,MAAK,MAAM,CAAC,mBAAmB,gBAAgB,OAAO,QAAQ,8BAA8B,CAC1F,MAAK,MAAM,cAAc,YACvB,KAAI,WAAW,gBAAgB,YAAY,QAEzC,uBAAsB,KAAK;EACzB,aAAa,YAAY;EACzB,YAAY,EAAE;EACd,uBAAuB,CACrB;GACE;GACA,YAAY,WAAW;GACvB,mBAAmB,WAAW;GAC/B,CACF;EACF,CAAC;UACO,WAAW,4BAA4B,WAChD,uBAAsB,KAAK;EACzB,aAAa,YAAY;EACzB,uBAAuB,CAAC;GAAE;GAAmB,YAAY,WAAW;GAAkB,CAAC;EACvF,YAAY,EAAE;EACf,CAAC;MACG;EACL,MAAM,YAAY,WAAW,iBAAiB,gBAAgB,qBAAqB;EAInF,MAAM,sBACJ,WAAW,iBAAiB,gBAAgB,gBAAgB,YAAY,QACpE,YAAY,QACZ,YAAY;EAElB,MAAM,gCAAgC,sBAAsB,MACzD,MACC,EAAE,gBAAgB,uBAAuB,QAAQ,EAAE,YAAY,SAAS,UAAU,CAAC,CACtF;AAED,MAAI,8BACF,+BAA8B,sBAAsB,KAAK;GACvD;GACA,YAAY,WAAW;GACxB,CAAC;MAEF,uBAAsB,KAAK;GACzB,aAAa;GACb,YAAY,CAAC,UAAU;GACvB,uBAAuB,CAAC;IAAE,YAAY,WAAW;IAAkB;IAAmB,CAAC;GACxF,CAAC;;AAMV,QAAO"}