generateWallet.mjs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // generateWallet.mjs
  2. import { ethers } from "ethers";
  3. async function generateWallets(mnemonic, numWallets) {
  4. const wallets = [];
  5. // 验证助记词是否有效
  6. if (!ethers.Mnemonic.isValidMnemonic(mnemonic)) {
  7. throw new Error("Invalid mnemonic phrase");
  8. }
  9. for (let i = 0; i < numWallets; i++) {
  10. // 使用 BIP-44 派生路径生成钱包
  11. const path = `m/44'/60'/0'/0/${i}`;
  12. // 显式创建 HD 节点并派生钱包
  13. const hdNode = ethers.HDNodeWallet.fromPhrase(mnemonic, undefined, path);
  14. const wallet = new ethers.Wallet(hdNode.privateKey);
  15. const address = wallet.address;
  16. const privateKey = wallet.privateKey;
  17. wallets.push({ address, privateKey, mnemonic, path }); // 包含路径以便调试
  18. }
  19. const addresses = new Set(wallets.map(w => w.address));
  20. if (addresses.size !== numWallets) {
  21. throw new Error(`Duplicate wallets detected! Generated ${addresses.size} unique addresses instead of ${numWallets}`);
  22. }
  23. return wallets;
  24. }
  25. 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";
  26. const numWallets = 100;
  27. generateWallets(mnemonic, numWallets)
  28. .then(wallets => {
  29. wallets.forEach((wallet, index) => {
  30. console.log(`Wallet ${index + 1}:`);
  31. console.log(` Address: ${wallet.address}`);
  32. console.log(` Private Key: ${wallet.privateKey}`);
  33. console.log(` Mnemonic: ${wallet.mnemonic}`);
  34. console.log(` Path: ${wallet.path}`);
  35. console.log("---");
  36. });
  37. })
  38. .catch(error => {
  39. console.error("Error generating wallets:", error.message);
  40. });