1

I would like to generate hash value within Truffle using the web3.sha3() function and the hash value is required to be consistent with that one generated in Solidity. The basic example is sha3(uint256(1)). I read the related question here How does Solidity's sha3 / keccak256 hash uints? However the LEFTPAD method is not supported in Truffle so I did the hashing in the following way:

(within Truffle) web3.sha3("0x000000......000001") which produced "0xcb371be217faa47dab94e0d0ff0840c6cbf41645f0dc1a6ae3f34447155a76f3"

(within solidity) sha3(uint256(1)) whcih produced "0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6"

These two values are apparently not the same. I would like to know how can I generate the same hash in Truffle with respect to this given example.

3

Try specifying hex encoding explicitly:

web3.sha3("0x0000000000000000000000000000000000000000000000000000000000000001", {encoding: "hex"});
'0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6'

Without specifying the encoding, the argument is treated as an ascii string by default. This behaviour is changed in web3.js v.1.0.0, but I guess Truffle is using an older version.

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.