pubToAddress method from ethereumjs-util throws AssertionError false true

0 votes

I have the following code on nodejs: var keythereum = require('keythereum'); var crypto = require("crypto"); var eccrypto = require("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");

While executing last line of code:

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

It throws an assertion error like this:

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)

Found out that the assertion that fails is this one:

/**
 * 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)
}

Should I just remove first byte (04) from address?

This piece of code is based on https://github.com/ethereumjs/keythereum/blob/master/test/keys.js trying to create an Ethereum private key and getting its corresponding public key and address. It fails at the final step. Any hint about what is going on would be very helpful.

Oct 8, 2018 in Blockchain by digger
• 26,740 points
1,259 views

1 answer to this question.

0 votes

This code works fine:

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'));

The point is using:

var publicKey = util.privateToPublic(privateKey);

instead of:

var publicKey = eccrypto.getPublic(privateKey);

because former one returns a 64 byte publicKey whilst latter one returns a 65 byte publicKey preceded by a "04" byte.

answered Oct 8, 2018 by Omkar
• 69,220 points

Related Questions In Blockchain

0 votes
1 answer

How to use Proc/lamba returned from a method in Ruby?

You need to remove the colon: list.select(&valid_transaction) The & ...READ MORE

answered Aug 24, 2018 in Blockchain by digger
• 26,740 points
712 views
0 votes
1 answer

Ethereum call contract method that emits an event, from another contract

You need to refer to the EventEmitter by ...READ MORE

answered Sep 25, 2018 in Blockchain by digger
• 26,740 points
2,228 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
1,468 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,947 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,472 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,461 views
+1 vote
1 answer

Permission is denied in Oracle VM when trying to move multichain util, multichaind and multichain-cli from tmp to /usr/local/bin

You will need root permissions to move a file ...READ MORE

answered Apr 8, 2019 in Blockchain by Omkar
• 69,220 points
885 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP