import b58 from 'bs58';
import * as base64 from '@juanelas/base64';
import { u8aToU8a } from '../../utils/types/bytes.js';
import Ed25519Keypair from '../../keypairs/keypair-ed25519.js';
import { Ed25519VerKeyName } from './constants.js';
import { decodeFromMultibase } from '../../utils/encoding/multibase.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 '@stablelib/ed25519';
import '../../types/public-keys/public-key.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/public-keys/public-key-x25519-value.js';
import '../../types/generic/sized.js';
import '../../utils/types/number.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/signatures/signature-value.js';
import '../../types/signatures/signature.js';
import '../../types/signatures/signature-ed25519-value.js';
import '../../types/signatures/signature-secp256k1-value.js';
import '../../types/signatures/signature-sr25519-value.js';
import '../../keypairs/dock-keypair.js';

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

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

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

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

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

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

    throw new Error(`Unsupported signature encoding for ${Ed25519VerKeyName}`);
  }

  /**
   * Construct the verifier factory that has the verify method using the current public key
   * @returns {object}
   */
  verifier() {
    return Ed25519VerificationKey2018.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 Ed25519Keypair.verify(data, signature, publicKey);
      },
    };
  }
}

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