question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

web3.soliditySha3 vs ethers.solidityKeccak256 different results for string numbers

See original GitHub issue

I am having a little trouble matching Sha3 hashes between ethers and web3, and was wondering what the issue is.

web3.utils.soliditySha3("hello") will yield the same hash as ethers.utils.solidityKeccak256(['string'],["hello"])

but,

web3.utils.soliditySha3("1234") does not yield the same hash as ethers.utils.solidityKeccak256(['string'],["1234"])

what is happening here?

thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
4skinSkywalkercommented, Oct 18, 2021

@ricmoo in my opinion you’ve structured ethers way better than Web3.js, infact it resonated well to me the way the API is organized. I particularly like the separation of concerns between Provider and Signer.

Anyway, in the end I imported Web3Utils and used soliditySha3 from it, because our users have value in mainnet already and if the hash functions changes its output they won’t be able to unlock those values anymore…

I hope that FIXME up there won’t eventually change/break stuff in the future.

1reaction
ricmoocommented, Dec 1, 2019

If you need to reverse engineer the guessing logic, it is here.

This will likely explain most of the discrepancies, and help figure out what type you need to pass into ethers to get the same result.

You could also probably use web3.utils.toHex(value, true), which will return the guessed type instead of the hex value (the “true” indicates “return the return type”).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get ethereumjs-abi's soliditySHA3 and web3 sign ...
Hello I'd like to create a hash using ethersjs that encodes a number of arguments. I previously used ethereumjs-abi's soliditySHA3 function ...
Read more >
Hashing Algorithms - ethers
The Ethereum Identity function computes the KECCAK256 hash of the text bytes. ethers.utils.keccak256( aBytesLike ) ⇒ ...
Read more >
What is the equivalent of keccak256 in solidity? - Stack Overflow
This is the code in my solidity file and I want to get the same value in the javascript file using ethers or...
Read more >
web3.utils — web3.js 1.0.0 documentation - Read the Docs
mixed - String|Number : A number, number string or HEX string to convert to a BN object. ... Note. To mimic the sha3...
Read more >
ethereum/solidity - Gitter
i just use javascrip and some library from web3.js ... {String} string to be padded * @param {Number} characters that result string should ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found