'use strict';

var b58 = require('bs58');
var base64 = require('@juanelas/base64');
var utils_types_bytes = require('../../utils/types/bytes.cjs');
var vc_crypto_constants = require('./constants.cjs');
var keypairs_keypairSecp256k1 = require('../../keypairs/keypair-secp256k1.cjs');
var utils_encoding_multibase = require('../../utils/encoding/multibase.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('elliptic');
require('js-sha256');
require('../../types/public-keys/public-key.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('../../types/public-keys/public-key-x25519-value.cjs');
require('../../types/generic/sized.cjs');
require('../../utils/types/number.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/signatures/signature-value.cjs');
require('../../types/signatures/signature.cjs');
require('../../types/signatures/signature-ed25519-value.cjs');
require('../../types/signatures/signature-secp256k1-value.cjs');
require('../../types/signatures/signature-sr25519-value.cjs');
require('../../keypairs/dock-keypair.cjs');

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

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var b58__default = /*#__PURE__*/_interopDefault(b58);
var base64__namespace = /*#__PURE__*/_interopNamespace(base64);

class EcdsaSecp256k1VerificationKey2019 {
  constructor(publicKey) {
    this.publicKey = utils_types_bytes.u8aToU8a(publicKey);
  }

  /**
   * Construct the public key object from the verification method
   * @param verificationMethod
   * @returns {EcdsaSecp256k1VerificationKey2019}
   */
  static from(verificationMethod) {
    if (
      !verificationMethod.type
      || verificationMethod.type.indexOf(vc_crypto_constants.EcdsaSecp256k1VerKeyName) === -1
    ) {
      throw new Error(
        `verification method should have type ${vc_crypto_constants.EcdsaSecp256k1VerKeyName} - got: ${verificationMethod.type}`,
      );
    }

    if (verificationMethod.publicKeyBase58) {
      return new this(b58__default.default.decode(verificationMethod.publicKeyBase58));
    }

    if (verificationMethod.publicKeyBase64) {
      return new this(base64__namespace.decode(verificationMethod.publicKeyBase64));
    }

    const secMultibase = verificationMethod['sec:publicKeyMultibase'];
    const multiBase = verificationMethod.publicKeyMultibase || (
      typeof secMultibase === 'object'
        ? secMultibase['@value']
        : secMultibase
    );

    if (multiBase) {
      return new this(utils_encoding_multibase.decodeFromMultibase(multiBase).slice(2));
    }

    throw new Error(
      `Unsupported signature encoding for ${vc_crypto_constants.EcdsaSecp256k1VerKeyName}`,
    );
  }

  /**
   * Construct the verifier factory that has the verify method using the current public key
   * @returns {object}
   */
  verifier() {
    return EcdsaSecp256k1VerificationKey2019.verifierFactory(this.publicKey);
  }

  /**
   * Verifier factory that returns the object with the verify method
   * @param publicKey
   * @returns {object}
   */
  static verifierFactory(publicKey) {
    return {
      async verify({ data, signature }) {
        return keypairs_keypairSecp256k1.verify(data, signature, publicKey);
      },
    };
  }
}

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