Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Wallet Connect structure #20042

Merged
merged 6 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/react_native/wallet_connect.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(ns react-native.wallet-connect
(:require
["@walletconnect/core" :refer [Core]]
["@walletconnect/utils" :refer [buildApprovedNamespaces]]
["@walletconnect/web3wallet" :refer [Web3Wallet]]))

(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.

1 change: 1 addition & 0 deletions src/status_im/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
;; NOTE: Do NOT sort i18n-resources because it MUST be loaded first.
[status-im.setup.i18n-resources :as i18n-resources]
#_{:clj-kondo/ignore [:unsorted-required-namespaces]}
["@walletconnect/react-native-compat"]
legacy.status-im.events
legacy.status-im.subs.root
[native-module.core :as native-module]
Expand Down