From caaf9250f3fd43f1ea36b1f68304f45968fa2bbf Mon Sep 17 00:00:00 2001 From: Andrea Salvatore Date: Wed, 17 Jul 2024 11:10:52 +0200 Subject: [PATCH] fix: fixes linting errors --- app/core/WalletConnect/WalletConnectV2.ts | 136 +++++++++++----------- 1 file changed, 69 insertions(+), 67 deletions(-) diff --git a/app/core/WalletConnect/WalletConnectV2.ts b/app/core/WalletConnect/WalletConnectV2.ts index 02f1f6b5736..cecb6e131dc 100644 --- a/app/core/WalletConnect/WalletConnectV2.ts +++ b/app/core/WalletConnect/WalletConnectV2.ts @@ -546,7 +546,7 @@ export class WC2Manager { web3Wallet.on( 'session_delete', async (event: SingleEthereumTypes.SessionDelete) => { - const session = sessions[event.topic]; + const session = sessions?.[event.topic]; if (session && deeplinkSessions[session?.pairingTopic]) { delete deeplinkSessions[session.pairingTopic]; await AsyncStorage.setItem( @@ -576,80 +576,82 @@ export class WC2Manager { } ).PermissionController; - Object.keys(sessions).forEach(async (sessionKey) => { - try { - const session = sessions[sessionKey]; - - this.sessions[sessionKey] = new WalletConnect2Session({ - web3Wallet, - channelId: sessionKey, - navigation: this.navigation, - deeplink: - typeof deeplinkSessions[session.pairingTopic] !== 'undefined', - session, - }); - - // Find approvedAccounts for current sessions - DevLogger.log( - `WC2::init getPermittedAccounts for ${sessionKey} origin=${session.peer.metadata.url}`, - JSON.stringify(permissionController.state, null, 2), - ); - const accountPermission = permissionController.getPermission( - session.peer.metadata.url, - 'eth_accounts', - ); - - DevLogger.log( - `WC2::init accountPermission`, - JSON.stringify(accountPermission, null, 2), - ); - let approvedAccounts = - (await getPermittedAccounts(accountPermission?.id ?? '')) ?? []; - const fromOrigin = await getPermittedAccounts( - session.peer.metadata.url, - ); + if (sessions) { + Object.keys(sessions).forEach(async (sessionKey) => { + try { + const session = sessions[sessionKey]; + + this.sessions[sessionKey] = new WalletConnect2Session({ + web3Wallet, + channelId: sessionKey, + navigation: this.navigation, + deeplink: + typeof deeplinkSessions[session.pairingTopic] !== 'undefined', + session, + }); + + // Find approvedAccounts for current sessions + DevLogger.log( + `WC2::init getPermittedAccounts for ${sessionKey} origin=${session.peer.metadata.url}`, + JSON.stringify(permissionController.state, null, 2), + ); + const accountPermission = permissionController.getPermission( + session.peer.metadata.url, + 'eth_accounts', + ); - DevLogger.log( - `WC2::init approvedAccounts id ${accountPermission?.id}`, - approvedAccounts, - ); - DevLogger.log( - `WC2::init fromOrigin ${session.peer.metadata.url}`, - fromOrigin, - ); + DevLogger.log( + `WC2::init accountPermission`, + JSON.stringify(accountPermission, null, 2), + ); + let approvedAccounts = + (await getPermittedAccounts(accountPermission?.id ?? '')) ?? []; + const fromOrigin = await getPermittedAccounts( + session.peer.metadata.url, + ); - // fallback to origin from metadata url - if (approvedAccounts.length === 0) { DevLogger.log( - `WC2::init fallback to metadata url ${session.peer.metadata.url}`, + `WC2::init approvedAccounts id ${accountPermission?.id}`, + approvedAccounts, ); - approvedAccounts = - (await getPermittedAccounts(session.peer.metadata.url)) ?? []; - } + DevLogger.log( + `WC2::init fromOrigin ${session.peer.metadata.url}`, + fromOrigin, + ); + + // fallback to origin from metadata url + if (approvedAccounts.length === 0) { + DevLogger.log( + `WC2::init fallback to metadata url ${session.peer.metadata.url}`, + ); + approvedAccounts = + (await getPermittedAccounts(session.peer.metadata.url)) ?? []; + } + + if (approvedAccounts?.length === 0) { + DevLogger.log( + `WC2::init fallback to parsing accountPermission`, + accountPermission, + ); + // FIXME: Why getPermitted accounts doesn't work??? + approvedAccounts = extractApprovedAccounts(accountPermission); + DevLogger.log(`WC2::init approvedAccounts`, approvedAccounts); + } - if (approvedAccounts?.length === 0) { + const nChainId = parseInt(chainId, 16); DevLogger.log( - `WC2::init fallback to parsing accountPermission`, - accountPermission, + `WC2::init updateSession session=${sessionKey} chainId=${chainId} nChainId=${nChainId} selectedAddress=${selectedAddress}`, + approvedAccounts, ); - // FIXME: Why getPermitted accounts doesn't work??? - approvedAccounts = extractApprovedAccounts(accountPermission); - DevLogger.log(`WC2::init approvedAccounts`, approvedAccounts); + await this.sessions[sessionKey].updateSession({ + chainId: nChainId, + accounts: approvedAccounts, + }); + } catch (err) { + console.warn(`WC2::init can't update session ${sessionKey}`); } - - const nChainId = parseInt(chainId, 16); - DevLogger.log( - `WC2::init updateSession session=${sessionKey} chainId=${chainId} nChainId=${nChainId} selectedAddress=${selectedAddress}`, - approvedAccounts, - ); - await this.sessions[sessionKey].updateSession({ - chainId: nChainId, - accounts: approvedAccounts, - }); - } catch (err) { - console.warn(`WC2::init can't update session ${sessionKey}`); - } - }); + }); + } } public static async init({