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.










share|improve this question









New contributor




Mishell Trickster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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.










share|improve this question









New contributor




Mishell Trickster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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.










share|improve this question









New contributor




Mishell Trickster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Mishell Trickster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Mishell Trickster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 17 at 7:23









Rohan Dhar

457210




457210






New contributor




Mishell Trickster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 17 at 6:28









Mishell Trickster

84




84




New contributor




Mishell Trickster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Mishell Trickster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Mishell Trickster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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










  • 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












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();





share|improve this answer





















  • Private key should always start with 0x.
    – Rohan Dhar
    Nov 17 at 7:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Mishell Trickster is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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

























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();





share|improve this answer





















  • Private key should always start with 0x.
    – Rohan Dhar
    Nov 17 at 7:37















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();





share|improve this answer





















  • Private key should always start with 0x.
    – Rohan Dhar
    Nov 17 at 7:37













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();





share|improve this answer












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();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 17 at 7:36









Rohan Dhar

457210




457210












  • 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




Private key should always start with 0x.
– Rohan Dhar
Nov 17 at 7:37










Mishell Trickster is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy