import { JsonWebKey } from '@transmute/json-web-signature';
import CustomLinkedDataSignature from './common/CustomLinkedDataSignature.js';
import 'jsonld-signatures';
import 'bs58';
import 'base64url';
import 'jsonld';
import '../../types/did/document/verification-method-ref.js';
import '../../types/generic/typed-enum.js';
import '../../utils/types/array.js';
import '../../utils/inheritance.js';
import '../../utils/types/object.js';
import '../../utils/types/iterable.js';
import '../../utils/types/set.js';
import 'crypto';
import '../../utils/interfaces.js';
import '../../utils/assert.js';
import '../../utils/error.js';
import 'multiformats/bases/base58';
import 'varint';
import '../../utils/encoding/ss-58.js';
import 'blake2b';
import '../../utils/types/bytes.js';
import '../../utils/types/string.js';
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/index.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 './constants.js';
import '../../types/public-keys/public-key-secp256k1-value.js';
import '../../types/public-keys/public-key-sr25519-value.js';
import '../../types/did/onchain/constants.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 '../../types/generic/typed-uuid.js';
import '../../types/did/onchain/typed-did/cheqd-did.js';
import '../jws.js';

const SUITE_CONTEXT_URL = 'https://w3id.org/security/suites/jws-2020/v1';

class JsonWebSignature2020 extends CustomLinkedDataSignature {
  /**
   * Creates a new JsonWebSignature2020 instance
   * @constructor
   * @param {object} config - Configuration options
   */
  constructor(options = {}) {
    const {
      keypair, verifier, signer, useProofValue, verificationMethod,
    } = options;

    super({
      type: 'JsonWebSignature2020',
      LDKeyClass: JsonWebKey,
      contextUrl: SUITE_CONTEXT_URL,
      signer: signer || (keypair && keypair.privateKey && keypair.signer()),
      // alg: keypair && keypair.publicKey && keypair.publicKey.algorithm.name,
      verifier: verifier || (keypair && keypair.verifier()),
      useProofValue,
    });

    this.requiredKeyType = 'JsonWebKey2020';
    this.verificationMethod = verificationMethod || (keypair && keypair.id);
  }

  /**
   * Adds a signature (proofValue) field to the proof object. Called by
   * LinkedDataSignature.createProof().
   *
   * @param {object} options - The options to use.
   * @param {Uint8Array} options.verifyData - Data to be signed (extracted
   *   from document, according to the suite's spec).
   * @param {object} options.proof - Proof object (containing the proofPurpose,
   *   verificationMethod, etc).
   *
   * @returns {Promise<object>} Resolves with the proof containing the signature
   *   value.
   */
  async sign({ verifyData, proof }) {
    const finalProof = { ...proof };
    finalProof.jws = await this.signer.sign({
      data: verifyData,
      detached: true,
    });
    return finalProof;
  }

  /**
   * Verifies the proof signature against the given data.
   *
   * @param {object} options - The options to use.
   * @param {Uint8Array} options.verifyData - Canonicalized hashed data.
   * @param {object} options.verificationMethod - Key object.
   * @param {object} options.proof - The proof to be verified.
   *
   * @returns {Promise<boolean>} Resolves with the verification result.
   */
  async verifySignature({ verifyData, verificationMethod, proof }) {
    const data = verifyData;
    let { verifier } = this;
    if (!verifier) {
      const key = await this.LDKeyClass.from(verificationMethod);
      verifier = key.verifier();
    }
    return verifier.verify({ data, signature: proof.jws });
  }

  /**
   * @param document {object} to be signed.
   * @param proof {object}
   * @param documentLoader {function}
   */
  async getVerificationMethod({ proof, documentLoader }) {
    let { verificationMethod } = proof;

    if (typeof verificationMethod === 'object') {
      verificationMethod = verificationMethod.id;
    }

    if (!verificationMethod) {
      throw new Error('No "verificationMethod" found in proof.');
    }

    const { document } = await documentLoader(verificationMethod);

    if (!document) {
      throw new Error(`Verification method ${verificationMethod} not found.`);
    }

    // ensure verification method has not been revoked
    if (document.revoked !== undefined) {
      throw new Error('The verification method has been revoked.');
    }

    return document;
  }
}

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