From 4341cfb4d2dcbbdc372c9f3036b39b23c5627587 Mon Sep 17 00:00:00 2001 From: Stefan Date: Mon, 15 Jul 2024 20:30:27 +0300 Subject: [PATCH] feat(dapps) reject request on closing of modal Closes: #15585 --- .../qmlTests/tests/tst_DAppsWorkflow.qml | 40 ++++++++++++++++--- .../Wallet/panels/DAppsWorkflow.qml | 19 ++++++++- .../popups/walletconnect/ConnectDAppModal.qml | 4 +- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/storybook/qmlTests/tests/tst_DAppsWorkflow.qml b/storybook/qmlTests/tests/tst_DAppsWorkflow.qml index 7c764613af..491469240a 100644 --- a/storybook/qmlTests/tests/tst_DAppsWorkflow.qml +++ b/storybook/qmlTests/tests/tst_DAppsWorkflow.qml @@ -51,6 +51,11 @@ Item { approveSessionCalls.push({sessionProposalJson, approvedNamespaces}) } + property var acceptSessionRequestCalls: [] + acceptSessionRequest: function(topic, id, signature) { + acceptSessionRequestCalls.push({topic, id, signature}) + } + property var rejectSessionRequestCalls: [] rejectSessionRequest: function(topic, id, error) { rejectSessionRequestCalls.push({topic, id, error}) @@ -615,28 +620,51 @@ Item { verify(!popup.visible) } - function test_RejectDappRequestModal() { + function showRequestModal() { waitForRendering(controlUnderTest) let service = controlUnderTest.wcService let td = mockSessionRequestEvent(this, service.wcSDK, service.walletRootStore.nonWatchAccounts, service.walletRootStore.filteredFlatModel) waitForRendering(controlUnderTest) - let popup = findChild(controlUnderTest, "dappsRequestModal") - verify(popup.opened) + td.popup = findChild(controlUnderTest, "dappsRequestModal") + verify(td.popup.opened) + return td + } - let rejectButton = findChild(popup, "rejectButton") + function test_RejectDappRequestModal() { + let td = showRequestModal() + + let rejectButton = findChild(td.popup, "rejectButton") mouseClick(rejectButton) compare(td.sdk.rejectSessionRequestCalls.length, 1, "expected a call to service.rejectSessionRequest") + compare(td.sdk.acceptSessionRequestCalls.length, 0, "expected no call to service.acceptSessionRequest") + let store = controlUnderTest.wcService.store + compare(store.authenticateUserCalls.length, 0, "expected no call to store.authenticateUser for rejection") let args = td.sdk.rejectSessionRequestCalls[0] compare(args.topic, td.topic, "expected topic to be set") compare(args.id, td.request.id, "expected id to be set") compare(args.error, false, "expected no error; it was user rejected") waitForRendering(controlUnderTest) - verify(!popup.opened) - verify(!popup.visible) + verify(!td.popup.opened) + verify(!td.popup.visible) + } + + function test_AcceptDappRequestModal() { + let td = showRequestModal() + + let signButton = findChild(td.popup, "signButton") + + mouseClick(signButton) + let store = controlUnderTest.wcService.store + compare(store.authenticateUserCalls.length, 1, "expected a call to store.authenticateUser") + compare(td.sdk.rejectSessionRequestCalls.length, 0, "regression, expected no call to service.rejectSessionRequest") + + waitForRendering(controlUnderTest) + verify(!td.popup.opened) + verify(!td.popup.visible) } } diff --git a/ui/app/AppLayouts/Wallet/panels/DAppsWorkflow.qml b/ui/app/AppLayouts/Wallet/panels/DAppsWorkflow.qml index 7d7937cf1e..f2de3de5b1 100644 --- a/ui/app/AppLayouts/Wallet/panels/DAppsWorkflow.qml +++ b/ui/app/AppLayouts/Wallet/panels/DAppsWorkflow.qml @@ -151,6 +151,7 @@ DappsComboBox { onLoaded: item.open() property SessionRequestResolved request: null + property bool requestHandled: false sourceComponent: DAppSignRequestModal { id: dappRequestModal @@ -206,17 +207,32 @@ DappsComboBox { } } - onClosed: Qt.callLater( () => sessionRequestLoader.active = false) + onClosed: { + Qt.callLater( () => { + rejectRequest() + sessionRequestLoader.active = false + }) + } onAccepted: { if (!request) { console.error("Error signing: request is null") return } + requestHandled = true root.wcService.requestHandler.authenticate(request) } onRejected: { + rejectRequest() + } + + function rejectRequest() { + // Allow rejecting only once + if (requestHandled) { + return + } + requestHandled = true let userRejected = true root.wcService.requestHandler.rejectSessionRequest(request, userRejected) } @@ -298,6 +314,7 @@ DappsComboBox { function onSessionRequest(request) { sessionRequestLoader.request = request + sessionRequestLoader.requestHandled = false sessionRequestLoader.active = true } } diff --git a/ui/imports/shared/popups/walletconnect/ConnectDAppModal.qml b/ui/imports/shared/popups/walletconnect/ConnectDAppModal.qml index a67bb2d74e..c0aa69645e 100644 --- a/ui/imports/shared/popups/walletconnect/ConnectDAppModal.qml +++ b/ui/imports/shared/popups/walletconnect/ConnectDAppModal.qml @@ -92,7 +92,7 @@ StatusDialog { width: 480 implicitHeight: !d.connectionAttempted ? 633 : 681 - + closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside title: d.connectionSuccessful ? qsTr("dApp connected") : @@ -205,7 +205,7 @@ StatusDialog { roleName: "chainId" aggregateFunction: (aggr, value) => [...aggr, value] - } + } property int connectionStatus: root.notConnectedStatus readonly property bool connectionSuccessful: d.connectionStatus === root.connectionSuccessfulStatus