Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow web3.sha3() to hash more than one thing, and numbers instead of strings. #226

Closed
Smithgift opened this issue May 29, 2015 · 4 comments
Labels
Enhancement Includes improvements or optimizations

Comments

@Smithgift
Copy link

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.

@frozeman frozeman added the Enhancement Includes improvements or optimizations label Jun 4, 2015
@tomlion
Copy link

tomlion commented Oct 1, 2015

Aha, I need this too to make life easier 😃

@axic
Copy link
Contributor

axic commented Jul 29, 2016

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

@marekkirejczyk
Copy link

marekkirejczyk commented Jun 27, 2017

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:
ethereumjs/ethereumjs-abi#43

@frozeman
Copy link
Contributor

frozeman commented Aug 8, 2017

I guess this is what you've wanted: http://web3js.readthedocs.io/en/1.0/web3-utils.html#soliditysha3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Includes improvements or optimizations
Projects
None yet
Development

No branches or pull requests

5 participants