Onlife/common/proAdress.js
2025-04-19 15:38:48 +08:00

35 lines
877 B
JavaScript

const bip39 = require('bip39');
const hdkey = require('hdkey');
const ethers = require('ethers');
const {
sha3_256
} = require('js-sha3');
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
import {
bufferToHex,
toBuffer
} from 'ethereumjs-util'; // 用于 Ethereum 数据转换
export function getAddressAndPrivateKey(mnemonic) {
const path = "m/44'/60'/0'/0/0";
// 从助记词生成种子
const seed = bip39.mnemonicToSeedSync(mnemonic);
// 创建HD钱包
const hdwallet = hdkey.fromMasterSeed(seed);
// 派生私钥
const childKey = hdwallet.derive(path);
const privateKey = childKey.privateKey;
// 3. **获取公钥**
const publicKey = ethers.utils.computePublicKey(privateKey);
const address = ethers.utils.getAddress(ethers.utils.computeAddress(publicKey));
return {
address: address,
privateKey: privateKey.toString('hex')
};
}