简体   繁体   English

通过web3访问Ganache帐户

[英]Access Ganache Accounts via web3

How do I create an account variable in web3 with a private network? 如何在具有专用网络的web3中创建帐户变量? Right now I am on ganache port:7545 现在我在ganache端口: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. 因此,如果我位于元掩码中的帐户1上,则此^ var记录地址帐户1,如果我位于帐户2上,则相同的var将记录帐户2地址。

So how can I make a var which can access an imported ganache meta mask account which I do not currently have selected? 那么,如何制作一个可以访问我当前未选择的导入的ganache meta mask帐户的var?

ie I have Account1 selected in my meta mask and I want 即我在元掩码中选择了Account1,我想要

  var account2 = web3.eth.accounts[1]

to grab Account2 抢Account2

Unfortunately, MetaMask doesn't support this. 不幸的是,MetaMask不支持此功能。 It will always return an array of size 1 when you use web3.eth.getAccounts(). 使用web3.eth.getAccounts()时,它将始终返回大小为1的数组。 If you're using Ganache, you can just connect directly to that (as opposed to changing the MetaMask provider to localhost). 如果您使用的是Ganache,则可以直接连接到该主机(与将MetaMask提供程序更改为localhost相对)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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