138 lines
5.2 KiB
JavaScript
138 lines
5.2 KiB
JavaScript
"use strict";
|
|
/**
|
|
* HD wallet derivation and key management.
|
|
* BIP39/BIP44 address generation for TRON and Polygon, plus encrypted private key helpers.
|
|
*/
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getTronWeb = getTronWeb;
|
|
exports.generateMnemonic = generateMnemonic;
|
|
exports.mnemonicToSeed = mnemonicToSeed;
|
|
exports.deriveTronFromMnemonic = deriveTronFromMnemonic;
|
|
exports.derivePolygonFromMnemonic = derivePolygonFromMnemonic;
|
|
exports.encryptPrivateKey = encryptPrivateKey;
|
|
exports.decryptPrivateKey = decryptPrivateKey;
|
|
exports.getTronAddressFromPrivateKey = getTronAddressFromPrivateKey;
|
|
const bip39 = __importStar(require("bip39"));
|
|
const ethers_1 = require("ethers");
|
|
const tronweb_1 = __importDefault(require("tronweb"));
|
|
const crypto_1 = require("../utils/crypto");
|
|
const TRON_PATH_PREFIX = "m/44'/195'/0'/0/";
|
|
const POLYGON_PATH_PREFIX = "m/44'/60'/0'/0/";
|
|
let cachedTronWeb = null;
|
|
/**
|
|
* Returns a singleton TronWeb client for address conversion and chain calls.
|
|
* @returns Configured TronWeb instance (TRON_NODE_URL or TronGrid default)
|
|
*/
|
|
function getTronWeb() {
|
|
if (!cachedTronWeb) {
|
|
const fullHost = process.env.TRON_NODE_URL || 'https://api.trongrid.io';
|
|
cachedTronWeb = new tronweb_1.default({ fullHost });
|
|
}
|
|
return cachedTronWeb;
|
|
}
|
|
/**
|
|
* Generates a new 24-word BIP39 mnemonic (256 bits of entropy).
|
|
* @returns Space-separated mnemonic phrase
|
|
*/
|
|
function generateMnemonic() {
|
|
return bip39.generateMnemonic(256);
|
|
}
|
|
/**
|
|
* Derives a BIP39 seed buffer from a mnemonic (sync).
|
|
* @param mnemonic Valid BIP39 mnemonic phrase
|
|
* @returns 64-byte seed buffer
|
|
*/
|
|
function mnemonicToSeed(mnemonic) {
|
|
return bip39.mnemonicToSeedSync(mnemonic);
|
|
}
|
|
/**
|
|
* Derives TRON address and private key at index using BIP44 path m/44'/195'/0'/0/index.
|
|
* @param mnemonic Platform BIP39 mnemonic
|
|
* @param index Derivation index (0-based)
|
|
* @returns Base58 TRON address and hex private key
|
|
*/
|
|
function deriveTronFromMnemonic(mnemonic, index) {
|
|
const path = `${TRON_PATH_PREFIX}${index}`;
|
|
const result = tronweb_1.default.fromMnemonic(mnemonic, path);
|
|
const privateKey = result.privateKey;
|
|
const addr = result.address;
|
|
const address = typeof addr === 'string' ? addr : (addr?.base58 ?? '');
|
|
return { address, privateKey };
|
|
}
|
|
/**
|
|
* Derives Polygon (Ethereum-compatible) address and private key at index using BIP44 path m/44'/60'/0'/0/index.
|
|
* @param mnemonic Platform BIP39 mnemonic
|
|
* @param index Derivation index (0-based)
|
|
* @returns Checksummed 0x address and hex private key (without 0x prefix)
|
|
*/
|
|
function derivePolygonFromMnemonic(mnemonic, index) {
|
|
const path = `${POLYGON_PATH_PREFIX}${index}`;
|
|
const wallet = ethers_1.HDNodeWallet.fromPhrase(mnemonic, undefined, path);
|
|
const privateKey = wallet.privateKey?.slice(2) || '';
|
|
return { address: wallet.address, privateKey };
|
|
}
|
|
/**
|
|
* Encrypts a private key for storage (AES via app crypto utils).
|
|
* @param privateKey Hex private key string
|
|
* @returns Encrypted ciphertext string
|
|
*/
|
|
function encryptPrivateKey(privateKey) {
|
|
return (0, crypto_1.encrypt)(privateKey);
|
|
}
|
|
/**
|
|
* Decrypts a stored private key.
|
|
* @param encrypted Ciphertext from encryptPrivateKey
|
|
* @returns Plain hex private key
|
|
*/
|
|
function decryptPrivateKey(encrypted) {
|
|
return (0, crypto_1.decrypt)(encrypted);
|
|
}
|
|
/**
|
|
* Derives a TRON base58 address from a hex private key.
|
|
* @param privateKeyHex Private key in hex (with or without 0x)
|
|
* @returns TRON base58 address
|
|
*/
|
|
function getTronAddressFromPrivateKey(privateKeyHex) {
|
|
const tronWeb = getTronWeb();
|
|
const addr = tronWeb.address.fromPrivateKey(privateKeyHex);
|
|
return typeof addr === 'object' && addr.base58
|
|
? addr.base58
|
|
: addr;
|
|
}
|
|
//# sourceMappingURL=hdWallet.js.map
|