Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

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

@gorgos

Description

@gorgos

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    1.x1.0 related issues2.x2.0 related issuesEnhancementIncludes improvements or optimizations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions