Javascript ethereumjs util中的pubToAddress方法抛出断言错误:false==true
我在nodejs上有以下代码: var Keythsum=要求(“Keythsum”); var crypto=要求(“加密”); var eccrypto=需要(“eccrypto”); var pubToAddress=require(“ethereumjs util”).pubToAddress; var ecdsa=新的(需要(“椭圆”).ec)(“secp256k1”) 它抛出如下断言错误:Javascript ethereumjs util中的pubToAddress方法抛出断言错误:false==true,javascript,node.js,public-key,assertion,ethereum,Javascript,Node.js,Public Key,Assertion,Ethereum,我在nodejs上有以下代码: var Keythsum=要求(“Keythsum”); var crypto=要求(“加密”); var eccrypto=需要(“eccrypto”); var pubToAddress=require(“ethereumjs util”).pubToAddress; var ecdsa=新的(需要(“椭圆”).ec)(“secp256k1”) 它抛出如下断言错误: AssertionError: false == true at exports.pu
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)吗
这段代码基于尝试创建以太坊私钥并获取其相应的公钥和地址。它在最后一步失败了。任何关于正在发生的事情的暗示都会很有帮助。谢谢 此代码工作正常:
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字节的公钥,而后者返回一个65字节的公钥,后跟一个“04”字节。此代码工作正常:
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字节的公钥,而后者返回一个65字节的公钥,后跟一个“04”字节。为什么是65字节的公钥?为什么是65字节的公钥?