From cc5e5c9d38917ee5f5b7814c7663aa9b53e1993f Mon Sep 17 00:00:00 2001 From: Francesco Ceccon Date: Mon, 30 Oct 2023 13:39:24 +0100 Subject: [PATCH] core: fix account detection when wallet not authorized **Summary** When the wallet is not authorized, the `account` method throws. This commit handles that case. --- packages/core/src/connectors/injected.ts | 2 ++ packages/core/src/hooks/useAccount.ts | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/core/src/connectors/injected.ts b/packages/core/src/connectors/injected.ts index 09160317..ab59f38a 100644 --- a/packages/core/src/connectors/injected.ts +++ b/packages/core/src/connectors/injected.ts @@ -123,6 +123,8 @@ export class InjectedConnector extends Connector { const account = this._wallet.account.address; const chainId = await this.chainId(); + this.emit("connect", { account, chainId }); + return { account, chainId, diff --git a/packages/core/src/hooks/useAccount.ts b/packages/core/src/hooks/useAccount.ts index b4e91cbb..b2817cb8 100644 --- a/packages/core/src/hooks/useAccount.ts +++ b/packages/core/src/hooks/useAccount.ts @@ -90,7 +90,13 @@ export function useAccount({ for (const connector of connectors) { if (!connector.available()) continue; - const connAccount = await connector.account(); + + // If the connector is not authorized, `.account()` will throw. + let connAccount; + try { + connAccount = await connector.account(); + } catch {} + if (connAccount && connAccount?.address === connectedAccount.address) { if (state.isDisconnected && onConnect !== undefined) { onConnect({ address: connectedAccount.address, connector });