I am attempting to replicate a bytes32 to uint256 conversion in nodejs. I would like to get the same result as this example contract

pragma solidity ^0.4.23;

contract TestContract {

    bytes32 internal lastHash;

    constructor() {
        lastHash = keccak256(abi.encodePacked(msg.sender, now));

    function getHash() public view returns(bytes32) {
        return lastHash;

    function getHashNum() public view returns(uint256) {
        return uint256(lastHash);

In a truffle test I've tried the following(along with many others):

const TestContract = artifacts.require('TestContract.sol');
const BigNumber = web3.BigNumber;


contract("TestContract", function(accounts) {

    it('can convert bytes32 to BigNumber in node', async function() {
        let contract = await TestContract.new({from:accounts[0]});
        let hash = await contract.getHash();
        let hashNum = await contract.getHashNum();
        let result = new BigNumber(hash);


Basically I am not sure how the type conversion happens or how to replicate it in node. Any suggestions would be awesome!


The code above does work except for the last line. I needed to use the big number methods for compairing e.g.

assert(result.eq(hashNum) == true, "Result should be equal to hashNum");
  • You should add your last edit as answer so you can later mark the question as resolved.
    – Ismael
    Jun 29 '18 at 2:19

As Ismael suggested here is the solution(in the edited op):

The code above does in fact work except for the last line


I haven't bothered to check into why exactly that isn't working but I did find a working solution. I simply used the big number tools and assert:

assert(result.eq(hashNum) == true, "Result should be equal to hashNum");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.