2018-08-26 02:45:03 +02:00
|
|
|
(ns status-im.init.core
|
|
|
|
(:require [re-frame.core :as re-frame]
|
2019-07-03 16:29:01 +02:00
|
|
|
[status-im.multiaccounts.login.core :as multiaccounts.login]
|
|
|
|
[status-im.multiaccounts.update.core :as multiaccounts.update]
|
2019-05-09 21:51:41 +02:00
|
|
|
[status-im.browser.core :as browser]
|
|
|
|
[status-im.chat.models :as chat-model]
|
|
|
|
[status-im.contact.core :as contact]
|
2018-09-06 12:04:12 +02:00
|
|
|
[status-im.data-store.core :as data-store]
|
2018-08-27 06:51:04 +02:00
|
|
|
[status-im.data-store.realm.core :as realm]
|
2019-05-22 17:00:34 +02:00
|
|
|
[status-im.ethereum.core :as ethereum]
|
2019-06-08 07:04:07 +03:00
|
|
|
[status-im.extensions.module :as extensions.module]
|
2018-08-27 06:51:04 +02:00
|
|
|
[status-im.i18n :as i18n]
|
2018-08-28 21:40:29 +02:00
|
|
|
[status-im.models.dev-server :as models.dev-server]
|
2018-09-06 12:04:12 +02:00
|
|
|
[status-im.native-module.core :as status]
|
2018-08-27 06:51:04 +02:00
|
|
|
[status-im.notifications.core :as notifications]
|
2019-05-09 21:51:41 +02:00
|
|
|
[status-im.pairing.core :as pairing]
|
|
|
|
[status-im.react-native.js-dependencies :as rn-dependencies]
|
|
|
|
[status-im.stickers.core :as stickers]
|
2018-08-26 02:45:03 +02:00
|
|
|
[status-im.ui.screens.db :refer [app-db]]
|
2018-08-27 06:51:04 +02:00
|
|
|
[status-im.ui.screens.navigation :as navigation]
|
2019-05-09 21:51:41 +02:00
|
|
|
[status-im.utils.fx :as fx]
|
2018-08-27 06:51:04 +02:00
|
|
|
[status-im.utils.keychain.core :as keychain]
|
2018-08-26 02:45:03 +02:00
|
|
|
[status-im.utils.platform :as platform]
|
2019-05-30 16:01:20 +02:00
|
|
|
[status-im.biometric-auth.core :as biometric-auth]
|
2019-05-09 21:51:41 +02:00
|
|
|
[taoensso.timbre :as log]))
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2018-09-06 12:04:12 +02:00
|
|
|
(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])))))
|
|
|
|
|
2018-12-18 19:30:26 +02:00
|
|
|
(defn restore-native-settings! []
|
|
|
|
(when platform/desktop?
|
|
|
|
(.getValue rn-dependencies/desktop-config "logging_enabled"
|
2018-12-24 16:08:42 +02:00
|
|
|
#(re-frame/dispatch [:set-in [:desktop/desktop :logging-enabled]
|
|
|
|
(if (boolean? %1)
|
|
|
|
%1 (cljs.reader/read-string %1))]))))
|
2018-12-18 19:30:26 +02:00
|
|
|
|
2018-08-26 02:45:03 +02:00
|
|
|
;; TODO (yenda) move keychain functions to dedicated namespace
|
2018-09-06 12:04:12 +02:00
|
|
|
(defn reset-keychain! []
|
2018-08-26 02:45:03 +02:00
|
|
|
(.. (keychain/reset)
|
|
|
|
(then
|
2018-09-06 12:04:12 +02:00
|
|
|
#(re-frame/dispatch [:init.callback/keychain-reset]))))
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2018-12-25 17:57:19 +03:00
|
|
|
(defn reset-data! []
|
2018-08-26 02:45:03 +02:00
|
|
|
(.. (realm/delete-realms)
|
2018-09-06 12:04:12 +02:00
|
|
|
(then reset-keychain!)
|
|
|
|
(catch reset-keychain!)))
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2019-07-03 16:29:01 +02:00
|
|
|
(defn reset-multiaccount-data! [address]
|
|
|
|
(let [callback #(re-frame/dispatch [:init.callback/multiaccount-db-removed])]
|
|
|
|
(.. (realm/delete-multiaccount-realm address)
|
2018-11-23 22:10:01 +02:00
|
|
|
(then callback)
|
|
|
|
(catch callback))))
|
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/defn initialize-keychain
|
2018-09-06 12:04:12 +02:00
|
|
|
"Entrypoint, fetches the key from the keychain and initialize the app"
|
|
|
|
[cofx]
|
|
|
|
{:keychain/get-encryption-key [:init.callback/get-encryption-key-success]})
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2018-09-28 16:48:59 +02:00
|
|
|
(fx/defn start-app [cofx]
|
|
|
|
(fx/merge cofx
|
2018-11-20 19:36:11 +01:00
|
|
|
{:init/get-device-UUID nil
|
2019-05-30 16:01:20 +02:00
|
|
|
:init/get-supported-biometric-auth nil
|
2018-11-20 19:36:11 +01:00
|
|
|
: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}
|
2018-09-28 16:48:59 +02:00
|
|
|
(initialize-keychain)))
|
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/defn initialize-app-db
|
2018-08-27 06:51:04 +02:00
|
|
|
"Initialize db to initial state"
|
2019-01-24 14:18:34 +01:00
|
|
|
[{{:keys [view-id hardwallet
|
2018-12-25 17:57:19 +03:00
|
|
|
initial-props desktop/desktop
|
|
|
|
network-status network peers-count peers-summary device-UUID
|
2019-05-30 16:01:20 +02:00
|
|
|
supported-biometric-auth push-notifications/stored network/type]
|
2018-10-05 12:27:17 +02:00
|
|
|
:node/keys [status]
|
2018-12-04 16:41:34 +03:00
|
|
|
:or {network (get app-db :network)}} :db}]
|
2019-02-08 16:30:44 +03:00
|
|
|
{: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
|
2019-05-30 16:01:20 +02:00
|
|
|
:supported-biometric-auth supported-biometric-auth
|
2019-02-08 16:30:44 +03:00
|
|
|
:view-id view-id
|
|
|
|
:push-notifications/stored stored)})
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/defn initialize-app
|
|
|
|
[cofx encryption-key]
|
|
|
|
(fx/merge cofx
|
2018-12-25 17:57:19 +03:00
|
|
|
{:init/init-store encryption-key
|
|
|
|
:hardwallet/check-nfc-support nil
|
|
|
|
:hardwallet/check-nfc-enabled nil}
|
2018-12-07 22:55:33 +02:00
|
|
|
(initialize-app-db)))
|
2018-09-24 17:59:02 +02:00
|
|
|
|
|
|
|
(fx/defn set-device-uuid
|
|
|
|
[{:keys [db]} device-uuid]
|
2018-09-06 12:04:12 +02:00
|
|
|
{:db (assoc db :device-UUID device-uuid)})
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2019-05-30 16:01:20 +02:00
|
|
|
(fx/defn set-supported-biometric-auth
|
|
|
|
{:events [:init.callback/get-supported-biometric-auth-success]}
|
|
|
|
[{:keys [db]} supported-biometric-auth]
|
|
|
|
{:db (assoc db :supported-biometric-auth supported-biometric-auth)})
|
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/defn handle-init-store-error
|
2018-09-06 12:04:12 +02:00
|
|
|
[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])}})
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2019-07-03 16:29:01 +02:00
|
|
|
(fx/defn load-multiaccounts [{:keys [db all-multiaccounts]}]
|
|
|
|
(let [multiaccounts (->> all-multiaccounts
|
|
|
|
(map (fn [{:keys [address] :as multiaccount}]
|
|
|
|
[address multiaccount]))
|
|
|
|
(into {}))]
|
|
|
|
{:db (assoc db :multiaccounts/multiaccounts multiaccounts)}))
|
2018-08-27 06:51:04 +02:00
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/defn initialize-views
|
|
|
|
[cofx]
|
2019-07-03 16:29:01 +02:00
|
|
|
(let [{{:multiaccounts/keys [multiaccounts] :as db} :db} cofx]
|
|
|
|
(if (empty? multiaccounts)
|
2019-04-08 18:03:08 +03:00
|
|
|
(navigation/navigate-to-cofx cofx :intro nil)
|
2019-07-03 16:29:01 +02:00
|
|
|
(let [multiaccount-with-notification
|
2018-11-20 19:36:11 +01:00
|
|
|
(when-not platform/desktop?
|
|
|
|
(notifications/lookup-contact-pubkey-from-hash
|
|
|
|
cofx
|
|
|
|
(first (keys (:push-notifications/stored db)))))
|
|
|
|
selection-fn
|
2019-07-03 16:29:01 +02:00
|
|
|
(if (not-empty multiaccount-with-notification)
|
|
|
|
#(filter (fn [multiaccount]
|
|
|
|
(= multiaccount-with-notification
|
|
|
|
(:public-key multiaccount)))
|
2018-11-20 19:36:11 +01:00
|
|
|
%)
|
|
|
|
#(sort-by :last-sign-in > %))
|
2019-07-19 16:11:10 +02:00
|
|
|
{:keys [address public-key photo-path name accounts]} (first (selection-fn (vals multiaccounts)))]
|
|
|
|
(multiaccounts.login/open-login cofx address photo-path name public-key accounts)))))
|
2018-08-27 06:51:04 +02:00
|
|
|
|
2019-07-03 16:29:01 +02:00
|
|
|
(fx/defn load-multiaccounts-and-initialize-views
|
|
|
|
"DB has been decrypted, load multiaccounts and initialize-view"
|
2018-09-06 12:04:12 +02:00
|
|
|
[cofx]
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/merge cofx
|
2019-07-03 16:29:01 +02:00
|
|
|
(load-multiaccounts)
|
2018-09-24 17:59:02 +02:00
|
|
|
(initialize-views)))
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2019-07-03 16:29:01 +02:00
|
|
|
(fx/defn initialize-multiaccount-db [{:keys [db web3]} address]
|
2018-08-27 06:51:04 +02:00
|
|
|
(let [{:universal-links/keys [url]
|
2019-07-03 16:29:01 +02:00
|
|
|
:keys [multiaccounts/multiaccounts multiaccounts/create networks/networks network
|
2018-12-25 17:57:19 +03:00
|
|
|
network-status peers-count peers-summary view-id navigation-stack
|
2019-03-29 19:00:12 +01:00
|
|
|
mailserver/mailservers
|
2019-05-13 10:58:41 +03:00
|
|
|
intro-wizard
|
2019-05-30 16:01:20 +02:00
|
|
|
desktop/desktop hardwallet custom-fleets supported-biometric-auth
|
2019-07-03 16:29:01 +02:00
|
|
|
device-UUID semaphores multiaccounts/login]
|
2018-12-25 17:57:19 +03:00
|
|
|
:node/keys [status on-ready]
|
|
|
|
:or {network (get app-db :network)}} db
|
2019-07-03 16:29:01 +02:00
|
|
|
current-multiaccount (get multiaccounts address)
|
|
|
|
multiaccount-network-id (get current-multiaccount :network network)
|
|
|
|
multiaccount-network (get-in current-multiaccount [:networks multiaccount-network-id])]
|
2018-08-26 02:45:03 +02:00
|
|
|
{:db (cond-> (assoc app-db
|
|
|
|
:view-id view-id
|
|
|
|
:navigation-stack navigation-stack
|
2018-10-05 12:27:17 +02:00
|
|
|
:node/status status
|
2019-05-13 10:58:41 +03:00
|
|
|
:intro-wizard intro-wizard
|
2018-12-18 18:12:02 +02:00
|
|
|
:node/on-ready on-ready
|
2019-07-03 16:29:01 +02:00
|
|
|
:multiaccounts/create create
|
2018-12-18 19:30:26 +02:00
|
|
|
:desktop/desktop (merge desktop (:desktop/desktop app-db))
|
2018-08-26 02:45:03 +02:00
|
|
|
:networks/networks networks
|
2019-07-03 16:29:01 +02:00
|
|
|
:multiaccount current-multiaccount
|
|
|
|
:multiaccounts/login login
|
|
|
|
:multiaccounts/multiaccounts multiaccounts
|
2019-03-29 19:00:12 +01:00
|
|
|
:mailserver/mailservers mailservers
|
2018-08-26 02:45:03 +02:00
|
|
|
:network-status network-status
|
2019-07-03 16:29:01 +02:00
|
|
|
:network multiaccount-network-id
|
2019-02-01 14:21:23 +02:00
|
|
|
:network/type (:network/type db)
|
2019-07-03 16:29:01 +02:00
|
|
|
:chain (ethereum/network->chain-name multiaccount-network)
|
2018-08-27 06:51:04 +02:00
|
|
|
:universal-links/url url
|
2019-03-29 19:00:12 +01:00
|
|
|
:custom-fleets custom-fleets
|
2018-08-26 02:45:03 +02:00
|
|
|
:peers-summary peers-summary
|
|
|
|
:peers-count peers-count
|
|
|
|
:device-UUID device-UUID
|
2019-05-30 16:01:20 +02:00
|
|
|
:supported-biometric-auth supported-biometric-auth
|
2018-08-26 02:45:03 +02:00
|
|
|
:semaphores semaphores
|
2018-12-04 16:41:34 +03:00
|
|
|
:hardwallet hardwallet
|
2018-08-26 02:45:03 +02:00
|
|
|
:web3 web3)
|
2019-07-03 16:29:01 +02:00
|
|
|
(= view-id :create-multiaccount)
|
|
|
|
(assoc-in [:multiaccounts/create :step] :enter-name))}))
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2019-05-13 10:58:41 +03:00
|
|
|
(defn login-only-events [{:keys [db] :as cofx} address stored-pns]
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/merge cofx
|
2019-05-13 10:58:41 +03:00
|
|
|
(when-not (:intro-wizard db)
|
|
|
|
(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 (or (:intro-wizard db) platform/ios?)
|
2019-02-25 17:39:21 +02:00
|
|
|
(navigation/navigate-to-cofx :home nil))
|
2018-11-20 19:36:11 +01:00
|
|
|
(notifications/process-stored-event address stored-pns)
|
2018-10-19 23:15:01 +03:00
|
|
|
(when platform/desktop?
|
2018-12-20 12:46:00 +02:00
|
|
|
(chat-model/update-dock-badge-label))))
|
2018-09-24 17:59:02 +02:00
|
|
|
|
|
|
|
(defn dev-mode? [cofx]
|
2019-07-03 16:29:01 +02:00
|
|
|
(get-in cofx [:db :multiaccount :dev-mode?]))
|
2018-09-24 17:59:02 +02:00
|
|
|
|
2019-07-03 16:29:01 +02:00
|
|
|
(defn creating-multiaccount? [cofx]
|
2018-09-24 17:59:02 +02:00
|
|
|
(= (get-in cofx [:db :view-id])
|
2019-07-03 16:29:01 +02:00
|
|
|
:create-multiaccount))
|
2018-09-24 17:59:02 +02:00
|
|
|
|
2019-08-01 18:49:33 +03:00
|
|
|
(defn recovering-multiaccount? [cofx]
|
|
|
|
(boolean (get-in cofx [:db :multiaccounts/recover])))
|
|
|
|
|
2019-07-10 14:05:29 +03:00
|
|
|
(defn- keycard-setup? [cofx]
|
|
|
|
(boolean (get-in cofx [:db :hardwallet :flow])))
|
2018-12-04 16:41:34 +03:00
|
|
|
|
2019-07-03 16:29:01 +02:00
|
|
|
(fx/defn initialize-multiaccount [{:keys [db] :as cofx} address]
|
2018-11-20 19:36:11 +01:00
|
|
|
(let [stored-pns (:push-notifications/stored db)]
|
|
|
|
(fx/merge cofx
|
|
|
|
{:notifications/get-fcm-token nil}
|
2019-07-03 16:29:01 +02:00
|
|
|
(initialize-multiaccount-db address)
|
2018-11-20 19:36:11 +01:00
|
|
|
#(when (dev-mode? %)
|
|
|
|
(models.dev-server/start))
|
2019-06-08 07:04:07 +03:00
|
|
|
(extensions.module/initialize)
|
2019-01-07 13:50:06 +01:00
|
|
|
(stickers/init-stickers-packs)
|
2019-07-03 16:29:01 +02:00
|
|
|
(multiaccounts.update/update-sign-in-time)
|
|
|
|
#(when-not (or (creating-multiaccount? %)
|
2019-08-01 18:49:33 +03:00
|
|
|
(recovering-multiaccount? %)
|
2019-07-10 14:05:29 +03:00
|
|
|
(keycard-setup? %))
|
2018-11-20 19:36:11 +01:00
|
|
|
(login-only-events % address stored-pns)))))
|
2018-09-06 12:04:12 +02:00
|
|
|
|
|
|
|
(re-frame/reg-fx
|
|
|
|
:init/init-store
|
|
|
|
init-store!)
|
|
|
|
|
2018-12-18 19:30:26 +02:00
|
|
|
(re-frame/reg-fx
|
|
|
|
:init/restore-native-settings
|
|
|
|
restore-native-settings!)
|
|
|
|
|
2018-09-06 12:04:12 +02:00
|
|
|
(re-frame/reg-fx
|
|
|
|
:init/get-device-UUID
|
|
|
|
(fn []
|
|
|
|
(status/get-device-UUID #(re-frame/dispatch [:init.callback/get-device-UUID-success %]))))
|
|
|
|
|
2019-05-30 16:01:20 +02:00
|
|
|
(re-frame/reg-fx
|
|
|
|
:init/get-supported-biometric-auth
|
|
|
|
(fn []
|
|
|
|
(biometric-auth/get-supported #(re-frame/dispatch [:init.callback/get-supported-biometric-auth-success %]))))
|
|
|
|
|
2018-09-06 12:04:12 +02:00
|
|
|
(re-frame/reg-fx
|
|
|
|
:init/reset-data
|
|
|
|
reset-data!)
|
2018-11-23 22:10:01 +02:00
|
|
|
|
|
|
|
(re-frame/reg-fx
|
2019-07-03 16:29:01 +02:00
|
|
|
:init/reset-multiaccount-data
|
|
|
|
reset-multiaccount-data!)
|