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

Web3Utils toHex/soliditySha3: Accept Buffer as input or throw error #3021

Closed
gorgos opened this issue Aug 12, 2019 · 0 comments
Closed

Web3Utils toHex/soliditySha3: Accept Buffer as input or throw error #3021

gorgos opened this issue Aug 12, 2019 · 0 comments
Labels
1.x 1.0 related issues 2.x 2.0 related issues Enhancement Includes improvements or optimizations

Comments

@gorgos
Copy link

gorgos commented Aug 12, 2019

Description

I just ran into the issue when using Web3Utils.soliditySha3. I was passing a Buffer as input instead of a String and then the resulting hash was different to the one inside the smart contract. The reason being that Web3Utils.toHex apparently doesn't accept a Buffer as input. However, it didn't throw an error / give a warning or anything.

For anyone reading this, I fixed it by passing a string:

const hexString = '0x' + bytesBuffer.toString('hex')
const hash = Web3Utils.soliditySha3(hexString , someDataString)

Alternatively, you could use https://github.com/ethereumjs/ethereumjs-util/blob/master/docs/README.md#const-buffertohex.

Expected behavior

Calling Web3Utils.soliditySha3/Web3Utils.toHex with Buffer as input should either work as expected or throw an error or at least give a warning. Web3Utils.toHex with a Buffer as input currently returns a very strange, very long result.

Versions

  • web3-utils 1.2.1
@nivida nivida added 1.x 1.0 related issues 2.x 2.0 related issues Enhancement Includes improvements or optimizations labels Aug 13, 2019
akhil2015 added a commit to akhil2015/web3.js that referenced this issue Sep 25, 2019
nivida added a commit that referenced this issue Oct 14, 2019
* fixed Web3Utils toHex for Buffer input #3021

* updated toHex funcDoc and CHANGELOG.md

* used toString instead of bytesToHex

* updated packages/web3-utils/src/utils.js

Co-Authored-By: Samuel Furter <nivida@users.noreply.github.com>
@nivida nivida mentioned this issue Oct 21, 2019
11 tasks
@nivida nivida closed this as completed Nov 25, 2019
nachomazzara pushed a commit to nachomazzara/web3.js that referenced this issue Jun 4, 2020
* fixed Web3Utils toHex for Buffer input web3#3021

* updated toHex funcDoc and CHANGELOG.md

* used toString instead of bytesToHex

* updated packages/web3-utils/src/utils.js

Co-Authored-By: Samuel Furter <nivida@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues 2.x 2.0 related issues Enhancement Includes improvements or optimizations
Projects
None yet
Development

No branches or pull requests

2 participants