import { evmHexToTronBase58Address as e } from "../../functions/utils/tron.utils.mjs";
import { hmacSha512 as t } from "../../functions/web3/crypto.mjs";
import { HDNodeWallet as n, Mnemonic as r, Wallet as i, ethers as a } from "ethers";
import { Keypair as o } from "@solana/web3.js";
import s from "bs58";
import { mnemonicToSeedSync as c } from "bip39";
//#region libs/shared/common/src/data-structures/WalletManager/WalletManager.ts
var l = class {
	mainWallet = null;
	tronMainWallet = null;
	mnemonicPhrase = null;
	ethDerivationBasePath = "m/44'/60'/0'";
	tronDerivationBasePath = "m/44'/195'/0'";
	generateMnemonic(e = 12) {
		let t = e === 24 ? 32 : 16;
		return r.fromEntropy(a.randomBytes(t)).phrase.split(" ");
	}
	isValidMnemonic(e) {
		return r.isValidMnemonic(e);
	}
	isValidWord(e) {
		return a.wordlists.en.getWordIndex(e.toLowerCase()) !== -1;
	}
	createEvmWallet(e) {
		if (!this.mainWallet) throw Error("Main wallet not initialized. Call createMainWallet first.");
		let t = new i(this.mainWallet.derivePath(`0/${e}`).privateKey);
		return {
			address: t.address,
			privateKey: t.privateKey
		};
	}
	createTronWallet(t) {
		if (!this.tronMainWallet) throw Error("Main wallet not initialized. Call createMainWallet first.");
		let n = new i(this.tronMainWallet.derivePath(`0/${t}`).privateKey);
		return {
			address: e(n.address),
			privateKey: n.privateKey
		};
	}
	async createSolanaAccount(e = 0) {
		if (!this.mnemonicPhrase) throw Error("Main wallet not initialized. Call createMainWallet first.");
		let t = c(this.mnemonicPhrase, ""), n = `m/44'/501'/${e}'/0'`, r = await this.deriveEd25519Path(n, new Uint8Array(t)), i = o.fromSeed(r);
		return {
			publicKey: i.publicKey.toBase58(),
			secretKey: s.encode(i.secretKey)
		};
	}
	async createChildWallet(e) {
		let t = this.createEvmWallet(e), n = this.createTronWallet(e), r = await this.createSolanaAccount(e);
		return {
			ethereum: {
				address: t.address,
				privateKey: t.privateKey
			},
			tron: {
				address: n.address,
				privateKey: n.privateKey
			},
			solana: {
				publicKey: r.publicKey,
				secretKey: r.secretKey
			}
		};
	}
	async deriveEd25519Path(e, n) {
		let r = e.split("/").slice(1).map((e) => {
			let t = e.endsWith("'"), n = parseInt(e.replace("'", ""), 10);
			return t ? n + 2147483648 : n;
		}), i = await t("ed25519 seed", n), a = i.slice(0, 32), o = i.slice(32);
		for (let e of r) {
			let n = new Uint8Array(37);
			n[0] = 0, n.set(a, 1), new DataView(n.buffer).setUint32(33, e, !1);
			let r = await t(o, n);
			a = r.slice(0, 32), o = r.slice(32);
		}
		return a;
	}
	async createMainWallet(e) {
		if (!this.isValidMnemonic(e)) throw Error("Invalid mnemonic phrase");
		this.mnemonicPhrase = e, this.mainWallet = n.fromPhrase(e, "", this.ethDerivationBasePath), this.tronMainWallet = n.fromPhrase(e, "", this.tronDerivationBasePath);
		let { ethereum: t, tron: r, solana: i } = await this.createChildWallet(0);
		return {
			ethereum: {
				address: t.address,
				privateKey: t.privateKey
			},
			tron: {
				address: r.address,
				privateKey: r.privateKey
			},
			solana: {
				publicKey: i.publicKey,
				secretKey: i.secretKey
			}
		};
	}
};
//#endregion
export { l as WalletManager };
