How to import ethereum account in web3 if I know address and private key?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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

























  • So, if I understand correctly, you want to access you ganache accounts in your code..right?

    – Rohan Dhar
    Nov 17 '18 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 '18 at 7:11











  • Could you also please upvote the answer, if it served your purpose? Thanks

    – Rohan Dhar
    Nov 17 '18 at 7:49











  • Sorry, reputation not enough for upvote. I am new user there.

    – Mishell Trickster
    Nov 17 '18 at 8:11


















1















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

























  • So, if I understand correctly, you want to access you ganache accounts in your code..right?

    – Rohan Dhar
    Nov 17 '18 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 '18 at 7:11











  • Could you also please upvote the answer, if it served your purpose? Thanks

    – Rohan Dhar
    Nov 17 '18 at 7:49











  • Sorry, reputation not enough for upvote. I am new user there.

    – Mishell Trickster
    Nov 17 '18 at 8:11














1












1








1


1






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
















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















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 7:23









Rohan Dhar

1,2051418




1,2051418










asked Nov 17 '18 at 6:28









Mishell TricksterMishell Trickster

84




84













  • So, if I understand correctly, you want to access you ganache accounts in your code..right?

    – Rohan Dhar
    Nov 17 '18 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 '18 at 7:11











  • Could you also please upvote the answer, if it served your purpose? Thanks

    – Rohan Dhar
    Nov 17 '18 at 7:49











  • Sorry, reputation not enough for upvote. I am new user there.

    – Mishell Trickster
    Nov 17 '18 at 8:11



















  • So, if I understand correctly, you want to access you ganache accounts in your code..right?

    – Rohan Dhar
    Nov 17 '18 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 '18 at 7:11











  • Could you also please upvote the answer, if it served your purpose? Thanks

    – Rohan Dhar
    Nov 17 '18 at 7:49











  • Sorry, reputation not enough for upvote. I am new user there.

    – Mishell Trickster
    Nov 17 '18 at 8:11

















So, if I understand correctly, you want to access you ganache accounts in your code..right?

– Rohan Dhar
Nov 17 '18 at 6:56





So, if I understand correctly, you want to access you ganache accounts in your code..right?

– Rohan Dhar
Nov 17 '18 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 '18 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 '18 at 7:11













Could you also please upvote the answer, if it served your purpose? Thanks

– Rohan Dhar
Nov 17 '18 at 7:49





Could you also please upvote the answer, if it served your purpose? Thanks

– Rohan Dhar
Nov 17 '18 at 7:49













Sorry, reputation not enough for upvote. I am new user there.

– Mishell Trickster
Nov 17 '18 at 8:11





Sorry, reputation not enough for upvote. I am new user there.

– Mishell Trickster
Nov 17 '18 at 8:11












1 Answer
1






active

oldest

votes


















1














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 '18 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',
autoActivateHeartbeat: false,
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
});


}
});














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









1














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 '18 at 7:37
















1














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 '18 at 7:37














1












1








1







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 '18 at 7:36









Rohan DharRohan Dhar

1,2051418




1,2051418













  • Private key should always start with 0x.

    – Rohan Dhar
    Nov 17 '18 at 7:37



















  • Private key should always start with 0x.

    – Rohan Dhar
    Nov 17 '18 at 7:37

















Private key should always start with 0x.

– Rohan Dhar
Nov 17 '18 at 7:37





Private key should always start with 0x.

– Rohan Dhar
Nov 17 '18 at 7:37




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly