How to import ethereum account in web3 if I know address and private key?
up vote
1
down vote
favorite
I start ganache-gui and see lot of accounts, they have private 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 returns new account. How does it work? Metamask can do this just by privateKey.
node.js web3js
New contributor
add a comment |
up vote
1
down vote
favorite
I start ganache-gui and see lot of accounts, they have private 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 returns new account. How does it work? Metamask can do this just by privateKey.
node.js web3js
New contributor
So, if I understand correctly, you want to access you ganache accounts in your code..right?
– Rohan Dhar
Nov 17 at 6:56
Right. I also want to know how to import any account knowing its private key, as metamask does.
– Mishell Trickster
Nov 17 at 7:11
Could you also please upvote the answer, if it served your purpose? Thanks
– Rohan Dhar
Nov 17 at 7:49
Sorry, reputation not enough for upvote. I am new user there.
– Mishell Trickster
Nov 17 at 8:11
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I start ganache-gui and see lot of accounts, they have private 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 returns new account. How does it work? Metamask can do this just by privateKey.
node.js web3js
New contributor
I start ganache-gui and see lot of accounts, they have private 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 returns new account. How does it work? Metamask can do this just by privateKey.
node.js web3js
node.js web3js
New contributor
New contributor
edited Nov 17 at 7:23
Rohan Dhar
457210
457210
New contributor
asked Nov 17 at 6:28
Mishell Trickster
84
84
New contributor
New contributor
So, if I understand correctly, you want to access you ganache accounts in your code..right?
– Rohan Dhar
Nov 17 at 6:56
Right. I also want to know how to import any account knowing its private key, as metamask does.
– Mishell Trickster
Nov 17 at 7:11
Could you also please upvote the answer, if it served your purpose? Thanks
– Rohan Dhar
Nov 17 at 7:49
Sorry, reputation not enough for upvote. I am new user there.
– Mishell Trickster
Nov 17 at 8:11
add a comment |
So, if I understand correctly, you want to access you ganache accounts in your code..right?
– Rohan Dhar
Nov 17 at 6:56
Right. I also want to know how to import any account knowing its private key, as metamask does.
– Mishell Trickster
Nov 17 at 7:11
Could you also please upvote the answer, if it served your purpose? Thanks
– Rohan Dhar
Nov 17 at 7:49
Sorry, reputation not enough for upvote. I am new user there.
– Mishell Trickster
Nov 17 at 8:11
So, if I understand correctly, you want to access you ganache accounts in your code..right?
– Rohan Dhar
Nov 17 at 6:56
So, if I understand correctly, you want to access you ganache accounts in your code..right?
– Rohan Dhar
Nov 17 at 6:56
Right. I also want to know how to import any account knowing its private key, as metamask does.
– Mishell Trickster
Nov 17 at 7:11
Right. I also want to know how to import any account knowing its private key, as metamask does.
– Mishell Trickster
Nov 17 at 7:11
Could you also please upvote the answer, if it served your purpose? Thanks
– Rohan Dhar
Nov 17 at 7:49
Could you also please upvote the answer, if it served your purpose? Thanks
– Rohan Dhar
Nov 17 at 7:49
Sorry, reputation not enough for upvote. I am new user there.
– Mishell Trickster
Nov 17 at 8:11
Sorry, reputation not enough for upvote. I am new user there.
– Mishell Trickster
Nov 17 at 8:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
To access the ganache accounts, you have to do the following:
const ganache = require('ganache-cli');
const Web3 = require('web3');
//ganache client running on port 7545
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
const getAccounts = async () =>{
//To get all accounts
let accounts = await web3.eth.getAccounts();
//To get accounts with private key
let account = await web3.eth.accounts.privateKeyToAccount('0x'+privateKey);
//privateKey is the key that you get from Ganache client
}
getAccounts();
Private key should always start with 0x.
– Rohan Dhar
Nov 17 at 7:37
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
To access the ganache accounts, you have to do the following:
const ganache = require('ganache-cli');
const Web3 = require('web3');
//ganache client running on port 7545
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
const getAccounts = async () =>{
//To get all accounts
let accounts = await web3.eth.getAccounts();
//To get accounts with private key
let account = await web3.eth.accounts.privateKeyToAccount('0x'+privateKey);
//privateKey is the key that you get from Ganache client
}
getAccounts();
Private key should always start with 0x.
– Rohan Dhar
Nov 17 at 7:37
add a comment |
up vote
0
down vote
accepted
To access the ganache accounts, you have to do the following:
const ganache = require('ganache-cli');
const Web3 = require('web3');
//ganache client running on port 7545
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
const getAccounts = async () =>{
//To get all accounts
let accounts = await web3.eth.getAccounts();
//To get accounts with private key
let account = await web3.eth.accounts.privateKeyToAccount('0x'+privateKey);
//privateKey is the key that you get from Ganache client
}
getAccounts();
Private key should always start with 0x.
– Rohan Dhar
Nov 17 at 7:37
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
To access the ganache accounts, you have to do the following:
const ganache = require('ganache-cli');
const Web3 = require('web3');
//ganache client running on port 7545
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
const getAccounts = async () =>{
//To get all accounts
let accounts = await web3.eth.getAccounts();
//To get accounts with private key
let account = await web3.eth.accounts.privateKeyToAccount('0x'+privateKey);
//privateKey is the key that you get from Ganache client
}
getAccounts();
To access the ganache accounts, you have to do the following:
const ganache = require('ganache-cli');
const Web3 = require('web3');
//ganache client running on port 7545
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
const getAccounts = async () =>{
//To get all accounts
let accounts = await web3.eth.getAccounts();
//To get accounts with private key
let account = await web3.eth.accounts.privateKeyToAccount('0x'+privateKey);
//privateKey is the key that you get from Ganache client
}
getAccounts();
answered Nov 17 at 7:36
Rohan Dhar
457210
457210
Private key should always start with 0x.
– Rohan Dhar
Nov 17 at 7:37
add a comment |
Private key should always start with 0x.
– Rohan Dhar
Nov 17 at 7:37
Private key should always start with 0x.
– Rohan Dhar
Nov 17 at 7:37
Private key should always start with 0x.
– Rohan Dhar
Nov 17 at 7:37
add a comment |
Mishell Trickster is a new contributor. Be nice, and check out our Code of Conduct.
Mishell Trickster is a new contributor. Be nice, and check out our Code of Conduct.
Mishell Trickster is a new contributor. Be nice, and check out our Code of Conduct.
Mishell Trickster is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348832%2fhow-to-import-ethereum-account-in-web3-if-i-know-address-and-private-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So, if I understand correctly, you want to access you ganache accounts in your code..right?
– Rohan Dhar
Nov 17 at 6:56
Right. I also want to know how to import any account knowing its private key, as metamask does.
– Mishell Trickster
Nov 17 at 7:11
Could you also please upvote the answer, if it served your purpose? Thanks
– Rohan Dhar
Nov 17 at 7:49
Sorry, reputation not enough for upvote. I am new user there.
– Mishell Trickster
Nov 17 at 8:11