'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.cjs');
var buffer = require('buffer');
var ed25519 = require('@noble/ed25519');
var sha512 = require('@noble/hashes/sha512');
var web3_js = require('@solarti/web3.js');
var errors = require('./errors.cjs');
var Amount = require('../../types/Amount.cjs');
var Signer = require('../../types/Signer.cjs');

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 ed25519__namespace = /*#__PURE__*/_interopNamespace(ed25519);

/**
 * @group Modules
 */
class DerivedIdentityClient {
  constructor(metaplex) {
    _rollupPluginBabelHelpers.defineProperty(this, "originalSigner", null);
    _rollupPluginBabelHelpers.defineProperty(this, "derivedKeypair", null);
    this.metaplex = metaplex;
  }
  get publicKey() {
    this.assertInitialized();
    return this.derivedKeypair.publicKey;
  }
  get secretKey() {
    this.assertInitialized();
    return this.derivedKeypair.secretKey;
  }
  get originalPublicKey() {
    this.assertInitialized();
    return this.originalSigner.publicKey;
  }
  async deriveFrom(message, originalSigner) {
    this.originalSigner = originalSigner ?? this.metaplex.identity().driver();
    const signature = await this.originalSigner.signMessage(buffer.Buffer.from(message));
    const seeds = sha512.sha512(signature).slice(0, 32);
    this.derivedKeypair = web3_js.Keypair.fromSeed(seeds);
  }
  fund(amount) {
    this.assertInitialized();
    return this.metaplex.system().transferSol({
      from: this.originalSigner,
      to: this.derivedKeypair.publicKey,
      amount
    }, {
      payer: this.originalSigner
    });
  }
  withdraw(amount) {
    this.assertInitialized();
    return this.metaplex.system().transferSol({
      from: this.derivedKeypair,
      to: this.originalSigner.publicKey,
      amount
    }, {
      payer: this.derivedKeypair
    });
  }
  async withdrawAll() {
    this.assertInitialized();
    const balance = await this.metaplex.rpc().getBalance(this.derivedKeypair.publicKey);
    const transactionFee = this.metaplex.utils().estimateTransactionFee();
    return this.withdraw(Amount.subtractAmounts(balance, transactionFee));
  }
  close() {
    this.originalSigner = null;
    this.derivedKeypair = null;
  }
  async signMessage(message) {
    return ed25519__namespace.sync.sign(message, this.secretKey);
  }
  async signTransaction(transaction) {
    transaction.partialSign(this);
    return transaction;
  }
  async signAllTransactions(transactions) {
    return Promise.all(transactions.map(transaction => this.signTransaction(transaction)));
  }
  verifyMessage(message, signature) {
    return ed25519__namespace.sync.verify(message, signature, this.publicKey.toBytes());
  }
  equals(that) {
    if (Signer.isSigner(that)) {
      that = that.publicKey;
    }
    return this.publicKey.equals(that);
  }
  assertInitialized() {
    if (this.derivedKeypair === null || this.originalSigner === null) {
      throw new errors.UninitializedDerivedIdentityError();
    }
  }
}

exports.DerivedIdentityClient = DerivedIdentityClient;
//# sourceMappingURL=DerivedIdentityClient.cjs.map
