我正在使用ethereumjs库,当我调用const address = ethUtil.toChecksumAddress(addr);时,似乎得到了错误:error to seed Error: This method only supports 0x-prefixed hex strings but input was: aa4c8571e8cb32a74abb302637b48b92e1a84452
变量addr似乎不是以0x前缀开头。我怎么才能添加这个?
如果我在https://iancoleman.io/bip39/#english上查看,我可以看到我创建的地址是正确的,但缺少0x。
此外,与ian coleman相比,public和privatekey是正确的,除了它们也缺少0x前缀。
如何添加这些内容?
代码:
var bip39 = require('bip39');
var hdkey = require('hdkey');
var ethUtil = require('ethereumjs-util');
var tx = require('ethereumjs-tx');
var createHash = require('create-hash');
//var btcLib = require('bitcoinjs-lib');
var bs58check = require('bs58check');
const mnemonic = bip39.generateMnemonic();
bip39.mnemonicToSeed(mnemonic)
.then(seed => {
console.log('Seed: ', seed.toString('hex'));
console.log('mnemonic: ', mnemonic);
//knowledge of the master keys can recreate the addresses underneath
const root = hdkey.fromMasterSeed(seed);
const masterPrivateKey = root.privateKey.toString('hex');
console.log('masterPrivateKey: ' + masterPrivateKey);
const masterpublicKey = root.publicKey.toString('hex');
console.log('masterpublicKey: ' + masterpublicKey);
const extendedPrivateKey = root.privateExtendedKey.toString('hex');
console.log('extendedPrivateKey: ' + extendedPrivateKey+'\n')
const extendedPublicKey = root.publicExtendedKey.toString('hex');
console.log('extendedPublicKey: ' + extendedPublicKey+'\n');
// var addrNode = root.derive("m/44'/60'/0'/0/0"); // "m / purpose' / coin_type' / account' / change / address_index"
// var pubKey = ethUtil.privateToPublic(addrNode._privateKey);
// var addr = ethUtil.publicToAddress(pubKey).toString('hex');
// var address = ethUtil.toChecksumAddress(addr);
for (i=0; i<10; i++){
const addrNode = root.derive(`m/44'/60'/0'/0/${i}`);
console.log(`Creating address node : m/44'/60'/0'/0/${i}`);
var addrpubkey = ethUtil.privateToPublic(addrNode._privateKey);
var addrprivkey = addrNode._privateKey.toString('hex');
console.log('addrnodePublicKey: '+ addrpubkey.toString('hex'));
console.log('addrnodePrivateKey: '+ addrprivkey);
const addr = ethUtil.publicToAddress(addrpubkey).toString('hex');
console.log('addr: '+ addr);
const address = ethUtil.toChecksumAddress(addr);
console.log('ETH address is: ' + address+'\n');
}
})
.catch(err => {
console.error('error to seed', err)
})输出:
Seed: 0bfcd3f0248e8d65c8f6fb13826b0cbb1afa61ffad0b15b75a0544d968ee5d559af973df56e67727a2d615190b9902f472e80371a84540bd0905962fece724e0
mnemonic: device toe hand tell outside dilemma seed alert mercy able actual wool
masterPrivateKey: 88c5ae7f279e717b6d31ef347f2957c89dda45769b2c38caef8686b6f638bbbb
masterpublicKey: 02213c39c1cf335c9b829ab9818b341a4341a6934c4c234c7d99533b1bbd9b7b1c
extendedPrivateKey: xprv9s21ZrQH143K45CTK9VJ9X57n3kFRHNeFUnZR6qpkNxbDodkzGqhcL58d7BjhoEZcsxyuWSoi42fRjHQNToeoEbpTEQ6xzCs23A5NGTDHZh
extendedPublicKey: xpub661MyMwAqRbcGZGvRB2JWf1rL5ajpk6VchiADVFSJiVa6bxuXp9xA8PcUMRuS8kHmymUh5dqaMuD8N9ktEn3Ky8oUybLFq7fEY2rWwsHqYv
Creating address node : m/44'/60'/0'/0/0
addrnodePublicKey: fa580215fc380df81925ea2213f02a68b88f2122f2f516e78735c186ae417c7e7598b27eb1479ac901d9b634fe22c6c6cd98664d5bf8cdf65efe2f6d620e4ca6
addrnodePrivateKey: bb48522c28346d7d05d48e7564a403af142ba9d838f267cf8257a294c6d71ff8
addr: 462a193795de30696a0c9da05741655a8a8a9fc2上面使用助记符的Ian coleman屏幕截图显示了地址pub/priv key和地址缺少的前缀:

发布于 2021-05-06 15:13:17
publicToAddress()返回Buffer (GitHub source)。当你将Buffer转换为十六进制字符串时,node.js不会添加0x。
但您可以简单地连接字符串:
const addr = '0x' + ethUtil.publicToAddress(addrpubkey).toString('hex'); // added 0x而不是
const addr = ethUtil.publicToAddress(addrpubkey).toString('hex'); // original codehttps://stackoverflow.com/questions/67410717
复制相似问题