以太坊用户账号地址长度为20字节。
它是由椭圆曲线算法从32字节的私钥,生成64字节的公钥,再压缩后取其最后的20字节得到的。
因此可以使用简单的脚本随机生成若干32字节的私钥,再计算出其公钥得到地址。实现批量地址生成。
const randomBytes = require('randombytes')
const secp256k1 = require('secp256k1')
const keccak = require('keccak')
const createRandomPrivateKey = function () {
return randomBytes(32)
}
const privateKeyToAddress = function (privateKey) {
return keccak('keccak256').update(Buffer.from(secp256k1.publicKeyCreate(privateKey, false).slice(1))).digest().slice(-20)
}
let privateKey = createRandomPrivateKey()
let address = privateKeyToAddress(privateKey)
console.log('0x'+address.toString('hex'))
console.log(privateKey.toString('hex'))
运行脚本需要电脑安装Node.js及相应库。
对以上代码进行简单的修改(添加循环和条件控制语句),就可以生成大量地址以满足测试需求或者生成特定格式的地址。
例如发布本文的账号地址最前面有7个0,就是运行了大概十分钟找到的地址。
