From e9f98fcf85e2de00ef135d8c6edc21eaa4342f2a Mon Sep 17 00:00:00 2001 From: Shivek Khurana Date: Wed, 19 Jun 2024 15:55:38 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=BE=20=20Persist=20Wallet=20Connect=20?= =?UTF-8?q?sessions=20(#20351)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 💾 Add persist WC session events * ✅ Persistence works - Also moved closing of modal to approval stage --- .../wallet/wallet_connect/events.cljs | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/status_im/contexts/wallet/wallet_connect/events.cljs b/src/status_im/contexts/wallet/wallet_connect/events.cljs index f98ebabd65..8dba642f7f 100644 --- a/src/status_im/contexts/wallet/wallet_connect/events.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/events.cljs @@ -110,7 +110,7 @@ (rf/reg-event-fx :wallet-connect/close-session-request (fn [_ _] - {:fx [[:dispatch [:navigate-back]] + {:fx [[:dispatch [:dismiss-modal :screen/wallet.wallet-connect-session-proposal]] [:dispatch [:wallet-connect/reset-current-session-request]]]})) (rf/reg-event-fx @@ -151,13 +151,20 @@ :supported-namespaces supported-namespaces :on-success (fn [] (log/info "Wallet Connect session approved") - (rf/dispatch [:wallet-connect/reset-current-session-proposal])) + (let [metadata (-> current-proposal :params :proposer :metadata)] + (rf/dispatch [:wallet-connect/reset-current-session-proposal]) + (rf/dispatch [:wallet-connect/persist-session + {:id (:id current-proposal) + :dapp-name (:name metadata) + :dapp-url (:url metadata) + :session-info current-proposal}]))) :on-fail (fn [error] (log/error "Wallet Connect session approval failed" {:error error :event :wallet-connect/approve-session}) (rf/dispatch - [:wallet-connect/reset-current-session-proposal]))}]]}))) + [:wallet-connect/reset-current-session-proposal]))}] + [:dispatch [:dismiss-modal :screen/wallet.wallet-connect-session-proposal]]]}))) (rf/reg-event-fx :wallet-connect/on-scan-connection @@ -185,5 +192,31 @@ {:version version})}]]]} :else - {:fx [[:dispatch [:wallet-connect/pair scanned-text]] - [:dispatch [:dismiss-modal :screen/wallet.wallet-connect-session-proposal]]]})))) + {:fx [[:dispatch [:wallet-connect/pair scanned-text]]]})))) + +(rf/reg-event-fx + :wallet-connect/persist-session + (fn [_ [{:keys [id dapp-name dapp-url session-info]}]] + {:fx [[:json-rpc/call + [{:method "wakuext_addWalletConnectSession" + :params [{:id (str id) + :dappName dapp-name + :dappUrl dapp-url + :info (-> session-info + clj->js + js/JSON.stringify)}] + :on-success #(log/info "Wallet Connect session persisted") + :on-error #(log/info "Wallet Connect session persistence failed" %)}]]]})) + +(rf/reg-event-fx + :wallet-connect/fetch-persisted-sessions-success + (fn [{:keys [db]} [sessions]] + {:db (assoc db :wallet-connect/persisted-sessions sessions)})) + +(rf/reg-event-fx + :wallet-connect/fetch-persisted-sessions + (fn [_ _] + {:fx [[:json-rpc/call + [{:method "wakuext_getWalletConnectSession" + :on-success [:wallet-connect/fetch-persisted-sessions-success] + :on-error #(log/info "Wallet Connect fetch persisted sessions failed")}]]]}))