Proper handling of disconnection, both from the dapp itself and from the list of connected dapps (#20817)

This commit is contained in:
Alexander 2024-07-22 15:20:52 +02:00 committed by GitHub
parent d623cc8444
commit 08b65cbcb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 43 additions and 36 deletions

View File

@ -62,16 +62,17 @@
(bean/->js {:id id (bean/->js {:id id
:namespaces approved-namespaces}))) :namespaces approved-namespaces})))
(defn disconnect-session
[{:keys [web3-wallet reason topic]}]
(oops/ocall web3-wallet
"disconnectSession"
(bean/->js {:topic topic
:reason reason})))
(defn get-active-sessions (defn get-active-sessions
[web3-wallet] [web3-wallet]
(oops/ocall web3-wallet "getActiveSessions")) (oops/ocall web3-wallet "getActiveSessions"))
(defn core-pairing-disconnnect
[web3-wallet topic]
(oops/ocall web3-wallet
"core.pairing.disconnect"
(bean/->js {:topic topic})))
(defn core-pairing-pair (defn core-pairing-pair
[web3-wallet url] [web3-wallet url]
(oops/ocall web3-wallet (oops/ocall web3-wallet

View File

@ -290,6 +290,7 @@
(def ^:const wallet-connect-session-request-event "session_request") (def ^:const wallet-connect-session-request-event "session_request")
(def ^:const wallet-connect-session-delete-event "session_delete") (def ^:const wallet-connect-session-delete-event "session_delete")
(def ^:const wallet-connect-user-rejected-error-key "USER_REJECTED") (def ^:const wallet-connect-user-rejected-error-key "USER_REJECTED")
(def ^:const wallet-connect-user-disconnected-reason-key "USER_DISCONNECTED")
(def ^:const transaction-pending-type-wallet-connect-transfer "WalletConnectTransfer") (def ^:const transaction-pending-type-wallet-connect-transfer "WalletConnectTransfer")

View File

@ -13,26 +13,25 @@
[utils.re-frame :as rf])) [utils.re-frame :as rf]))
(defn- on-disconnect (defn- on-disconnect
[wallet-account {:keys [name topic pairing-topic]}] [wallet-account {:keys [name topic]}]
(rf/dispatch [:hide-bottom-sheet]) (rf/dispatch [:hide-bottom-sheet])
(rf/dispatch (rf/dispatch
[:wallet-connect/disconnect-dapp [:wallet-connect/disconnect-dapp
{:topic topic {:topic topic
:pairing-topic pairing-topic :on-success (fn []
:on-success (fn [] (rf/dispatch [:toasts/upsert
(rf/dispatch [:toasts/upsert {:id :dapp-disconnect-success
{:id :dapp-disconnect-success :type :positive
:type :positive :text (i18n/label :t/disconnect-dapp-success
:text (i18n/label :t/disconnect-dapp-success {:dapp name
{:dapp name :account (:name wallet-account)})}]))
:account (:name wallet-account)})}])) :on-fail (fn []
:on-fail (fn [] (rf/dispatch [:toasts/upsert
(rf/dispatch [:toasts/upsert {:id :dapp-disconnect-failure
{:id :dapp-disconnect-failure :type :negative
:type :negative :text (i18n/label :t/disconnect-dapp-fail
:text (i18n/label :t/disconnect-dapp-fail {:dapp name
{:dapp name :account (:name wallet-account)})}]))}]))
:account (:name wallet-account)})}]))}]))
(defn- on-dapp-disconnect-press (defn- on-dapp-disconnect-press
[wallet-account dapp] [wallet-account dapp]

View File

@ -41,8 +41,10 @@
(rf/reg-fx (rf/reg-fx
:effects.wallet-connect/disconnect :effects.wallet-connect/disconnect
(fn [{:keys [web3-wallet topic on-success on-fail]}] (fn [{:keys [web3-wallet topic reason on-success on-fail]}]
(-> (wallet-connect/core-pairing-disconnnect web3-wallet topic) (-> (wallet-connect/disconnect-session {:web3-wallet web3-wallet
:topic topic
:reason reason})
(promesa/then on-success) (promesa/then on-success)
(promesa/catch on-fail)))) (promesa/catch on-fail))))

View File

@ -115,14 +115,16 @@
(rf/reg-event-fx (rf/reg-event-fx
:wallet-connect/disconnect-dapp :wallet-connect/disconnect-dapp
(fn [{:keys [db]} [{:keys [pairing-topic on-success on-fail]}]] (fn [{:keys [db]} [{:keys [topic on-success on-fail]}]]
(let [web3-wallet (get db :wallet-connect/web3-wallet)] (let [web3-wallet (get db :wallet-connect/web3-wallet)]
{:fx [[:effects.wallet-connect/disconnect {:fx [[:effects.wallet-connect/disconnect
{:web3-wallet web3-wallet {:web3-wallet web3-wallet
:topic pairing-topic :topic topic
:reason (wallet-connect/get-sdk-error
constants/wallet-connect-user-disconnected-reason-key)
:on-fail on-fail :on-fail on-fail
:on-success (fn [] :on-success (fn []
(rf/dispatch [:wallet-connect/disconnect-session pairing-topic]) (rf/dispatch [:wallet-connect/disconnect-session topic])
(when on-success (when on-success
(on-success)))}]]}))) (on-success)))}]]})))
@ -211,12 +213,14 @@
(map wallet-connect-core/sdk-session->db-session) (map wallet-connect-core/sdk-session->db-session)
(wallet-connect-core/filter-sessions-for-account-addresses (wallet-connect-core/filter-sessions-for-account-addresses
account-addresses)) account-addresses))
expired-sessions (remove session-topics (set (map :topic sessions))
(fn [{:keys [expiry]}] expired-sessions (filter
(> expiry (/ now 1000))) (fn [{:keys [expiry topic]}]
(or (< expiry (/ now 1000))
(not (contains? session-topics topic))))
persisted-sessions)] persisted-sessions)]
{:fx (mapv (fn [{:keys [pairingTopic]}] {:fx (mapv (fn [{:keys [topic]}]
[:wallet-connect/disconnect-session pairingTopic]) [:dispatch [:wallet-connect/disconnect-session topic]])
expired-sessions) expired-sessions)
:db (assoc db :wallet-connect/sessions sessions)}))) :db (assoc db :wallet-connect/sessions sessions)})))
@ -274,15 +278,15 @@
(rf/reg-event-fx (rf/reg-event-fx
:wallet-connect/disconnect-session :wallet-connect/disconnect-session
(fn [{:keys [db]} [pairing-topic]] (fn [{:keys [db]} [topic]]
{:db (update db {:db (update db
:wallet-connect/sessions :wallet-connect/sessions
(fn [sessions] (fn [sessions]
(->> sessions (->> sessions
(remove #(= (:pairingTopic %) pairing-topic)) (remove #(= (:topic %) topic))
(into [])))) (into []))))
:fx [[:json-rpc/call :fx [[:json-rpc/call
[{:method "wallet_disconnectWalletConnectSession" [{:method "wallet_disconnectWalletConnectSession"
:params [pairing-topic] :params [topic]
:on-success #(log/info "Wallet Connect session disconnected") :on-success #(log/info "Wallet Connect session disconnected")
:on-error #(log/info "Wallet Connect session persistence failed" %)}]]]})) :on-error #(log/info "Wallet Connect session persistence failed" %)}]]]}))