From f00d7f3ec457ec7f1baa155eddc5f02f5531ab9e Mon Sep 17 00:00:00 2001 From: Alex Jbanca Date: Fri, 9 Aug 2024 13:39:24 +0300 Subject: [PATCH] fix(WalletConnect): Fixing crash on authentication screen when the app resumes from a few minutes of inactivity The root cause in this case was the usage of js stored QObjects coming from the model. (cherry picked from commit 24a386d078986afd1c24fcc99e4f009b8cc70bf6) --- .../services/dapps/DAppsRequestHandler.qml | 29 ++++++++++++++++-- .../services/dapps/DappsConnectorSDK.qml | 30 +++++++++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/ui/app/AppLayouts/Wallet/services/dapps/DAppsRequestHandler.qml b/ui/app/AppLayouts/Wallet/services/dapps/DAppsRequestHandler.qml index b8d2230829..2d56b0d4da 100644 --- a/ui/app/AppLayouts/Wallet/services/dapps/DAppsRequestHandler.qml +++ b/ui/app/AppLayouts/Wallet/services/dapps/DAppsRequestHandler.qml @@ -271,7 +271,20 @@ SQUtils.QObject { const account = SQUtils.ModelUtils.getFirstModelEntryIf(root.accountsModel, (account) => { return account.address.toLowerCase() === address.toLowerCase(); }) - return { account, success: true } + + if (!account) { + return { account: null, success: true } + } + + // deep copy to avoid operations on the original object + try { + const accountCopy = JSON.parse(JSON.stringify(account)) + return { account: accountCopy, success: true } + } + catch (e) { + console.error("Error parsing account", e) + return { account: null, success: false } + } } /// Returns null if the network is not found @@ -280,7 +293,19 @@ SQUtils.QObject { return null } const chainId = DAppsHelpers.chainIdFromEip155(event.params.chainId) - return SQUtils.ModelUtils.getByKey(root.networksModel, "chainId", chainId) + const network = SQUtils.ModelUtils.getByKey(root.networksModel, "chainId", chainId) + + if (!network) { + return null + } + + // deep copy to avoid operations on the original object + try { + return JSON.parse(JSON.stringify(network)) + } catch (e) { + console.error("Error parsing network", network) + return null + } } /// Returns null if the network is not found diff --git a/ui/app/AppLayouts/Wallet/services/dapps/DappsConnectorSDK.qml b/ui/app/AppLayouts/Wallet/services/dapps/DappsConnectorSDK.qml index f13ff0e307..b333aaceb1 100644 --- a/ui/app/AppLayouts/Wallet/services/dapps/DappsConnectorSDK.qml +++ b/ui/app/AppLayouts/Wallet/services/dapps/DappsConnectorSDK.qml @@ -148,9 +148,22 @@ WalletConnectSDKBase { } address = event.params.request.params[0] } - return SQUtils.ModelUtils.getFirstModelEntryIf(root.wcService.validAccounts, (account) => { + const account = SQUtils.ModelUtils.getFirstModelEntryIf(root.wcService.validAccounts, (account) => { return account.address.toLowerCase() === address.toLowerCase(); }) + + if (!account) { + return null + } + + // deep copy to avoid operations on the original object + try { + return JSON.parse(JSON.stringify(account)) + } + catch (e) { + console.error("Error parsing account", e.message) + return null + } } /// Returns null if the network is not found @@ -159,7 +172,20 @@ WalletConnectSDKBase { return null } const chainId = DAppsHelpers.chainIdFromEip155(event.params.chainId) - return SQUtils.ModelUtils.getByKey(networksModule.flatNetworks, "chainId", chainId) + const network = SQUtils.ModelUtils.getByKey(networksModule.flatNetworks, "chainId", chainId) + + if (!network) { + return null + } + + // deep copy to avoid operations on the original object + try { + return JSON.parse(JSON.stringify(network)) + } + catch (e) { + console.error("Error parsing network", e) + return null + } } function extractMethodData(event, method) {