5

How can I get the same resulting hash from Keccak-256 ()in solidity and web3.sha3() in web3?

IN solidity using keccak256( address ) and using web3.sha3 ( address ) gives different results.

In web3 the address field is a string while in solidity it is an address.

How can I convert web3 string to an address type field?

I tried converting the address type field to a string in solidity and I still received different results in hashes.

For address to sting in solidity I used

function toString(address x) returns (string) {
    bytes memory b = new bytes(20);
    for (uint i = 0; i < 20; i++)
        b[i] = byte(uint8(uint(x) / (2**(8*(19 - i)))));
    return string(b);
}
  • @RichardHorrocks this isn't a sha3 vs keccak256 issue, it's an input argument encoding issue, so I don't believe that particular question is a proper duplicate – carver Aug 10 '17 at 17:41
  • Okay, yep, looks like you're probably right - apologies for jumping the gun. +1 :-) – Richard Horrocks Aug 10 '17 at 18:14
8

web3.sha3() takes an encoding parameter to specify that you are passing in a hex address:

address_string = '0x5b2063246f2191f18f2675cedb8b28102e957458';
web3.sha3(address_string, {encoding: 'hex'});

As a general side-note: you have to pay gas costs to do any custom work in Solidity so, when possible, you are best off working on the javascript side.

3

Also, you can perform keccak256( address ) in a constant solidity function locally, and then pass the result into a transaction. Of course, this is not safe for production, if you let users do that in their browser.

  • what's the issue with using this for production? it works. – k26dr Dec 25 '17 at 7:34
  • 1
    the resulting hash could be moedified before it gets passed into a transaction. "Don't trust the client" doctrine. – Nikita Fuchs Dec 30 '17 at 8:25
2

The latest version of web3 has a web3.utils.soliditySha3 function you can use that mimics Solidity's hashing:

address = '0x407D73d8a49eeb85D32Cf465507dd71d507100c1';
hash = web3.utils.soliditySha3(address); 

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.