-
Notifications
You must be signed in to change notification settings - Fork 980
/
core.cljs
338 lines (299 loc) · 14.1 KB
/
core.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
(ns status-im.multiaccounts.create.core
(:require [clojure.set :refer [map-invert]]
[re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.ethereum.core :as ethereum]
[status-im.native-module.core :as status]
[status-im.node.core :as node]
[status-im.ui.components.colors :as colors]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.fx :as fx]
[status-im.utils.gfycat.core :as gfycat]
[status-im.utils.identicon :as identicon]
[status-im.utils.security :as security]
[status-im.utils.signing-phrase.core :as signing-phrase]
[status-im.utils.types :as types]
[status-im.utils.utils :as utils]
[status-im.utils.platform :as platform]))
(defn get-signing-phrase [cofx]
(assoc cofx :signing-phrase (signing-phrase/generate)))
(def step-kw-to-num
{:generate-key 1
:choose-key 2
:select-key-storage 3
:create-code 4
:confirm-code 5
:enable-fingerprint 6
:enable-notifications 7})
(defn dec-step [step]
(let [inverted (map-invert step-kw-to-num)]
(if (and (= step :create-code)
(not platform/android?))
:choose-key
(inverted (dec (step-kw-to-num step))))))
(defn inc-step [step]
(let [inverted (map-invert step-kw-to-num)]
(if (and (= step :choose-key)
(not platform/android?))
:create-code
(inverted (inc (step-kw-to-num step))))))
;; multiaccounts create module
(defn get-selected-multiaccount [{:keys [db]}]
(let [{:keys [selected-id multiaccounts]} (:intro-wizard db)]
(some #(when (= selected-id (:id %)) %) multiaccounts)))
(fx/defn create-multiaccount
[{:keys [db] :as cofx}]
(let [{:keys [selected-id address key-code]} (:intro-wizard db)
{:keys [address]} (get-selected-multiaccount cofx)
hashed-password (ethereum/sha3 (security/safe-unmask-data key-code))
callback #(re-frame/dispatch [::store-multiaccount-success key-code %])]
{::store-multiaccount [selected-id address hashed-password callback]}))
(fx/defn intro-wizard
{:events [:multiaccounts.create.ui/intro-wizard]}
[{:keys [db] :as cofx} first-time-setup?]
(fx/merge {:db (assoc db :intro-wizard {:step :generate-key
:weak-password? true
:encrypt-with-password? true
:first-time-setup? first-time-setup?})}
(navigation/navigate-to-cofx :intro-wizard nil)))
(fx/defn intro-step-back
{:events [:intro-wizard/step-back-pressed]}
[{:keys [db] :as cofx}]
(let [step (get-in db [:intro-wizard :step])
first-time-setup? (get-in db [:intro-wizard :first-time-setup?])]
(if (not= :generate-key step)
(fx/merge {:db (cond-> (assoc-in db [:intro-wizard :step] (dec-step step))
(#{:create-code :confirm-code} step)
(update :intro-wizard assoc :weak-password? true :key-code nil)
(= step :confirm-code)
(assoc-in [:intro-wizard :confirm-failure?] false))}
(navigation/navigate-to-cofx :intro-wizard nil))
(fx/merge {:db (dissoc db :intro-wizard)}
(navigation/navigate-back)))))
(fx/defn exit-wizard [{:keys [db] :as cofx}]
(fx/merge {:db (dissoc db :intro-wizard)}
(navigation/navigate-to-cofx :home nil)))
(fx/defn init-key-generation
[{:keys [db] :as cofx}]
{:db (assoc-in db [:intro-wizard :generating-keys?] true)
:intro-wizard/start-onboarding nil})
(fx/defn on-confirm-failure [{:keys [db] :as cofx}]
(do
(utils/vibrate)
{:db (assoc-in db [:intro-wizard :confirm-failure?] true)}))
(defn confirm-failure? [db]
(let [step (get-in db [:intro-wizard :step])]
(and (= step :confirm-code)
(not (:multiaccounts/login db))
(get-in db [:intro-wizard :encrypt-with-password?])
(not= (get-in db [:intro-wizard :stored-key-code]) (get-in db [:intro-wizard :key-code])))))
(fx/defn store-key-code [{:keys [db] :as cofx}]
(let [key-code (get-in db [:intro-wizard :key-code])]
{:db (update db :intro-wizard
assoc :stored-key-code key-code
:key-code nil
:step :confirm-code)}))
(fx/defn intro-step-forward
{:events [:intro-wizard/step-forward-pressed]}
[{:keys [db] :as cofx} {:keys [skip?] :as opts}]
(let [{:keys [step first-time-setup? selected-storage-type processing?]} (:intro-wizard db)]
(cond (confirm-failure? db)
(on-confirm-failure cofx)
(or (= step :enable-notifications)
(and (not first-time-setup?) (= step :confirm-code)
(:multiaccounts/login db)))
(fx/merge cofx
(when (and (= step :enable-notifications) (not skip?))
{:notifications/request-notifications-permissions nil})
exit-wizard)
(= step :generate-key)
(init-key-generation cofx)
(= step :create-code)
(store-key-code cofx)
(and (= step :confirm-code)
(not (:multiaccounts/login db))
(not processing?))
(fx/merge cofx
{:db (assoc-in db [:intro-wizard :processing?] true)}
create-multiaccount)
(and (= step :select-key-storage)
(= :advanced selected-storage-type))
{:dispatch [:keycard/start-onboarding-flow]}
:else {:db (update db :intro-wizard
assoc :processing? false
:step (inc-step step))})))
(defn prepare-accounts-data
[multiaccount]
[(let [{:keys [publicKey address]}
(get-in multiaccount [:derived constants/path-default-wallet-keyword])]
{:publicKey publicKey
:address address
:color colors/blue
:wallet true
:path constants/path-default-wallet
:name "Status account"})
(let [{:keys [publicKey address]}
(get-in multiaccount [:derived constants/path-whisper-keyword])]
{:publicKey publicKey
:address address
:path constants/path-whisper
:chat true})])
(fx/defn save-account-and-login-with-keycard
[_ multiaccount-data password node-config chat-key]
{::save-account-and-login-with-keycard [(types/clj->json multiaccount-data)
password
node-config
chat-key]})
(fx/defn save-account-and-login
[_ multiaccount-data password node-config accounts-data]
{::save-account-and-login [(types/clj->json multiaccount-data)
password
node-config
(types/clj->json accounts-data)]})
(fx/defn on-multiaccount-created
[{:keys [signing-phrase random-guid-generator db] :as cofx}
{:keys [address chat-key keycard-instance-uid keycard-key-uid keycard-pairing keycard-paired-on mnemonic] :as multiaccount}
password
{:keys [seed-backed-up? login?] :or {login? true}}]
(let [[wallet-account {:keys [publicKey]} :as accounts-data] (prepare-accounts-data multiaccount)
name (gfycat/generate-gfy publicKey)
photo-path (identicon/identicon publicKey)
multiaccount-data {:name name :address address :photo-path photo-path}
new-multiaccount (cond-> {; address of the master key
:address address
;; The address from which we derive any wallet
:wallet-root-address (get-in multiaccount [:derived constants/path-wallet-root-keyword :address])
;; The address from which we derive any chat account/encryption keys
:eip1581-address (get-in multiaccount [:derived constants/path-eip1581-keyword :address])
:name name
:photo-path photo-path
; public key of the chat account
:public-key publicKey
:latest-derived-path 0
:accounts [wallet-account]
:signing-phrase signing-phrase
:installation-id (random-guid-generator)
:mnemonic mnemonic
:settings constants/default-multiaccount-settings}
keycard-key-uid (assoc :keycard-instance-uid keycard-instance-uid
:keycard-key-uid keycard-key-uid
:keycard-pairing keycard-pairing
:keycard-paired-on keycard-paired-on))
db (assoc db
:multiaccounts/login {:address address
:name name
:photo-path photo-path
:password password
:creating? true
:processing true}
:multiaccount new-multiaccount
:networks/current-network constants/default-network
:networks/networks constants/default-networks)]
(fx/merge cofx
{:db (cond-> db
seed-backed-up?
(assoc-in [:multiaccount :seed-backed-up?] true))}
(if keycard-key-uid
(save-account-and-login-with-keycard new-multiaccount
password
(node/get-new-config db)
chat-key)
(save-account-and-login multiaccount-data
(ethereum/sha3 (security/safe-unmask-data password))
(node/get-new-config db)
accounts-data))
(when (:intro-wizard db)
(intro-step-forward {})))))
(re-frame/reg-fx
:intro-wizard/start-onboarding
(fn []
(status/multiaccount-generate-and-derive-addresses
5
12
[constants/path-whisper constants/path-default-wallet]
#(re-frame/dispatch [:intro-wizard/on-keys-generated (types/json->clj %)]))))
(fx/defn on-keys-generated
{:events [:intro-wizard/on-keys-generated]}
[{:keys [db] :as cofx} result]
(fx/merge
{:db (update db :intro-wizard
(fn [data]
(-> data
(dissoc :generating-keys?)
(assoc :multiaccounts result
:selected-storage-type :default
:selected-id (-> result first :id)
:step :choose-key))))}
(navigation/navigate-to-cofx :intro-wizard nil)))
(fx/defn on-key-selected
{:events [:intro-wizard/on-key-selected]}
[{:keys [db] :as cofx} id]
{:db (assoc-in db [:intro-wizard :selected-id] id)})
(fx/defn on-key-storage-selected
{:events [:intro-wizard/on-key-storage-selected]}
[{:keys [db] :as cofx} storage-type]
{:db (assoc-in db [:intro-wizard :selected-storage-type] storage-type)})
(fx/defn on-encrypt-with-password-pressed
{:events [:intro-wizard/on-encrypt-with-password-pressed]}
[{:keys [db] :as cofx}]
{:db (assoc-in db [:intro-wizard :encrypt-with-password?] true)})
(fx/defn on-learn-more-pressed
{:events [:intro-wizard/on-learn-more-pressed]}
[{:keys [db] :as cofx}]
{:db (assoc-in db [:intro-wizard :show-learn-more?] true)})
(defn get-new-key-code [current-code sym encrypt-with-password?]
(cond (or (= sym :remove) (= sym "Backspace"))
(subs current-code 0 (dec (count current-code)))
(and (not encrypt-with-password?) (= (count current-code) 6))
current-code
(= (count sym) 1)
(str current-code sym)
:else current-code))
(fx/defn code-symbol-pressed
{:events [:intro-wizard/code-symbol-pressed]}
[{:keys [db] :as cofx} new-key-code]
(let [encrypt-with-password? (get-in db [:intro-wizard :encrypt-with-password?])]
{:db (update db :intro-wizard assoc :key-code new-key-code
:confirm-failure? false
:weak-password? (< (count new-key-code) 6))}))
(re-frame/reg-cofx
::get-signing-phrase
(fn [cofx _]
(get-signing-phrase cofx)))
(fx/defn create-multiaccount-success
{:events [::store-multiaccount-success]
:interceptors [(re-frame/inject-cofx :random-guid-generator)
(re-frame/inject-cofx ::get-signing-phrase)]}
[cofx password derived]
(on-multiaccount-created cofx
(assoc
(get-selected-multiaccount cofx)
:derived
(types/json->clj derived))
password
{:seed-backed-up? false}))
(re-frame/reg-fx
::store-multiaccount
(fn [[id address hashed-password callback]]
(status/multiaccount-store-derived
id
[constants/path-wallet-root
constants/path-eip1581
constants/path-whisper
constants/path-default-wallet]
hashed-password
callback)))
(re-frame/reg-fx
::save-account-and-login
(fn [[multiaccount-data hashed-password config accounts-data]]
(status/save-account-and-login multiaccount-data
hashed-password
config
accounts-data)))
(re-frame/reg-fx
::save-account-and-login-with-keycard
(fn [[multiaccount-data password config chat-key]]
(status/save-account-and-login-with-keycard multiaccount-data
(security/safe-unmask-data password)
config
chat-key)))