Changes to `web3.sha3`
See original GitHub issueWhat was wrong?
Currently, the implementation of web3.sha3
has some unexpected behavior and non ideal behavior.
- It uses the
web3_sha3
RPC endpoint which allows for different providers to return different values. - By default it expects the value to be hashed to be passed in as a hex encoded string.
- If you specify any encoding besides hex, it treats it as if it was bytes.
How can it be fixed?
1. Use eth_utils.keccak
to compute the hash.
This is more reliably going to produce the result that users expect as well as reducing RPC request overhead.
2. Change the default encoding to bytes
.
This would be a breaking change so figuring out how to deprecate the current behavior while giving users the appropriate time to upgrade would be ideal. This could be done in a two step process.
We should have an explicit list of encodings that are supported and have logic for dealing with those appropriately. I suggest bytes
, utf8
, and hex
.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
web3_sha3 - Alchemy Docs
Web3's Most Powerful SDK ... The Alchemy SDK is the easiest way to connect your dApp to the blockchain. Just download, write two...
Read more >Solidity and Web3 sha3() methods return something else
In my contract, I have a function that returns the sha3 hash of a certain set of values. While running some tests I...
Read more >Are you really using SHA-3 or old code? | by ConsenSys
However, in 2014, NIST made slight changes to the Keccak submission and published FIPS 202, which became the official SHA-3 standard in August...
Read more >How to get the same output using `sha3` by providing same ...
You need to parse the address first. Right now, you're computing the hash of the string representation of the address. Try this:
Read more >web3.utils — web3.js 1.0.0 documentation - Read the Docs
Will calculate the sha3 of given input parameters in the same way solidity would. This means arguments will be ABI converted and tightly...
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
Went with
hexstr
. It grew on me.Encoding Background
There is a caveat to # 2 above, which is that if someone passes
bytes
, then the encoding is meaningless and should be ignored. Also, “encoding” as the keyword argument is confusing here, because it actually means a different thing for each of these options:Also confusing is that python supports hex literals, which seems reasonable to support too, like:
web3.sha3(0x74657874)
. The primitive here isint
. Again, likebytes
, if an int is passed, then “encoding” should be ignored.The “encoding” here is only ever referring to the type of string we’re looking at. We can (and should) auto-detect all the rest. We’d be done, if it wasn’t for those meddling kids and their hex-string encodings.
New API
So what I’m ramping up to is a new API (the breaking change):
My least favorite part is overwriting the builtin
hex
, but I really want a short syntax here. Even core python does stuff like this (seefile
kwarg inprint
).So you could call any of:
This is concise, easy to read, and should cover all cases. It’s not terribly important that people understand that the text is encoded into utf8, because text in all clients should always use this same encoding.
Migration Plan
So the migration plan for web3.sha3 would be something like:
bytes
andint
, and encode them appropriately, ignoring the ‘encoding’ kwarghex
andtext
sha3
calls, unless they are using the new functionality in 2 and 3