From 8d78f0fab2f7a1d9f990d96d5727347e24ac8a83 Mon Sep 17 00:00:00 2001 From: Alex Jbanca Date: Thu, 8 Aug 2024 22:38:32 +0300 Subject: [PATCH] fix(WalletConnect): dApp connection state presented in the UI is out-of sync 1. The DAppsListProvider needs to receive all the user accounts so that it can process all dapps the user is connected to (the dapps filtering based on selected account is an internal impl. detail) 2. Call `revokeSession` on disconnect only if the dapp cannot be found in the WC active sessions. There is some overlapping between Browser connector and Wallet connect because both operate on the same data stored in the DB. If Browser connector controller removes WC data, the WC disconnect flows cannot be successfully completed. 3. Reuse the same notification flow for Browser connector as it's used for WC 4. Fix dapp filtering when processing the dapps that will be displayed in the UI. (cherry picked from commit 21227893c2f18ab487c367853f9648fc94d67afb) --- .../services/dapps/DAppsListProvider.qml | 16 +++++-------- .../services/dapps/DappsConnectorSDK.qml | 1 - .../services/dapps/WalletConnectService.qml | 24 ++++++++----------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/ui/app/AppLayouts/Wallet/services/dapps/DAppsListProvider.qml b/ui/app/AppLayouts/Wallet/services/dapps/DAppsListProvider.qml index ac43169266..a12a45545e 100644 --- a/ui/app/AppLayouts/Wallet/services/dapps/DAppsListProvider.qml +++ b/ui/app/AppLayouts/Wallet/services/dapps/DAppsListProvider.qml @@ -60,19 +60,15 @@ QObject { let dappsList = JSON.parse(dappsJson); for (let i = 0; i < dappsList.length; i++) { const cachedEntry = dappsList[i]; - let accountAddresses = cachedEntry.accountAddresses - if (!accountAddresses) { - accountAddresses = [{address: ''}]; - } - + // TODO #15075: on SDK dApps refresh update the model that has data source from persistence instead of using reset const dappEntryWithRequiredRoles = { - description: cachedEntry.description, + description: "", url: cachedEntry.url, name: cachedEntry.name, - iconUrl: cachedEntry.url, - accountAddresses: cachedEntry.accountAddresses + iconUrl: cachedEntry.iconUrl, + accountAddresses: [{address: ''}] } - dapps.append(dappsList[i]); + dapps.append(dappEntryWithRequiredRoles); } } root.store.dappsListReceived.connect(dappsListReceivedFn); @@ -103,7 +99,7 @@ QObject { if (existingDApp) { // In Qt5.15.2 this is the way to make a "union" of two arrays // more modern syntax (ES-6) is not available yet - const combinedAddresses = new Set(existingDApp.accountAddresses.concat(dapp.accountAddresses)); + const combinedAddresses = new Set(existingDApp.accountAddresses.concat(accounts)); existingDApp.accountAddresses = Array.from(combinedAddresses); } else { dapp.accountAddresses = accounts diff --git a/ui/app/AppLayouts/Wallet/services/dapps/DappsConnectorSDK.qml b/ui/app/AppLayouts/Wallet/services/dapps/DappsConnectorSDK.qml index 9637842835..f13ff0e307 100644 --- a/ui/app/AppLayouts/Wallet/services/dapps/DappsConnectorSDK.qml +++ b/ui/app/AppLayouts/Wallet/services/dapps/DappsConnectorSDK.qml @@ -503,7 +503,6 @@ WalletConnectSDKBase { controller.recallDAppPermission(dAppUrl) const session = { url: dAppUrl, name: "", icon: "" } root.wcService.connectorDAppsProvider.revokeSession(JSON.stringify(session)) - root.wcService.displayToastMessage(qsTr("Disconnected from %1").arg(dAppUrl), false) } } diff --git a/ui/app/AppLayouts/Wallet/services/dapps/WalletConnectService.qml b/ui/app/AppLayouts/Wallet/services/dapps/WalletConnectService.qml index a1298cfc53..ec9de28352 100644 --- a/ui/app/AppLayouts/Wallet/services/dapps/WalletConnectService.qml +++ b/ui/app/AppLayouts/Wallet/services/dapps/WalletConnectService.qml @@ -128,6 +128,7 @@ QObject { function disconnectDapp(url) { wcSDK.getActiveSessions((allSessionsAllProfiles) => { const sessions = DAppsHelpers.filterActiveSessionsForKnownAccounts(allSessionsAllProfiles, validAccounts) + let dappFoundInWcSessions = false for (const sessionID in sessions) { const session = sessions[sessionID] const accountsInSession = DAppsHelpers.getAccountsInSession(session) @@ -137,13 +138,19 @@ QObject { if (!dappsProvider.selectedAddress || (accountsInSession.includes(dappsProvider.selectedAddress))) { + dappFoundInWcSessions = true wcSDK.disconnectSession(topic) } } } - }); - root.revokeSession(url) + // TODO: #16044 - Refactor Wallet connect service to handle multiple SDKs + if (!dappFoundInWcSessions) { + // Revoke browser plugin session + root.revokeSession(url) + d.notifyDappDisconnect(url, false) + } + }); } function getDApp(dAppUrl) { @@ -332,18 +339,7 @@ QObject { sdk: root.wcSDK store: root.store - supportedAccountsModel: SortFilterProxyModel { - objectName: "SelectedAddressModelForDAppsListProvider" - sourceModel: d.supportedAccountsModel - filters: FastExpressionFilter { - enabled: !root.walletRootStore.showAllAccounts - - expression: root.walletRootStore.selectedAddress.toLowerCase() === model.address.toLowerCase() - - expectedRoles: ["address"] - } - } - + supportedAccountsModel: d.supportedAccountsModel selectedAddress: root.walletRootStore.selectedAddress }