1

This question already has an answer here:

I'm building a DApp using web3js that sends ERC20 Tokens from a browser Metamask account to an escrow contract on the Rinkeby testnet. I'm using a Rinkeby node from Infura, and when I call web3.eth.accounts[0] or web3.eth.getAccounts(), they both return an empty array. I did some digging and figured that since I'm connected to Infura I can't use local accounts. Is there any way to circumvent this? I tried manually providing the account address but that didn't work either.

EDIT: I want to use the accounts already in a client's browser in the metamask extension

marked as duplicate by Ismael, Achala Dissanayake, shane, Nicolas Massart, Rob Hitchens - B9lab Feb 26 at 15:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

An Infura provider will not provide accounts. It will just allow you to broadcast transactions and make calls to the Ethereum network (testnets or mainnet).

It's useful if you want to deploy / interact with ethereum network from a server.

However, when it comes to user interacting with your Dapp from their browser, Metamask will both allow users to sign their transactions AND relay these transactions to the Ethereum network, using an Infura node. You don't have to do anything there, except switching to the desired testnet on Metamask. Your own Infura node will not be involved there.

With this being said, there is another thing that you need to understand. Metamask added a new privacy feature to force Dapp to ask users their permissions before Metamask respond to the getBalance() method. So, before you execute getBalance(), you need to execute this method:

await window.ethereum.enable();

It will prompt the user for authorizing this Dapp. Only after that you can call getBalance().

More information here:

https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

  • Thanks for the answer Julien, but I figured a way around it using truffle-hd-wallet, though I'd like to know how I can do it without using that because even though I was signed in and switched to the correct testnet on Metamask, the accounts array was empty. Does it just work when I call/perform a transaction with a method on a smart contract? I thought I had to set the default account, didn't know the pop-up would just appear? – Abhi Thosar Feb 25 at 4:29

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