Web3.js: How to import account if I know address and private key?

Created on 16 Nov 2018  路  3Comments  路  Source: ChainSafe/web3.js

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.

Most helpful comment

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

All 3 comments

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

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

praveengupta0895 picture praveengupta0895  路  3Comments

gabmontes picture gabmontes  路  3Comments

SCBuergel picture SCBuergel  路  3Comments

zamoore picture zamoore  路  3Comments

oktapodia picture oktapodia  路  3Comments