{"version":3,"file":"DcqlPresentationsToCreate.mjs","names":[],"sources":["../../../../src/modules/dcql/utils/DcqlPresentationsToCreate.ts"],"sourcesContent":["import type { DcqlMdocCredential, DcqlSdJwtVcCredential, DcqlW3cVcCredential } from 'dcql'\nimport type { JsonObject } from '../../../types'\nimport { CredentialMultiInstanceUseMode, canUseInstanceFromCredentialRecord } from '../../../utils/credentialUse'\nimport { MdocRecord } from '../../mdoc'\nimport { SdJwtVcRecord } from '../../sd-jwt-vc'\nimport { ClaimFormat, W3cCredentialRecord, W3cV2CredentialRecord } from '../../vc'\nimport { DcqlError } from '../DcqlError'\nimport type { DcqlCredentialsForRequest } from '../models'\n\n//  - the credentials included in the presentation\nexport interface DcqlSdJwtVcPresentationToCreate {\n  claimFormat: ClaimFormat.SdJwtDc\n  credentialRecord: SdJwtVcRecord\n  disclosedPayload: DcqlSdJwtVcCredential.Claims\n\n  /**\n   * Additional payload to include in the Key Binding JWT\n   */\n  additionalPayload?: JsonObject\n  useMode: CredentialMultiInstanceUseMode\n}\n\nexport interface DcqlJwtVpPresentationToCreate {\n  claimFormat: ClaimFormat.JwtVp\n  credentialRecord: W3cCredentialRecord\n  disclosedPayload: DcqlW3cVcCredential.Claims\n  useMode: CredentialMultiInstanceUseMode\n}\n\nexport interface DcqlLdpVpPresentationToCreate {\n  claimFormat: ClaimFormat.LdpVp\n  credentialRecord: W3cCredentialRecord\n  disclosedPayload: DcqlW3cVcCredential.Claims\n  useMode: CredentialMultiInstanceUseMode\n}\n\nexport interface DcqlMdocPresentationToCreate {\n  claimFormat: ClaimFormat.MsoMdoc\n  credentialRecord: MdocRecord\n  disclosedPayload: DcqlMdocCredential.NameSpaces\n  useMode: CredentialMultiInstanceUseMode\n}\n\nexport interface DcqlJwtW3cVpPresentationToCreate {\n  claimFormat: ClaimFormat.JwtW3cVp\n  credentialRecord: W3cV2CredentialRecord\n  disclosedPayload: DcqlW3cVcCredential.Claims\n}\n\nexport interface DcqlSdJwtW3cVpPresentationToCreate {\n  claimFormat: ClaimFormat.SdJwtW3cVp\n  credentialRecord: W3cV2CredentialRecord\n  disclosedPayload: DcqlW3cVcCredential.Claims\n}\n\ntype DcqlPresentationToCreate =\n  | DcqlSdJwtVcPresentationToCreate\n  | DcqlJwtVpPresentationToCreate\n  | DcqlLdpVpPresentationToCreate\n  | DcqlMdocPresentationToCreate\n  | DcqlJwtW3cVpPresentationToCreate\n  | DcqlSdJwtW3cVpPresentationToCreate\n\nexport type DcqlPresentationsToCreate = Record<string, [DcqlPresentationToCreate, ...DcqlPresentationToCreate[]]>\n\nexport function dcqlGetPresentationsToCreate(\n  credentialsForInputDescriptor: DcqlCredentialsForRequest\n): DcqlPresentationsToCreate {\n  const presentationsToCreate: DcqlPresentationsToCreate = {}\n\n  for (const [credentialQueryId, matches] of Object.entries(credentialsForInputDescriptor)) {\n    for (const match of matches) {\n      const matchIndex = matches.indexOf(match)\n      let presentationToCreate: DcqlPresentationToCreate\n\n      const useMode = match.useMode ?? CredentialMultiInstanceUseMode.NewOrFirst\n      if (!canUseInstanceFromCredentialRecord({ credentialRecord: match.credentialRecord, useMode })) {\n        throw new DcqlError(\n          `Unable to prepare presentation for credential at index ${matchIndex} for query credential '${credentialQueryId}'. No new credential instance available on the credential record.`\n        )\n      }\n\n      switch (match.claimFormat) {\n        case ClaimFormat.SdJwtDc:\n          presentationToCreate = {\n            claimFormat: ClaimFormat.SdJwtDc,\n            credentialRecord: match.credentialRecord,\n            disclosedPayload: match.disclosedPayload as DcqlSdJwtVcCredential.Claims,\n            additionalPayload: match.additionalPayload,\n            useMode,\n          }\n          break\n        case ClaimFormat.MsoMdoc:\n          presentationToCreate = {\n            claimFormat: ClaimFormat.MsoMdoc,\n            credentialRecord: match.credentialRecord,\n            disclosedPayload: match.disclosedPayload as DcqlMdocCredential.NameSpaces,\n            useMode,\n          }\n          break\n        case ClaimFormat.JwtW3cVc:\n        case ClaimFormat.SdJwtW3cVc:\n          presentationToCreate = {\n            claimFormat: match.claimFormat === ClaimFormat.JwtW3cVc ? ClaimFormat.JwtW3cVp : ClaimFormat.SdJwtW3cVp,\n            credentialRecord: match.credentialRecord,\n            disclosedPayload: match.disclosedPayload as DcqlW3cVcCredential.Claims,\n          }\n          break\n\n        default:\n          presentationToCreate = {\n            claimFormat: match.claimFormat === ClaimFormat.LdpVc ? ClaimFormat.LdpVp : ClaimFormat.JwtVp,\n            credentialRecord: match.credentialRecord,\n            disclosedPayload: match.disclosedPayload as DcqlW3cVcCredential.Claims,\n            useMode,\n          }\n      }\n\n      if (!presentationsToCreate[credentialQueryId]) {\n        presentationsToCreate[credentialQueryId] = [presentationToCreate]\n      } else {\n        presentationsToCreate[credentialQueryId].push(presentationToCreate)\n      }\n    }\n  }\n\n  return presentationsToCreate\n}\n"],"mappings":";;;;;;;;;;;AAiEA,SAAgB,6BACd,+BAC2B;CAC3B,MAAM,wBAAmD,EAAE;AAE3D,MAAK,MAAM,CAAC,mBAAmB,YAAY,OAAO,QAAQ,8BAA8B,CACtF,MAAK,MAAM,SAAS,SAAS;EAC3B,MAAM,aAAa,QAAQ,QAAQ,MAAM;EACzC,IAAI;EAEJ,MAAM,UAAU,MAAM,WAAW,+BAA+B;AAChE,MAAI,CAAC,mCAAmC;GAAE,kBAAkB,MAAM;GAAkB;GAAS,CAAC,CAC5F,OAAM,IAAI,UACR,0DAA0D,WAAW,yBAAyB,kBAAkB,mEACjH;AAGH,UAAQ,MAAM,aAAd;GACE,KAAK,YAAY;AACf,2BAAuB;KACrB,aAAa,YAAY;KACzB,kBAAkB,MAAM;KACxB,kBAAkB,MAAM;KACxB,mBAAmB,MAAM;KACzB;KACD;AACD;GACF,KAAK,YAAY;AACf,2BAAuB;KACrB,aAAa,YAAY;KACzB,kBAAkB,MAAM;KACxB,kBAAkB,MAAM;KACxB;KACD;AACD;GACF,KAAK,YAAY;GACjB,KAAK,YAAY;AACf,2BAAuB;KACrB,aAAa,MAAM,gBAAgB,YAAY,WAAW,YAAY,WAAW,YAAY;KAC7F,kBAAkB,MAAM;KACxB,kBAAkB,MAAM;KACzB;AACD;GAEF,QACE,wBAAuB;IACrB,aAAa,MAAM,gBAAgB,YAAY,QAAQ,YAAY,QAAQ,YAAY;IACvF,kBAAkB,MAAM;IACxB,kBAAkB,MAAM;IACxB;IACD;;AAGL,MAAI,CAAC,sBAAsB,mBACzB,uBAAsB,qBAAqB,CAAC,qBAAqB;MAEjE,uBAAsB,mBAAmB,KAAK,qBAAqB;;AAKzE,QAAO"}