简体   繁体   中英

Access Ganache Accounts via web3

How do I create an account variable in web3 with a private network? Right now I am on ganache port:7545

I have this variable written in my code:

    var account1 = web3.eth.accounts[0];

And when I log it, it returns only the variable which is currently selected in my meta mask.

So if I am on Account 1 in meta mask then this^ var logs the address Account 1 and if I am on Account 2 the same var will log the Account 2 address.

So how can I make a var which can access an imported ganache meta mask account which I do not currently have selected?

ie I have Account1 selected in my meta mask and I want

  var account2 = web3.eth.accounts[1]

to grab Account2

Unfortunately, MetaMask doesn't support this. It will always return an array of size 1 when you use web3.eth.getAccounts(). If you're using Ganache, you can just connect directly to that (as opposed to changing the MetaMask provider to localhost).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM