diff --git a/packages/ethers/src/client.ts b/packages/ethers/src/client.ts index 163160088d..aaad55ae93 100644 --- a/packages/ethers/src/client.ts +++ b/packages/ethers/src/client.ts @@ -345,21 +345,18 @@ export class Web3Modal extends Web3ModalScaffold { ) { const ethProvider = provider await (ethProvider as unknown as EthereumProvider).disconnect() - // eslint-disable-next-line no-negated-condition } else if (providerType === ConstantsUtil.AUTH_CONNECTOR_ID) { await this.authProvider?.disconnect() } else if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID && provider) { - await this.disconnectProvider(provider) - provider.emit('disconnect') + await this.revokeProviderPermissions(provider) } else if (providerType === ConstantsUtil.INJECTED_CONNECTOR_ID) { const InjectedProvider = ethersConfig.injected if (InjectedProvider) { - await this.disconnectProvider(InjectedProvider) - InjectedProvider.emit('disconnect') + await this.revokeProviderPermissions(InjectedProvider) } - } else { - provider?.emit('disconnect') } + + provider?.emit?.('disconnect') localStorage.removeItem(EthersConstantsUtil.WALLET_ID) EthersStoreUtil.reset() }, @@ -646,7 +643,7 @@ export class Web3Modal extends Web3ModalScaffold { if (providerType === ConstantsUtil.AUTH_CONNECTOR_ID) { await this.authProvider?.disconnect() } else if (provider && (providerType === 'injected' || providerType === 'eip6963')) { - await this.disconnectProvider(provider) + await this.revokeProviderPermissions(provider) provider?.emit('disconnect') } else if (providerType === 'walletConnect' || providerType === 'coinbaseWalletSDK') { const ethereumProvider = provider as unknown as EthereumProvider @@ -697,7 +694,7 @@ export class Web3Modal extends Web3ModalScaffold { await this.checkActiveWalletConnectProvider() } - private async disconnectProvider(provider: Provider | CombinedProvider) { + private async revokeProviderPermissions(provider: Provider | CombinedProvider) { try { const permissions: { parentCapability: string }[] = await provider.request({ method: 'wallet_getPermissions' @@ -713,7 +710,8 @@ export class Web3Modal extends Web3ModalScaffold { }) } } catch (error) { - throw new Error('Error revoking permissions:') + // eslint-disable-next-line no-console + console.info('Could not revoke permissions from wallet. Disconnecting...', error) } } diff --git a/packages/ethers5/src/client.ts b/packages/ethers5/src/client.ts index fdaff1534f..75971f8851 100644 --- a/packages/ethers5/src/client.ts +++ b/packages/ethers5/src/client.ts @@ -311,12 +311,24 @@ export class Web3Modal extends Web3ModalScaffold { const { SIWEController } = await import('@web3modal/siwe') await SIWEController.signOut() } - if (providerType === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID) { - const WalletConnectProvider = provider - await (WalletConnectProvider as unknown as EthereumProvider).disconnect() - } else if (provider) { - provider.emit('disconnect') + if ( + providerType === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID || + providerType === 'coinbaseWalletSDK' + ) { + const ethProvider = provider + await (ethProvider as unknown as EthereumProvider).disconnect() + } else if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID && provider) { + await this.revokeProviderPermissions(provider) + } else if (providerType === ConstantsUtil.INJECTED_CONNECTOR_ID) { + const InjectedProvider = ethersConfig.injected + if (InjectedProvider) { + await this.revokeProviderPermissions(InjectedProvider) + } } + + provider?.emit?.('disconnect') + localStorage.removeItem(EthersConstantsUtil.WALLET_ID) + EthersStoreUtil.reset() }, signMessage: async (message: string) => { @@ -1152,6 +1164,27 @@ export class Web3Modal extends Web3ModalScaffold { } } + private async revokeProviderPermissions(provider: Provider) { + try { + const permissions: { parentCapability: string }[] = await provider.request({ + method: 'wallet_getPermissions' + }) + const ethAccountsPermission = permissions.find( + permission => permission.parentCapability === 'eth_accounts' + ) + + if (ethAccountsPermission) { + await provider.request({ + method: 'wallet_revokePermissions', + params: [{ eth_accounts: {} }] + }) + } + } catch (error) { + // eslint-disable-next-line no-console + console.info('Could not revoke permissions from wallet. Disconnecting...', error) + } + } + private listenConnectors(enableEIP6963: boolean) { if (typeof window !== 'undefined' && enableEIP6963) { const handler = this.eip6963EventHandler.bind(this)