{"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/ClaimFormat'\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    // 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 (header.alg === 'none') {\n      throw new CredoError(`The provided W3C VP JWT uses 'alg'='none', which is not allowed.`)\n    }\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 ('cty' in header && header.cty !== 'vp') {\n      throw new CredoError(`The provided W3C VP JWT does not have the correct 'cty' header.`)\n    }\n\n    if (payload.iss && this.resolvedPresentation.holderId) {\n      if (this.resolvedPresentation.holderId !== payload.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,YAAmB,SAAgD;EACjE,KAAK,MAAM,QAAQ;EACnB,KAAK,uBAAuB,gBAAgB,SAAS,QAAQ,IAAI,QAAQ,kBAAkB,mBAAmB,EAC5G,UAAU,MACZ,CAAC;EAGD,KAAK,SAAS;CAChB;CAEA,OAAc,YAAY,SAAiB;EACzC,MAAM,MAAM,IAAI,kBAAkB,OAAO;EAEzC,OAAO,IAAI,+BAA+B,EACxC,IACF,CAAC;CACH;;;;CAeA,IAAW,UAAU;EACnB,OAAO,KAAK,IAAI;CAClB;;;;;;CAOA,IAAW,cAAoC;EAC7C,OAAA;CACF;;;;CAKA,WAAkB;EAEhB,iBAAiB,aAAa,KAAK,oBAAoB;EAGvD,MAAM,MAAM,KAAK;EACjB,MAAM,SAAS,IAAI;EACnB,MAAM,UAAU,IAAI;EAEpB,IAAI,OAAO,QAAQ,QACjB,MAAM,IAAI,WAAW,kEAAkE;EAGzF,IAAI,SAAS,UAAU,OAAO,QAAQ,UACpC,MAAM,IAAI,WAAW,iEAAiE;EAGxF,IAAI,SAAS,UAAU,OAAO,QAAQ,MACpC,MAAM,IAAI,WAAW,iEAAiE;EAGxF,IAAI,QAAQ,OAAO,KAAK,qBAAqB,UACvC;OAAA,KAAK,qBAAqB,aAAa,QAAQ,KACjD,MAAM,IAAI,WAAW,8EAA8E;EAAA;CAGzG;AACF"}