Search code examples
node.jsexpressethereumblockchainweb3js

How to login ethereum account by privateKey via web3?


Im creating a simple ethereum network and connect via web3js but i dont find any function to find address from privateKey, I use web3.eth.accounts.privateKeyToAddress, but it create a new account with balance 0 while exist address with this privatekey have balance 100


Solution

  • Please use web3.eth.accounts.privateKeyToAccount.

    web3.eth.accounts.privateKeyToAccount('0x348ce564d427...');
    
    > {
          address: '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01',
          privateKey: '0x348ce564d427...',
          signTransaction: function(tx){...},
          sign: function(data){...},
          encrypt: function(password){...}
      }