🙅 Gracefully reject proposals and requests (#20649)
* 🙅 Gracefully reject proposals and requests * 🔑 Remove redudant fx, send correct error * ➡️ Docstring to comment --------- Co-authored-by: Lungu Cristian <lungucristian95@gmail.com>
This commit is contained in:
parent
d74edff9b9
commit
072370b0d6
|
@ -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))
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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)))))
|
||||
|
|
|
@ -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}])
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)}]]]}))
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue