[Ethereum] web3.personal.newAccount how to get address

accountsweb3js

If I do web3.personal.newAccount("passphrase") from my code then how can I get back the address that was created using my passphrase?
Moreover can anyone tell me the difference of web3.personal.newAccount & web3.eth.accounts.create both are creating accounts and if by second method i store the key in keystore then how can I unlock the account using web3.personal.unlockAccount ?

Best Answer

Moreover can anyone tell me the difference of web3.personal.newAccount & web3.eth.accounts.create both are creating accounts and if by second method i store the key in keystore then how can I unlock the account using web3.personal.unlockAccount?

The big difference is:

  1. web3.personal.newAccount is used to generate addresses on the wallet associated with your node/provider. This is misleading as personal method is not necessarily local. Do not use this method unless you are running your own node (i.e. Geth or Parity). If you are using Infura or similar provider service, I doubt you'd want to create a wallet account on their node.

  2. web3.eth.accounts.create will generate a local key pair. If you use this method, definitely make sure to store your key info, as it is only kept TEMPORARY in memory. You can then send transactions with web3.eth.accounts.signTransaction(tx, privateKey), but you have to encode any contract data with an encodeABI method first before placing it in tx. This is not a recommended method, and this approach is used under the hood by browser-based wallet providers like Metamask.

To query the list of accounts controlled by your node wallet use web3.eth.getAccounts(). I need to look into how you can get a list of locally generated addresses.