我启动ganache-gui并看到很多帐户,它们具有私钥和助记词。然后,我使用nodejs和web3 1.x.x连接到该测试网,所以我的wallet.length为0。我想通过助记词组从ganache导入所有钱包,或者最好使用私钥导入一个地址。我可以这样做吗?我尝试了web3.eth.accounts.privateKeyToAccount(privateKey);
,但返回了新帐户。它是如何工作的? Metamask只能通过privateKey来做到这一点。
答案 0 :(得分:1)
要访问ganache帐户,您必须执行以下操作:
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();