Web3.js: Allow web3.sha3() to hash more than one thing, and numbers instead of strings.

Created on 30 May 2015  路  4Comments  路  Source: ChainSafe/web3.js

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.

enhancement

Most helpful comment

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')

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joeriexelmans picture joeriexelmans  路  3Comments

zamoore picture zamoore  路  3Comments

gabmontes picture gabmontes  路  3Comments

connectdotz picture connectdotz  路  3Comments

dzarezenko picture dzarezenko  路  3Comments