From 721aa51d8d051018cc98a02a9026dbf5622e32ac Mon Sep 17 00:00:00 2001 From: Shivek Khurana Date: Fri, 12 Jul 2024 14:47:25 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A3=20Fix=20wallet=20connect=20init=20?= =?UTF-8?q?issue=20in=20release=20bundles=20(#20665)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 👀 Added alerts so I peek into build * 🆙 Update logs * 🅰️ Annotate * 📤 Don't refer * 🖊️ Use default import * added ^js * ➕ Added ocall * 🎣 Isolate WC js calls to rn ns * 🧯Fix on-scan-connection issue --------- Co-authored-by: Lungu Cristian --- src/react_native/wallet_connect.cljs | 86 +++++++++++++++---- .../wallet/wallet_connect/effects.cljs | 54 ++++++------ .../wallet/wallet_connect/events.cljs | 2 +- 3 files changed, 96 insertions(+), 46 deletions(-) diff --git a/src/react_native/wallet_connect.cljs b/src/react_native/wallet_connect.cljs index b43e272bdd..cd2a1c2625 100644 --- a/src/react_native/wallet_connect.cljs +++ b/src/react_native/wallet_connect.cljs @@ -1,35 +1,91 @@ (ns react-native.wallet-connect (:require - ["@walletconnect/core" :refer [Core]] - ["@walletconnect/utils" :refer - [buildApprovedNamespaces getSdkError parseUri]] - ["@walletconnect/web3wallet" :refer [Web3Wallet]])) + ["@walletconnect/core" :as wc-core] + ["@walletconnect/utils" :as wc-utils] + ["@walletconnect/web3wallet$default" :as Web3Wallet] + [cljs-bean.core :as bean] + [oops.core :as oops])) (defn- wallet-connect-core [project-id] - (Core. #js {:projectId project-id})) + (new ^js wc-core/Core (clj->js {:projectId project-id}))) (defn init [project-id metadata] (let [core (wallet-connect-core project-id)] - (Web3Wallet.init - (clj->js {:core core - :metadata metadata})))) + (oops/ocall Web3Wallet + "init" + (bean/->js {:core core + :metadata metadata})))) (defn build-approved-namespaces [proposal supported-namespaces] - (buildApprovedNamespaces - (clj->js {:proposal proposal - :supportedNamespaces supported-namespaces}))) + (oops/ocall wc-utils + "buildApprovedNamespaces" + (bean/->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)) + (oops/ocall wc-utils "getSdkError" error-key)) (defn parse-uri [uri] - (-> uri - parseUri - (js->clj :keywordize-keys true))) + (-> (oops/ocall wc-utils "parseUri" uri) + (bean/->clj))) + +(defn respond-session-request + [{:keys [web3-wallet topic id result error]}] + (oops/ocall web3-wallet + "respondSessionRequest" + (bean/->js {:topic topic + :response + (merge {:id id + :jsonrpc "2.0"} + (when result + {:result result}) + (when error + {:error error}))}))) + +(defn reject-session + [{:keys [web3-wallet id reason]}] + (.rejectSession web3-wallet + (clj->js {:id id + :reason reason}))) + +(defn approve-session + [{:keys [web3-wallet id approved-namespaces]}] + (oops/ocall web3-wallet + "approveSession" + (bean/->js {:id id + :namespaces approved-namespaces}))) + +(defn get-active-sessions + [web3-wallet] + (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 + [web3-wallet url] + (oops/ocall web3-wallet + "core.pairing.pair" + (bean/->js {:uri url}))) + +(defn get-pairings + [web3-wallet] + (oops/ocall web3-wallet "core.pairing.getPairings")) + +(defn register-handler + [{:keys [web3-wallet event handler]}] + (oops/ocall web3-wallet + "on" + event + #(-> (bean/->clj %) + handler))) diff --git a/src/status_im/contexts/wallet/wallet_connect/effects.cljs b/src/status_im/contexts/wallet/wallet_connect/effects.cljs index 35a4bb3622..cb9364e7be 100644 --- a/src/status_im/contexts/wallet/wallet_connect/effects.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/effects.cljs @@ -29,18 +29,15 @@ (rf/reg-fx :effects.wallet-connect/register-event-listener (fn [[web3-wallet wc-event handler]] - (.on web3-wallet - wc-event - (fn [js-proposal] - (-> js-proposal - (js->clj :keywordize-keys true) - handler))))) + (wallet-connect/register-handler + {:web3-wallet web3-wallet + :event wc-event + :handler handler}))) (rf/reg-fx :effects.wallet-connect/fetch-pairings (fn [{:keys [web3-wallet on-success on-fail]}] - (-> (.. web3-wallet -core -pairing) - (.getPairings) + (-> (wallet-connect/get-pairings web3-wallet) (promesa/then on-success) (promesa/catch on-fail)))) @@ -48,23 +45,21 @@ :effects.wallet-connect/pair (fn [{:keys [web3-wallet url on-success on-fail]}] (when web3-wallet - (-> (.. web3-wallet -core -pairing) - (.pair (clj->js {:uri url})) + (-> (wallet-connect/core-pairing-pair web3-wallet url) (promesa/then on-success) (promesa/catch on-fail))))) (rf/reg-fx :effects.wallet-connect/disconnect (fn [{:keys [web3-wallet topic on-success on-fail]}] - (-> (.. web3-wallet -core -pairing) - (.disconnect (clj->js {:topic topic})) + (-> (wallet-connect/core-pairing-disconnnect web3-wallet topic) (promesa/then on-success) (promesa/catch on-fail)))) (rf/reg-fx :effects.wallet-connect/fetch-active-sessions (fn [{:keys [web3-wallet on-success on-fail]}] - (-> (.getActiveSessions web3-wallet) + (-> (wallet-connect/get-active-sessions web3-wallet) (promesa/then on-success) (promesa/catch on-fail)))) @@ -75,9 +70,10 @@ approved-namespaces (wallet-connect/build-approved-namespaces params supported-namespaces)] - (-> (.approveSession web3-wallet - (clj->js {:id id - :namespaces approved-namespaces})) + (-> (wallet-connect/approve-session + {:web3-wallet web3-wallet + :id id + :approved-namespaces approved-namespaces}) (promesa/then on-success) (promesa/catch on-fail))))) @@ -119,17 +115,14 @@ (rf/reg-fx :effects.wallet-connect/respond-session-request (fn [{:keys [web3-wallet topic id result error on-success on-error]}] - (-> - (.respondSessionRequest web3-wallet - (clj->js {:topic topic - :response (merge {:id id - :jsonrpc "2.0"} - (when result - {:result result}) - (when error - {:error error}))})) - (promesa/then on-success) - (promesa/catch on-error)))) + (-> (wallet-connect/respond-session-request + {:web3-wallet web3-wallet + :topic topic + :id id + :result result + :error error}) + (promesa/then on-success) + (promesa/catch on-error)))) (rf/reg-fx :effects.wallet-connect/reject-session-proposal @@ -137,8 +130,9 @@ (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})) + (-> (wallet-connect/reject-session + {:web3-wallet web3-wallet + :id id + :reason reason}) (promesa/then on-success) (promesa/catch on-error))))) diff --git a/src/status_im/contexts/wallet/wallet_connect/events.cljs b/src/status_im/contexts/wallet/wallet_connect/events.cljs index 250776568b..51856c97da 100644 --- a/src/status_im/contexts/wallet/wallet_connect/events.cljs +++ b/src/status_im/contexts/wallet/wallet_connect/events.cljs @@ -183,7 +183,7 @@ (fn [_ [scanned-text]] (let [parsed-uri (wallet-connect/parse-uri scanned-text) version (:version parsed-uri) - valid-wc-uri? (wc-utils/valid-uri? parsed-uri) + valid-wc-uri? (wc-utils/valid-wc-uri? parsed-uri) expired? (-> parsed-uri :expiryTimestamp wc-utils/timestamp-expired?)