web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

Update doc of Web3.utils.soliditySha3 to specify Solidity version

Open ss-build opened this issue 7 years ago • 7 comments

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:

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)

ss-build avatar Apr 25 '19 23:04 ss-build

Thanks for opening this issue! I will test and fix it asap.

nivida avatar Apr 26 '19 06:04 nivida

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.

ss-build avatar Apr 26 '19 07:04 ss-build

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.

nivida avatar Apr 26 '19 10:04 nivida

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.

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 avatar Jun 17 '19 18:06 Xychun

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.

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.

darosior avatar Jul 12 '19 14:07 darosior

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 all a, 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 to web3.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 to keccak256( abi.encodePacked(a, b, c, d) ) in modern Solidity (0.5.x).

theoretical2019 avatar Jan 20 '20 21:01 theoretical2019

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

github-actions[bot] avatar Jul 04 '20 00:07 github-actions[bot]