-
Notifications
You must be signed in to change notification settings - Fork 987
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Wallet Connect base implementation of the connection flow (#19909)
* feat: added base implementation of WC connect flow * test: mocking the wc dependency * fix: addressed review comments
- Loading branch information
Showing
8 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
(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])) | ||
|
||
(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)))) | ||
|
||
(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))))) | ||
|
||
(rf/reg-fx | ||
:effects.wallet-connect/pair | ||
(fn [{:keys [web3-wallet url on-success on-fail]}] | ||
(-> (.. web3-wallet -core -pairing) | ||
(.pair (clj->js {:uri url})) | ||
(promesa/then on-success) | ||
(promesa/catch on-fail)))) | ||
|
||
(rf/reg-fx | ||
: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)] | ||
(-> (.approveSession web3-wallet | ||
(clj->js {:id id | ||
:namespaces approved-namespaces})) | ||
(promesa/then on-success) | ||
(promesa/catch on-fail))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
(ns status-im.contexts.wallet.wallet-connect.events | ||
(: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 | ||
:wallet-connect/init | ||
(fn [] | ||
{:fx [[:effects.wallet-connect/init | ||
{:on-success #(rf/dispatch [:wallet-connect/on-init-success %]) | ||
:on-fail #(rf/dispatch [:wallet-connect/on-init-fail %])}]]})) | ||
|
||
(rf/reg-event-fx | ||
:wallet-connect/on-init-success | ||
(fn [{:keys [db]} [web3-wallet]] | ||
{:db (assoc db :wallet-connect/web3-wallet web3-wallet) | ||
:fx [[:dispatch [:wallet-connect/register-event-listeners]]]})) | ||
|
||
(rf/reg-event-fx | ||
:wallet-connect/register-event-listeners | ||
(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 %])]]}))) | ||
|
||
(rf/reg-event-fx | ||
:wallet-connect/on-init-fail | ||
(fn [error] | ||
(log/error "Failed to initialize Wallet Connect" | ||
{:error error | ||
:event :wallet-connect/on-init-fail}))) | ||
|
||
(rf/reg-event-fx | ||
:wallet-connect/on-session-proposal | ||
(fn [{:keys [db]} [proposal]] | ||
{:db (assoc db :wallet-connect/current-proposal proposal)})) | ||
|
||
(rf/reg-event-fx | ||
:wallet-connect/reset-current-session | ||
(fn [{:keys [db]}] | ||
{:db (dissoc db :wallet-connect/current-proposal)})) | ||
|
||
(rf/reg-event-fx | ||
:wallet-connect/pair | ||
(fn [{:keys [db]} [url]] | ||
(let [web3-wallet (get db :wallet-connect/web3-wallet)] | ||
{:fx [[:effects.wallet-connect/pair | ||
{:web3-wallet web3-wallet | ||
:url url | ||
:on-fail #(log/error "Failed to pair with dApp") | ||
:on-success #(log/info "dApp paired successfully")}]]}))) | ||
|
||
(rf/reg-event-fx | ||
:wallet-connect/approve-session | ||
(fn [{:keys [db]}] | ||
;; NOTE: hardcoding optimism for the base implementation | ||
(let [crosschain-ids [constants/optimism-crosschain-id] | ||
web3-wallet (get db :wallet-connect/web3-wallet) | ||
current-proposal (get db :wallet-connect/current-proposal) | ||
accounts (get-in db [:wallet :accounts]) | ||
;; NOTE: for now using the first account, but should be using the account selected by the | ||
;; user on the connection screen. The default would depend on where the connection started | ||
;; from: | ||
;; - 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) | ||
supported-namespaces (clj->js {:eip155 | ||
{:chains crosschain-ids | ||
:methods constants/wallet-connect-supported-methods | ||
:events constants/wallet-connect-supported-events | ||
:accounts [formatted-address]}})] | ||
{:fx [[:effects.wallet-connect/approve-session | ||
{:web3-wallet web3-wallet | ||
:proposal current-proposal | ||
:supported-namespaces supported-namespaces | ||
:on-success (fn [] | ||
(log/debug "Wallet Connect session approved") | ||
(rf/dispatch [:wallet-connect/reset-current-session])) | ||
:on-fail (fn [error] | ||
(log/error "Wallet Connect session approval failed" | ||
{:error error | ||
:event :wallet-connect/approve-session}) | ||
(rf/dispatch [:wallet-connect/reset-current-session]))}]]}))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters