🎣 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
(: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)))

View File

@ -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)))))

View File

@ -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?)