0

What is the reason that when i try to get the keccak256 values for the same string (which is "0x0000000000000000000000000000000000000000000000000000000000000001" or simply value of 1 in bytes32 representation) with web3 and Solidity I get two different values.

  1. When I use the following code :

    await web3.utils.keccak256("0x0000000000000000000000000000000000000000000000000000000000000001")

The output is : 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6

  1. When I type run the same thing in solidity:

     function getBytes32() public pure returns (bytes32) 
     {
        return keccak256("0x0000000000000000000000000000000000000000000000000000000000000001");
     }
    

The output is: 0xcb371be217faa47dab94e0d0ff0840c6cbf41645f0dc1a6ae3f34447155a76f3

P.S. There is a similar question here but there the issue was with padding. I have used the exact same string. Both are 32 bytes long (i.e.. 64 chars) and two chars for "0x".

1 Answer 1

0

in the first case the output is based on the Hex value, on the second one is based on the string value.

return keccak256(abi.encode(0x0000000000000000000000000000000000000000000000000000000000000001));

it returns the value that you're expecting

0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6
3
  • This shows an error. Undeclared Identifier error Mar 16, 2022 at 14:53
  • @MaksimjeetChowdhary this is another issue, here is the code you can test it on remix it works gist.github.com/Aboudjem/7ef81d0c8d44f98e785d34b77da17f4a
    – Adam Boudj
    Mar 16, 2022 at 15:32
  • Correct, so as you shared, the following will return the hash the same as web in js: bytes32 withString = keccak256(abi.encode("0x0000000000000000000000000000000000000000000000000000000000000001")); Closing this. Mar 17, 2022 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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