Skip to content

Commit

Permalink
fix: wallet-connect structure & small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
clauxx committed May 15, 2024
1 parent 5ab97db commit 0629d8d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 55 deletions.
24 changes: 24 additions & 0 deletions src/react_native/wallet_connect.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns react-native.wallet-connect
;; NOTE: Not sorting namespaces since @walletconnect/react-native-compat should be the first
#_{:clj-kondo/ignore [:unsorted-required-namespaces]}
(:require ["@walletconnect/react-native-compat"]
["@walletconnect/core" :refer [Core]]
["@walletconnect/web3wallet" :refer [Web3Wallet]]
["@walletconnect/utils" :refer [buildApprovedNamespaces]]))

(defn- wallet-connect-core
[project-id]
(Core. #js {:projectId project-id}))

(defn init
[project-id metadata]
(let [core (wallet-connect-core project-id)]
(Web3Wallet.init
(clj->js {:core core
:metadata metadata}))))

(defn build-approved-namespaces
[proposal supported-namespaces]
(buildApprovedNamespaces
(clj->js {:proposal proposal
:supportedNamespaces supported-namespaces})))
3 changes: 3 additions & 0 deletions src/status_im/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@
(def regx-full-or-partial-address #"^0x[a-fA-F0-9]{1,40}$")

;; Wallet Connect
(def ^:const wallet-connect-metadata-icon
"https://res.cloudinary.com/dhgck7ebz/image/upload/f_auto,c_limit,w_1080,q_auto/Brand/Logo%20Section/Mark/Mark_01")
(def ^:const wallet-connect-metadata-url "https://status.app")
(def ^:const optimism-crosschain-id "eip155:10")
(def ^:const wallet-connect-supported-methods ["eth_sendTransaction" "personal_sign"])
(def ^:const wallet-connect-supported-events ["accountsChanged" "chainChanged"])
Expand Down
23 changes: 16 additions & 7 deletions src/status_im/contexts/wallet/wallet_connect/effects.cljs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
(ns status-im.contexts.wallet.wallet-connect.effects
(:require [promesa.core :as promesa]
[re-frame.core :as rf]
[status-im.contexts.wallet.wallet-connect.utils :as wallet-connect.utils]))
[react-native.wallet-connect :as wallet-connect]
[status-im.config :as config]
[status-im.constants :as constants]
[utils.i18n :as i18n]))

(rf/reg-fx
:effects.wallet-connect/init
(fn [{:keys [on-success on-fail]}]
(-> (wallet-connect.utils/init)
(promesa/then on-success)
(promesa/catch on-fail))))
(let
[project-id config/WALLET_CONNECT_PROJECT_ID
metadata {:name (i18n/label :t/status)
:description (i18n/label :t/status-is-a-secure-messaging-app)
:url constants/wallet-connect-metadata-url
:icons [constants/wallet-connect-metadata-icon]}]
(-> (wallet-connect/init project-id metadata)
(promesa/then on-success)
(promesa/catch on-fail)))))

(rf/reg-fx
:effects.wallet-connect/register-event-listener
(fn [web3-wallet wc-event handler]
(fn [[web3-wallet wc-event handler]]
(.on web3-wallet
wc-event
(fn [js-proposal]
Expand All @@ -32,8 +41,8 @@
:effects.wallet-connect/approve-session
(fn [{:keys [web3-wallet proposal supported-namespaces on-success on-fail]}]
(let [{:keys [params id]} proposal
approved-namespaces (wallet-connect.utils/build-approved-namespaces params
supported-namespaces)]
approved-namespaces (wallet-connect/build-approved-namespaces params
supported-namespaces)]
(-> (.approveSession web3-wallet
(clj->js {:id id
:namespaces approved-namespaces}))
Expand Down
16 changes: 7 additions & 9 deletions src/status_im/contexts/wallet/wallet_connect/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
(:require [re-frame.core :as rf]
[status-im.constants :as constants]
status-im.contexts.wallet.wallet-connect.effects
[status-im.contexts.wallet.wallet-connect.utils :as wallet-connect.utils]
[taoensso.timbre :as log]))

(rf/reg-event-fx
Expand All @@ -23,13 +22,13 @@
(fn [{:keys [db]}]
(let [web3-wallet (get db :wallet-connect/web3-wallet)]
{:fx [[:effects.wallet-connect/register-event-listener
web3-wallet
constants/wallet-connect-session-proposal-event
#(rf/dispatch [:wallet-connect/on-session-proposal %])]]})))
[web3-wallet
constants/wallet-connect-session-proposal-event
#(rf/dispatch [:wallet-connect/on-session-proposal %])]]]})))

(rf/reg-event-fx
:wallet-connect/on-init-fail
(fn [error]
(fn [_ [error]]
(log/error "Failed to initialize Wallet Connect"
{:error error
:event :wallet-connect/on-init-fail})))
Expand All @@ -51,7 +50,7 @@
{:fx [[:effects.wallet-connect/pair
{:web3-wallet web3-wallet
:url url
:on-fail #(log/error "Failed to pair with dApp")
:on-fail #(log/error "Failed to pair with dApp" {:error %})
:on-success #(log/info "dApp paired successfully")}]]})))

(rf/reg-event-fx
Expand All @@ -68,8 +67,7 @@
;; - global scanner -> first account in list
;; - wallet account dapps -> account that is selected
address (-> accounts keys first)
formatted-address (wallet-connect.utils/format-address (first crosschain-ids)
address)
formatted-address (str (first crosschain-ids) ":" address)
supported-namespaces (clj->js {:eip155
{:chains crosschain-ids
:methods constants/wallet-connect-supported-methods
Expand All @@ -80,7 +78,7 @@
:proposal current-proposal
:supported-namespaces supported-namespaces
:on-success (fn []
(log/debug "Wallet Connect session approved")
(log/info "Wallet Connect session approved")
(rf/dispatch [:wallet-connect/reset-current-session]))
:on-fail (fn [error]
(log/error "Wallet Connect session approval failed"
Expand Down
39 changes: 0 additions & 39 deletions src/status_im/contexts/wallet/wallet_connect/utils.cljs

This file was deleted.

0 comments on commit 0629d8d

Please sign in to comment.