Web3.js: web3.utils.sha3 doesn't take a BN type argument.

Created on 3 Dec 2018  路  4Comments  路  Source: ChainSafe/web3.js

There is a example which takes an BN type argument in https://web3js.readthedocs.io/en/1.0/web3-utils.html#sha3.

However, it throws an error when take an argument with BN type. like below:
TypeError: str.slice is not a function

I think example is wrong, we should fix it.

bug

Most helpful comment

Well they should be able to pass BN which the function can handle. BN has a toString() function on it already so its as simple as new Bn(3).toString().

I think parameter type should accept string | BN and in method if it's a object and not a string we do web3.utils.isBN(value) and if that's true then we turn it into a string so Hash.keccak256 can do it's magic 馃憤 think that would work nicely and means it's sticking to the public api docs. (as i'm guessing it was mean't to work that way 馃槃)

All 4 comments

You are correct - in theory it should only accept a string as Hash.keccak256 can only take a string, we have 2 options here

1 - Make web3.utils.sha3 toString anything which comes in which means it will then will be able to accept any number or BN or string which could be nice
2 - Update docs + types to only accept a string all the time

@nivida happy to do this in PR #2082 as it effects the types as well - let me know which option you fancy!

@nujabes403 Thanks for spotting this dude 馃憤

@joshstevens19 Thank you.

Accepting number as an argument could be confusing.
How about only transforming BN instance to string type through .toString(10)?

Well they should be able to pass BN which the function can handle. BN has a toString() function on it already so its as simple as new Bn(3).toString().

I think parameter type should accept string | BN and in method if it's a object and not a string we do web3.utils.isBN(value) and if that's true then we turn it into a string so Hash.keccak256 can do it's magic 馃憤 think that would work nicely and means it's sticking to the public api docs. (as i'm guessing it was mean't to work that way 馃槃)

I've closed it because the fix is now merged.

Was this page helpful?
0 / 5 - 0 ratings