"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TRON_DERIVATION_PATH = exports.EVM_DERIVATION_PATH = void 0;
exports.generateVaultWallet = generateVaultWallet;
exports.deriveVaultWallet = deriveVaultWallet;
const ethers_1 = require("ethers");
const tronweb_1 = require("tronweb");
exports.EVM_DERIVATION_PATH = "m/44'/60'/0'/0/0";
exports.TRON_DERIVATION_PATH = "m/44'/195'/0'/0/0";
function generateVaultWallet() {
    const mnemonic = ethers_1.ethers.Mnemonic.fromEntropy(ethers_1.ethers.randomBytes(16)).phrase.trim();
    const evmHdWallet = ethers_1.ethers.HDNodeWallet.fromPhrase(mnemonic, "", exports.EVM_DERIVATION_PATH);
    const evmAddress = evmHdWallet.address;
    const tronHdWallet = ethers_1.ethers.HDNodeWallet.fromPhrase(mnemonic, "", exports.TRON_DERIVATION_PATH);
    const tronPrivateKey = tronHdWallet.privateKey.startsWith("0x")
        ? tronHdWallet.privateKey.slice(2)
        : tronHdWallet.privateKey;
    const tronAddress = tronweb_1.TronWeb.address.fromPrivateKey(tronPrivateKey);
    if (!tronAddress || typeof tronAddress !== "string") {
        throw new Error("Failed to derive Tron address from vault private key.");
    }
    return {
        seedPhrase: mnemonic,
        privateKey: evmHdWallet.privateKey,
        evmAddress,
        tronAddress
    };
}
function deriveVaultWallet(seedPhrase, network) {
    if (network === "tron") {
        const hdWallet = ethers_1.ethers.HDNodeWallet.fromPhrase(seedPhrase, "", exports.TRON_DERIVATION_PATH);
        const privateKey = hdWallet.privateKey.startsWith("0x") ? hdWallet.privateKey.slice(2) : hdWallet.privateKey;
        const address = tronweb_1.TronWeb.address.fromPrivateKey(privateKey);
        if (!address || typeof address !== "string") {
            throw new Error("Failed to derive Tron address from vault seed phrase.");
        }
        return { address, privateKey };
    }
    const hdWallet = ethers_1.ethers.HDNodeWallet.fromPhrase(seedPhrase, "", exports.EVM_DERIVATION_PATH);
    return { address: hdWallet.address, privateKey: hdWallet.privateKey };
}
