Update doc of Web3.utils.soliditySha3 to specify Solidity version
Description
In current documentation (v1.0) of Web3.utils.soliditySha3, it says:
Will calculate the sha3 of given input parameters in the same way solidity would. This means arguments will be ABI converted and tightly packed before being hashed.
However, there are two breaking changes of semantics and syntactics about the hash function in Solidity v0.5.x, which makes this API Web3.utils.soliditySha3 not mimic the latest Solidity any more, or not even have a counterpart sha3() in latest Solidity. These two changes are:
-
- Even for
keccak256, it only accepts a single bytes now and doesn't pack the parameters anymore, see docs here. Also, packing parameters before hash actually is not safe, see the Warnings in Solidity's documentation.
So I think the current documentation about Web3.utils.soliditySha3 is confusing for users. If the web3.js team wants to keep this API to support old Solidity versions, I think at least we need to specify that this API is for Solidity v0.4.x's behaviors and not applicable for Solidity v0.5.x.
Versions
- web3.js: 1.0.0-beta.37
- nodejs: v11.13.0
- ethereum node: Ganache CLI v6.4.2 (ganache-core: 2.5.4)
Thanks for opening this issue! I will test and fix it asap.
After second thought, I think it's good to keep this API (or maybe just rename it or refine the doc). Because web3js has web3.eth.abi.encodeParameters to mimic Solidity v0.5.x's abi.encode but seems not to have such an API which mimics Solidity v0.5.x's abi.encodePacked. This API does abi.encodePacked + keccak256 in Solidity v0.5.x, which is a very common and useful case.
Yes, I thought to pass the AbiCoder through but this would give as some architectural problems. I will update the documentation of the soliditySha3 method.
Edit:
I will also add a depreciation message and redirect the developer to the eth.abi class.
After second thought, I think it's good to keep this API (or maybe just rename it or refine the doc). Because web3js has
web3.eth.abi.encodeParametersto mimic Solidity v0.5.x'sabi.encodebut seems not to have such an API which mimics Solidity v0.5.x'sabi.encodePacked. This API doesabi.encodePacked+keccak256in Solidity v0.5.x, which is a very common and useful case.
Sorry, can you clarify your comment?
If I use keccak256(abi.encodePacked(a, b, c, d)); in Solidity v0.5.x, what is the correct counter part for web3?
Thanks!
After second thought, I think it's good to keep this API (or maybe just rename it or refine the doc). Because web3js has
web3.eth.abi.encodeParametersto mimic Solidity v0.5.x'sabi.encodebut seems not to have such an API which mimics Solidity v0.5.x'sabi.encodePacked. This API doesabi.encodePacked+keccak256in Solidity v0.5.x, which is a very common and useful case.Sorry, can you clarify your comment?
If I use
keccak256(abi.encodePacked(a, b, c, d));in Solidity v0.5.x, what is the correct counter part for web3?Thanks!
@Xychun according to my tests, keccak256(abi.encodePacked(a, b, c, d)); in Solidity is equivalent to web3.utils.soliditySha3(a, b, c, d); in web3.
I was bitten by this. The use case for the smart contract developer is like this:
- (i) You have some smart-contract-specific data items,
a, b, c, d. - (ii) You want to serialize and hash the data in Solidity,
H_sol(a, b, c, d). - (iii) You want to serialize and hash the data in JS,
H_js(a, b, c, d). - (iv) For your application to function correctly, you must have
H_sol(a, b, c, d) = H_js(a, b, c, d)for alla, b, c, d.
The docs should clarify how H_sol(a, b, c, d) and H_js(a, b, c, d) "should" be "spelled" if you're developing a new contract (and possibly also explain how to interop with older already-deployed contracts). The documentation implies H_sol(a, b, c, d) = keccak256( abi.encode(a, b, c, d) ) and H_js(a, b, c, d) = web3.soliditySha3(a, b, c, d), which doesn't work (fails (iv) ).
I'd suggest the docs should maybe include language to help devs navigate the most common use cases, for example:
-
For new contracts / applications, Solidity code should use
keccak256( abi.encode( a, b, c, d ))which corresponds toweb3.utils.soliditySha3(web3.eth.abi.encodeParameters([ta, tb, tc, td], [a, b, c, d]))in Javascript. -
For interoperability with Solidity 0.4.x or earlier contracts, you may need to use
web3.utils.soliditySha3({"type" : ta, "value" : a}, {"type" : tb, "value" : b}, {"type" : tc, "value" : c}, {"type" : td, "value" : d})in Javascript, which corresponds tokeccak256( abi.encodePacked(a, b, c, d) )in modern Solidity (0.5.x).
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions