MetaMask / extension-provider

A module for allowing a WebExtension to access the web3 provider from an installed MetaMask instance
MIT License
68 stars 28 forks source link

getAccounts no longer triggers Metamask #21

Closed JeffWScott closed 3 years ago

JeffWScott commented 3 years ago

So the metamask-extension-provider was working really good for awhile up until probably a few weeks ago I would say. Then my users started reporting connection issues.

I read somewhere that maybe v9 of Metamask broke something so I updated all my package versions as you can see below.

  "dependencies": {
    "metamask-extension-provider": "^3.0.0",
    "web3": "^1.3.3"
  }

I also validated Metamask is v9.0.3 image

I can get the provider and I can get the web3 instance. But what web3.eth.getAccounts() doesn't do anything anymore, it returns an empty array without ever prompting the user to connect their MetaMask.

Here is basically the code. Maybe something in the syntax has changed and someone can help me out please.

const Web3 = require('web3')
const createMetaMaskProvider = require('metamask-extension-provider')

let provider = createMetaMaskProvider()

provider.on('error', (error) => {
    console.log(error) // Doesn't error
})

web3 = new Web3(provider)

let response = await web3.eth.getAccounts()
console.log(response) // []

Any help would be appreciated as I have a few hundred users that are currently out in the cold here so I'm eager to get a fix.

Thanks!

Gudahtt commented 3 years ago

Thanks for the report!

In that example, it doesn't look like you are requesting the user's accounts. You'll need to call eth_requestAccounts to request permission to access a user's accounts first (documented here: https://docs.metamask.io/guide/rpc-api.html#permissions). This is exposed by web3.js as web3.eth.requestAccounts I believe.

I'm surprised this had worked prior to 2 weeks ago :thinking: We've required that step for years now.

JeffWScott commented 3 years ago

As you can see getAccounts exists on the Web3 object and did work before. It's possible it's a depreciated method that just recently got removed from metamask 9.

The good news is requestAccounts now triggers Metamask like before. I don't know how to thank you!

image