Skip to content

Commit abd2b43

Browse files
authored
Wallet: finalize kp (#18991)
Wallet: finalize kp
1 parent ac78dea commit abd2b43

File tree

13 files changed

+189
-66
lines changed

13 files changed

+189
-66
lines changed

src/quo/components/wallet/keypair/component_spec.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
:action :selector
5656
:details other-details}]
5757
theme)
58-
(h/is-truthy (h/get-by-label-text :radio-on)))
58+
(h/is-truthy (h/get-by-label-text :radio-off)))
5959

6060
(h/test "Options action renders"
6161
(h/render-with-theme-provider [keypair/view

src/quo/components/wallet/keypair/style.cljs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns quo.components.wallet.keypair.style
22
(:require
3-
[quo.foundations.colors :as colors]))
3+
[quo.foundations.colors :as colors]
4+
[react-native.platform :as platform]))
45

56
(defn container
67
[{:keys [blur? customization-color theme selected?]}]
@@ -34,3 +35,8 @@
3435
{:color (if blur?
3536
colors/white-opa-40
3637
(colors/theme-colors colors/neutral-50 colors/neutral-40 theme))})
38+
39+
(defn dot
40+
[blur? theme]
41+
(merge (subtitle blur? theme)
42+
{:bottom (if platform/ios? 2 -2)}))

src/quo/components/wallet/keypair/view.cljs

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
[quo.foundations.colors :as colors]
1212
[quo.theme :as quo.theme]
1313
[react-native.core :as rn]
14-
[react-native.platform :as platform]
1514
[reagent.core :as reagent]
1615
[utils.i18n :as i18n]))
1716

@@ -60,7 +59,7 @@
6059
:accessibility-label :options-button}]])]))
6160

6261
(defn details-view
63-
[{:keys [details stored blur? theme]}]
62+
[{:keys [details stored type blur? theme]}]
6463
(let [{:keys [address]} details]
6564
[rn/view
6665
{:style {:flex-direction :row
@@ -70,10 +69,11 @@
7069
{:size :paragraph-2
7170
:style (style/subtitle blur? theme)}
7271
address]
73-
[text/text
74-
{:size :paragraph-2
75-
:style (merge (style/subtitle blur? theme) {:bottom (if platform/ios? 2 -2)})}
76-
""]
72+
(when (= type :default-keypair)
73+
[text/text
74+
{:size :paragraph-2
75+
:style (style/dot blur? theme)}
76+
""])
7777
[text/text
7878
{:size :paragraph-2
7979
:style (style/subtitle blur? theme)}
@@ -87,8 +87,8 @@
8787
(colors/theme-colors colors/neutral-50 colors/neutral-40 theme))}]])]))
8888

8989
(defn- view-internal
90-
[]
91-
(let [selected? (reagent/atom true)]
90+
[{:keys [default-selected?]}]
91+
(let [selected? (reagent/atom default-selected?)]
9292
(fn [{:keys [accounts action container-style] :as props}]
9393
[rn/pressable
9494
{:style (merge (style/container (merge props {:selected? @selected?})) container-style)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
secret-phrase (reagent/atom [])
5050
random-phrase (reagent/atom [])]
5151
(fn []
52-
(rn/use-effect
52+
(rn/use-mount
5353
(fn []
5454
(native-module/get-random-mnemonic #(reset! secret-phrase (string/split % #"\s")))
5555
(native-module/get-random-mnemonic #(reset! random-phrase (string/split % #"\s")))))

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

+51-31
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
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]
810
[utils.i18n :as i18n]
911
[utils.re-frame :as rf]))
1012

11-
(defn keypair-options
13+
(defn- keypair-options
1214
[]
1315
[quo/action-drawer
1416
[[{:icon :i/add
@@ -26,25 +28,51 @@
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}])
31+
(defn- parse-accounts
32+
[given-accounts]
33+
(->> given-accounts
34+
(filter (fn [{:keys [path]}]
35+
(not (string/starts-with? path constants/path-eip1581))))
36+
(map (fn [{:keys [customization-color emoji name address]}]
37+
{:account-props {:customization-color customization-color
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}))))
4048

49+
(defn- keypair
50+
[item index _ {:keys [profile-picture compressed-key]}]
51+
(let [main-account (first (:accounts item))
52+
color (:customization-color main-account)
53+
accounts (parse-accounts (:accounts item))]
54+
[quo/keypair
55+
{:customization-color color
56+
:profile-picture (when (zero? index) profile-picture)
57+
:status-indicator false
58+
:type (if (zero? index) :default-keypair :other)
59+
:stored :on-device
60+
:on-options-press #(js/alert "Options pressed")
61+
:action :selector
62+
:blur? false
63+
:details {:full-name (:name item)
64+
:address (when (zero? index)
65+
(utils/get-shortened-compressed-key compressed-key))}
66+
:accounts accounts
67+
:default-selected? (zero? index)
68+
:container-style {:margin-horizontal 20
69+
:margin-vertical 8}}]))
4170
(defn view
4271
[]
43-
(let [{:keys [public-key compressed-key
44-
customization-color]} (rf/sub [:profile/profile])
45-
[display-name _] (rf/sub [:contacts/contact-two-names-by-identity public-key])
46-
profile-with-image (rf/sub [:profile/profile-with-image])
47-
profile-picture (profile.utils/photo profile-with-image)]
72+
(let [{:keys [compressed-key customization-color]} (rf/sub [:profile/profile])
73+
profile-with-image (rf/sub [:profile/profile-with-image])
74+
keypairs (rf/sub [:wallet/keypairs])
75+
profile-picture (profile.utils/photo profile-with-image)]
4876
[rn/view {:style {:flex 1}}
4977
[quo/page-nav
5078
{:icon-name :i/close
@@ -60,20 +88,12 @@
6088
[:show-bottom-sheet {:content keypair-options}])}
6189
:description :text
6290
:description-text (i18n/label :t/keypairs-description)}]
63-
[quo/keypair
64-
{:customization-color customization-color
65-
:profile-picture profile-picture
66-
:status-indicator false
67-
:type :default-keypair
68-
:stored :on-device
69-
:on-options-press #(js/alert "Options pressed")
70-
:action :selector
71-
:blur? false
72-
:details {:full-name display-name
73-
:address (utils/get-shortened-compressed-key compressed-key)}
74-
:accounts accounts
75-
:container-style {:margin-horizontal 20
76-
:margin-vertical 8}}]
91+
[rn/flat-list
92+
{:data keypairs
93+
:render-fn keypair
94+
:render-data {:profile-picture profile-picture
95+
:compressed-key compressed-key}
96+
:content-container-style {:padding-bottom 60}}]
7797
[quo/bottom-actions
7898
{:actions :one-action
7999
:button-one-label (i18n/label :t/confirm-account-origin)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(ns status-im.contexts.wallet.create-account.utils)
2+
3+
(defn prepare-new-keypair
4+
[{:keys [new-keypair address account-name account-color emoji derivation-path]}]
5+
(assoc new-keypair
6+
:name (:keypair-name new-keypair)
7+
:key-uid (:keyUid new-keypair)
8+
:type :seed
9+
:derived-from address
10+
:accounts [{:keypair-name (:keypair-name new-keypair)
11+
:key-uid (:keyUid new-keypair)
12+
:seed-phrase (:mnemonic new-keypair)
13+
:public-key (:publicKey new-keypair)
14+
:name account-name
15+
:type :seed
16+
:emoji emoji
17+
:colorID account-color
18+
:path derivation-path
19+
:address (:address new-keypair)}]))

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

+31-19
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
[status-im.contexts.wallet.common.sheets.account-origin.view :as account-origin]
1414
[status-im.contexts.wallet.common.utils :as utils]
1515
[status-im.contexts.wallet.create-account.style :as style]
16-
[status-im.feature-flags :as ff]
16+
[status-im.contexts.wallet.create-account.utils :as create-account.utils]
1717
[utils.i18n :as i18n]
1818
[utils.re-frame :as rf]
1919
[utils.responsiveness :refer [iphone-11-Pro-20-pixel-from-width]]
2020
[utils.security.core :as security]
2121
[utils.string]))
2222

23+
2324
(defn- get-keypair-data
2425
[primary-name derivation-path account-color {:keys [keypair-name]}]
2526
[{:title (or keypair-name (i18n/label :t/keypair-title {:name primary-name}))
@@ -30,9 +31,8 @@
3031
:size :xxs
3132
:customization-color account-color})
3233
:action (when-not keypair-name :button)
33-
:action-props {:on-press #(ff/alert ::ff/wallet.edit-default-keypair
34-
(fn []
35-
(rf/dispatch [:navigate-to :wallet-select-keypair])))
34+
:action-props {:on-press (fn []
35+
(rf/dispatch [:navigate-to :wallet-select-keypair]))
3636
:button-text (i18n/label :t/edit)
3737
:alignment :flex-start}
3838
:description :text
@@ -50,22 +50,23 @@
5050

5151
(defn- f-view
5252
[]
53-
(let [top (safe-area/get-top)
54-
bottom (safe-area/get-bottom)
55-
account-color (reagent/atom (rand-nth colors/account-colors))
56-
emoji (reagent/atom (emoji-picker.utils/random-emoji))
57-
number-of-accounts (count (rf/sub [:wallet/accounts-without-watched-accounts]))
58-
account-name (reagent/atom "")
59-
placeholder (i18n/label :t/default-account-placeholder
60-
{:number (inc number-of-accounts)})
61-
derivation-path (reagent/atom (utils/get-derivation-path number-of-accounts))
62-
{:keys [public-key]} (rf/sub [:profile/profile])
63-
on-change-text #(reset! account-name %)
64-
primary-name (first (rf/sub [:contacts/contact-two-names-by-identity public-key]))
65-
{window-width :width} (rn/get-window)]
53+
(let [top (safe-area/get-top)
54+
bottom (safe-area/get-bottom)
55+
account-color (reagent/atom (rand-nth colors/account-colors))
56+
emoji (reagent/atom (emoji-picker.utils/random-emoji))
57+
number-of-accounts (count (rf/sub [:wallet/accounts-without-watched-accounts]))
58+
account-name (reagent/atom "")
59+
placeholder (i18n/label :t/default-account-placeholder
60+
{:number (inc number-of-accounts)})
61+
derivation-path (reagent/atom (utils/get-derivation-path number-of-accounts))
62+
{:keys [public-key address]} (rf/sub [:profile/profile])
63+
on-change-text #(reset! account-name %)
64+
primary-name (first (rf/sub [:contacts/contact-two-names-by-identity
65+
public-key]))
66+
{window-width :width} (rn/get-window)]
6667
(fn [{:keys [theme]}]
6768
(let [{:keys [new-keypair]} (rf/sub [:wallet/create-account])]
68-
(rn/use-effect (fn [] #(rf/dispatch [:wallet/clear-new-keypair])))
69+
(rn/use-unmount #(rf/dispatch [:wallet/clear-new-keypair]))
6970
[rn/view {:style {:flex 1}}
7071
[quo/page-nav
7172
{:type :no-title
@@ -127,7 +128,18 @@
127128
:customization-color @account-color
128129
:on-auth-success (fn [entered-password]
129130
(if new-keypair
130-
(js/alert "Feature under development")
131+
(rf/dispatch
132+
[:wallet/add-keypair-and-create-account
133+
{:sha3-pwd (security/safe-unmask-data
134+
entered-password)
135+
:new-keypair (create-account.utils/prepare-new-keypair
136+
{:new-keypair new-keypair
137+
:address address
138+
:account-name @account-name
139+
:account-color @account-color
140+
:emoji @emoji
141+
:derivation-path
142+
@derivation-path})}])
131143
(rf/dispatch [:wallet/derive-address-and-add-account
132144
{:sha3-pwd (security/safe-unmask-data
133145
entered-password)

src/status_im/contexts/wallet/data_store.cljs

+22-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
(assoc account :watch-only? (= (:type account) :watch)))
2424

2525
(defn- sanitize-emoji
26-
"As Desktop uses Twemoji, the emoji received can be an img tag
27-
with raw emoji in alt attribute. This function help us to extract
26+
"As Desktop uses Twemoji, the emoji received can be an img tag
27+
with raw emoji in alt attribute. This function help us to extract
2828
the emoji from it as mobile doesn't support HTML rendering and Twemoji"
2929
[emoji]
3030
(if (string/starts-with? emoji "<img")
@@ -104,3 +104,23 @@
104104
:blockExplorerUrl :block-explorer-url
105105
:nativeCurrencySymbol :native-currency-symbol
106106
:nativeCurrencyName :native-currency-symbol})))
107+
108+
(defn rename-color-id-in-data
109+
[data]
110+
(map (fn [item]
111+
(update item
112+
:accounts
113+
(fn [accounts]
114+
(map (fn [account]
115+
(let [renamed-account (set/rename-keys account
116+
{:colorId :customization-color})]
117+
(if (contains? account :colorId)
118+
renamed-account
119+
(assoc renamed-account :customization-color :blue))))
120+
accounts))))
121+
data))
122+
123+
(defn parse-keypairs
124+
[keypairs]
125+
(let [renamed-data (rename-color-id-in-data keypairs)]
126+
(cske/transform-keys csk/->kebab-case-keyword renamed-data)))

src/status_im/contexts/wallet/events.cljs

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

205+
(defn add-keypair-and-create-account
206+
[_ [{:keys [sha3-pwd new-keypair]}]]
207+
(let [lowercase-address (if (:address new-keypair)
208+
(string/lower-case (:address new-keypair))
209+
(:address new-keypair))]
210+
{:fx [[:json-rpc/call
211+
[{:method "accounts_addKeypair"
212+
:params [sha3-pwd new-keypair]
213+
:on-success [:wallet/add-account-success lowercase-address]
214+
:on-error #(log/info "failed to create keypair " %)}]]]}))
215+
216+
(rf/reg-event-fx :wallet/add-keypair-and-create-account add-keypair-and-create-account)
217+
218+
(defn get-keypairs
219+
[_]
220+
{:fx [[:json-rpc/call
221+
[{:method "accounts_getKeypairs"
222+
:params []
223+
:on-success [:wallet/get-keypairs-success]
224+
:on-error #(log/info "failed to get keypairs " %)}]]]})
225+
226+
(rf/reg-event-fx :wallet/get-keypairs get-keypairs)
227+
228+
(defn get-keypairs-success
229+
[{:keys [db]} [keypairs]]
230+
{:db (assoc-in db [:wallet :keypairs] (data-store/parse-keypairs keypairs))})
231+
232+
(rf/reg-event-fx :wallet/get-keypairs-success get-keypairs-success)
233+
205234
(rf/reg-event-fx :wallet/bridge-select-token
206235
(fn [{:keys [db]} [{:keys [token stack-id]}]]
207236
(let [to-address (get-in db [:wallet :current-viewing-account-address])]
@@ -371,7 +400,8 @@
371400
(fn []
372401
{:fx [[:dispatch [:wallet/start-wallet]]
373402
[:dispatch [:wallet/get-ethereum-chains]]
374-
[:dispatch [:wallet/get-accounts]]]}))
403+
[:dispatch [:wallet/get-accounts]]
404+
[:dispatch [:wallet/get-keypairs]]]}))
375405

376406
(rf/reg-event-fx :wallet/share-account
377407
(fn [_ [{:keys [content title]}]]

src/status_im/contexts/wallet/events_test.cljs

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

58-
5958
(deftest store-collectibles
6059
(testing "(displayable-collectible?) helper function"
6160
(let [expected-results [[true

src/status_im/feature_flags.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
(defonce ^:private feature-flags-config
1212
(reagent/atom
13-
{::wallet.edit-default-keypair (enabled-in-env? :FLAG_EDIT_DEFAULT_KEYPAIR_ENABLED)
13+
{::wallet.edit-default-keypair true
1414
::wallet.bridge-token (enabled-in-env? :FLAG_BRIDGE_TOKEN_ENABLED)
1515
::wallet.remove-account (enabled-in-env? :FLAG_REMOVE_ACCOUNT_ENABLED)
1616
::wallet.network-filter (enabled-in-env? :FLAG_NETWORK_FILTER_ENABLED)

0 commit comments

Comments
 (0)