From 3fbdea6be237d11eeaa43ed9634a4360fe2460ec Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Wed, 15 Jun 2022 15:36:29 +0800 Subject: [PATCH] fix: recover from previous connection --- .../Connection/providers/WalletConnect.ts | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/plugins/EVM/src/state/Connection/providers/WalletConnect.ts b/packages/plugins/EVM/src/state/Connection/providers/WalletConnect.ts index d4368343bedb..9d4662ae35e3 100644 --- a/packages/plugins/EVM/src/state/Connection/providers/WalletConnect.ts +++ b/packages/plugins/EVM/src/state/Connection/providers/WalletConnect.ts @@ -4,7 +4,7 @@ import type { RequestArguments } from 'web3-core' import { defer } from '@dimensiondev/kit' import WalletConnect from '@walletconnect/client' import type { ITxData } from '@walletconnect/types' -import { ChainId, chainResolver, EthereumMethodType, ProviderType } from '@masknet/web3-shared-evm' +import { ChainId, chainResolver, EthereumMethodType, isValidAddress, ProviderType } from '@masknet/web3-shared-evm' import { BaseProvider } from './Base' import type { EVM_Provider } from '../types' import type { Account } from '@masknet/web3-shared-base' @@ -122,15 +122,31 @@ export default class WalletConnectProvider extends BaseProvider implements EVM_P this.connector = this.createConnector() - await this.connector.createSession({ - chainId, - }) - this.connection = { resolve, reject, } + if (this.connector.connected) { + const { chainId, accounts } = this.connector + const account = first(accounts) + if (chainId !== 0 && account && isValidAddress(account)) { + this.connection.resolve({ + chainId, + account, + }) + } else { + await this.connector.killSession() + await this.connector.createSession({ + chainId, + }) + } + } else { + await this.connector.createSession({ + chainId, + }) + } + return deferred.finally(() => { this.connection = null })