2018-08-26 02:45:03 +02:00
|
|
|
(ns status-im.init.core
|
|
|
|
(:require [re-frame.core :as re-frame]
|
2018-09-14 12:36:15 +02:00
|
|
|
[status-im.chat.models.loading :as chat-loading]
|
2018-11-06 16:23:49 +03:00
|
|
|
[status-im.accounts.core :as accounts.core]
|
2018-09-06 12:04:12 +02:00
|
|
|
[status-im.accounts.login.core :as accounts.login]
|
|
|
|
[status-im.accounts.update.core :as accounts.update]
|
2018-08-26 02:45:03 +02:00
|
|
|
[status-im.constants :as constants]
|
2018-12-18 19:30:26 +02:00
|
|
|
[status-im.react-native.js-dependencies :as rn-dependencies]
|
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]
|
2018-11-07 16:13:41 +01:00
|
|
|
[status-im.extensions.registry :as extensions.registry]
|
2018-08-27 06:51:04 +02:00
|
|
|
[status-im.i18n :as i18n]
|
2018-09-21 15:41:40 +02:00
|
|
|
[status-im.browser.core :as browser]
|
2018-10-11 10:38:23 +02:00
|
|
|
[status-im.contact.core :as contact]
|
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.protocol.core :as protocol]
|
2018-10-09 12:43:07 +02:00
|
|
|
[status-im.pairing.core :as pairing]
|
2018-08-26 02:45:03 +02:00
|
|
|
[status-im.models.transactions :as transactions]
|
|
|
|
[status-im.models.wallet :as models.wallet]
|
2018-09-06 12:04:12 +02:00
|
|
|
[status-im.native-module.core :as status]
|
|
|
|
[status-im.node.core :as node]
|
2018-08-27 06:51:04 +02:00
|
|
|
[status-im.notifications.core :as notifications]
|
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]
|
2018-09-06 12:04:12 +02:00
|
|
|
[status-im.utils.config :as config]
|
2018-08-26 02:45:03 +02:00
|
|
|
[status-im.utils.ethereum.core :as ethereum]
|
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]
|
2018-08-27 06:51:04 +02:00
|
|
|
[status-im.utils.universal-links.core :as universal-links]
|
2018-09-06 12:04:12 +02:00
|
|
|
[status-im.utils.utils :as utils]
|
2018-09-24 17:59:02 +02:00
|
|
|
[taoensso.timbre :as log]
|
2018-10-19 23:15:01 +03:00
|
|
|
[status-im.utils.fx :as fx]
|
2018-11-23 22:10:01 +02:00
|
|
|
[status-im.chat.models :as chat-model]
|
|
|
|
[status-im.accounts.db :as accounts.db]))
|
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"
|
|
|
|
#(re-frame/dispatch [:set-in [:desktop/desktop :logging-enabled] %1]))))
|
|
|
|
|
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-09-06 12:04:12 +02: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
|
|
|
|
2018-11-23 22:10:01 +02:00
|
|
|
(defn reset-account-data! [address]
|
|
|
|
(let [callback #(re-frame/dispatch [:init.callback/account-db-removed])]
|
|
|
|
(.. (realm/delete-account-realm address)
|
|
|
|
(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
|
|
|
|
{:init/get-device-UUID nil
|
2018-12-18 19:30:26 +02:00
|
|
|
:init/restore-native-settings nil
|
2018-09-28 16:48:59 +02:00
|
|
|
:ui/listen-to-window-dimensions-change nil
|
|
|
|
:notifications/handle-initial-push-notification nil
|
|
|
|
:network/listen-to-network-status nil
|
|
|
|
:network/listen-to-connection-status nil
|
|
|
|
:hardwallet/check-nfc-support nil
|
|
|
|
:hardwallet/check-nfc-enabled nil}
|
|
|
|
(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"
|
2018-09-28 16:48:59 +02:00
|
|
|
[{{:keys [status-module-initialized? view-id hardwallet
|
2018-12-18 19:30:26 +02:00
|
|
|
initial-props desktop/desktop
|
2018-08-26 02:45:03 +02:00
|
|
|
network-status network peers-count peers-summary device-UUID]
|
2018-10-05 12:27:17 +02:00
|
|
|
:node/keys [status]
|
2018-08-22 19:16:32 +03:00
|
|
|
:or {network (get app-db :network)}} :db}]
|
2018-08-26 02:45:03 +02:00
|
|
|
{:db (assoc app-db
|
|
|
|
:contacts/contacts {}
|
2018-10-27 21:24:47 +02:00
|
|
|
:initial-props initial-props
|
2018-12-18 19:30:26 +02:00
|
|
|
:desktop/desktop (merge desktop (:desktop/desktop app-db))
|
2018-08-26 02:45:03 +02:00
|
|
|
:network-status network-status
|
|
|
|
:peers-count (or peers-count 0)
|
|
|
|
:peers-summary (or peers-summary [])
|
|
|
|
:status-module-initialized? (or platform/ios? js/goog.DEBUG status-module-initialized?)
|
2018-10-05 12:27:17 +02:00
|
|
|
:node/status status
|
2018-08-26 02:45:03 +02:00
|
|
|
:network network
|
2018-08-22 19:16:32 +03:00
|
|
|
:device-UUID device-UUID
|
2018-09-28 16:48:59 +02:00
|
|
|
:view-id view-id
|
|
|
|
:hardwallet (select-keys hardwallet [:nfc-enabled? :nfc-supported?]))})
|
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-09-28 16:48:59 +02:00
|
|
|
{:init/init-store encryption-key}
|
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
|
|
|
|
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
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/defn load-accounts [{:keys [db all-accounts]}]
|
2018-08-27 06:51:04 +02:00
|
|
|
(let [accounts (->> all-accounts
|
|
|
|
(map (fn [{:keys [address] :as account}]
|
|
|
|
[address account]))
|
|
|
|
(into {}))]
|
|
|
|
{:db (assoc db :accounts/accounts accounts)}))
|
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/defn initialize-views
|
|
|
|
[cofx]
|
2018-08-27 06:51:04 +02:00
|
|
|
(let [{{:accounts/keys [accounts] :as db} :db} cofx]
|
|
|
|
(if (empty? accounts)
|
2018-09-24 17:59:02 +02:00
|
|
|
(navigation/navigate-to-clean cofx :intro nil)
|
2018-09-28 16:48:59 +02:00
|
|
|
(let [account-with-notification (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)))]
|
2018-09-24 17:59:02 +02:00
|
|
|
(accounts.login/open-login cofx address photo-path name)))))
|
2018-08-27 06:51:04 +02:00
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/defn load-accounts-and-initialize-views
|
2018-09-06 12:04:12 +02:00
|
|
|
"DB has been decrypted, load accounts and initialize-view"
|
|
|
|
[cofx]
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/merge cofx
|
|
|
|
(load-accounts)
|
|
|
|
(initialize-views)))
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/defn initialize-account-db [{:keys [db web3]} address]
|
2018-08-27 06:51:04 +02:00
|
|
|
(let [{:universal-links/keys [url]
|
2018-11-26 17:52:29 +02:00
|
|
|
:keys [accounts/accounts accounts/create networks/networks network
|
|
|
|
network-status peers-count peers-summary view-id navigation-stack
|
2018-12-18 19:30:26 +02:00
|
|
|
desktop/desktop
|
2018-11-26 17:52:29 +02:00
|
|
|
status-module-initialized? device-UUID semaphores accounts/login]
|
2018-12-18 18:12:02 +02:00
|
|
|
:node/keys [status on-ready]
|
2018-08-26 02:45:03 +02:00
|
|
|
:or {network (get app-db :network)}} db
|
2018-08-27 06:51:04 +02:00
|
|
|
current-account (get accounts address)
|
2018-08-26 02:45:03 +02:00
|
|
|
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
|
|
|
|
:status-module-initialized? (or platform/ios? js/goog.DEBUG status-module-initialized?)
|
2018-10-05 12:27:17 +02:00
|
|
|
:node/status status
|
2018-12-18 18:12:02 +02:00
|
|
|
:node/on-ready on-ready
|
2018-08-26 02:45:03 +02:00
|
|
|
:accounts/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
|
|
|
|
:account/account current-account
|
2018-11-26 17:52:29 +02:00
|
|
|
:accounts/login login
|
|
|
|
:accounts/accounts accounts
|
2018-08-26 02:45:03 +02:00
|
|
|
:network-status network-status
|
|
|
|
:network network
|
|
|
|
:chain (ethereum/network->chain-name account-network)
|
2018-08-27 06:51:04 +02:00
|
|
|
:universal-links/url url
|
2018-08-26 02:45:03 +02:00
|
|
|
:peers-summary peers-summary
|
|
|
|
:peers-count peers-count
|
|
|
|
:device-UUID device-UUID
|
|
|
|
:semaphores semaphores
|
|
|
|
:web3 web3)
|
2018-08-27 06:51:04 +02:00
|
|
|
(= view-id :create-account)
|
2018-09-14 12:36:15 +02:00
|
|
|
(assoc-in [:accounts/create :step] :enter-name))}))
|
2018-08-26 02:45:03 +02:00
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(defn login-only-events [cofx address]
|
|
|
|
(fx/merge cofx
|
|
|
|
{:notifications/request-notifications-permissions nil}
|
|
|
|
(navigation/navigate-to-cofx :home nil)
|
|
|
|
(universal-links/process-stored-event)
|
2018-10-19 23:15:01 +03:00
|
|
|
(notifications/process-stored-event address)
|
|
|
|
(when platform/desktop?
|
2018-11-06 16:23:49 +03:00
|
|
|
(chat-model/update-dock-badge-label))
|
|
|
|
(accounts.core/show-desktop-alpha-release-warning)))
|
2018-09-24 17:59:02 +02:00
|
|
|
|
|
|
|
(defn dev-mode? [cofx]
|
2018-10-11 12:05:50 +02:00
|
|
|
(get-in cofx [:db :account/account :dev-mode?]))
|
2018-09-24 17:59:02 +02:00
|
|
|
|
|
|
|
(defn creating-account? [cofx]
|
|
|
|
(= (get-in cofx [:db :view-id])
|
|
|
|
:create-account))
|
|
|
|
|
2018-11-26 17:52:29 +02:00
|
|
|
(fx/defn initialize-account [cofx address]
|
2018-09-24 17:59:02 +02:00
|
|
|
(fx/merge cofx
|
2018-11-26 17:52:29 +02:00
|
|
|
{:notifications/get-fcm-token nil}
|
2018-09-24 17:59:02 +02:00
|
|
|
(initialize-account-db address)
|
2018-10-11 10:38:23 +02:00
|
|
|
(contact/load-contacts)
|
2018-10-09 12:43:07 +02:00
|
|
|
(pairing/load-installations)
|
2018-09-24 17:59:02 +02:00
|
|
|
#(when (dev-mode? %)
|
|
|
|
(models.dev-server/start))
|
|
|
|
(browser/initialize-browsers)
|
2018-11-26 17:52:29 +02:00
|
|
|
|
2018-09-24 17:59:02 +02:00
|
|
|
(browser/initialize-dapp-permissions)
|
2018-11-07 16:13:41 +01:00
|
|
|
(extensions.registry/initialize)
|
2018-09-24 17:59:02 +02:00
|
|
|
(accounts.update/update-sign-in-time)
|
|
|
|
#(when-not (creating-account? %)
|
|
|
|
(login-only-events % address))))
|
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/status-module-initialized
|
|
|
|
status/module-initialized!)
|
|
|
|
|
|
|
|
(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!)
|
2018-11-23 22:10:01 +02:00
|
|
|
|
|
|
|
(re-frame/reg-fx
|
|
|
|
:init/reset-account-data
|
|
|
|
reset-account-data!)
|
2018-12-18 19:30:26 +02:00
|
|
|
|