chore(wallet): refactor keypairs data in app-db (#20469)

This commit refactors the keypairs data in app-db to save them by indexed by key-uid which will help to fetch, update, and delete easily instead of using filter and some methods every time we need to perform those operations.

Signed-off-by: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com>
This commit is contained in:
Mohamed Javid 2024-06-21 20:47:26 +05:30 committed by GitHub
parent f509d132f3
commit fb84f105db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 207 additions and 204 deletions

View File

@ -1,24 +1,5 @@
(ns status-im.contexts.settings.wallet.data-store) (ns status-im.contexts.settings.wallet.data-store)
(defn extract-keypair-name
[db key-uids-set]
(when (= (count key-uids-set) 1)
(let [key-uid (first key-uids-set)
keypairs (get-in db [:wallet :keypairs])]
(->> (filter #(= (:key-uid %) key-uid) keypairs)
first
:name))))
(defn update-keypair
[keypairs key-uid update-fn]
(mapcat (fn [keypair]
(if (= (keypair :key-uid) key-uid)
(if-let [updated (update-fn keypair)]
[updated]
[])
[keypair]))
keypairs))
(defn make-accounts-fully-operable (defn make-accounts-fully-operable
"Updates accounts to be fully operable based on specified key-uids and an optional operable condition. "Updates accounts to be fully operable based on specified key-uids and an optional operable condition.
@ -32,10 +13,10 @@
- A new map with accounts updated to be fully operable where the conditions are met." - A new map with accounts updated to be fully operable where the conditions are met."
[{:keys [accounts key-uids-set operable-condition]}] [{:keys [accounts key-uids-set operable-condition]}]
(reduce-kv (reduce-kv
(fn [acc k account] (fn [acc k {:keys [key-uid] :as account}]
(if (and (contains? key-uids-set (:key-uid account)) (if (and (contains? key-uids-set key-uid)
(or (nil? operable-condition) (or (nil? operable-condition)
(= (keyword (:operable account)) operable-condition))) (= (:operable account) operable-condition)))
(assoc acc k (assoc account :operable :fully)) (assoc acc k (assoc account :operable :fully))
(assoc acc k account))) (assoc acc k account)))
{} {}
@ -44,7 +25,7 @@
(defn- make-keypairs-accounts-fully-operable (defn- make-keypairs-accounts-fully-operable
[accounts operable-condition] [accounts operable-condition]
(map (fn [account] (map (fn [account]
(if (or (nil? operable-condition) (= (keyword (:operable account)) operable-condition)) (if (or (nil? operable-condition) (= (:operable account) operable-condition))
(assoc account :operable :fully) (assoc account :operable :fully)
account)) account))
accounts)) accounts))
@ -53,27 +34,30 @@
"Updates keypairs' accounts to be fully operable based on specified key-uids and an optional operable condition. "Updates keypairs' accounts to be fully operable based on specified key-uids and an optional operable condition.
Parameters: Parameters:
- :keypairs (seq): A sequence of keypair maps. - :keypairs (map): A map of keypair key-uid to keypair details.
- :key-uids-set (set): A set of key-uids that need to be checked and updated. - :key-uids-set (set): A set of key-uids that need to be checked and updated.
- :operable-condition (keyword, optional): The condition that the keypair's accounts' operability must meet to be updated. - :operable-condition (keyword, optional): The condition that the keypair's accounts' operability must meet to be updated.
If nil or not provided, the function will update all keypairs' accounts. If nil or not provided, the function will update all keypairs' accounts.
Returns: Returns:
- A new sequence with keypairs updated to be fully operable where the conditions are met." - A new map with keypairs updated to be fully operable where the conditions are met."
[{:keys [keypairs key-uids-set operable-condition]}] [{:keys [keypairs key-uids-set operable-condition]}]
(map (fn [keypair] (reduce-kv (fn [acc k keypair]
(if (contains? key-uids-set (:key-uid keypair)) (if (contains? key-uids-set k)
(-> keypair (assoc acc
(update :accounts #(make-keypairs-accounts-fully-operable % operable-condition)) k
(assoc :lowest-operability :fully)) (-> keypair
keypair)) (update :accounts make-keypairs-accounts-fully-operable operable-condition)
keypairs)) (assoc :lowest-operability :fully)))
(assoc acc k keypair)))
{}
keypairs))
(defn map-addresses-to-key-uids (defn get-keypair-key-uids-set-from-addresses
[db addresses] [db addresses]
(reduce (fn [key-uid-set address] (reduce (fn [key-uid-set address]
(if-let [account (get-in db [:wallet :accounts address])] (if-let [account-key-uid (get-in db [:wallet :accounts address :key-uid])]
(conj key-uid-set (:key-uid account)) (conj key-uid-set account-key-uid)
key-uid-set)) key-uid-set))
#{} #{}
addresses)) addresses))

View File

@ -10,9 +10,7 @@
(rf/reg-event-fx (rf/reg-event-fx
:wallet/rename-keypair-success :wallet/rename-keypair-success
(fn [{:keys [db]} [key-uid name]] (fn [{:keys [db]} [key-uid name]]
{:db (update-in db {:db (update-in db [:wallet :keypairs key-uid] assoc :name name)
[:wallet :keypairs]
#(data-store/update-keypair % key-uid (fn [keypair] (assoc keypair :name name))))
:fx [[:dispatch [:navigate-back]] :fx [[:dispatch [:navigate-back]]
[:dispatch [:dispatch
[:toasts/upsert [:toasts/upsert
@ -49,9 +47,7 @@
(rf/reg-event-fx :wallet/remove-keypair-success (rf/reg-event-fx :wallet/remove-keypair-success
(fn [{:keys [db]} [key-uid]] (fn [{:keys [db]} [key-uid]]
{:db (update-in db {:db (update-in db [:wallet :keypairs] dissoc key-uid)
[:wallet :keypairs]
#(data-store/update-keypair % key-uid (fn [_] nil)))
:fx [[:dispatch [:hide-bottom-sheet]] :fx [[:dispatch [:hide-bottom-sheet]]
[:dispatch [:dispatch
[:toasts/upsert [:toasts/upsert
@ -71,8 +67,11 @@
(defn make-keypairs-accounts-fully-operable (defn make-keypairs-accounts-fully-operable
[{:keys [db]} [key-uids-to-update]] [{:keys [db]} [key-uids-to-update]]
(let [key-uids-set (set key-uids-to-update) (let [key-uids-set (set key-uids-to-update)
keypair-name (data-store/extract-keypair-name db key-uids-set)] single-keypair-to-update? (= (count key-uids-to-update) 1)
keypair-name (when single-keypair-to-update?
(let [key-uid (first key-uids-set)]
(get-in db [:wallet :keypairs key-uid :name])))]
{:db (-> {:db (->
db db
(update-in [:wallet :accounts] (update-in [:wallet :accounts]
@ -85,7 +84,7 @@
[:toasts/upsert [:toasts/upsert
{:type :positive {:type :positive
:theme :dark :theme :dark
:text (if (= (count key-uids-to-update) 1) :text (if single-keypair-to-update?
(i18n/label :t/key-pair-imported-successfully {:name keypair-name}) (i18n/label :t/key-pair-imported-successfully {:name keypair-name})
(i18n/label :t/key-pairs-successfully-imported (i18n/label :t/key-pairs-successfully-imported
{:count (count key-uids-to-update)}))}]]]})) {:count (count key-uids-to-update)}))}]]]}))
@ -231,7 +230,7 @@
(defn make-partially-operable-accounts-fully-operable-success (defn make-partially-operable-accounts-fully-operable-success
[{:keys [db]} [addresses]] [{:keys [db]} [addresses]]
(let [key-uids-to-update (data-store/map-addresses-to-key-uids db addresses)] (let [key-uids-to-update (data-store/get-keypair-key-uids-set-from-addresses db addresses)]
{:db (-> db {:db (-> db
(update-in [:wallet :accounts] (update-in [:wallet :accounts]
#(data-store/make-accounts-fully-operable {:accounts % #(data-store/make-accounts-fully-operable {:accounts %

View File

@ -1,104 +1,100 @@
(ns status-im.contexts.settings.wallet.events-test (ns status-im.contexts.settings.wallet.events-test
(:require (:require
[cljs.test :refer-macros [deftest is testing]] [cljs.test :refer-macros [deftest is]]
matcher-combinators.test matcher-combinators.test
[native-module.core :as native-module] [native-module.core :as native-module]
[status-im.contexts.settings.wallet.events :as sut] [status-im.contexts.settings.wallet.events :as sut]
[utils.security.core :as security])) [utils.security.core :as security]))
(def mock-key-uid "key-1") (def test-profile {:key-uid "test-key-uid"})
(defn mock-db (def test-keypair-key-uid "key-1")
[keypairs accounts]
{:wallet {:keypairs keypairs
:accounts accounts}
:profile/profile {:key-uid "test-key-uid"}})
(deftest rename-keypair-test (deftest rename-keypair-test
(let [new-keypair-name "key pair new" (let [new-keypair-name "key pair new"
cofx {:db {}}] cofx {:db {}}
(testing "rename-keypair" expected {:fx [[:json-rpc/call
(let [expected {:fx [[:json-rpc/call [{:method "accounts_updateKeypairName"
[{:method "accounts_updateKeypairName" :params [test-keypair-key-uid new-keypair-name]
:params [mock-key-uid new-keypair-name] :on-success [:wallet/rename-keypair-success test-keypair-key-uid
:on-success [:wallet/rename-keypair-success mock-key-uid new-keypair-name]
new-keypair-name] :on-error fn?}]]]}]
:on-error fn?}]]]}] (is (match? expected
(is (match? expected (sut/rename-keypair cofx
(sut/rename-keypair cofx [{:key-uid test-keypair-key-uid
[{:key-uid mock-key-uid :keypair-name new-keypair-name}])))))
:keypair-name new-keypair-name}])))))))
(deftest get-keypair-export-connection-test (deftest get-keypair-export-connection-test
(let [cofx {:db (mock-db [] {})} (let [cofx {:db {:profile/profile test-profile}}
sha3-pwd "test-password" sha3-pwd "test-password"
user-key-uid "test-key-uid" user-key-uid "test-key-uid"
callback (fn [connect-string] (println "callback" connect-string))] callback (fn [connect-string] (println "callback" connect-string))
(testing "get-keypair-export-connection" expected {:fx [[:effects.syncing/export-keypairs-keystores
(let [expected {:fx [[:effects.syncing/export-keypairs-keystores
{:key-uid user-key-uid {:key-uid user-key-uid
:sha3-pwd sha3-pwd :sha3-pwd sha3-pwd
:keypair-key-uid mock-key-uid :keypair-key-uid test-keypair-key-uid
:on-success fn? :on-success fn?
:on-fail fn?}]]}] :on-fail fn?}]]}]
(is (match? expected (is (match?
(sut/get-keypair-export-connection expected
cofx (sut/get-keypair-export-connection
[{:sha3-pwd sha3-pwd :keypair-key-uid mock-key-uid :callback callback}]))))))) cofx
[{:sha3-pwd sha3-pwd :keypair-key-uid test-keypair-key-uid :callback callback}])))))
(deftest remove-keypair-test (deftest remove-keypair-test
(let [cofx {:db {}}] (let [cofx {:db {}}
(testing "remove-keypair" expected {:fx [[:json-rpc/call
(let [expected {:fx [[:json-rpc/call [{:method "accounts_deleteKeypair"
[{:method "accounts_deleteKeypair" :params [test-keypair-key-uid]
:params [mock-key-uid] :on-success [:wallet/remove-keypair-success test-keypair-key-uid]
:on-success [:wallet/remove-keypair-success mock-key-uid] :on-error fn?}]]]}]
:on-error fn?}]]]}] (is (match? expected
(is (match? expected (sut/remove-keypair cofx [test-keypair-key-uid])))))
(sut/remove-keypair cofx [mock-key-uid])))))))
(deftest make-keypairs-accounts-fully-operable-test (deftest make-keypairs-accounts-fully-operable-test
(let [db (mock-db [{:key-uid mock-key-uid (let [db {:wallet {:keypairs {test-keypair-key-uid {:key-uid
:lowest-operability :no test-keypair-key-uid
:accounts [{:key-uid mock-key-uid :operable "no"}]}] :lowest-operability :no
{"0x1" {:key-uid mock-key-uid :operable "no"}}) :accounts
key-uids-to-update [mock-key-uid]] [{:key-uid
(testing "make-keypairs-accounts-fully-operable" test-keypair-key-uid
(let [effects (sut/make-keypairs-accounts-fully-operable {:db db} [key-uids-to-update]) :operable :no}]}}
result-db (:db effects) :accounts {"0x1" {:key-uid test-keypair-key-uid
updated-keypair (some #(when (= (:key-uid %) mock-key-uid) %) :operable :no}}}
(get-in result-db [:wallet :keypairs])) :profile/profile test-profile}
updated-account (get-in result-db [:wallet :accounts "0x1"])] key-uids-to-update [test-keypair-key-uid]
(is (= (keyword (-> updated-keypair :accounts first :operable)) :fully)) effects (sut/make-keypairs-accounts-fully-operable {:db db} [key-uids-to-update])
(is (= (keyword (:operable updated-account)) :fully)) result-db (:db effects)
(is (= (:lowest-operability updated-keypair) :fully)))))) updated-keypair (get-in result-db [:wallet :keypairs test-keypair-key-uid])
updated-account (get-in result-db [:wallet :accounts "0x1"])]
(is (= (-> updated-keypair :accounts first :operable) :fully))
(is (= (:operable updated-account) :fully))
(is (= (:lowest-operability updated-keypair) :fully))))
(deftest connection-string-for-import-keypair-test (deftest connection-string-for-import-keypair-test
(let [cofx {:db (mock-db [] {})} (let [cofx {:db {:profile/profile test-profile}}
sha3-pwd "test-password" sha3-pwd "test-password"
user-key-uid "test-key-uid" user-key-uid "test-key-uid"
connection-string "test-connection-string"] connection-string "test-connection-string"
(testing "connection-string-for-import-keypair" expected {:fx [[:effects.syncing/import-keypairs-keystores
(let [expected {:fx [[:effects.syncing/import-keypairs-keystores {:key-uid user-key-uid
{:key-uid user-key-uid :sha3-pwd sha3-pwd
:sha3-pwd sha3-pwd :keypairs-key-uids [test-keypair-key-uid]
:keypairs-key-uids [mock-key-uid] :connection-string connection-string
:connection-string connection-string :on-success fn?
:on-success fn? :on-fail fn?}]]}]
:on-fail fn?}]]}] (is (match? expected
(is (match? expected (sut/connection-string-for-import-keypair cofx
(sut/connection-string-for-import-keypair cofx [{:sha3-pwd sha3-pwd
[{:sha3-pwd sha3-pwd :keypairs-key-uids [test-keypair-key-uid]
:keypairs-key-uids [mock-key-uid] :connection-string
:connection-string connection-string}])))))
connection-string}])))))))
(deftest success-keypair-qr-scan-test (deftest success-keypair-qr-scan-test
(let [connection-string "valid-connection-string" (let [connection-string "valid-connection-string"
keypairs-key-uids ["keypair-uid"]] keypairs-key-uids [test-keypair-key-uid]
(testing "success-keypair-qr-scan" effects (sut/success-keypair-qr-scan nil [connection-string keypairs-key-uids])
(let [effects (sut/success-keypair-qr-scan nil [connection-string keypairs-key-uids]) fx (:fx effects)]
fx (:fx effects)] (is (some? fx))))
(is (some? fx))))))
(deftest wallet-validate-seed-phrase-test (deftest wallet-validate-seed-phrase-test
(let [cofx {:db {}} (let [cofx {:db {}}

View File

@ -70,7 +70,15 @@
{:keys [address path watch-only?]} (rf/sub [:wallet/current-viewing-account]) {:keys [address path watch-only?]} (rf/sub [:wallet/current-viewing-account])
{keypair-name :name {keypair-name :name
keypair-type :type} (rf/sub [:wallet/current-viewing-account-keypair]) keypair-type :type} (rf/sub [:wallet/current-viewing-account-keypair])
networks (rf/sub [:wallet/network-preference-details])] networks (rf/sub [:wallet/network-preference-details])
origin-type (case keypair-type
:seed
:recovery-phrase
:key
:private-key
:default-keypair)]
[rn/scroll-view [rn/scroll-view
{:style style/about-tab {:style style/about-tab
:content-container-style {:padding-bottom (+ constants/floating-shell-button-height 8)}} :content-container-style {:padding-bottom (+ constants/floating-shell-button-height 8)}}
@ -89,7 +97,7 @@
:on-press #(rf/dispatch [:show-bottom-sheet {:content about-options}])}] :on-press #(rf/dispatch [:show-bottom-sheet {:content about-options}])}]
(when (not watch-only?) (when (not watch-only?)
[quo/account-origin [quo/account-origin
{:type (if (= keypair-type "seed") :recovery-phrase :default-keypair) {:type origin-type
:stored :on-device :stored :on-device
:profile-picture (profile.utils/photo profile) :profile-picture (profile.utils/photo profile)
:customization-color customization-color :customization-color customization-color

View File

@ -5,6 +5,7 @@
[status-im.contexts.wallet.add-account.create-account.utils :as create-account.utils] [status-im.contexts.wallet.add-account.create-account.utils :as create-account.utils]
[status-im.contexts.wallet.data-store :as data-store] [status-im.contexts.wallet.data-store :as data-store]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[utils.collection]
[utils.re-frame :as rf] [utils.re-frame :as rf]
[utils.security.core :as security] [utils.security.core :as security]
[utils.transforms :as transforms])) [utils.transforms :as transforms]))
@ -12,9 +13,11 @@
(defn get-keypairs-success (defn get-keypairs-success
[{:keys [db]} [keypairs]] [{:keys [db]} [keypairs]]
(let [parsed-keypairs (data-store/rpc->keypairs keypairs) (let [parsed-keypairs (data-store/rpc->keypairs keypairs)
default-key-uid (:key-uid (some #(when (= (:type %) :profile) %) parsed-keypairs))] default-key-uid (->> parsed-keypairs
(some #(when (= (:type %) :profile) %))
:key-uid)]
{:db (-> db {:db (-> db
(assoc-in [:wallet :keypairs] parsed-keypairs) (assoc-in [:wallet :keypairs] (utils.collection/index-by :key-uid parsed-keypairs))
(assoc-in [:wallet :ui :create-account :selected-keypair-uid] default-key-uid))})) (assoc-in [:wallet :ui :create-account :selected-keypair-uid] default-key-uid))}))
(rf/reg-event-fx :wallet/get-keypairs-success get-keypairs-success) (rf/reg-event-fx :wallet/get-keypairs-success get-keypairs-success)
@ -41,10 +44,10 @@
(defn seed-phrase-validated (defn seed-phrase-validated
[{:keys [db]} [seed-phrase key-uid on-error]] [{:keys [db]} [seed-phrase key-uid on-error]]
(let [keypair-already-added? (->> db (let [keypair-already-added? (-> db
:wallet :wallet
:keypairs :keypairs
(some #(= key-uid (:key-uid %))))] (contains? key-uid))]
(if keypair-already-added? (if keypair-already-added?
(do (do
(on-error) (on-error)

View File

@ -73,7 +73,7 @@
[] []
(let [compressed-key (rf/sub [:profile/compressed-key]) (let [compressed-key (rf/sub [:profile/compressed-key])
customization-color (rf/sub [:profile/customization-color]) customization-color (rf/sub [:profile/customization-color])
keypairs (rf/sub [:wallet/keypairs]) keypairs (rf/sub [:wallet/keypairs-list])
selected-keypair (rf/sub [:wallet/selected-keypair-uid]) selected-keypair (rf/sub [:wallet/selected-keypair-uid])
profile-picture (rf/sub [:profile/image]) profile-picture (rf/sub [:profile/image])
[selected-key-uid set-selected-key-uid] (rn/use-state selected-keypair)] [selected-key-uid set-selected-key-uid] (rn/use-state selected-keypair)]

View File

@ -529,10 +529,9 @@
:wallet/process-keypair-from-backup :wallet/process-keypair-from-backup
(fn [{:keys [db]} [{:keys [backedUpKeypair]}]] (fn [{:keys [db]} [{:keys [backedUpKeypair]}]]
(let [{:keys [key-uid accounts]} backedUpKeypair (let [{:keys [key-uid accounts]} backedUpKeypair
keypairs-db (get-in db [:wallet :keypairs]) updated-keypairs (assoc-in db
updated-keypairs (-> (filter #(not= (:key-uid %) key-uid) keypairs-db) [:wallet :keypairs key-uid]
(conj backedUpKeypair) (data-store/rpc->keypair backedUpKeypair))
data-store/rpc->keypairs)
accounts-fx (mapv (fn [{:keys [chat] :as account}] accounts-fx (mapv (fn [{:keys [chat] :as account}]
;; We exclude the chat account from the profile keypair ;; We exclude the chat account from the profile keypair
;; for fetching the assets ;; for fetching the assets

View File

@ -199,6 +199,12 @@
:<- [:wallet] :<- [:wallet]
:-> :keypairs) :-> :keypairs)
(rf/reg-sub
:wallet/keypairs-list
:<- [:wallet]
(fn [{:keys [keypairs]}]
(vals keypairs)))
(rf/reg-sub (rf/reg-sub
:wallet/selected-keypair-uid :wallet/selected-keypair-uid
:<- [:wallet/create-account] :<- [:wallet/create-account]
@ -209,19 +215,15 @@
:<- [:wallet/keypairs] :<- [:wallet/keypairs]
:<- [:wallet/selected-keypair-uid] :<- [:wallet/selected-keypair-uid]
(fn [[keypairs selected-keypair-uid]] (fn [[keypairs selected-keypair-uid]]
(some #(when (= (:key-uid %) selected-keypair-uid) (get keypairs selected-keypair-uid)))
%)
keypairs)))
(rf/reg-sub (rf/reg-sub
:wallet/selected-primary-keypair? :wallet/selected-primary-keypair?
:<- [:wallet/keypairs] :<- [:wallet/keypairs]
:<- [:wallet/selected-keypair-uid] :<- [:wallet/selected-keypair-uid]
(fn [[keypairs selected-keypair-uid]] (fn [[keypairs selected-keypair-uid]]
(let [primary-keypair-uid (->> keypairs (= (get-in keypairs [selected-keypair-uid :type])
(some #(when (= (:type %) "profile") %)) :profile)))
(:key-uid))]
(= selected-keypair-uid primary-keypair-uid))))
(rf/reg-sub (rf/reg-sub
:wallet/selected-networks->chain-ids :wallet/selected-networks->chain-ids
@ -264,29 +266,29 @@
(rf/reg-sub (rf/reg-sub
:wallet/settings-keypairs-accounts :wallet/settings-keypairs-accounts
:<- [:wallet/keypairs] :<- [:wallet/keypairs-list]
(fn [keypairs [_ format-options]] (fn [keypairs [_ format-options]]
(let [grouped-keypairs (group-by :lowest-operability keypairs) (reduce
operable-keypair-ids (->> (concat (:fully grouped-keypairs) (fn [acc {:keys [accounts name type key-uid lowest-operability]}]
(:partially grouped-keypairs)) (if (= lowest-operability :no)
(map :key-uid) (update acc
(into #{})) :missing
missing-keypair-ids (->> (map :key-uid (:no grouped-keypairs)) conj
(into #{}))] {:type type
{:operable (->> keypairs :name name
(filter #(contains? operable-keypair-ids (:key-uid %))) :key-uid key-uid
(map (fn [{:keys [accounts name type key-uid]}] :accounts (format-settings-missing-keypair-accounts accounts)})
{:type (keyword type) (update acc
:name name :operable
:key-uid key-uid conj
:accounts (format-settings-keypair-accounts accounts format-options)}))) {:type type
:missing (->> keypairs :name name
(filter #(contains? missing-keypair-ids (:key-uid %))) :key-uid key-uid
(map (fn [{:keys [accounts name type key-uid]}] :accounts (format-settings-keypair-accounts accounts format-options)})))
{:type (keyword type) {:missing []
:name name :operable []}
:key-uid key-uid keypairs)))
:accounts (format-settings-missing-keypair-accounts accounts)})))})))
(rf/reg-sub (rf/reg-sub
:wallet/derivation-path-state :wallet/derivation-path-state
:<- [:wallet/create-account] :<- [:wallet/create-account]
@ -343,7 +345,7 @@
(= operable :no) :missing-keypair (= operable :no) :missing-keypair
watch-only? :watch-only watch-only? :watch-only
:else :empty) :else :empty)
keypair (first (filter #(= key-uid (:key-uid %)) keypairs))] keypair (get keypairs key-uid)]
(assoc account (assoc account
:customization-color color :customization-color color
:type (cond :type (cond
@ -387,7 +389,7 @@
:<- [:wallet/current-viewing-account] :<- [:wallet/current-viewing-account]
:<- [:wallet/keypairs] :<- [:wallet/keypairs]
(fn [[{:keys [key-uid]} keypairs]] (fn [[{:keys [key-uid]} keypairs]]
(first (filter #(= key-uid (:key-uid %)) keypairs)))) (get keypairs key-uid)))
(rf/reg-sub (rf/reg-sub
:wallet/current-viewing-account-tokens-in-selected-networks :wallet/current-viewing-account-tokens-in-selected-networks

View File

@ -600,18 +600,6 @@
(assoc :network-preferences-names #{}))] (assoc :network-preferences-names #{}))]
(rf/sub [sub-name]))))) (rf/sub [sub-name])))))
(def keypairs
[{:key-uid "abc"}])
(h/deftest-sub :wallet/keypairs
[sub-name]
(testing "returns all keypairs"
(swap! rf-db/app-db
#(assoc-in % [:wallet :keypairs] keypairs))
(is
(= keypairs
(rf/sub [sub-name])))))
(def chat-account (def chat-account
{:path "m/43'/60'/1581'/0'/0" {:path "m/43'/60'/1581'/0'/0"
:emoji "" :emoji ""
@ -654,20 +642,40 @@
:operable :no :operable :no
:removed false}) :removed false})
(def default-keypair-accounts (def profile-key-pair-key-uid "abc")
{:key-uid "abc" (def seed-phrase-key-pair-key-uid "def")
(def profile-keypair
{:key-uid profile-key-pair-key-uid
:name "My Profile" :name "My Profile"
:type "profile" :type :profile
:lowest-operability :fully :lowest-operability :fully
:accounts []}) :accounts []})
(def seed-phrase-keypair-accounts (def seed-phrase-keypair
{:key-uid "def" {:key-uid seed-phrase-key-pair-key-uid
:name "My Key Pair" :name "My Key Pair"
:type "seed" :type :seed
:lowest-operability :no :lowest-operability :no
:accounts []}) :accounts []})
(h/deftest-sub :wallet/keypairs
[sub-name]
(testing "returns keypairs map"
(swap! rf-db/app-db assoc-in [:wallet :keypairs] {profile-key-pair-key-uid profile-keypair})
(is (match? {profile-key-pair-key-uid profile-keypair} (rf/sub [sub-name])))))
(h/deftest-sub :wallet/keypairs-list
[sub-name]
(swap! rf-db/app-db assoc-in
[:wallet :keypairs]
{profile-key-pair-key-uid profile-keypair
seed-phrase-key-pair-key-uid seed-phrase-keypair})
(let [result (rf/sub [sub-name])
expected (list profile-keypair seed-phrase-keypair)]
(is (= 2 (count result)))
(is (match? expected result))))
(h/deftest-sub :wallet/settings-keypairs-accounts (h/deftest-sub :wallet/settings-keypairs-accounts
[sub-name] [sub-name]
(testing "returns formatted key-pairs and accounts" (testing "returns formatted key-pairs and accounts"
@ -676,12 +684,14 @@
(-> db (-> db
(assoc-in (assoc-in
[:wallet :keypairs] [:wallet :keypairs]
[(assoc default-keypair-accounts {profile-key-pair-key-uid (update profile-keypair
:accounts :accounts
[operable-wallet-account]) conj
(assoc seed-phrase-keypair-accounts operable-wallet-account)
:accounts seed-phrase-key-pair-key-uid (update seed-phrase-keypair
[inoperable-wallet-account])]) :accounts
conj
inoperable-wallet-account)})
(assoc-in (assoc-in
[:wallet :accounts] [:wallet :accounts]
{(:address operable-wallet-account) operable-wallet-account {(:address operable-wallet-account) operable-wallet-account
@ -689,15 +699,15 @@
(is (is
(match? (match?
{:missing [{:name (:name seed-phrase-keypair-accounts) {:missing [{:name (:name seed-phrase-keypair)
:key-uid (:key-uid seed-phrase-keypair-accounts) :key-uid (:key-uid seed-phrase-keypair)
:type (keyword (:type seed-phrase-keypair-accounts)) :type (:type seed-phrase-keypair)
:accounts [{:customization-color (:color inoperable-wallet-account) :accounts [{:customization-color (:color inoperable-wallet-account)
:emoji (:emoji inoperable-wallet-account) :emoji (:emoji inoperable-wallet-account)
:type :default}]}] :type :default}]}]
:operable [{:name (:name default-keypair-accounts) :operable [{:name (:name profile-keypair)
:key-uid (:key-uid default-keypair-accounts) :key-uid (:key-uid profile-keypair)
:type (keyword (:type default-keypair-accounts)) :type (:type profile-keypair)
:accounts [{:account-props {:customization-color (:color operable-wallet-account) :accounts [{:account-props {:customization-color (:color operable-wallet-account)
:size 32 :size 32
:emoji (:emoji operable-wallet-account) :emoji (:emoji operable-wallet-account)
@ -715,9 +725,10 @@
(-> db (-> db
(assoc-in (assoc-in
[:wallet :keypairs] [:wallet :keypairs]
[(assoc default-keypair-accounts {profile-key-pair-key-uid (update profile-keypair
:accounts :accounts
[operable-wallet-account])]) conj
operable-wallet-account)})
(assoc-in (assoc-in
[:wallet :accounts] [:wallet :accounts]
{(:address operable-wallet-account) operable-wallet-account})))) {(:address operable-wallet-account) operable-wallet-account}))))
@ -732,9 +743,9 @@
size-option 20] size-option 20]
(is (is
(match? {:missing [] (match? {:missing []
:operable [{:name (:name default-keypair-accounts) :operable [{:name (:name profile-keypair)
:key-uid (:key-uid default-keypair-accounts) :key-uid (:key-uid profile-keypair)
:type (keyword (:type default-keypair-accounts)) :type (:type profile-keypair)
:accounts [{:account-props {:customization-color color :accounts [{:account-props {:customization-color color
:size size-option :size size-option
:emoji emoji :emoji emoji
@ -754,10 +765,11 @@
(-> db (-> db
(assoc-in (assoc-in
[:wallet :keypairs] [:wallet :keypairs]
[(assoc default-keypair-accounts {profile-key-pair-key-uid (update profile-keypair
:accounts :accounts
[operable-wallet-account conj
chat-account])]) operable-wallet-account
chat-account)})
(assoc-in (assoc-in
[:wallet :accounts] [:wallet :accounts]
{(:address operable-wallet-account) operable-wallet-account {(:address operable-wallet-account) operable-wallet-account
@ -765,9 +777,9 @@
(is (is
(match? (match?
{:missing [] {:missing []
:operable [{:name (:name default-keypair-accounts) :operable [{:name (:name profile-keypair)
:key-uid (:key-uid default-keypair-accounts) :key-uid (:key-uid profile-keypair)
:type (keyword (:type default-keypair-accounts)) :type (:type profile-keypair)
:accounts [{:account-props {:customization-color (:color operable-wallet-account) :accounts [{:account-props {:customization-color (:color operable-wallet-account)
:size 32 :size 32
:emoji (:emoji operable-wallet-account) :emoji (:emoji operable-wallet-account)