1

I have two accounts in Metamask, one connected to my site and one isn't. I can use 'eth_requestAccounts' to get the connected account, the problem is that if I switch to the other account which isn't connected to my site, it still returns the connected account.

This is a problem because a user may change to another account in Metamask which isn't connected to my site. The 'accountsChanged' event doesn't fire because the account they've changed to isn't connected.

What is the best way to deal with this? I want to be able to know which is the currently selected account (even if it isn't connected), or at least know if a specific address is currently selected in Metamask.

Thanks!

1 Answer 1

0

You don't. If the user has chosen not to give permission to view certain accounts those will be invisible to you and you must simply pretend that they don't exist / assume that the user is still under his old account.

If the user has chosen to let multiple accounts be visible to the frontend, only then will you be able to handle the case of an account switch via accountsChanged.

2
  • 1
    I don't need to know their accounts, I just need to know if the account they have chosen to connect to my site is actually the account they have currently selected in Metamask. Otherwise, they could connect an account, then switch to an unconnected account and forget to connect it, then initiate a transaction thinking it's being executed by the second account when in reality it's being executed by the first account. Surely there's a way to handle this?
    – Rick
    Commented May 16, 2022 at 17:12
  • 1
    This has happened to me as well, unfortunately I don't think there's a way to handle this. As far as I know, MetaMask is entirely in charge of what accounts they show are available and "currently connected" (and there is no special event to listen for changes). I agree that this can lead to problems and that they should make this more visible / disconnect last connected account if the new one is not connected.
    – phaze
    Commented May 16, 2022 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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