🎣 Fix wallet connect init issue in release bundles (#20665)

* 👀 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 <lungucristian95@gmail.com>
This commit is contained in:
Shivek Khurana 2024-07-12 14:47:25 +05:30 committed by GitHub
parent 23c25d7715
commit 721aa51d8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 96 additions and 46 deletions

View File

@ -1,35 +1,91 @@
(ns react-native.wallet-connect (ns react-native.wallet-connect
(:require (:require
["@walletconnect/core" :refer [Core]] ["@walletconnect/core" :as wc-core]
["@walletconnect/utils" :refer ["@walletconnect/utils" :as wc-utils]
[buildApprovedNamespaces getSdkError parseUri]] ["@walletconnect/web3wallet$default" :as Web3Wallet]
["@walletconnect/web3wallet" :refer [Web3Wallet]])) [cljs-bean.core :as bean]
[oops.core :as oops]))
(defn- wallet-connect-core (defn- wallet-connect-core
[project-id] [project-id]
(Core. #js {:projectId project-id})) (new ^js wc-core/Core (clj->js {:projectId project-id})))
(defn init (defn init
[project-id metadata] [project-id metadata]
(let [core (wallet-connect-core project-id)] (let [core (wallet-connect-core project-id)]
(Web3Wallet.init (oops/ocall Web3Wallet
(clj->js {:core core "init"
:metadata metadata})))) (bean/->js {:core core
:metadata metadata}))))
(defn build-approved-namespaces (defn build-approved-namespaces
[proposal supported-namespaces] [proposal supported-namespaces]
(buildApprovedNamespaces (oops/ocall wc-utils
(clj->js {:proposal proposal "buildApprovedNamespaces"
:supportedNamespaces supported-namespaces}))) (bean/->js {:proposal proposal
:supportedNamespaces supported-namespaces})))
;; Get an error from this list: ;; Get an error from this list:
;; https://github.com/WalletConnect/walletconnect-monorepo/blob/c6e9529418a0c81d4efcc6ac4e61f242a50b56c5/packages/utils/src/errors.ts ;; https://github.com/WalletConnect/walletconnect-monorepo/blob/c6e9529418a0c81d4efcc6ac4e61f242a50b56c5/packages/utils/src/errors.ts
(defn get-sdk-error (defn get-sdk-error
[error-key] [error-key]
(getSdkError error-key)) (oops/ocall wc-utils "getSdkError" error-key))
(defn parse-uri (defn parse-uri
[uri] [uri]
(-> uri (-> (oops/ocall wc-utils "parseUri" uri)
parseUri (bean/->clj)))
(js->clj :keywordize-keys true)))
(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)))

View File

@ -29,18 +29,15 @@
(rf/reg-fx (rf/reg-fx
:effects.wallet-connect/register-event-listener :effects.wallet-connect/register-event-listener
(fn [[web3-wallet wc-event handler]] (fn [[web3-wallet wc-event handler]]
(.on web3-wallet (wallet-connect/register-handler
wc-event {:web3-wallet web3-wallet
(fn [js-proposal] :event wc-event
(-> js-proposal :handler handler})))
(js->clj :keywordize-keys true)
handler)))))
(rf/reg-fx (rf/reg-fx
:effects.wallet-connect/fetch-pairings :effects.wallet-connect/fetch-pairings
(fn [{:keys [web3-wallet on-success on-fail]}] (fn [{:keys [web3-wallet on-success on-fail]}]
(-> (.. web3-wallet -core -pairing) (-> (wallet-connect/get-pairings web3-wallet)
(.getPairings)
(promesa/then on-success) (promesa/then on-success)
(promesa/catch on-fail)))) (promesa/catch on-fail))))
@ -48,23 +45,21 @@
:effects.wallet-connect/pair :effects.wallet-connect/pair
(fn [{:keys [web3-wallet url on-success on-fail]}] (fn [{:keys [web3-wallet url on-success on-fail]}]
(when web3-wallet (when web3-wallet
(-> (.. web3-wallet -core -pairing) (-> (wallet-connect/core-pairing-pair web3-wallet url)
(.pair (clj->js {:uri url}))
(promesa/then on-success) (promesa/then on-success)
(promesa/catch on-fail))))) (promesa/catch on-fail)))))
(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 on-success on-fail]}]
(-> (.. web3-wallet -core -pairing) (-> (wallet-connect/core-pairing-disconnnect web3-wallet topic)
(.disconnect (clj->js {:topic topic}))
(promesa/then on-success) (promesa/then on-success)
(promesa/catch on-fail)))) (promesa/catch on-fail))))
(rf/reg-fx (rf/reg-fx
:effects.wallet-connect/fetch-active-sessions :effects.wallet-connect/fetch-active-sessions
(fn [{:keys [web3-wallet on-success on-fail]}] (fn [{:keys [web3-wallet on-success on-fail]}]
(-> (.getActiveSessions web3-wallet) (-> (wallet-connect/get-active-sessions web3-wallet)
(promesa/then on-success) (promesa/then on-success)
(promesa/catch on-fail)))) (promesa/catch on-fail))))
@ -75,9 +70,10 @@
approved-namespaces (wallet-connect/build-approved-namespaces approved-namespaces (wallet-connect/build-approved-namespaces
params params
supported-namespaces)] supported-namespaces)]
(-> (.approveSession web3-wallet (-> (wallet-connect/approve-session
(clj->js {:id id {:web3-wallet web3-wallet
:namespaces approved-namespaces})) :id id
:approved-namespaces approved-namespaces})
(promesa/then on-success) (promesa/then on-success)
(promesa/catch on-fail))))) (promesa/catch on-fail)))))
@ -119,17 +115,14 @@
(rf/reg-fx (rf/reg-fx
:effects.wallet-connect/respond-session-request :effects.wallet-connect/respond-session-request
(fn [{:keys [web3-wallet topic id result error on-success on-error]}] (fn [{:keys [web3-wallet topic id result error on-success on-error]}]
(-> (-> (wallet-connect/respond-session-request
(.respondSessionRequest web3-wallet {:web3-wallet web3-wallet
(clj->js {:topic topic :topic topic
:response (merge {:id id :id id
:jsonrpc "2.0"} :result result
(when result :error error})
{:result result}) (promesa/then on-success)
(when error (promesa/catch on-error))))
{:error error}))}))
(promesa/then on-success)
(promesa/catch on-error))))
(rf/reg-fx (rf/reg-fx
:effects.wallet-connect/reject-session-proposal :effects.wallet-connect/reject-session-proposal
@ -137,8 +130,9 @@
(let [{:keys [id]} proposal (let [{:keys [id]} proposal
reason (wallet-connect/get-sdk-error reason (wallet-connect/get-sdk-error
constants/wallet-connect-user-rejected-error-key)] constants/wallet-connect-user-rejected-error-key)]
(-> (.rejectSession web3-wallet (-> (wallet-connect/reject-session
(clj->js {:id id {:web3-wallet web3-wallet
:reason reason})) :id id
:reason reason})
(promesa/then on-success) (promesa/then on-success)
(promesa/catch on-error))))) (promesa/catch on-error)))))

View File

@ -183,7 +183,7 @@
(fn [_ [scanned-text]] (fn [_ [scanned-text]]
(let [parsed-uri (wallet-connect/parse-uri scanned-text) (let [parsed-uri (wallet-connect/parse-uri scanned-text)
version (:version parsed-uri) 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 expired? (-> parsed-uri
:expiryTimestamp :expiryTimestamp
wc-utils/timestamp-expired?) wc-utils/timestamp-expired?)