🥅 Filter connected dapps based on testnet mode, reject proposals and requests gracefully (#20799)

* 🥅 Filter connected dapps based on testnet mode

- Fixes #20794

* 🥅 Remove map, just filter

* 💿 Rebase

*  Remove greedy fetch

* 🙅‍♀️ Properly reject proposals and requests

* 🎗️ Remove newline and move `set`

- `set` was applied at the wrong place here

* ✏️ Address review comments

* 👀 Read proposal to reject from state

* ◀️ Bring back network filtering

* 🧹 Cleanup

* ✏️ Move comment around

* 🎣 Use filter operable accounts helper

*  Add back events deleted during rebase

* 🧰 Fix Issue 2, Testnet sessions not visible

* 🖊️ Fix lint

* 🔗 Make testnet filtering more explicit

* 🥢 Use union instead of two subsets call

* ✏️ Fix lint

* 🔇 Undo changes that creeped in an unrelated ns
This commit is contained in:
Shivek Khurana 2024-07-26 14:54:53 +02:00 committed by GitHub
parent 2e9fa22e4f
commit a45991b6dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 56 additions and 14 deletions

View File

@ -291,6 +291,7 @@
(def ^:const wallet-connect-session-delete-event "session_delete")
(def ^:const wallet-connect-user-rejected-error-key "USER_REJECTED")
(def ^:const wallet-connect-user-disconnected-reason-key "USER_DISCONNECTED")
(def ^:const wallet-connect-user-rejected-chains-error-key "USER_REJECTED_CHAINS")
(def ^:const transaction-pending-type-wallet-connect-transfer "WalletConnectTransfer")

View File

@ -41,7 +41,6 @@
(not watch-only?))
{:icon-name :i/dapps
:on-press #(rf/dispatch [:navigate-to :screen/wallet.connected-dapps])})
{:content-type :account-switcher
:customization-color color
:on-press #(on-dapps-press switcher-type)

View File

@ -84,10 +84,10 @@
[]
(let [{:keys [bottom]} (safe-area/get-insets)
{:keys [color] :as wallet-account} (rf/sub [:wallet/current-viewing-account])
customization-color (rf/sub [:profile/customization-color])
sessions (rf/sub
[:wallet-connect/sessions-for-current-account])
theme (quo.theme/use-theme)
customization-color (rf/sub [:profile/customization-color])]
[:wallet-connect/sessions-for-current-account-and-networks])
theme (quo.theme/use-theme)]
[rn/view {:flex 1}
[header
{:title (i18n/label :t/connected-dapps)

View File

@ -134,6 +134,7 @@
:iconUrl (get-in session [:peer :metadata :icons 0])
:url (get-in session [:peer :metadata :url])
:accounts (get-in session [:namespaces :eip155 :accounts])
:chains (get-in session [:namespaces :eip155 :chains])
:disconnected false})
(defn filter-operable-accounts

View File

@ -79,7 +79,6 @@
(if (and (not-empty session-networks) required-networks-supported?)
{:db (update db
:wallet-connect/current-proposal assoc
:response-sent? false
:request proposal
:session-networks session-networks
:address (or current-viewing-address
@ -89,23 +88,30 @@
:fx [[:dispatch
[:open-modal :screen/wallet.wallet-connect-session-proposal]]]}
{:fx [[:dispatch
[:wallet-connect/session-networks-unsupported proposal]]]}))))
[:wallet-connect/show-session-networks-unsupported-toast proposal]]
[:dispatch
[:wallet-connect/reject-session-proposal proposal]]]}))))
(rf/reg-event-fx
:wallet-connect/session-networks-unsupported
:wallet-connect/show-session-networks-unsupported-toast
(fn [{:keys [db]} [proposal]]
(let [{:keys [name]} (wallet-connect-core/get-session-dapp-metadata proposal)]
(let [{:keys [name url]} (wallet-connect-core/get-session-dapp-metadata proposal)]
{:fx [[:dispatch
[:toasts/upsert
{:type :negative
:theme (:theme db)
:text (i18n/label :t/wallet-connect-networks-not-supported {:dapp name})}]]]})))
:text (i18n/label :t/wallet-connect-networks-not-supported
{:dapp (wallet-connect-core/compute-dapp-name name url)})}]]]})))
(rf/reg-event-fx
:wallet-connect/on-session-request
(fn [{:keys [db]} [event]]
(when (wallet-connect-core/event-should-be-handled? db event)
{:fx [[:dispatch [:wallet-connect/process-session-request event]]]})))
(if (wallet-connect-core/event-should-be-handled? db event)
{:fx [[:dispatch [:wallet-connect/process-session-request event]]]}
{:fx [[:dispatch
[:wallet-connect/send-response
{:error (wallet-connect/get-sdk-error
constants/wallet-connect-user-rejected-chains-error-key)}]]]})))
(rf/reg-event-fx
:wallet-connect/on-session-delete

View File

@ -146,10 +146,25 @@
[:dispatch [:wallet-connect/dismiss-request-modal]]]}))
(rf/reg-event-fx
:wallet-connect/reject-session-proposal
:wallet-connect/dismiss-request-modal
(fn [{:keys [db]} _]
(let [screen (-> db
(get-in [:wallet-connect/current-request :event])
wallet-connect-core/get-request-method
wallet-connect-core/method-to-screen)]
{:fx [[:dispatch [:dismiss-modal screen]]]})))
(rf/reg-event-fx
:wallet-connect/finish-session-request
(fn [_ [result]]
{:fx [[:dispatch [:wallet-connect/send-response {:result result}]]
[:dispatch [:wallet-connect/dismiss-request-modal]]]}))
(rf/reg-event-fx
:wallet-connect/reject-session-proposal
(fn [{:keys [db]} [proposal]]
(let [web3-wallet (get db :wallet-connect/web3-wallet)
{:keys [request response-sent?]} (:wallet-connect/current-proposal db)]
{:keys [request response-sent?]} (or proposal (:wallet-connect/current-proposal db))]
{:fx [(when-not response-sent?
[:effects.wallet-connect/reject-session-proposal
{:web3-wallet web3-wallet

View File

@ -1,6 +1,8 @@
(ns status-im.subs.wallet.wallet-connect
(:require [clojure.string :as string]
(:require [clojure.set :as set]
[clojure.string :as string]
[re-frame.core :as rf]
[status-im.constants :as constants]
[status-im.contexts.wallet.common.utils :as wallet-utils]
[status-im.contexts.wallet.wallet-connect.core :as wallet-connect-core]
[status-im.contexts.wallet.wallet-connect.transactions :as transactions]
@ -58,6 +60,24 @@
(some #(string/includes? % address) accounts))
sessions)))
(rf/reg-sub
:wallet-connect/sessions-for-current-account-and-networks
:<- [:wallet-connect/sessions-for-current-account]
:<- [:profile/test-networks-enabled?]
(fn [[sessions testnet-mode?]]
(filter
(fn [{:keys [chains]}]
(let [chain-ids (set (map (fn [chain]
(-> chain
(string/split ":")
second
js/parseInt))
chains))]
(if testnet-mode?
(set/subset? chain-ids (set/union constants/sepolia-chain-ids constants/goerli-chain-ids))
(set/subset? chain-ids constants/mainnet-chain-ids))))
sessions)))
(rf/reg-sub
:wallet-connect/chain-id
:<- [:wallet-connect/current-request]