Repository navigation
Expand file tree
/
Copy pathcore.cljs
More file actions
270 lines (243 loc) · 10.8 KB
/
Copy pathcore.cljs
File metadata and controls
270 lines (243 loc) · 10.8 KB
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
(ns status-im.init.core
(:require [re-frame.core :as re-frame]
[status-im.chat.models.loading :as chat-loading]
[status-im.accounts.core :as accounts.core]
[status-im.accounts.login.core :as accounts.login]
[status-im.accounts.update.core :as accounts.update]
[status-im.constants :as constants]
[status-im.react-native.js-dependencies :as rn-dependencies]
[status-im.data-store.core :as data-store]
[status-im.data-store.realm.core :as realm]
[status-im.extensions.registry :as extensions.registry]
[status-im.i18n :as i18n]
[status-im.browser.core :as browser]
[status-im.contact.core :as contact]
[status-im.models.dev-server :as models.dev-server]
[status-im.protocol.core :as protocol]
[status-im.pairing.core :as pairing]
[status-im.models.transactions :as transactions]
[status-im.models.wallet :as models.wallet]
[status-im.native-module.core :as status]
[status-im.node.core :as node]
[status-im.notifications.core :as notifications]
[status-im.ui.screens.db :refer [app-db]]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.config :as config]
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.keychain.core :as keychain]
[status-im.utils.platform :as platform]
[status-im.utils.utils :as utils]
[taoensso.timbre :as log]
[status-im.utils.fx :as fx]
[status-im.chat.models :as chat-model]
[status-im.accounts.db :as accounts.db]
[status-im.stickers.core :as stickers]))
(defn init-store!
"Try to decrypt the database, move on if successful otherwise go back to
initial state"
[encryption-key]
(.. (data-store/init encryption-key)
(then #(re-frame/dispatch [:init.callback/init-store-success]))
(catch (fn [error]
(log/warn "Could not decrypt database" error)
(re-frame/dispatch [:init.callback/init-store-error encryption-key])))))
(defn restore-native-settings! []
(when platform/desktop?
(.getValue rn-dependencies/desktop-config "logging_enabled"
#(re-frame/dispatch [:set-in [:desktop/desktop :logging-enabled]
(if (boolean? %1)
%1 (cljs.reader/read-string %1))]))))
;; TODO (yenda) move keychain functions to dedicated namespace
(defn reset-keychain! []
(.. (keychain/reset)
(then
#(re-frame/dispatch [:init.callback/keychain-reset]))))
(defn reset-data! []
(.. (realm/delete-realms)
(then reset-keychain!)
(catch reset-keychain!)))
(defn reset-account-data! [address]
(let [callback #(re-frame/dispatch [:init.callback/account-db-removed])]
(.. (realm/delete-account-realm address)
(then callback)
(catch callback))))
(fx/defn initialize-keychain
"Entrypoint, fetches the key from the keychain and initialize the app"
[cofx]
{:keychain/get-encryption-key [:init.callback/get-encryption-key-success]})
(fx/defn start-app [cofx]
(fx/merge cofx
{:init/get-device-UUID nil
:init/restore-native-settings nil
:ui/listen-to-window-dimensions-change nil
:notifications/init nil
:network/listen-to-network-status nil
:network/listen-to-connection-status nil
:hardwallet/register-card-events nil}
(initialize-keychain)))
(fx/defn initialize-app-db
"Initialize db to initial state"
[{{:keys [view-id hardwallet
initial-props desktop/desktop
network-status network peers-count peers-summary device-UUID
push-notifications/stored network/type]
:node/keys [status]
:or {network (get app-db :network)}} :db}]
{:db (assoc app-db
:contacts/contacts {}
:initial-props initial-props
:desktop/desktop (merge desktop (:desktop/desktop app-db))
:network-status network-status
:peers-count (or peers-count 0)
:peers-summary (or peers-summary [])
:node/status status
:network network
:network/type type
:hardwallet hardwallet
:device-UUID device-UUID
:view-id view-id
:push-notifications/stored stored)})
(fx/defn initialize-app
[cofx encryption-key]
(fx/merge cofx
{:init/init-store encryption-key
:hardwallet/check-nfc-support nil
:hardwallet/check-nfc-enabled nil}
(initialize-app-db)))
(fx/defn set-device-uuid
[{:keys [db]} device-uuid]
{:db (assoc db :device-UUID device-uuid)})
(fx/defn handle-init-store-error
[encryption-key cofx]
{:ui/show-confirmation
{:title (i18n/label :decryption-failed-title)
:content (i18n/label :decryption-failed-content)
:confirm-button-text (i18n/label :decryption-failed-confirm)
;; On cancel we initialize the app with the same key, in case the error was
;; not related/fs error
:on-cancel #(re-frame/dispatch [:init.ui/data-reset-cancelled encryption-key])
:on-accept #(re-frame/dispatch [:init.ui/data-reset-accepted])}})
(fx/defn load-accounts [{:keys [db all-accounts]}]
(let [accounts (->> all-accounts
(map (fn [{:keys [address] :as account}]
[address account]))
(into {}))]
{:db (assoc db :accounts/accounts accounts)}))
(fx/defn initialize-views
[cofx]
(let [{{:accounts/keys [accounts] :as db} :db} cofx]
(if (empty? accounts)
(navigation/navigate-to-clean cofx :intro nil)
(let [account-with-notification
(when-not platform/desktop?
(notifications/lookup-contact-pubkey-from-hash
cofx
(first (keys (:push-notifications/stored db)))))
selection-fn
(if (not-empty account-with-notification)
#(filter (fn [account]
(= account-with-notification
(:public-key account)))
%)
#(sort-by :last-sign-in > %))
{:keys [address photo-path name]} (first (selection-fn (vals accounts)))]
(accounts.login/open-login cofx address photo-path name)))))
(fx/defn load-accounts-and-initialize-views
"DB has been decrypted, load accounts and initialize-view"
[cofx]
(fx/merge cofx
(load-accounts)
(initialize-views)))
(fx/defn initialize-account-db [{:keys [db web3]} address]
(let [{:universal-links/keys [url]
:keys [accounts/accounts accounts/create networks/networks network
network-status peers-count peers-summary view-id navigation-stack
desktop/desktop hardwallet
device-UUID semaphores accounts/login]
:node/keys [status on-ready]
:or {network (get app-db :network)}} db
current-account (get accounts address)
account-network-id (get current-account :network network)
account-network (get-in current-account [:networks account-network-id])]
{:db (cond-> (assoc app-db
:view-id view-id
:navigation-stack navigation-stack
:node/status status
:node/on-ready on-ready
:accounts/create create
:desktop/desktop (merge desktop (:desktop/desktop app-db))
:networks/networks networks
:account/account current-account
:accounts/login login
:accounts/accounts accounts
:network-status network-status
:network network
:network/type (:network/type db)
:chain (ethereum/network->chain-name account-network)
:universal-links/url url
:peers-summary peers-summary
:peers-count peers-count
:device-UUID device-UUID
:semaphores semaphores
:hardwallet hardwallet
:web3 web3)
(= view-id :create-account)
(assoc-in [:accounts/create :step] :enter-name))}))
(defn login-only-events [cofx address stored-pns]
(fx/merge cofx
(cond->
{:notifications/request-notifications-permissions nil}
platform/ios?
;; on ios navigation state might be not initialized yet when
;; navigate-to call happens.
;; That's why it should be delayed a bit.
;; TODO(rasom): revisit this later and find better solution
(assoc :dispatch-later
[{:ms 1
:dispatch [:navigate-to :home]}]))
(when-not platform/ios?
(navigation/navigate-to-cofx :home nil))
(notifications/process-stored-event address stored-pns)
(when platform/desktop?
(chat-model/update-dock-badge-label))))
(defn dev-mode? [cofx]
(get-in cofx [:db :account/account :dev-mode?]))
(defn creating-account? [cofx]
(= (get-in cofx [:db :view-id])
:create-account))
(defn finishing-hardwallet-setup? [cofx]
(= (get-in cofx [:db :view-id])
:hardwallet-success))
(fx/defn initialize-account [{:keys [db] :as cofx} address]
(let [stored-pns (:push-notifications/stored db)]
(fx/merge cofx
{:notifications/get-fcm-token nil}
(initialize-account-db address)
(contact/load-contacts)
(pairing/load-installations)
#(when (dev-mode? %)
(models.dev-server/start))
(browser/initialize-browsers)
(browser/initialize-dapp-permissions)
(extensions.registry/initialize)
(stickers/init-stickers-packs)
(accounts.update/update-sign-in-time)
#(when-not (or (creating-account? %)
(finishing-hardwallet-setup? %))
(login-only-events % address stored-pns)))))
(re-frame/reg-fx
:init/init-store
init-store!)
(re-frame/reg-fx
:init/restore-native-settings
restore-native-settings!)
(re-frame/reg-fx
:init/get-device-UUID
(fn []
(status/get-device-UUID #(re-frame/dispatch [:init.callback/get-device-UUID-success %]))))
(re-frame/reg-fx
:init/reset-data
reset-data!)
(re-frame/reg-fx
:init/reset-account-data
reset-account-data!)