You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
web3Interactive/reference/tools/generateWallet.mjs

48 lines
1.8 KiB

// 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);
});