Solidity's sha3() can hash a group of numbers, while web3.sha3() can only hash a single string.
Consider a contract (an auction, say, or in my case a simple game) with secret numerical bids. Suppose the contract stores preimages of the bid hashed with a nonce and maybe a salt. When the bidding is over, the bidders can submit the bid and the nonce to confirm the bid, and solidity can hash them all to confirm the bid is accurate, then work with the bid. This part works fine.
But how does the client generate the preimages to send? web3.sha3() can only hash one string. The string "100" is not the same as the integer one hundred. If I send the string "100" however, the contract would have to waste gas parsing the string. It'd be far easier if web3.sha3 could just hash numbers on its own.
Aha, I need this too to make life easier :smiley:
This is actually implemented in ethereumjs-abi:
var abi = require('ethereumjs-abi')
var BN = require('bn.js')
abi.soliditySHA3(
[ "address", "address", "uint", "uint" ],
[ new BN("43989fb883ba8111221e89123897538475893837", 16), 0, 10000, 1448075779 ]
).toString('hex')
That would be lovely if it fully worked. Unfortunately, does not work for type bytes32. See examples below:
contract TestTightHash {
function coded() constant returns (bytes32) {
return keccak256(bytes32("Volume"), bytes32("Volume"));
}
}
coded() returns 0x7ab276f38b269dc879fb051ca72ac238c1a03c2db62bbd7a499c1dd822c3e020
var t = abi.soliditySHA3([ "bytes32", "bytes32" ], [ "Volume", "Volume" ]).toString('hex');
t == 8239dae94d559efaf979a8ffdb349ed1d13c6636d28ab641f43ae2bc0c0b985a
Reported on ethereumjs-abi:
https://github.com/ethereumjs/ethereumjs-abi/issues/43
I guess this is what you've wanted: http://web3js.readthedocs.io/en/1.0/web3-utils.html#soliditysha3
Most helpful comment
This is actually implemented in ethereumjs-abi: