【问题标题】:pubToAddress method from ethereumjs-util throws AssertionError: false == trueethereumjs-util 的 pubToAddress 方法抛出 AssertionError: false == true
【发布时间】:2017-05-26 13:06:27
【问题描述】:

我在 nodejs 上有以下代码: var keythereum = 要求('keythereum'); var crypto = 要求(“加密”); var eccrypto = 要求(“eccrypto”); var pubToAddress = require("ethereumjs-util").pubToAddress; var ecdsa = new (require("elliptic").ec)("secp256k1");

// A new random 32-byte private key. 
var privateKey = crypto.randomBytes(32);

// console.log(privateKey);
// <Buffer 06 79 35 4c 5e dd a2 1d b3 cf 70 8d e7 92 06 50 a7 f3 a3 88 3c e0 8c 57 3a 45 7c 53 d1 71 46 a5>

// Corresponding uncompressed (65-byte) public key. 
var publicKey = eccrypto.getPublic(privateKey);

console.log(publicKey);
// <Buffer 04 fa c7 15 ed c6 41 86 1e 18 fb e8 8d 6c e4 f7 75 1e d6 13 d9 b1 b5 f9 ba dc bc c6 48 1b c7 06 cb 28 4d b8 71 e6 74 75 5b e1 9e 49 15 07 76 80 21 3e ... >

var address = pubToAddress(publicKey).toString("hex");

在执行最后一行代码时:

var address = pubToAddress(publicKey).toString("hex");

它会抛出这样的断言错误:

AssertionError: false == true
    at exports.pubToAddress.exports.publicToAddress (/myHomeDir/node_modules/ethereumjs-util/index.js:323:3)
    at repl:1:15
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:544:10)
    at emitOne (events.js:101:20)

发现失败的断言是这个:

/**
 * Returns the ethereum address of a given public key.
 * Accepts "Ethereum public keys" and SEC1 encoded keys.
 * @param {Buffer} pubKey The two points of an uncompressed key, unless sanitize is enabled
 * @param {Boolean} [sanitize=false] Accept public keys in other formats
 * @return {Buffer}
 */
exports.pubToAddress = exports.publicToAddress = function (pubKey, sanitize) {
  pubKey = exports.toBuffer(pubKey)
  if (sanitize && (pubKey.length !== 64)) {
    pubKey = secp256k1.publicKeyConvert(pubKey, false).slice(1)
  }
  assert(pubKey.length === 64)
  // Only take the lower 160bits of the hash
  return exports.sha3(pubKey).slice(-20)
}

我应该从地址中删除第一个字节 (04) 吗?

这段代码基于https://github.com/ethereumjs/keythereum/blob/master/test/keys.js试图创建一个以太坊私钥并获取其对应的公钥和地址。它在最后一步失败。任何关于正在发生的事情的提示都会非常有帮助。谢谢!

【问题讨论】:

  • 为什么要使用 65 字节的公钥?

标签: javascript node.js public-key assertion ethereum


【解决方案1】:

这段代码运行良好:

var keythereum = require('keythereum');
var crypto = require("crypto");
var util = require("ethereumjs-util");

// A new random 32-byte private key.
var privateKey = crypto.randomBytes(32);

var publicKey = util.privateToPublic(privateKey);
console.log(publicKey);

var address = util.pubToAddress(publicKey).toString("hex");
console.log(address);

var Tx = require('ethereumjs-tx');
// var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex');

var rawTx = {
  nonce: '0x00',
  gasPrice: '0x09184e72a000',
  gasLimit: '0x2710',
  to: '0x0000000000000000000000000000000000000000',
  value: '0x00',
  data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();
console.log(serializedTx.toString('hex'));

重点是使用:

var publicKey = util.privateToPublic(privateKey);

代替:

var publicKey = eccrypto.getPublic(privateKey);

因为前一个返回一个 64 字节的 publicKey,而后一个返回一个 65 字节的 publicKey,前面有一个“04”字节。

【讨论】:

    猜你喜欢
    • 2015-10-10
    • 2022-08-09
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 2016-07-23
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多