'use strict';

var jsonld = require('jsonld');
var jsigs = require('jsonld-signatures');
var vc_helpers = require('./helpers.cjs');
var vc_crypto_constants = require('./crypto/constants.cjs');
var types_did_onchain_typedDid_index = require('../types/did/onchain/typed-did/index.cjs');
require('@transmute/json-web-signature');
require('./document-loader.cjs');
require('./contexts.cjs');
require('./contexts/vc-examples-v1.cjs');
require('./contexts/security_context.cjs');
require('./contexts/status-list-21.cjs');
require('./contexts/private-status-list-21.cjs');
require('../utils/json-fetch.cjs');
require('./crypto/Bls12381BBDT16KeyPairDock2024.cjs');
require('@docknetwork/crypto-wasm-ts');
require('./crypto/common/DockCryptoKeyPair.cjs');
require('bs58');
require('../utils/types/bytes.cjs');
require('crypto');
require('../utils/interfaces.cjs');
require('../utils/assert.cjs');
require('../utils/error.cjs');
require('../utils/types/string.cjs');
require('../utils/types/iterable.cjs');
require('../utils/types/object.cjs');
require('../utils/inheritance.cjs');
require('../utils/types/set.cjs');
require('./crypto/Bls12381BBDT16MACDock2024.cjs');
require('@docknetwork/crypto-wasm-ts/lib/index.js');
require('./crypto/common/DockCryptoSignature.cjs');
require('json-stringify-deterministic');
require('semver/preload.js');
require('./crypto/common/CustomLinkedDataSignature.cjs');
require('base64url');
require('../types/did/document/verification-method-ref.cjs');
require('../types/generic/typed-enum.cjs');
require('../utils/types/array.cjs');
require('multiformats/bases/base58');
require('varint');
require('../utils/encoding/ss-58.cjs');
require('blake2b');
require('../types/generic/with-base.cjs');
require('../types/generic/with-catch-null.cjs');
require('../types/generic/with-eq.cjs');
require('../types/generic/any-of.cjs');
require('../types/generic/with-from.cjs');
require('../types/generic/with-from-json-bytes.cjs');
require('../types/generic/typed-null.cjs');
require('../types/generic/create-placeholder.cjs');
require('../types/generic/typed-bytes.cjs');
require('../types/generic/typed-string.cjs');
require('../types/generic/typed-array.cjs');
require('../types/generic/typed-tuple.cjs');
require('../types/generic/typed-number.cjs');
require('../types/generic/typed-set.cjs');
require('../types/generic/typed-map.cjs');
require('../types/generic/typed-struct.cjs');
require('uuid');
require('js-sha256');
require('../types/generic/with-qualifier.cjs');
require('../types/generic/utils.cjs');
require('../types/generic/typed-bytes-array.cjs');
require('../types/did/onchain/typed-did/cheqd-did.cjs');
require('../types/did/onchain/constants.cjs');
require('../types/generic/typed-uuid.cjs');
require('../types/did/onchain/typed-did/did-method-key/did-method-key-public-key.cjs');
require('../utils/encoding/base-58-btc.cjs');
require('../types/generic/sized.cjs');
require('../utils/types/number.cjs');
require('../types/public-keys/public-key.cjs');
require('../types/public-keys/public-key-x25519-value.cjs');
require('../types/public-keys/public-key-ed25519-value.cjs');
require('../types/public-keys/public-key-secp256k1-value.cjs');
require('../types/public-keys/public-key-sr25519-value.cjs');
require('../types/did/onchain/typed-did/signature.cjs');
require('../types/did/onchain/typed-did/did-method-key/did-method-key-signature.cjs');
require('../types/signatures/signature-value.cjs');
require('../types/signatures/signature-ed25519-value.cjs');
require('../types/signatures/signature-secp256k1-value.cjs');
require('../types/signatures/signature.cjs');
require('../types/signatures/signature-sr25519-value.cjs');
require('../types/did/onchain/typed-did/dock-did-value.cjs');
require('./jws.cjs');
require('./crypto/Bls12381BBDT16MACProofDock2024.cjs');
require('./crypto/common/DockCryptoSignatureProof.cjs');
require('./crypto/Bls12381BBSKeyPairDock2023.cjs');
require('./crypto/Bls12381BBSSignatureDock2022.cjs');
require('./crypto/Bls12381G2KeyPairDock2022.cjs');
require('./crypto/Bls12381BBSSignatureDock2023.cjs');
require('./crypto/Bls12381BBSSignatureProofDock2022.cjs');
require('./crypto/Bls12381BBSSignatureProofDock2023.cjs');
require('./crypto/Bls12381BDDT16KeyPairDock2024.cjs');
require('./crypto/Bls12381BDDT16MACDock2024.cjs');
require('./crypto/Bls12381BDDT16MACProofDock2024.cjs');
require('./crypto/Bls12381PSKeyPairDock2023.cjs');
require('./crypto/Bls12381PSSignatureDock2023.cjs');
require('./crypto/Bls12381PSSignatureProofDock2023.cjs');
require('./crypto/EcdsaSecp256k1Signature2019.cjs');
require('./crypto/EcdsaSecp256k1VerificationKey2019.cjs');
require('@juanelas/base64');
require('../keypairs/keypair-secp256k1.cjs');
require('elliptic');
require('../keypairs/dock-keypair.cjs');
require('../utils/encoding/multibase.cjs');
require('./crypto/Ed25519Signature2018.cjs');
require('./crypto/Ed25519VerificationKey2018.cjs');
require('../keypairs/keypair-ed25519.cjs');
require('@stablelib/ed25519');
require('./crypto/Ed25519Signature2020.cjs');
require('./crypto/Ed25519VerificationKey2020.cjs');
require('./crypto/JsonWebSignature2020.cjs');
require('../keypairs/did-keypair.cjs');
require('../types/did/onchain/did-key.cjs');
require('../types/offchain-signatures/params/index.cjs');
require('../types/offchain-signatures/params/value.cjs');
require('../types/generic/option.cjs');
require('../types/offchain-signatures/curve-type.cjs');
require('../types/offchain-signatures/params/id.cjs');
require('../types/generic/with-from-dock-id.cjs');
require('../types/offchain-signatures/public-keys/index.cjs');
require('../types/generic/with-prop.cjs');
require('../types/generic/with-props.cjs');
require('../types/offchain-signatures/public-keys/value.cjs');
require('../types/offchain-signatures/public-keys/ref.cjs');
require('../types/offchain-signatures/public-keys/id.cjs');
require('../types/did/onchain/verification-relationship.cjs');
require('../types/did/document/verification-method-ref-or-ident-ref.cjs');
require('../types/did/document/ident-ref.cjs');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var jsonld__default = /*#__PURE__*/_interopDefault(jsonld);
var jsigs__default = /*#__PURE__*/_interopDefault(jsigs);

/*!
 * Copyright (c) 2019-2020 Digital Bazaar, Inc. All rights reserved.
 */


const { AssertionProofPurpose } = jsigs__default.default.purposes;

/**
 * Creates a proof purpose that will validate whether or not the verification
 * method in a proof was authorized by its declared controller for the
 * proof's purpose.
 */
class CredentialIssuancePurpose extends AssertionProofPurpose {
  /**
   * @param {object} options - The options to use.
   * @param {object} [options.controller] - The description of the controller,
   *   if it is not to be dereferenced via a `documentLoader`.
   * @param {string|Date|number} [options.date] - The expected date for
   *   the creation of the proof.
   * @param {number} [options.maxTimestampDelta=Infinity] - A maximum number
   *   of seconds that the date on the signature can deviate from.
   */
  constructor({ controller, date, maxTimestampDelta } = {}) {
    super({ controller, date, maxTimestampDelta });
  }

  /**
   * Validates the purpose of a proof. This method is called during
   * proof verification, after the proof value has been checked against the
   * given verification method (in the case of a digital signature, the
   * signature has been cryptographically verified against the public key).
   *
   * @param {object} proof - The proof to validate.
   * @param {object} options - The options to use.
   * @param {object} options.document - The document whose signature is
   *   being verified.
   * @param {object} options.suite - Signature suite used in
   *   the proof.
   * @param {string} options.verificationMethod - Key id URL to the paired
   *   public key.
   * @param {object} [options.documentLoader] - A document loader.
   * @param {object} [options.expansionMap] - An expansion map.
   *
   * @throws {Error} If verification method not authorized by controller.
   * @throws {Error} If proof's created timestamp is out of range.
   *
   * @returns {Promise<{valid: boolean, error: Error}>} Resolves on completion.
   */
  async validate(
    proof,
    {
      document, suite, verificationMethod, documentLoader, expansionMap,
    },
  ) {
    try {
      if (proof.type === vc_crypto_constants.Bls12381BBDT16MacProofDockName) {
        return { valid: true, error: null };
      }
      const result = await super.validate(proof, {
        document,
        suite,
        verificationMethod,
        documentLoader,
        expansionMap,
      });

      if (!result.valid) {
        throw result.error;
      }

      const expandedDoc = await vc_helpers.expandJSONLD(document, {
        documentLoader,
      });

      const issuer = jsonld__default.default.getValues(
        expandedDoc,
        'https://www.w3.org/2018/credentials#issuer',
      );

      if (!issuer || issuer.length === 0) {
        throw new Error('Credential issuer is required.');
      }

      if (
        types_did_onchain_typedDid_index.possibleDids(issuer[0]['@id']).every(
          (did) => did !== result.controller.id,
        )
      ) {
        throw new Error(
          'Credential issuer must match the verification method controller.',
        );
      }

      return { valid: true, error: null };
    } catch (error) {
      return { valid: false, error };
    }
  }
}

module.exports = CredentialIssuancePurpose;
//# sourceMappingURL=CredentialIssuancePurpose.cjs.map
