web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

web3.eth.accounts.privateKeyToAccount generates different accounts from the same privatekey

Open Keith-CY opened this issue 8 years ago • 5 comments

web3.eth.accounts.privateKeyToAccount treats '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' and 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' as different private keys.

It's a bug in eth-lib, would you please add a patch to Account Module until the pr is merged?

Keith-CY avatar Oct 18 '18 13:10 Keith-CY

Hi there, hopefully this helps https://dev.to/yukkurisinai/big-trap-of-web3js-ffi

Jason-Soo avatar Oct 24 '18 02:10 Jason-Soo

@Jason-Soo Thanks, that's almost what I PRed in eth-lib.

Keith-CY avatar Oct 24 '18 03:10 Keith-CY

@Keith-CY I recently tried using ethers.js not as big community but its quite simple as well do check them out and I used this very simple amateur code to solve it haha

if (key.substring(0, 2) !== '0x') {
    key = '0x' + key;  //checking
}

Jason-Soo avatar Oct 24 '18 03:10 Jason-Soo

+1, spent some time to figure out why my public address isn't valid. When you create account from the private key without 0x - everything will be executed without error. But later on will crash, for exmaple because:

  var sig = secp256k1.sign(msgHash, privateKey);
                      ^

RangeError: private key length is invalid

Also, according to documentation https://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html#privatekeytoaccount input should always start from 0x therefore private key correct format checking is necessary.

lebed2045 avatar Mar 03 '19 06:03 lebed2045

Closed because of the answer from @lebed2045

nivida avatar Apr 17 '19 11:04 nivida