Skip to content

Commit

Permalink
fix: ethers disconnect issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir committed Aug 9, 2024
1 parent fc8a93b commit 56064bb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
18 changes: 8 additions & 10 deletions packages/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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)
}
}

Expand Down
43 changes: 38 additions & 5 deletions packages/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 56064bb

Please sign in to comment.