| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- // generateWallet.mjs
- import { ethers } from "ethers";
- async function generateWallets(mnemonic, numWallets) {
- const wallets = [];
- // 验证助记词是否有效
- if (!ethers.Mnemonic.isValidMnemonic(mnemonic)) {
- throw new Error("Invalid mnemonic phrase");
- }
- for (let i = 0; i < numWallets; i++) {
- // 使用 BIP-44 派生路径生成钱包
- const path = `m/44'/60'/0'/0/${i}`;
- // 显式创建 HD 节点并派生钱包
- const hdNode = ethers.HDNodeWallet.fromPhrase(mnemonic, undefined, path);
- const wallet = new ethers.Wallet(hdNode.privateKey);
- const address = wallet.address;
- const privateKey = wallet.privateKey;
- wallets.push({ address, privateKey, mnemonic, path }); // 包含路径以便调试
- }
- const addresses = new Set(wallets.map(w => w.address));
- if (addresses.size !== numWallets) {
- throw new Error(`Duplicate wallets detected! Generated ${addresses.size} unique addresses instead of ${numWallets}`);
- }
- return wallets;
- }
- const mnemonic = "need rare control glove luxury punch orbit beef antique return average appear trumpet cattle hundred unknown unable exist rotate village produce address guilt naive";
- const numWallets = 100;
- generateWallets(mnemonic, numWallets)
- .then(wallets => {
- wallets.forEach((wallet, index) => {
- console.log(`Wallet ${index + 1}:`);
- console.log(` Address: ${wallet.address}`);
- console.log(` Private Key: ${wallet.privateKey}`);
- console.log(` Mnemonic: ${wallet.mnemonic}`);
- console.log(` Path: ${wallet.path}`);
- console.log("---");
- });
- })
- .catch(error => {
- console.error("Error generating wallets:", error.message);
- });
|