web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.32k stars 4.95k forks source link

How to import account if I know address and private key? #2053

Closed mishell-trickster closed 5 years ago

mishell-trickster commented 5 years ago

I start ganache-gui and see lot of accounts, they has privat keys and mnemonic phrase. Then I connect to this testnet with nodejs and web3 1.x.x, so my wallet.length is 0. I want to import all wallet from ganache by mnemonic phrase or better import one address using private key. Could I do this? I tried web3.eth.accounts.privateKeyToAccount(privateKey); but its return new account. How it works? Metamask can do this by privateKey.

mishell-trickster commented 5 years ago

I did not know that the key must start with 0x.

yehan-xiao commented 5 years ago

Just post for people might meet the same situation like me... for web3.eth.accounts.wallet.add(account) the private key must start with 0x too.

ankita-p17 commented 5 years ago

I am using geth 1.9.0 which has web3 version api: "0.20.1", How can I import account here using key and address? web3.eth.accounts.wallet.add is returning undefined