web3.soliditySha3 vs ethers.solidityKeccak256 different results for string numbers
See original GitHub issueI 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:
- Created 4 years ago
- Comments:12 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.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”).