const e=require(`../../_virtual/_rolldown/runtime.cjs`),t=require(`../../functions/utils/tron.utils.cjs`),n=require(`../../functions/web3/crypto.cjs`);let r=require(`ethers`),i=require(`@solana/web3.js`),a=require(`bs58`);a=e.__toESM(a);let o=require(`bip39`);var s=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.Mnemonic.fromEntropy(r.ethers.randomBytes(t)).phrase.split(` `)}isValidMnemonic(e){return r.Mnemonic.isValidMnemonic(e)}isValidWord(e){return r.ethers.wordlists.en.getWordIndex(e.toLowerCase())!==-1}createEvmWallet(e){if(!this.mainWallet)throw Error(`Main wallet not initialized. Call createMainWallet first.`);let t=new r.Wallet(this.mainWallet.derivePath(`0/${e}`).privateKey);return{address:t.address,privateKey:t.privateKey}}createTronWallet(e){if(!this.tronMainWallet)throw Error(`Main wallet not initialized. Call createMainWallet first.`);let n=new r.Wallet(this.tronMainWallet.derivePath(`0/${e}`).privateKey);return{address:t.evmHexToTronBase58Address(n.address),privateKey:n.privateKey}}async createSolanaAccount(e=0){if(!this.mnemonicPhrase)throw Error(`Main wallet not initialized. Call createMainWallet first.`);let t=(0,o.mnemonicToSeedSync)(this.mnemonicPhrase,``),n=`m/44'/501'/${e}'/0'`,r=await this.deriveEd25519Path(n,new Uint8Array(t)),s=i.Keypair.fromSeed(r);return{publicKey:s.publicKey.toBase58(),secretKey:a.default.encode(s.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,t){let r=e.split(`/`).slice(1).map(e=>{let t=e.endsWith(`'`),n=parseInt(e.replace(`'`,``),10);return t?n+2147483648:n}),i=await n.hmacSha512(`ed25519 seed`,t),a=i.slice(0,32),o=i.slice(32);for(let e of r){let t=new Uint8Array(37);t[0]=0,t.set(a,1),new DataView(t.buffer).setUint32(33,e,!1);let r=await n.hmacSha512(o,t);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=r.HDNodeWallet.fromPhrase(e,``,this.ethDerivationBasePath),this.tronMainWallet=r.HDNodeWallet.fromPhrase(e,``,this.tronDerivationBasePath);let{ethereum:t,tron:n,solana:i}=await this.createChildWallet(0);return{ethereum:{address:t.address,privateKey:t.privateKey},tron:{address:n.address,privateKey:n.privateKey},solana:{publicKey:i.publicKey,secretKey:i.secretKey}}}};exports.WalletManager=s;