Web3.js: web3.eth.accounts.privateKeyToAccount accepts private key without "0x" prefix but returns incorrect result

Created on 19 Aug 2019  路  3Comments  路  Source: ChainSafe/web3.js

Description

According to the example in the documentation, function web3.eth.accounts.privateKeyToAccount expects private key to be passed to hexadecimal string prefixed with "0x". If private key is passed without "0x" prefix, the function silently returns incorrect result.

Expected behavior

Function web3.eth.accounts.privateKeyToAccount throws on incorrect private key format, and always returns correct result on correctly formatted private keys. Whether hexadecimal private key without "0x" prefix is correct or not is discussible.

Actual behavior

On hexadecimal private key without "0x" prefix function web3.eth.accounts.privateKeyToAccount silently returns incorrect result.

Steps to reproduce the behavior

Call function web3.eth.accounts.privateKeyToAccount with parameters 0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709 and 348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709 and compare the results.

Versions

  • web3.js: 1.0.0-beta.55
1.x enhancement

Most helpful comment

@3sGgpQ8H @DalderupMaurice I had the same issue, it looks like the fix has already landed in the [email protected] pre-release (fixed in #2929) so this should be fixed in the next release.

Yes! I saw it was mentioned in the release notes afterward.
It should be an easy backport until the v2 goes live though

All 3 comments

same for web3.js version 1.2.1
It returns a faulty result WITHOUT the 0x prefix

@3sGgpQ8H @DalderupMaurice I had the same issue, it looks like the fix has already landed in the [email protected] pre-release (fixed in https://github.com/ethereum/web3.js/pull/2929) so this should be fixed in the next release.

@3sGgpQ8H @DalderupMaurice I had the same issue, it looks like the fix has already landed in the [email protected] pre-release (fixed in #2929) so this should be fixed in the next release.

Yes! I saw it was mentioned in the release notes afterward.
It should be an easy backport until the v2 goes live though

Was this page helpful?
0 / 5 - 0 ratings