{"version":3,"file":"W3cV2JwtVerifiableCredential.mjs","names":[],"sources":["../../../../src/modules/vc/jwt-vc/W3cV2JwtVerifiableCredential.ts"],"sourcesContent":["import { Jwt } from '../../../crypto/jose/jwt/Jwt'\nimport { CredoError } from '../../../error'\nimport { JsonTransformer, MessageValidator } from '../../../utils'\nimport { ClaimFormat } from '../models/ClaimFormat'\nimport { W3cV2Credential } from '../models/credential/W3cV2Credential'\n\nexport interface W3cV2JwtVerifiableCredentialOptions {\n  jwt: Jwt\n}\n\n/**\n * Represents a Verifiable Credential encoded as a JWT.\n *\n * @see https://www.w3.org/TR/vc-jose-cose/#securing-with-jose\n */\nexport class W3cV2JwtVerifiableCredential {\n  public constructor(options: W3cV2JwtVerifiableCredentialOptions) {\n    this.jwt = options.jwt\n    this.resolvedCredential = JsonTransformer.fromJSON(options.jwt.payload.additionalClaims, W3cV2Credential, {\n      validate: false,\n    })\n\n    // Validates the JWT and resolved credential\n    this.validate()\n  }\n\n  public static fromCompact(compact: string) {\n    const jwt = Jwt.fromSerializedJwt(compact)\n\n    return new W3cV2JwtVerifiableCredential({\n      jwt,\n    })\n  }\n\n  /**\n   * The original JWT.\n   */\n  public readonly jwt: Jwt\n\n  /**\n   * Resolved credential is the fully resolved {@link W3cV2Credential} instance.\n   */\n  public readonly resolvedCredential: W3cV2Credential\n\n  /**\n   * The encoded version of this credential.\n   */\n  public get encoded() {\n    return this.jwt.serializedJwt\n  }\n\n  /**\n   * The {@link ClaimFormat} of the credential.\n   *\n   * For W3C VC JWT credentials this is always `vc+jwt`.\n   */\n  public get claimFormat(): ClaimFormat.JwtW3cVc {\n    return ClaimFormat.JwtW3cVc\n  }\n\n  /**\n   * Validates the JWT and the resolved credential contained.\n   */\n  public validate() {\n    // Validate the resolved credential according to the data model\n    MessageValidator.validateSync(this.resolvedCredential)\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 VC JWT uses 'alg'='none', which is not allowed.`)\n    }\n\n    if ('typ' in header && header.typ !== 'vc+jwt') {\n      throw new CredoError(`The provided W3C VC JWT does not have the correct 'typ' header.`)\n    }\n\n    if ('cty' in header && header.cty !== 'vc') {\n      throw new CredoError(`The provided W3C VC JWT does not have the correct 'cty' header.`)\n    }\n\n    if (payload.iss) {\n      if (this.resolvedCredential.issuerId !== payload.iss) {\n        throw new CredoError(`The provided W3C VC JWT has both 'iss' and 'issuer' claims, but they differ.`)\n      }\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;AAeA,IAAa,+BAAb,MAAa,6BAA6B;CACxC,YAAmB,SAA8C;EAC/D,KAAK,MAAM,QAAQ;EACnB,KAAK,qBAAqB,gBAAgB,SAAS,QAAQ,IAAI,QAAQ,kBAAkB,iBAAiB,EACxG,UAAU,MACZ,CAAC;EAGD,KAAK,SAAS;CAChB;CAEA,OAAc,YAAY,SAAiB;EACzC,MAAM,MAAM,IAAI,kBAAkB,OAAO;EAEzC,OAAO,IAAI,6BAA6B,EACtC,IACF,CAAC;CACH;;;;CAeA,IAAW,UAAU;EACnB,OAAO,KAAK,IAAI;CAClB;;;;;;CAOA,IAAW,cAAoC;EAC7C,OAAA;CACF;;;;CAKA,WAAkB;EAEhB,iBAAiB,aAAa,KAAK,kBAAkB;EAGrD,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,KACN;OAAA,KAAK,mBAAmB,aAAa,QAAQ,KAC/C,MAAM,IAAI,WAAW,8EAA8E;EAAA;CAGzG;AACF"}