Fix Wallet Connect structure (#20042)

* fix: wallet-connect structure & small bugs

* fix: formatting issue with sorting wc dependency
This commit is contained in:
Lungu Cristian 2024-05-16 18:02:44 +03:00 committed by GitHub
parent db26a878c3
commit c05be0f2c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 49 additions and 55 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -1,39 +0,0 @@
(ns status-im.contexts.wallet.wallet-connect.utils
;; NOTE: Not sorting namespaces since @walletconnect/react-native-compat should be the first
#_{:clj-kondo/ignore [:unsorted-required-namespaces]}
(:require ["@walletconnect/core" :refer [Core]]
["@walletconnect/react-native-compat"]
["@walletconnect/utils" :refer [buildApprovedNamespaces]]
["@walletconnect/web3wallet" :refer [Web3Wallet]]
[status-im.config :as config]
[utils.i18n :as i18n]))
(defn- wallet-connect-metadata
[]
#js
{:name (i18n/label :t/status)
:description (i18n/label :t/status-is-a-secure-messaging-app)
:url "https://status.app"
:icons
["https://res.cloudinary.com/dhgck7ebz/image/upload/f_auto,c_limit,w_1080,q_auto/Brand/Logo%20Section/Mark/Mark_01"]})
(defn- wallet-connect-core
[]
(Core. #js {:projectId config/WALLET_CONNECT_PROJECT_ID}))
(defn init
[]
(let [core (wallet-connect-core)]
(Web3Wallet.init
(clj->js {:core core
:metadata wallet-connect-metadata}))))
(defn build-approved-namespaces
[proposal supported-namespaces]
(buildApprovedNamespaces
(clj->js {:proposal proposal
:supportedNamespaces supported-namespaces})))
(defn format-address
[chain-id address]
(str chain-id ":" address))

View File

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