Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injected connector: multiple addresses connection mismatches currently selected account #4140

Closed
1 task done
manneredboor opened this issue Jul 15, 2024 · 1 comment
Closed
1 task done

Comments

@manneredboor
Copy link

manneredboor commented Jul 15, 2024

Check existing issues

Describe the bug

Hello! Here is a bug was discovered I want to bring here as an issue with a proposed solution.

If you set shimDisconnect flag as true while creating injected() connector and try to connect multiple accounts from Metamask to your dapp it will get the list of the accounts from wallet_requestPermissions response

const permissions = await provider.request({
method: 'wallet_requestPermissions',
params: [{ eth_accounts: {} }],
})
accounts = (permissions[0]?.caveats?.[0]?.value as string[])?.map(
(x) => getAddress(x),

But the list from this response is not sorted in the same way as it is sorted when you get it from eth_requestAccounts

const requestedAccounts = await provider.request({
method: 'eth_requestAccounts',
})
accounts = requestedAccounts.map((x) => getAddress(x))

The difference is that eth_requestAccounts returns an array where an item on the first position is the currently selected account in your wallet, independently to how addresses are sorted in the wallet. In other hand, wallet_requestPermissions returns the accounts list sorted in the same way as it displayed in your account selection dialog. So it leads to a bug that appears when you select 2+ accounts during connect, and your current account is any of them but first from the list. The wagmi state receives the first account from the list as current account which is not valid. Then if you refresh the page and if you have the reconnect option enabled wagmi will gather accounts in the right way with eth_requestAccounts, because wallet_requestPermissions is being called only at manual the connect phase.

My suggestion here is to make extra eth_requestAccounts request after wallet_requestPermissions to gather an accounts list because it seems like that the permissions list was not meant to be sorted in the same way by the wallet side.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/new-wagmi-kq3bh1?file=src%2Fwagmi.ts

Steps To Reproduce

  1. Create a few accounts in your wallet that can be connected with injected connector, I do use Metamask.
  2. Setup wagmi with injected connector and enable shimDisconnect option for it.
  3. Set your current account in the wallet to any but first from the list.
  4. Try to connect the wallet to the dapp, check 2 or more accounts from the list, there is must be at least one account permission checked above the currently selected account.
  5. Check the current account at the wagmi state, using useAccount for example, it should display an account that was first on the permission list, not the actual current account from your wallet.
  6. Refresh the page, and after reconnection the account will be actual now.

What Wagmi package(s) are you using?

@wagmi/connectors, @wagmi/core

Wagmi Package(s) Version(s)

latest

Viem Version

latest

TypeScript Version

5.4.5

Anything else?

Framework is React
Metamask version: 11.16.14

@tmm tmm closed this as completed in b08013e Jul 15, 2024
Copy link
Contributor

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Wagmi version. If you have any questions or comments you can create a new discussion thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant