'use strict';

var keypairs_dockKeypair = require('./dock-keypair.cjs');
var types_did_onchain_didKey = require('../types/did/onchain/did-key.cjs');
var types_did_document_verificationMethodRefOrIdentRef = require('../types/did/document/verification-method-ref-or-ident-ref.cjs');
require('../utils/types/array.cjs');
require('crypto');
var utils_inheritance = require('../utils/inheritance.cjs');
require('bs58');
require('multiformats/bases/base58');
require('varint');
require('../utils/encoding/ss-58.cjs');
var types_did_onchain_typedDid_index = require('../types/did/onchain/typed-did/index.cjs');
require('../utils/types/bytes.cjs');
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('../types/generic/typed-enum.cjs');
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/public-keys/public-key.cjs');
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('../vc/crypto/constants.cjs');
require('../types/public-keys/public-key-secp256k1-value.cjs');
require('../types/public-keys/public-key-sr25519-value.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/typed-uuid.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('../vc/crypto/Bls12381BBDT16KeyPairDock2024.cjs');
require('@docknetwork/crypto-wasm-ts');
require('../vc/crypto/common/DockCryptoKeyPair.cjs');
require('../vc/crypto/Bls12381BBDT16MACDock2024.cjs');
require('@docknetwork/crypto-wasm-ts/lib/index.js');
require('../vc/crypto/common/DockCryptoSignature.cjs');
require('jsonld-signatures');
require('json-stringify-deterministic');
require('semver/preload.js');
require('../vc/crypto/common/CustomLinkedDataSignature.cjs');
require('base64url');
require('jsonld');
require('../types/did/document/verification-method-ref.cjs');
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('../vc/jws.cjs');
require('../vc/crypto/Bls12381BBDT16MACProofDock2024.cjs');
require('../vc/crypto/common/DockCryptoSignatureProof.cjs');
require('../vc/crypto/Bls12381BBSKeyPairDock2023.cjs');
require('../vc/crypto/Bls12381BBSSignatureDock2022.cjs');
require('../vc/crypto/Bls12381G2KeyPairDock2022.cjs');
require('../vc/crypto/Bls12381BBSSignatureDock2023.cjs');
require('../vc/crypto/Bls12381BBSSignatureProofDock2022.cjs');
require('../vc/crypto/Bls12381BBSSignatureProofDock2023.cjs');
require('../vc/crypto/Bls12381BDDT16KeyPairDock2024.cjs');
require('../vc/crypto/Bls12381BDDT16MACDock2024.cjs');
require('../vc/crypto/Bls12381BDDT16MACProofDock2024.cjs');
require('../vc/crypto/Bls12381PSKeyPairDock2023.cjs');
require('../vc/crypto/Bls12381PSSignatureDock2023.cjs');
require('../vc/crypto/Bls12381PSSignatureProofDock2023.cjs');
require('@juanelas/base64');
require('./keypair-secp256k1.cjs');
require('elliptic');
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('@stablelib/ed25519');
require('@transmute/json-web-signature');
require('../types/offchain-signatures/public-keys/value.cjs');
require('../types/offchain-signatures/public-keys/ref.cjs');
require('../types/did/onchain/typed-did/dock-did-value.cjs');
require('../types/did/onchain/typed-did/signature.cjs');
require('../types/offchain-signatures/public-keys/id.cjs');
require('../types/did/onchain/verification-relationship.cjs');
require('../types/did/document/ident-ref.cjs');
require('../utils/types/set.cjs');
require('blake2b');
require('../types/did/onchain/typed-did/did-method-key/did-method-key-public-key.cjs');
require('../utils/encoding/base-58-btc.cjs');
require('../types/did/onchain/typed-did/did-method-key/did-method-key-signature.cjs');

/**
 * A class representation of DID Keypair which includes a signing keypair and its associated verification method reference identifier.
 */
class DidKeypair {
  /**
   * Constructs a `DidKeypair` instance from the supplied keypair.
   *
   * @param {VerificationMethodRefOrIdentRef} keyRef - The reference of the verification method.
   * @param {DockKeypair} keyPair - Key pair provided.
   */
  constructor(keyRef, keyPair) {
    this.verificationMethodId = types_did_document_verificationMethodRefOrIdentRef.from(keyRef);
    this.keyPair = utils_inheritance.ensureInstanceOf(keyPair, keypairs_dockKeypair);
  }

  /**
   * Static function to create a `DidKeypair` instance from the provided keyPair.
   *
   * @param {DockKeypair} keyPair - Key pair provided.
   * @return The new instance of `DidKeypair`.
   */
  static didMethodKey(keyPair) {
    const did = types_did_onchain_typedDid_index.DidMethodKey.fromKeypair(keyPair);

    return new this([did, did.asDidMethodKey.toEncodedString()], keyPair);
  }

  /**
   * Get the public key from the current key pair.
   *
   * @return The public key.
   */
  publicKey() {
    return this.keyPair.publicKey();
  }

  /**
   * Creates a new `DidKey` instance from the public key of the current key pair.
   *
   * @param {?number} verRels - Number indicating verification relationships.
   * @return The new instance of `DidKey`.
   */
  didKey(verRels) {
    return new types_did_onchain_didKey.DidKey(this.keyPair.publicKey(), verRels);
  }

  /**
   * Get the DID from the current verification method ID.
   *
   * @return The DID.
   */
  get did() {
    return this.verificationMethodId[0];
  }

  /**
   * Get the Key ID from the current verification method ID.
   *
   * @return The Key ID.
   */
  get keyId() {
    return this.verificationMethodId[1];
  }

  /**
   * Sign the provided message with the key pair.
   *
   * @param {Uint8Array} message - The message to sign.
   * @return The signature.
   */
  sign(message) {
    return this.keyPair.sign(message);
  }

  /**
   * Detects a key prefix from a key document
   *
   * @return The DidKeypair instance
   */
  static detectKeyPrefix(keyDocument) {
    if (typeof keyDocument.id === 'string') {
      return keyDocument.id.split('#')[1]?.split('-')[0] || 'keys';
    }
    return 'keys';
  }
}

module.exports = DidKeypair;
//# sourceMappingURL=did-keypair.cjs.map
