😸

Ethereumアドレスを大量に生成するスニペット

2022/07/22に公開

テスト用に Ethereumアドレス が大量に欲しくなったので、簡単な Node.js スニペットです。

const ethers = require("ethers")
const crypto = require("crypto")

function generateWallet() {
  const privateKey = `0x${crypto.randomBytes(32).toString("hex")}`
  const wallet = new ethers.Wallet(privateKey)
  return { privateKey, walletAddress: wallet.address }
}

function batchGenerateWallet(num = 10) {
  const wallets = []
  while (num > 0) {
    wallets.push(generateWallet())
    num--
  }
  return wallets
}

使い方

// 秘密鍵とウォレットアドレスのリスト
> batchGenerateWallet(3)
[
  {
    privateKey: '0xd94cd3c139b89f0203fa251506a844663a97a3a73e3d74596af4c5b76fb3bbe0',
    walletAddress: '0x5B3B1fCE7A8317589A767Ae9305cA1A0695FC1cD'
  },
  {
    privateKey: '0xdc8ba9a5e8e45edc98c6a9fe5ea1aafef0d86dc6decd06efe19acfcbb78aeed2',
    walletAddress: '0xD1d2e9c74b4728A1BD7c701473E35fC2F940A79c'
  },
  {
    privateKey: '0x05688b0de205699ab644ebbf0d99d39f91f7653d95e21e3c7380f8495442ee1f',
    walletAddress: '0xEB9e5BC03D11E17090E19f3A38D8B253b2aCC125'
  }
]

// ウォレットアドレスだけ欲しい時
> batchGenerateWallet(5).map(item => item.walletAddress)
[
  '0x737Ec613E0373402AC50c5E60bF8afBDf407ceca',
  '0xBd394840cCd00454F0aCD48e2A56E236c85B8521',
  '0x535B962F3B443E51ed45505f1e019AA7050DAE3f',
  '0xC275bD804471BcDa1DA5f0d3Aa7c0e8fBb4104E0',
  '0x28Fc45820216CA4A4c9247a29BD28D6EAcCdF0e1'
]
  • 検証環境
    • Node.js: v16.13.0
    • ethers.version: ethers/5.6.9

参考:
https://www.quicknode.com/guides/web3-sdks/how-to-generate-a-new-ethereum-address-in-javascript

Discussion