{"version":3,"file":"W3cV2JwtVerifiablePresentation.mjs","names":[],"sources":["../../../../src/modules/vc/jwt-vc/W3cV2JwtVerifiablePresentation.ts"],"sourcesContent":["import { Jwt } from '../../../crypto/jose/jwt/Jwt'\nimport { CredoError } from '../../../error'\nimport { JsonTransformer, MessageValidator } from '../../../utils'\nimport { ClaimFormat } from '../models'\nimport { W3cV2Presentation } from '../models/presentation/W3cV2Presentation'\n\nexport interface W3cV2JwtVerifiablePresentationOptions {\n  jwt: Jwt\n}\n\n/**\n * Represents a Verifiable Presentation encoded as a JWT.\n *\n * @see https://www.w3.org/TR/vc-jose-cose/#securing-vps-with-jose\n */\nexport class W3cV2JwtVerifiablePresentation {\n  public constructor(options: W3cV2JwtVerifiablePresentationOptions) {\n    this.jwt = options.jwt\n    this.resolvedPresentation = JsonTransformer.fromJSON(options.jwt.payload.additionalClaims, W3cV2Presentation, {\n      validate: false,\n    })\n\n    // Validates the JWT and resolved presentation\n    this.validate()\n  }\n\n  public static fromCompact(compact: string) {\n    const jwt = Jwt.fromSerializedJwt(compact)\n\n    return new W3cV2JwtVerifiablePresentation({\n      jwt,\n    })\n  }\n\n  /**\n   * The original JWT.\n   */\n  public readonly jwt: Jwt\n\n  /**\n   * Resolved presentation is the fully resolved {@link W3cV2Presentation} instance.\n   */\n  public readonly resolvedPresentation: W3cV2Presentation\n\n  /**\n   * The encoded version of this presentation.\n   */\n  public get encoded() {\n    return this.jwt.serializedJwt\n  }\n\n  /**\n   * The {@link ClaimFormat} of the presentation.\n   *\n   * For W3C VP JWT credentials this is always `vp+jwt`.\n   */\n  public get claimFormat(): ClaimFormat.JwtW3cVp {\n    return ClaimFormat.JwtW3cVp\n  }\n\n  /**\n   * Validates the JWT and the resolved presentation.\n   */\n  public validate() {\n    // Validate the resolved credential according to the data model\n    MessageValidator.validateSync(this.resolvedPresentation)\n\n    // Basic JWT validations to ensure compliance to the specification\n    const jwt = this.jwt\n    const header = jwt.header\n    const payload = jwt.payload\n\n    if ('typ' in header && header.typ !== 'vp+jwt') {\n      throw new CredoError(`The provided W3C VP JWT does not have the correct 'typ' header.`)\n    }\n\n    if ('cyt' in header && header.cyt !== 'vp') {\n      throw new CredoError(`The provided W3C VP JWT does not have the correct 'cyt' header.`)\n    }\n\n    const iss = header.iss ?? payload.iss\n    if (iss && this.resolvedPresentation.holderId) {\n      if (this.resolvedPresentation.holderId !== iss) {\n        throw new CredoError(`The provided W3C VP JWT has both 'iss' and 'holder' claims, but they differ.`)\n      }\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAeA,IAAa,iCAAb,MAAa,+BAA+B;CAC1C,AAAO,YAAY,SAAgD;AACjE,OAAK,MAAM,QAAQ;AACnB,OAAK,uBAAuB,gBAAgB,SAAS,QAAQ,IAAI,QAAQ,kBAAkB,mBAAmB,EAC5G,UAAU,OACX,CAAC;AAGF,OAAK,UAAU;;CAGjB,OAAc,YAAY,SAAiB;AAGzC,SAAO,IAAI,+BAA+B,EACxC,KAHU,IAAI,kBAAkB,QAAQ,EAIzC,CAAC;;;;;CAgBJ,IAAW,UAAU;AACnB,SAAO,KAAK,IAAI;;;;;;;CAQlB,IAAW,cAAoC;AAC7C,SAAO,YAAY;;;;;CAMrB,AAAO,WAAW;AAEhB,mBAAiB,aAAa,KAAK,qBAAqB;EAGxD,MAAM,MAAM,KAAK;EACjB,MAAM,SAAS,IAAI;EACnB,MAAM,UAAU,IAAI;AAEpB,MAAI,SAAS,UAAU,OAAO,QAAQ,SACpC,OAAM,IAAI,WAAW,kEAAkE;AAGzF,MAAI,SAAS,UAAU,OAAO,QAAQ,KACpC,OAAM,IAAI,WAAW,kEAAkE;EAGzF,MAAM,MAAM,OAAO,OAAO,QAAQ;AAClC,MAAI,OAAO,KAAK,qBAAqB,UACnC;OAAI,KAAK,qBAAqB,aAAa,IACzC,OAAM,IAAI,WAAW,+EAA+E"}