{"version":3,"file":"CredentialIssuancePurpose.cjs","sources":["../../../src/vc/CredentialIssuancePurpose.js"],"sourcesContent":["/*!\n * Copyright (c) 2019-2020 Digital Bazaar, Inc. All rights reserved.\n */\n\nimport jsonld from 'jsonld';\nimport jsigs from 'jsonld-signatures';\nimport { expandJSONLD } from './helpers';\nimport { Bls12381BBDT16MacProofDockName } from './crypto/constants';\nimport { possibleDids } from '../types/did/onchain/typed-did';\n\nconst { AssertionProofPurpose } = jsigs.purposes;\n\n/**\n * Creates a proof purpose that will validate whether or not the verification\n * method in a proof was authorized by its declared controller for the\n * proof's purpose.\n */\nexport default class CredentialIssuancePurpose extends AssertionProofPurpose {\n  /**\n   * @param {object} options - The options to use.\n   * @param {object} [options.controller] - The description of the controller,\n   *   if it is not to be dereferenced via a `documentLoader`.\n   * @param {string|Date|number} [options.date] - The expected date for\n   *   the creation of the proof.\n   * @param {number} [options.maxTimestampDelta=Infinity] - A maximum number\n   *   of seconds that the date on the signature can deviate from.\n   */\n  constructor({ controller, date, maxTimestampDelta } = {}) {\n    super({ controller, date, maxTimestampDelta });\n  }\n\n  /**\n   * Validates the purpose of a proof. This method is called during\n   * proof verification, after the proof value has been checked against the\n   * given verification method (in the case of a digital signature, the\n   * signature has been cryptographically verified against the public key).\n   *\n   * @param {object} proof - The proof to validate.\n   * @param {object} options - The options to use.\n   * @param {object} options.document - The document whose signature is\n   *   being verified.\n   * @param {object} options.suite - Signature suite used in\n   *   the proof.\n   * @param {string} options.verificationMethod - Key id URL to the paired\n   *   public key.\n   * @param {object} [options.documentLoader] - A document loader.\n   * @param {object} [options.expansionMap] - An expansion map.\n   *\n   * @throws {Error} If verification method not authorized by controller.\n   * @throws {Error} If proof's created timestamp is out of range.\n   *\n   * @returns {Promise<{valid: boolean, error: Error}>} Resolves on completion.\n   */\n  async validate(\n    proof,\n    {\n      document, suite, verificationMethod, documentLoader, expansionMap,\n    },\n  ) {\n    try {\n      if (proof.type === Bls12381BBDT16MacProofDockName) {\n        return { valid: true, error: null };\n      }\n      const result = await super.validate(proof, {\n        document,\n        suite,\n        verificationMethod,\n        documentLoader,\n        expansionMap,\n      });\n\n      if (!result.valid) {\n        throw result.error;\n      }\n\n      const expandedDoc = await expandJSONLD(document, {\n        documentLoader,\n      });\n\n      const issuer = jsonld.getValues(\n        expandedDoc,\n        'https://www.w3.org/2018/credentials#issuer',\n      );\n\n      if (!issuer || issuer.length === 0) {\n        throw new Error('Credential issuer is required.');\n      }\n\n      if (\n        possibleDids(issuer[0]['@id']).every(\n          (did) => did !== result.controller.id,\n        )\n      ) {\n        throw new Error(\n          'Credential issuer must match the verification method controller.',\n        );\n      }\n\n      return { valid: true, error: null };\n    } catch (error) {\n      return { valid: false, error };\n    }\n  }\n}\n"],"names":["jsigs","Bls12381BBDT16MacProofDockName","expandJSONLD","jsonld","possibleDids"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AACA;AACA;;;AAQA,MAAM,EAAE,qBAAqB,EAAE,GAAGA,sBAAK,CAAC,QAAQ;;AAEhD;AACA;AACA;AACA;AACA;AACe,MAAM,yBAAyB,SAAS,qBAAqB,CAAC;AAC7E;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,WAAW,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,iBAAiB,EAAE,GAAG,EAAE,EAAE;AAC5D,IAAI,KAAK,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,iBAAiB,EAAE,CAAC;AAClD,EAAE;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,MAAM,QAAQ;AAChB,IAAI,KAAK;AACT,IAAI;AACJ,MAAM,QAAQ,EAAE,KAAK,EAAE,k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