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

tweak: reconnect #3782

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/six-grapes-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": patch
---

Refactored injected connector connection logic.
46 changes: 23 additions & 23 deletions packages/core/src/connectors/injected.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type Address,
type EIP1193Provider,
type ProviderConnectInfo,
ProviderRpcError,
Expand Down Expand Up @@ -144,32 +145,31 @@ export function injected(parameters: InjectedParameters = {}) {
const provider = await this.getProvider()
if (!provider) throw new ProviderNotFoundError()

let accounts = await this.getAccounts().catch(() => null)
if (!isReconnecting) {
// Attempt to show another prompt for selecting account if already connected and `shimDisconnect` flag is enabled
const isAuthorized = shimDisconnect && !!accounts?.length
if (isAuthorized)
try {
const permissions = await provider.request({
method: 'wallet_requestPermissions',
params: [{ eth_accounts: {} }],
})
accounts = (permissions[0]?.caveats?.[0]?.value as string[])?.map(
(x) => getAddress(x),
)
} catch (err) {
const error = err as RpcError
// Not all injected providers support `wallet_requestPermissions` (e.g. MetaMask iOS).
// Only bubble up error if user rejects request
if (error.code === UserRejectedRequestError.code)
throw new UserRejectedRequestError(error)
// Or prompt is already open
if (error.code === ResourceUnavailableRpcError.code) throw error
}
let accounts: readonly Address[] = []
if (isReconnecting) accounts = await this.getAccounts().catch(() => [])
else if (shimDisconnect) {
// Attempt to show another prompt for selecting account if `shimDisconnect` flag is enabled
try {
const permissions = await provider.request({
method: 'wallet_requestPermissions',
params: [{ eth_accounts: {} }],
})
accounts = (permissions[0]?.caveats?.[0]?.value as string[])?.map(
(x) => getAddress(x),
)
} catch (err) {
const error = err as RpcError
// Not all injected providers support `wallet_requestPermissions` (e.g. MetaMask iOS).
// Only bubble up error if user rejects request
if (error.code === UserRejectedRequestError.code)
throw new UserRejectedRequestError(error)
// Or prompt is already open
if (error.code === ResourceUnavailableRpcError.code) throw error
}
}

try {
if (!accounts?.length) {
if (!accounts?.length && !isReconnecting) {
const requestedAccounts = await provider.request({
method: 'eth_requestAccounts',
})
Expand Down
Loading