首页 > 解决方案 > EthereumJS 库没有为我提供正确的地址(缺少 0x)

问题描述

我正在使用 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 相比,公钥和私钥也是正确的,除了它们也缺少 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 密钥和地址缺少前缀:

在此处输入图像描述

标签: node.jsblockchainethereum

解决方案


publicToAddress()返回BufferGitHub 源)。0x当您将 Buffer 转换为十六进制字符串时,node.js 不会添加。

但是您可以简单地连接字符串:

const addr = '0x' + ethUtil.publicToAddress(addrpubkey).toString('hex'); // added 0x

代替

const addr = ethUtil.publicToAddress(addrpubkey).toString('hex'); // original code

推荐阅读