Skip to content

Commit 3b40d67

Browse files
committed
feat: keypair list
1 parent e6e4a29 commit 3b40d67

File tree

4 files changed

+129
-88
lines changed

4 files changed

+129
-88
lines changed

src/status_im/contexts/wallet/create_account/select_keypair/view.cljs

+26-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
(ns status-im.contexts.wallet.create-account.select-keypair.view
22
(:require
3+
[clojure.string :as string]
34
[quo.core :as quo]
45
[react-native.core :as rn]
6+
[status-im.constants :as constants]
57
[status-im.contexts.profile.utils :as profile.utils]
68
[status-im.contexts.wallet.create-account.select-keypair.style :as style]
79
[utils.address :as utils]
@@ -26,38 +28,29 @@
2628
:accessibility-label :import-private-key
2729
:label (i18n/label :t/import-private-key)}]]])
2830

29-
(def accounts
30-
[{:account-props {:customization-color :turquoise
31-
:size 32
32-
:emoji "\uD83C\uDFB2"
33-
:type :default
34-
:name "Trip to Vegas"
35-
:address "0x0ah...71a"}
36-
:networks [{:network-name :ethereum :short-name "eth"}
37-
{:network-name :optimism :short-name "opt"}]
38-
:state :default
39-
:action :none}])
40-
4131
(defn parse-accounts
4232
[given-accounts]
43-
(map (fn [{:keys [colorId emoji name address]}]
44-
{:account-props {:customization-color (if (not-empty colorId) (keyword colorId) :blue)
45-
:size 32
46-
:emoji emoji
47-
:type :default
48-
:name name
49-
:address address}
50-
:networks [{:network-name :ethereum :short-name "eth"}
51-
{:network-name :optimism :short-name "opt"}]
52-
:state :default
53-
:action :none})
54-
given-accounts))
33+
(->> given-accounts
34+
(filter (fn [{:keys [path]}]
35+
(not (string/starts-with? path constants/path-eip1581))))
36+
(map (fn [{:keys [colorId emoji name address]}]
37+
{:account-props {:customization-color (if (not-empty colorId) (keyword colorId) :blue)
38+
:size 32
39+
:emoji emoji
40+
:type :default
41+
:name name
42+
:address address}
43+
:networks [{:network-name :ethereum :short-name "eth"}
44+
{:network-name :optimism :short-name "opt"}
45+
{:network-name :arbitrum :short-name "arb1"}]
46+
:state :default
47+
:action :none}))))
5548

5649
(defn keypair
5750
[item index _ {:keys [profile-picture compressed-key]}]
58-
(let [main-account (first (:accounts item))
59-
color (keyword (:colorId main-account))
60-
parsed-accounts (parse-accounts (:accounts item))]
51+
(let [main-account (first (:accounts item))
52+
color (keyword (:colorId main-account))
53+
accounts (parse-accounts (:accounts item))]
6154
[quo/keypair
6255
{:customization-color (if (not-empty (:colorId main-account)) color :blue)
6356
:profile-picture (when (zero? index) profile-picture)
@@ -70,7 +63,7 @@
7063
:details {:full-name (:name item)
7164
:address (when (zero? index)
7265
(utils/get-shortened-compressed-key compressed-key))}
73-
:accounts parsed-accounts
66+
:accounts accounts
7467
:default-selected? (zero? index)
7568
:container-style {:margin-horizontal 20
7669
:margin-vertical 8}}]))
@@ -97,10 +90,11 @@
9790
:description :text
9891
:description-text (i18n/label :t/keypairs-description)}]
9992
[rn/flat-list
100-
{:data keypairs
101-
:render-fn keypair
102-
:render-data {:profile-picture profile-picture
103-
:compressed-key compressed-key}}]
93+
{:data keypairs
94+
:render-fn keypair
95+
:render-data {:profile-picture profile-picture
96+
:compressed-key compressed-key}
97+
:content-container-style {:padding-bottom 60}}]
10498
[quo/bottom-actions
10599
{:actions :one-action
106100
:button-one-label (i18n/label :t/confirm-account-origin)

src/status_im/contexts/wallet/create_account/view.cljs

+44-33
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@
1818
[utils.security.core :as security]
1919
[utils.string]))
2020

21+
(defn prepare-new-keypair
22+
[{:keys [new-keypair address account-name account-color emoji derivation-path]}]
23+
(assoc
24+
(assoc new-keypair
25+
:name (:keypair-name new-keypair)
26+
:key-uid (:keyUid new-keypair)
27+
:type :seed
28+
:derived-from address)
29+
:accounts
30+
[{:keypair-name (:keypair-name new-keypair)
31+
:key-uid (:keyUid new-keypair)
32+
:seed-phrase (:mnemonic new-keypair)
33+
:public-key (:publicKey new-keypair)
34+
:name account-name
35+
:type :seed
36+
:emoji emoji
37+
:colorID account-color
38+
:path derivation-path
39+
:address (:address new-keypair)}]))
40+
2141
(defn- get-keypair-data
2242
[primary-name derivation-path account-color {:keys [keypair-name]}]
2343
[{:title (or keypair-name (i18n/label :t/keypair-title {:name primary-name}))
@@ -118,43 +138,34 @@
118138
:label (i18n/label :t/origin)
119139
:data (get-keypair-data primary-name @derivation-path @account-color new-keypair)}]
120140
[standard-auth/slide-button
121-
{:size :size-48
122-
:track-text (i18n/label :t/slide-to-create-account)
141+
{:size :size-48
142+
:track-text (i18n/label :t/slide-to-create-account)
123143
:customization-color @account-color
124-
:on-auth-success (fn [entered-password]
125-
(if new-keypair
126-
(rf/dispatch
127-
[:wallet/finalize-new-keypair
128-
{:sha3-pwd (security/safe-unmask-data
129-
entered-password)
130-
:new-keypair (merge
131-
(merge new-keypair
132-
{:name (:keypair-name new-keypair)
133-
:key-uid (:keyUid new-keypair)
134-
:type :seed
135-
:derived-from address})
136-
{:accounts [{:keypair-name (:keypair-name new-keypair)
137-
:key-uid (:keyUid new-keypair)
138-
:seed-phrase (:mnemonic new-keypair)
139-
:public-key (:publicKey new-keypair)
140-
:name @account-name
141-
:type :seed
142-
:emoji @emoji
143-
:colorID @account-color
144-
:path @derivation-path
145-
:address (:address new-keypair)}]})}])
146-
(rf/dispatch [:wallet/derive-address-and-add-account
147-
{:sha3-pwd (security/safe-unmask-data
148-
entered-password)
149-
:emoji @emoji
150-
:color @account-color
151-
:path @derivation-path
152-
:account-name @account-name}])))
153-
:auth-button-label (i18n/label :t/confirm)
144+
:on-auth-success (fn [entered-password]
145+
(if new-keypair
146+
(rf/dispatch
147+
[:wallet/finalize-new-keypair
148+
{:sha3-pwd (security/safe-unmask-data
149+
entered-password)
150+
:new-keypair (prepare-new-keypair {:new-keypair new-keypair
151+
:address address
152+
:account-name @account-name
153+
:account-color @account-color
154+
:emoji @emoji
155+
:derivation-path
156+
@derivation-path})}])
157+
(rf/dispatch [:wallet/derive-address-and-add-account
158+
{:sha3-pwd (security/safe-unmask-data
159+
entered-password)
160+
:emoji @emoji
161+
:color @account-color
162+
:path @derivation-path
163+
:account-name @account-name}])))
164+
:auth-button-label (i18n/label :t/confirm)
154165
;; TODO (@rende11) Add this property when sliding button issue will fixed
155166
;; https://github.com/status-im/status-mobile/pull/18683#issuecomment-1941564785
156167
;; :disabled? (empty? @account-name)
157-
:container-style (style/slide-button-container bottom)}]]))))
168+
:container-style (style/slide-button-container bottom)}]]))))
158169

159170
(defn- view-internal
160171
[]

src/status_im/contexts/wallet/events.cljs

+26-23
Original file line numberDiff line numberDiff line change
@@ -202,29 +202,32 @@
202202
(first derived-address-details)]))]
203203
{:fx [[:dispatch [:wallet/create-derived-addresses account-details on-success]]]})))
204204

205-
(rf/reg-event-fx
206-
:wallet/finalize-new-keypair
207-
(fn [_ [{:keys [sha3-pwd new-keypair]}]]
208-
{:fx [[:json-rpc/call
209-
[{:method "accounts_addKeypair"
210-
:params [sha3-pwd new-keypair]
211-
:on-success [:wallet/add-account-success (string/lower-case (:address new-keypair))]
212-
:on-error #(log/info "failed to create keypair " %)}]]]}))
213-
214-
(rf/reg-event-fx
215-
:wallet/get-keypairs
216-
(fn [{:keys [db]}]
217-
{:fx [[:json-rpc/call
218-
[{:method "accounts_getKeypairs"
219-
:params []
220-
:on-success [:wallet/get-keypairs-success]
221-
:on-error #(log/info "failed to get keypairs " %)}]]]}))
222-
223-
(rf/reg-event-fx
224-
:wallet/get-keypairs-success
225-
(fn [{:keys [db]} [keypairs]]
226-
(let []
227-
{:db (assoc-in db [:wallet :keypairs] keypairs)})))
205+
(defn finalize-new-keypair
206+
[_ [{:keys [sha3-pwd new-keypair]}]]
207+
{:fx [[:json-rpc/call
208+
[{:method "accounts_addKeypair"
209+
:params [sha3-pwd new-keypair]
210+
:on-success [:wallet/add-account-success (string/lower-case (:address new-keypair))]
211+
:on-error #(log/info "failed to create keypair " %)}]]]})
212+
213+
(rf/reg-event-fx :wallet/finalize-new-keypair finalize-new-keypair)
214+
215+
(defn get-keypairs
216+
[_]
217+
{:fx [[:json-rpc/call
218+
[{:method "accounts_getKeypairs"
219+
:params []
220+
:on-success [:wallet/get-keypairs-success]
221+
:on-error #(log/info "failed to get keypairs " %)}]]]})
222+
223+
(rf/reg-event-fx :wallet/get-keypairs get-keypairs)
224+
225+
(defn get-keypairs-success
226+
[{:keys [db]} [keypairs]]
227+
(let []
228+
{:db (assoc-in db [:wallet :keypairs] keypairs)}))
229+
230+
(rf/reg-event-fx :wallet/get-keypairs-success get-keypairs-success)
228231

229232
(rf/reg-event-fx :wallet/bridge-select-token
230233
(fn [{:keys [db]} [{:keys [token stack-id]}]]

src/status_im/contexts/wallet/events_test.cljs

+33
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,39 @@
5555
effects (events/clear-new-keypair {:db db})]
5656
(is (match? (:db effects) expected-db))))
5757

58+
(deftest finalize-new-keypair
59+
(let [db {}
60+
sha3-pwd "password"
61+
new-keypair {:public-key "public_key" :private-key "private_key"}
62+
expected-db {:wallet {:ui {:create-account {:new-keypair "test-keypair"}}
63+
:keypairs [new-keypair]}}
64+
effects (events/finalize-new-keypair {:db db}
65+
[{:sha3-pwd sha3-pwd :new-keypair new-keypair}])
66+
result-db (:db effects)]
67+
(is (match? result-db expected-db))))
68+
69+
(deftest get-keypairs
70+
(let [db {}
71+
keypairs [{:public-key "public_key1" :private-key "private_key1"}
72+
{:public-key "public_key2" :private-key "private_key2"}]
73+
expected-db {:wallet {:ui {:create-account {:new-keypair "test-keypair"}}
74+
:keypairs keypairs}}
75+
effects (events/get-keypairs {:db db})
76+
result-db (:db effects)]
77+
(is (match? result-db expected-db))))
78+
79+
(deftest get-keypairs-success
80+
(let [db {}
81+
keypairs [{:public-key "public_key1" :private-key "private_key1"}
82+
{:public-key "public_key2" :private-key "private_key2"}]
83+
expected-db {:wallet {:ui {:create-account {:new-keypair "test-keypair"}}
84+
:keypairs keypairs}}
85+
effects (events/get-keypairs-success {:db db} keypairs)
86+
result-db (:db effects)]
87+
(is (match? result-db expected-db))))
88+
89+
90+
5891

5992
(deftest store-collectibles
6093
(testing "(displayable-collectible?) helper function"

0 commit comments

Comments
 (0)