From 072370b0d6700530b43136f05a12cc5d404642d9 Mon Sep 17 00:00:00 2001 From: Shivek Khurana Date: Thu, 11 Jul 2024 15:33:14 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=99=85=20Gracefully=20reject=20proposals?= =?UTF-8?q?=20and=20requests=20(#20649)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🙅 Gracefully reject proposals and requests * 🔑 Remove redudant fx, send correct error * ➡️ Docstring to comment --------- Co-authored-by: Lungu Cristian --- src/react_native/wallet_connect.cljs | 2 + src/status_im/constants.cljs | 1 + .../wallet/wallet_connect/effects.cljs | 23 +++++++++-- .../modals/common/page_nav/view.cljs | 12 ++++++ .../modals/send_transaction/view.cljs | 8 ++-- .../modals/sign_message/view.cljs | 8 ++-- .../modals/sign_transaction/view.cljs | 8 ++-- .../wallet_connect/responding_events.cljs | 41 +++++++++++++++---- .../wallet_connect/session_proposal/view.cljs | 2 +- 9 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 src/status_im/contexts/wallet/wallet_connect/modals/common/page_nav/view.cljs diff --git a/src/react_native/wallet_connect.cljs b/src/react_native/wallet_connect.cljs index e7809efb2b..b43e272bdd 100644 --- a/src/react_native/wallet_connect.cljs +++ b/src/react_native/wallet_connect.cljs @@ -22,6 +22,8 @@ (clj->js {:proposal proposal :supportedNamespaces supported-namespaces}))) +;; Get an error from this list: +;; https://github.com/WalletConnect/walletconnect-monorepo/blob/c6e9529418a0c81d4efcc6ac4e61f242a50b56c5/packages/utils/src/errors.ts (defn get-sdk-error [error-key] (getSdkError error-key)) diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index 530d2824d1..e1d32923e6 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -281,6 +281,7 @@ (def ^:const wallet-connect-supported-events #{"accountsChanged" "chainChanged"}) (def ^:const wallet-connect-session-proposal-event "session_proposal") (def ^:const wallet-connect-session-request-event "session_request") +(def ^:const wallet-connect-user-rejected-error-key "USER_REJECTED") (def ^:const transaction-pending-type-wallet-connect-transfer "WalletConnectTransfer") diff --git a/src/status_im/contexts/wallet/wallet_connect/effects.cljs b/src/status_im/contexts/wallet/wallet_connect/effects.cljs index 1258487225..35a4bb3622 100644 --- a/src/status_im/contexts/wallet/wallet_connect/effects.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/effects.cljs @@ -118,12 +118,27 @@ (rf/reg-fx :effects.wallet-connect/respond-session-request - (fn [{:keys [web3-wallet topic id result on-success on-error]}] + (fn [{:keys [web3-wallet topic id result error on-success on-error]}] (-> (.respondSessionRequest web3-wallet (clj->js {:topic topic - :response {:id id - :jsonrpc "2.0" - :result result}})) + :response (merge {:id id + :jsonrpc "2.0"} + (when result + {:result result}) + (when error + {:error error}))})) (promesa/then on-success) (promesa/catch on-error)))) + +(rf/reg-fx + :effects.wallet-connect/reject-session-proposal + (fn [{:keys [web3-wallet proposal on-success on-error]}] + (let [{:keys [id]} proposal + reason (wallet-connect/get-sdk-error + constants/wallet-connect-user-rejected-error-key)] + (-> (.rejectSession web3-wallet + (clj->js {:id id + :reason reason})) + (promesa/then on-success) + (promesa/catch on-error))))) diff --git a/src/status_im/contexts/wallet/wallet_connect/modals/common/page_nav/view.cljs b/src/status_im/contexts/wallet/wallet_connect/modals/common/page_nav/view.cljs new file mode 100644 index 0000000000..b16a526223 --- /dev/null +++ b/src/status_im/contexts/wallet/wallet_connect/modals/common/page_nav/view.cljs @@ -0,0 +1,12 @@ +(ns status-im.contexts.wallet.wallet-connect.modals.common.page-nav.view + (:require [quo.core :as quo] + [utils.re-frame :as rf])) + +(defn view + [{:keys [accessibility-label]}] + [quo/page-nav + {:icon-name :i/close + :background :blur + :on-press #(do (rf/dispatch [:navigate-back]) + (rf/dispatch [:wallet-connect/reject-session-request])) + :accessibility-label accessibility-label}]) diff --git a/src/status_im/contexts/wallet/wallet_connect/modals/send_transaction/view.cljs b/src/status_im/contexts/wallet/wallet_connect/modals/send_transaction/view.cljs index 6f74cb6676..3f4724972f 100644 --- a/src/status_im/contexts/wallet/wallet_connect/modals/send_transaction/view.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/modals/send_transaction/view.cljs @@ -5,6 +5,7 @@ [status-im.contexts.wallet.wallet-connect.modals.common.data-block.view :as data-block] [status-im.contexts.wallet.wallet-connect.modals.common.footer.view :as footer] [status-im.contexts.wallet.wallet-connect.modals.common.header.view :as header] + [status-im.contexts.wallet.wallet-connect.modals.common.page-nav.view :as page-nav] [status-im.contexts.wallet.wallet-connect.modals.common.style :as style] [utils.i18n :as i18n] [utils.re-frame :as rf])) @@ -20,11 +21,8 @@ error-state]} (rf/sub [:wallet-connect/current-request-transaction-information])] [rn/view {:style (style/container bottom)} [quo/gradient-cover {:customization-color customization-color}] - [quo/page-nav - {:icon-name :i/close - :background :blur - :on-press #(rf/dispatch [:navigate-back]) - :accessibility-label :wallet-connect-sign-message-close}] + [page-nav/view + {:accessibility-label :wallet-connect-sign-message-close}] [rn/view {:flex 1} [rn/view {:style style/data-content-container} [header/view diff --git a/src/status_im/contexts/wallet/wallet_connect/modals/sign_message/view.cljs b/src/status_im/contexts/wallet/wallet_connect/modals/sign_message/view.cljs index ac631677ef..7ba821434d 100644 --- a/src/status_im/contexts/wallet/wallet_connect/modals/sign_message/view.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/modals/sign_message/view.cljs @@ -5,6 +5,7 @@ [status-im.contexts.wallet.wallet-connect.modals.common.data-block.view :as data-block] [status-im.contexts.wallet.wallet-connect.modals.common.footer.view :as footer] [status-im.contexts.wallet.wallet-connect.modals.common.header.view :as header] + [status-im.contexts.wallet.wallet-connect.modals.common.page-nav.view :as page-nav] [status-im.contexts.wallet.wallet-connect.modals.common.style :as style] [utils.i18n :as i18n] [utils.re-frame :as rf])) @@ -17,11 +18,8 @@ dapp (rf/sub [:wallet-connect/current-request-dapp])] [rn/view {:style (style/container bottom)} [quo/gradient-cover {:customization-color customization-color}] - [quo/page-nav - {:icon-name :i/close - :background :blur - :on-press #(rf/dispatch [:navigate-back]) - :accessibility-label :wallet-connect-sign-message-close}] + [page-nav/view + {:accessibility-label :wallet-connect-sign-message-close}] [rn/view {:flex 1} [rn/view {:style style/sign-message-content-container} [header/view diff --git a/src/status_im/contexts/wallet/wallet_connect/modals/sign_transaction/view.cljs b/src/status_im/contexts/wallet/wallet_connect/modals/sign_transaction/view.cljs index 15f54467b2..6504f21118 100644 --- a/src/status_im/contexts/wallet/wallet_connect/modals/sign_transaction/view.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/modals/sign_transaction/view.cljs @@ -5,6 +5,7 @@ [status-im.contexts.wallet.wallet-connect.modals.common.data-block.view :as data-block] [status-im.contexts.wallet.wallet-connect.modals.common.footer.view :as footer] [status-im.contexts.wallet.wallet-connect.modals.common.header.view :as header] + [status-im.contexts.wallet.wallet-connect.modals.common.page-nav.view :as page-nav] [status-im.contexts.wallet.wallet-connect.modals.common.style :as style] [utils.i18n :as i18n] [utils.re-frame :as rf])) @@ -20,11 +21,8 @@ error-state]} (rf/sub [:wallet-connect/current-request-transaction-information])] [rn/view {:style (style/container bottom)} [quo/gradient-cover {:customization-color customization-color}] - [quo/page-nav - {:icon-name :i/close - :background :blur - :on-press #(rf/dispatch [:navigate-back]) - :accessibility-label :wallet-connect-sign-message-close}] + [page-nav/view + {:accessibility-label :wallet-connect-sign-message-close}] [rn/view {:flex 1} [rn/view {:style style/data-content-container} [header/view diff --git a/src/status_im/contexts/wallet/wallet_connect/responding_events.cljs b/src/status_im/contexts/wallet/wallet_connect/responding_events.cljs index 6a6422fba5..6f0a5fed0f 100644 --- a/src/status_im/contexts/wallet/wallet_connect/responding_events.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/responding_events.cljs @@ -1,5 +1,6 @@ (ns status-im.contexts.wallet.wallet-connect.responding-events (:require [re-frame.core :as rf] + [react-native.wallet-connect :as wallet-connect] [status-im.constants :as constants] [status-im.contexts.wallet.wallet-connect.core :as wallet-connect-core] [taoensso.timbre :as log])) @@ -37,8 +38,7 @@ :address address :data raw-data :on-error #(rf/dispatch [:wallet-connect/on-sign-error %]) - :on-success #(rf/dispatch [:wallet-connect/send-response %])}]]}))) - + :on-success #(rf/dispatch [:wallet-connect/send-response {:result %}])}]]}))) (rf/reg-event-fx :wallet-connect/respond-personal-sign @@ -49,7 +49,7 @@ :address address :data raw-data :on-error #(rf/dispatch [:wallet-connect/on-sign-error %]) - :on-success #(rf/dispatch [:wallet-connect/send-response %])}]]}))) + :on-success #(rf/dispatch [:wallet-connect/send-response {:result %}])}]]}))) (rf/reg-event-fx :wallet-connect/respond-sign-typed-data @@ -61,7 +61,7 @@ :data raw-data :version typed-data-version :on-error #(rf/dispatch [:wallet-connect/on-sign-error %]) - :on-success #(rf/dispatch [:wallet-connect/send-response %])}]]}))) + :on-success #(rf/dispatch [:wallet-connect/send-response {:result %}])}]]}))) (rf/reg-event-fx :wallet-connect/respond-send-transaction-data @@ -73,7 +73,7 @@ :chain-id chain-id :tx raw-data :on-error #(rf/dispatch [:wallet-connect/on-sign-error %]) - :on-success #(rf/dispatch [:wallet-connect/send-response %])}]]}))) + :on-success #(rf/dispatch [:wallet-connect/send-response {:result %}])}]]}))) (rf/reg-event-fx :wallet-connect/respond-sign-transaction-data @@ -85,7 +85,7 @@ :chain-id chain-id :tx raw-data :on-error #(rf/dispatch [:wallet-connect/on-sign-error %]) - :on-success #(rf/dispatch [:wallet-connect/send-response %])}]]}))) + :on-success #(rf/dispatch [:wallet-connect/send-response {:result %}])}]]}))) (rf/reg-event-fx :wallet-connect/on-sign-error @@ -104,10 +104,9 @@ {:fx [[:dispatch [:dismiss-modal screen]] [:dispatch [:wallet-connect/reset-current-request]]]}))) - (rf/reg-event-fx :wallet-connect/send-response - (fn [{:keys [db]} [result]] + (fn [{:keys [db]} [{:keys [result error]}]] (let [{:keys [id topic] :as event} (get-in db [:wallet-connect/current-request :event]) method (wallet-connect-core/get-request-method event) screen (wallet-connect-core/method-to-screen method) @@ -117,6 +116,7 @@ :topic topic :id id :result result + :error error :on-error (fn [error] (log/error "Failed to send Wallet Connect response" {:error error @@ -129,3 +129,28 @@ (log/info "Successfully sent Wallet Connect response to dApp") (rf/dispatch [:dismiss-modal screen]) (rf/dispatch [:wallet-connect/reset-current-request]))}]]}))) + +(rf/reg-event-fx + :wallet-connect/reject-session-proposal + (fn [{:keys [db]} _] + (let [web3-wallet (get db :wallet-connect/web3-wallet) + current-proposal (get-in db [:wallet-connect/current-proposal :request])] + {:fx [[:effects.wallet-connect/reject-session-proposal + {:web3-wallet web3-wallet + :proposal current-proposal + :on-success #(log/info "Wallet Connect session proposal rejected") + :on-error #(log/error "Wallet Connect unable to reject session proposal")}] + [:dispatch [:wallet-connect/reset-current-session]]]}))) + +;; NOTE: Currently we only reject a session if the user rejected it +;; But this needs to be solidified to ensure other cases: +;; - Unsupported WC version +;; - Invalid params from dapps +;; - Unsupported method +(rf/reg-event-fx + :wallet-connect/reject-session-request + (fn [_ _] + {:fx [[:dispatch + [:wallet-connect/send-response + {:error (wallet-connect/get-sdk-error + constants/wallet-connect-user-rejected-error-key)}]]]})) diff --git a/src/status_im/contexts/wallet/wallet_connect/session_proposal/view.cljs b/src/status_im/contexts/wallet/wallet_connect/session_proposal/view.cljs index 1062d154e6..3c953d686b 100644 --- a/src/status_im/contexts/wallet/wallet_connect/session_proposal/view.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/session_proposal/view.cljs @@ -142,7 +142,7 @@ :accessibility-label :wc-deny-connection :on-press #(do (rf/dispatch [:navigate-back]) (rf/dispatch - [:wallet-connect/reset-current-session]))} + [:wallet-connect/reject-session-proposal]))} :button-one-label (i18n/label :t/connect) :button-one-props {:customization-color customization-color :type :primary