0

I'm using for deploying contracts hdwallet-provider, but I have issue on client side to get multiple accounts using web3.eth.getAccounts() call.

I know this is like duplicate question and truffle already has solution for this, but I have issue getting more than one account address.

On metamask I have three accounts, but only the default one is showing on the client side. From the first snippet u can see that I'm setting 3 as fourth parametar

Deploy code

const provider = new HDWalletProvider(
  'mnemonic....',
  'https://rinkeby.infura.io/v3/bba91a6c349c44afbde264a1bda1af75', 0, 3
);

How I import web3 in client code

import Web3 from 'web3';

if (window.ethereum) {
  window.web3 = new Web3(window.ethereum);

  (async () => {
    await window.ethereum.enable();
  })();
}
// Legacy dapp browsers...
else if (window.web3) {
  window.web3 = new Web3(window.web3.currentProvider);
}
// Non-dapp browsers...
else {
  console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
}

export default window.web3;

Client code

web3.eth.getAccounts().then(console.log);

BR, Igor

1

This is the expected behavior that you will get the only account selected in the Metamask. This is for privacy reasons.

Metamsk even don't expose one account be default, Dapps need to send an access request to the account by calling ethereum.enable(). Dapps will get the access to the selected metamask account once the acces request is approved by user.

You can read more about in this blog post.

| improve this answer | |
  • Thanks. My concern was, how I get current account address, but I can use web3.currentProvider.selectedAddress – Игор Ташевски Nov 14 '19 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.