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

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


const { AssertionProofPurpose } = jsigs.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 === 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 expandJSONLD(document, {
        documentLoader,
      });

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

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

      if (
        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 };
    }
  }
}

export { CredentialIssuancePurpose as default };
//# sourceMappingURL=CredentialIssuancePurpose.js.map
