mirror of
https://github.com/status-im/status-react.git
synced 2025-02-11 02:16:32 +00:00
parent
e690dbdf89
commit
38fc712f07
@ -506,7 +506,7 @@
|
|||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:network.ui/remove-network-confirmed
|
:network.ui/remove-network-confirmed
|
||||||
(fn [cofx [_ network]]
|
(fn [cofx [_ network]]
|
||||||
(network/remove-network cofx network)))
|
(network/remove-network cofx network [:navigate-back])))
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:network.ui/connect-network-pressed
|
:network.ui/connect-network-pressed
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
[status-im.utils.fx :as fx]
|
[status-im.utils.fx :as fx]
|
||||||
status-im.extensions.ethereum
|
status-im.extensions.ethereum
|
||||||
status-im.extensions.camera
|
status-im.extensions.camera
|
||||||
|
status-im.extensions.network
|
||||||
[status-im.extensions.map :as map]
|
[status-im.extensions.map :as map]
|
||||||
[status-im.utils.ethereum.tokens :as tokens]
|
[status-im.utils.ethereum.tokens :as tokens]
|
||||||
[status-im.utils.ethereum.core :as ethereum]
|
[status-im.utils.ethereum.core :as ethereum]
|
||||||
@ -550,6 +551,26 @@
|
|||||||
'store/clear-all
|
'store/clear-all
|
||||||
{:permissions [:read]
|
{:permissions [:read]
|
||||||
:data :store/clear-all}
|
:data :store/clear-all}
|
||||||
|
'network/add
|
||||||
|
{:permissions [:read]
|
||||||
|
:data :network/add
|
||||||
|
:arguments {:chain-id :number
|
||||||
|
:name :string
|
||||||
|
:url :string
|
||||||
|
:on-success? :event
|
||||||
|
:on-failure? :event}}
|
||||||
|
'network/select
|
||||||
|
{:permissions [:read]
|
||||||
|
:data :network/select
|
||||||
|
:arguments {:chain-id :number
|
||||||
|
:on-success? :event
|
||||||
|
:on-failure? :event}}
|
||||||
|
'network/remove
|
||||||
|
{:permissions [:read]
|
||||||
|
:data :network/remove
|
||||||
|
:arguments {:chain-id :number
|
||||||
|
:on-success? :event
|
||||||
|
:on-failure? :event}}
|
||||||
'http/get
|
'http/get
|
||||||
{:permissions [:read]
|
{:permissions [:read]
|
||||||
:data :http/get
|
:data :http/get
|
||||||
|
41
src/status_im/extensions/network.cljs
Normal file
41
src/status_im/extensions/network.cljs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
(ns status-im.extensions.network
|
||||||
|
(:require [status-im.utils.handlers :as handlers]
|
||||||
|
[status-im.i18n :as i18n]
|
||||||
|
[status-im.network.core :as network]))
|
||||||
|
|
||||||
|
(handlers/register-handler-fx
|
||||||
|
:extensions/network-on-success
|
||||||
|
(fn [cofx [_ on-success result]]
|
||||||
|
(when on-success (on-success {:value result}))))
|
||||||
|
|
||||||
|
(defn- network-id [extension-id chain-id]
|
||||||
|
(str extension-id "_" chain-id))
|
||||||
|
|
||||||
|
(handlers/register-handler-fx
|
||||||
|
:network/select
|
||||||
|
(fn [cofx [_ _ {:keys [chain-id on-success on-failure]}]]
|
||||||
|
(if-let [network-id (network/get-network-id-for-chain-id cofx chain-id)]
|
||||||
|
(network/connect cofx {:network-id network-id
|
||||||
|
:on-success (when on-success #(on-success {:value %}))
|
||||||
|
:on-failure (when on-failure #(on-failure {:value %}))})
|
||||||
|
(when on-failure (on-failure {:value (i18n/label :t/extensions-network-not-found)})))))
|
||||||
|
|
||||||
|
(handlers/register-handler-fx
|
||||||
|
:network/add
|
||||||
|
(fn [{:keys [db] :as cofx} [_ {extension-id :id} {:keys [chain-id name url on-success on-failure]}]]
|
||||||
|
(network/save cofx {:data {:name {:value name}
|
||||||
|
:url {:value url}
|
||||||
|
:network-id {:value chain-id}
|
||||||
|
:chain {:value :custom}}
|
||||||
|
:network-id (network-id extension-id chain-id)
|
||||||
|
:success-event (when on-success [:extensions/network-on-success on-success chain-id])
|
||||||
|
:on-failure (when on-failure #(on-failure {:value %}))
|
||||||
|
:chain-id-unique? false})))
|
||||||
|
|
||||||
|
(handlers/register-handler-fx
|
||||||
|
:network/remove
|
||||||
|
(fn [cofx [_ {extension-id :id} {:keys [chain-id on-success on-failure]}]]
|
||||||
|
(let [network-id (network-id extension-id chain-id)]
|
||||||
|
(if (network/get-network cofx network-id)
|
||||||
|
(network/remove-network cofx network-id (when on-success [:extensions/network-on-success on-success chain-id]))
|
||||||
|
(when on-failure (on-failure {:value (i18n/label :t/extensions-chain-id-not-found)}))))))
|
@ -53,9 +53,18 @@
|
|||||||
(map :error)
|
(map :error)
|
||||||
(not-any? identity)))
|
(not-any? identity)))
|
||||||
|
|
||||||
(defn new-network [random-id network-name upstream-url type network-id]
|
(defn get-network-id-for-chain-id [{:keys [db]} chain-id]
|
||||||
|
(let [networks (get-in db [:account/account :networks])
|
||||||
|
filtered (filter #(= chain-id (get-in % [1 :config :NetworkId])) networks)]
|
||||||
|
(first (keys filtered))))
|
||||||
|
|
||||||
|
(defn chain-id-available? [current-networks network]
|
||||||
|
(let [chain-id (get-in network [:config :NetworkId])]
|
||||||
|
(every? #(not= chain-id (get-in % [1 :config :NetworkId])) current-networks)))
|
||||||
|
|
||||||
|
(defn new-network [random-id network-name upstream-url type chain-id]
|
||||||
(let [data-dir (str "/ethereum/" (name type) "_rpc")
|
(let [data-dir (str "/ethereum/" (name type) "_rpc")
|
||||||
config {:NetworkId (or (when network-id (int network-id))
|
config {:NetworkId (or (when chain-id (int chain-id))
|
||||||
(ethereum/chain-keyword->chain-id type))
|
(ethereum/chain-keyword->chain-id type))
|
||||||
:DataDir data-dir
|
:DataDir data-dir
|
||||||
:UpstreamConfig {:Enabled true
|
:UpstreamConfig {:Enabled true
|
||||||
@ -68,6 +77,9 @@
|
|||||||
(let [network (get (:networks (:account/account db)) (:network db))]
|
(let [network (get (:networks (:account/account db)) (:network db))]
|
||||||
(ethereum/network->chain-keyword network)))
|
(ethereum/network->chain-keyword network)))
|
||||||
|
|
||||||
|
(defn get-network [{:keys [db]} network-id]
|
||||||
|
(get-in db [:account/account :networks network-id]))
|
||||||
|
|
||||||
(fx/defn set-input
|
(fx/defn set-input
|
||||||
[{:keys [db]} input-key value]
|
[{:keys [db]} input-key value]
|
||||||
{:db (-> db
|
{:db (-> db
|
||||||
@ -82,25 +94,31 @@
|
|||||||
(handler data cofx))))
|
(handler data cofx))))
|
||||||
|
|
||||||
(fx/defn save
|
(fx/defn save
|
||||||
[{{:network/keys [manage] :account/keys [account] :as db} :db
|
[{{:networks/keys [manage] :account/keys [account] :as db} :db
|
||||||
random-id-generator :random-id-generator :as cofx}
|
random-id-generator :random-id-generator :as cofx}
|
||||||
{:keys [data success-event on-success on-failure]}]
|
{:keys [data success-event on-success on-failure network-id chain-id-unique?]}]
|
||||||
(let [data (or data manage)]
|
(let [data (or data manage)]
|
||||||
(if (valid-manage? data)
|
(if (valid-manage? data)
|
||||||
(let [{:keys [name url chain network-id]} data
|
;; rename network-id from UI to chain-id
|
||||||
network (new-network (random-id-generator)
|
(let [{:keys [name url chain] chain-id :network-id} data
|
||||||
|
;; network-id overrides random id
|
||||||
|
network (new-network (or network-id (random-id-generator))
|
||||||
(:value name)
|
(:value name)
|
||||||
(:value url)
|
(:value url)
|
||||||
(:value chain)
|
(:value chain)
|
||||||
(:value network-id))
|
(:value chain-id))
|
||||||
new-networks (merge {(:id network) network} (:networks account))]
|
current-networks (:networks account)
|
||||||
|
new-networks (merge {(:id network) network} current-networks)]
|
||||||
|
(if (or (not chain-id-unique?)
|
||||||
|
(chain-id-available? current-networks network))
|
||||||
(fx/merge cofx
|
(fx/merge cofx
|
||||||
{:db (dissoc db :networks/manage)}
|
{:db (dissoc db :networks/manage)}
|
||||||
#(action-handler on-success (:id network) %)
|
#(action-handler on-success (:id network) %)
|
||||||
(accounts.update/account-update
|
(accounts.update/account-update
|
||||||
{:networks new-networks}
|
{:networks new-networks}
|
||||||
{:success-event success-event})))
|
{:success-event success-event}))
|
||||||
(action-handler on-failure))))
|
(action-handler on-failure "chain-id already defined" nil)))
|
||||||
|
(action-handler on-failure "invalid network parameters" nil))))
|
||||||
|
|
||||||
;; No edit functionality actually implemented
|
;; No edit functionality actually implemented
|
||||||
(fx/defn edit
|
(fx/defn edit
|
||||||
@ -218,12 +236,12 @@
|
|||||||
{:success-event [:accounts.update.callback/save-settings-success]}))
|
{:success-event [:accounts.update.callback/save-settings-success]}))
|
||||||
|
|
||||||
(fx/defn remove-network
|
(fx/defn remove-network
|
||||||
[{:keys [db now] :as cofx} network]
|
[{:keys [db now] :as cofx} network success-event]
|
||||||
(let [networks (dissoc (get-in db [:account/account :networks]) network)]
|
(let [networks (dissoc (get-in db [:account/account :networks]) network)]
|
||||||
(accounts.update/account-update cofx
|
(accounts.update/account-update cofx
|
||||||
{:networks networks
|
{:networks networks
|
||||||
:last-updated now}
|
:last-updated now}
|
||||||
{:success-event [:navigate-back]})))
|
{:success-event success-event})))
|
||||||
|
|
||||||
(fx/defn save-network
|
(fx/defn save-network
|
||||||
[cofx]
|
[cofx]
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
(ns status-im.test.models.network
|
(ns status-im.test.models.network
|
||||||
(:require [cljs.test :refer-macros [deftest is testing]]
|
(:require [cljs.test :refer-macros [deftest is testing]]
|
||||||
[status-im.network.core :as model]))
|
[status-im.network.core :as model]
|
||||||
|
[reagent.core :as reagent]))
|
||||||
|
|
||||||
(deftest valid-rpc-url-test
|
(deftest valid-rpc-url-test
|
||||||
(testing "nil?"
|
(testing "nil?"
|
||||||
@ -96,7 +97,7 @@
|
|||||||
:error false}}}}
|
:error false}}}}
|
||||||
:url "http://valid.com")))))
|
:url "http://valid.com")))))
|
||||||
|
|
||||||
(deftest save
|
(deftest not-save-invalid-url
|
||||||
(testing "it does not save a network with an invalid url"
|
(testing "it does not save a network with an invalid url"
|
||||||
(is (nil? (model/save {:random-id-generator (constantly "random")
|
(is (nil? (model/save {:random-id-generator (constantly "random")
|
||||||
:db {:networks/manage {:url {:value "wrong"}
|
:db {:networks/manage {:url {:value "wrong"}
|
||||||
@ -104,3 +105,66 @@
|
|||||||
:name {:value "empty"}}
|
:name {:value "empty"}}
|
||||||
:account/account {}}}
|
:account/account {}}}
|
||||||
{})))))
|
{})))))
|
||||||
|
|
||||||
|
(deftest save-valid-network
|
||||||
|
(testing "save a valid network"
|
||||||
|
(is (some? (model/save {:random-id-generator (constantly "random")
|
||||||
|
:db {:networks/manage {:url {:value "http://valid.com"}
|
||||||
|
:chain {:value "mainnet"}
|
||||||
|
:name {:value "valid"}}
|
||||||
|
:account/account {}}}
|
||||||
|
{})))))
|
||||||
|
|
||||||
|
(deftest not-save-non-unique-id
|
||||||
|
(testing "it does not save a network with network-id already defined"
|
||||||
|
(let [failure (reagent/atom false)]
|
||||||
|
(do (model/save {:random-id-generator (constantly "random")
|
||||||
|
:db {:networks/manage {:url {:value "http://valid.com"}
|
||||||
|
:chain {:value :mainnet}
|
||||||
|
:name {:value "valid"}}
|
||||||
|
:account/account {:networks {"randomid"
|
||||||
|
{:id "randomid"
|
||||||
|
:name "network-name"
|
||||||
|
:config {:NetworkId 1
|
||||||
|
:DataDir "/ethereum/mainnet_rpc"
|
||||||
|
:UpstreamConfig {:Enabled true
|
||||||
|
:URL "upstream-url"}}}}}}}
|
||||||
|
{:chain-id-unique? true
|
||||||
|
:on-failure #(reset! failure true)})
|
||||||
|
(is @failure)))))
|
||||||
|
|
||||||
|
(deftest save-valid-network-with-unique-check
|
||||||
|
(testing "save a valid network with network-id not already defined"
|
||||||
|
(is (some? (model/save {:random-id-generator (constantly "random")
|
||||||
|
:db {:networks/manage {:url {:value "http://valid.com"}
|
||||||
|
:chain {:value :mainnet}
|
||||||
|
:name {:value "valid"}}
|
||||||
|
:account/account {:networks {"randomid"
|
||||||
|
{:id "randomid"
|
||||||
|
:name "network-name"
|
||||||
|
:config {:NetworkId 3
|
||||||
|
:DataDir "/ethereum/mainnet_rpc"
|
||||||
|
:UpstreamConfig {:Enabled true
|
||||||
|
:URL "upstream-url"}}}}}}}
|
||||||
|
{:chain-id-unique? true})))))
|
||||||
|
|
||||||
|
(deftest save-with-id-override
|
||||||
|
(testing "save a valid network with id override"
|
||||||
|
(let [result (model/save {:random-id-generator (constantly "random")
|
||||||
|
:db {:networks/manage {:url {:value "http://valid.com"}
|
||||||
|
:chain {:value :mainnet}
|
||||||
|
:name {:value "valid"}}
|
||||||
|
:account/account {}}}
|
||||||
|
{:network-id "override"})]
|
||||||
|
(is (some? (get-in result [:db :account/account :networks "override"]))))))
|
||||||
|
|
||||||
|
(deftest get-network-id-for-chain-id
|
||||||
|
(testing "get the first network id for the given chain-id"
|
||||||
|
(is (= "randomid" (model/get-network-id-for-chain-id {:db {:account/account {:networks {"randomid"
|
||||||
|
{:id "randomid"
|
||||||
|
:name "network-name"
|
||||||
|
:config {:NetworkId 1
|
||||||
|
:DataDir "/ethereum/mainnet_rpc"
|
||||||
|
:UpstreamConfig {:Enabled true
|
||||||
|
:URL "upstream-url"}}}}}}}
|
||||||
|
1)))))
|
@ -960,6 +960,8 @@
|
|||||||
"dapps-permissions": "DApp permissions",
|
"dapps-permissions": "DApp permissions",
|
||||||
"revoke-access": "Revoke access",
|
"revoke-access": "Revoke access",
|
||||||
"extensions-camera-send-picture": "Send picture",
|
"extensions-camera-send-picture": "Send picture",
|
||||||
|
"extensions-network-not-found": "network not found for the given chain-id",
|
||||||
|
"extensions-chain-id-not-found": "chain-id not found or was not created by this extension",
|
||||||
"mobile-syncing-sheet-title": "Sync using Mobile data",
|
"mobile-syncing-sheet-title": "Sync using Mobile data",
|
||||||
"mobile-syncing-sheet-details": "Status tends to use a lot of data when syncing chats. You can choose not to sync when on mobile network",
|
"mobile-syncing-sheet-details": "Status tends to use a lot of data when syncing chats. You can choose not to sync when on mobile network",
|
||||||
"mobile-network-continue-syncing": "Continue syncing",
|
"mobile-network-continue-syncing": "Continue syncing",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user