Web3.js: web3.eth.accounts.privateKeyToAccount returns a different address than shown on MyEtherWallet

Created on 23 Sep 2017  路  4Comments  路  Source: ChainSafe/web3.js

When I call web3.eth.accounts.privateKeyToAccount it returns a different address than what I get on MyEtherWallet. Do you know why? Or it's a bug?

Most helpful comment

Nevermind, I had to add 0x to it

All 4 comments

Nevermind, I had to add 0x to it

Just to clarify for any future wanderers, like I was.
You need to prepend 0x to your private key:

const privateKey = '555';
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey);

thx adnan-i. I guess nobody knows why this is not documented in the API docs...

Also good to know: for me, with [email protected] , passing the key without 0x did resulted in a successful transaction while the address was wrong oO So apparently the signing does check for the prefix and handles the prefix-missing case, while this is not done on the address-derivation. I guess.

Was this page helpful?
0 / 5 - 0 ratings