diff --git a/src/status_im/accounts/create/core.cljs b/src/status_im/accounts/create/core.cljs index 5a77624bdb..4ac0e178dd 100644 --- a/src/status_im/accounts/create/core.cljs +++ b/src/status_im/accounts/create/core.cljs @@ -19,7 +19,8 @@ [status-im.utils.signing-phrase.core :as signing-phrase] [status-im.utils.types :as types] [taoensso.timbre :as log] - [status-im.utils.fx :as fx])) + [status-im.utils.fx :as fx] + [status-im.node.core :as node])) (defn get-signing-phrase [cofx] (assoc cofx :signing-phrase (signing-phrase/generate))) @@ -33,10 +34,17 @@ #(re-frame/dispatch [:accounts.create.callback/create-account-success (types/json->clj %) password]))) ;;;; Handlers - -(defn create-account [{{:accounts/keys [create] :as db} :db}] - {:db (update db :accounts/create assoc :step :account-creating :error nil) - :accounts.create/create-account (:password create)}) +(defn create-account + [{:keys [db random-guid-generator] :as cofx}] + (fx/merge + cofx + {:db (-> db + (update :accounts/create assoc + :step :account-creating + :error nil) + (assoc :node/on-ready :create-account + :accounts/new-installation-id (random-guid-generator)))} + (node/initialize nil))) (fx/defn add-account "Takes db and new account, creates map of effects describing adding account to database and realm" @@ -51,14 +59,13 @@ :data-store/base-tx [(accounts-store/save-account-tx enriched-account)]})) (fx/defn on-account-created - [{:keys [random-guid-generator - signing-phrase + [{:keys [signing-phrase status db] :as cofx} - {:keys [pubkey address mnemonic]} password seed-backed-up] + {:keys [pubkey address mnemonic installation-id]} password seed-backed-up] (let [normalized-address (utils.hex/normalize-hex address) account {:public-key pubkey - :installation-id (random-guid-generator) + :installation-id (or installation-id (get-in db [:accounts/new-installation-id])) :address normalized-address :name (gfycat/generate-gfy pubkey) :status status @@ -72,11 +79,11 @@ (log/debug "account-created") (when-not (string/blank? pubkey) (fx/merge cofx - {:db (assoc db :accounts/login {:address normalized-address - :password password + {:db (assoc db :accounts/login {:address normalized-address + :password password :processing true})} (add-account account) - (accounts.login/user-login))))) + (accounts.login/user-login true))))) (defn reset-account-creation [{db :db}] {:db (update db :accounts/create assoc diff --git a/src/status_im/accounts/login/core.cljs b/src/status_im/accounts/login/core.cljs index d5612aaa0e..7ed7ffe0ba 100644 --- a/src/status_im/accounts/login/core.cljs +++ b/src/status_im/accounts/login/core.cljs @@ -55,14 +55,16 @@ (models.wallet/update-wallet) (transactions/start-sync))) -(fx/defn user-login [{:keys [db] :as cofx}] +(fx/defn user-login [{:keys [db] :as cofx} create-database?] (let [{:keys [address password save-password?]} (accounts.db/credentials cofx)] (fx/merge cofx (merge - {:db (assoc-in db [:accounts/login :processing] true) + {:db (-> db + (assoc-in [:accounts/login :processing] true) + (assoc :node/on-ready :login)) :accounts.login/clear-web-data nil - :data-store/change-account [address password false]} + :data-store/change-account [address password create-database?]} (when save-password? {:keychain/save-user-password [address password]}))))) @@ -199,7 +201,7 @@ (fx/merge cofx {:db (assoc-in db [:accounts/login :password] password)} (navigation/navigate-to-cofx :progress nil) - (user-login)) + (user-login false)) (navigation/navigate-to-clean cofx :login nil))) (re-frame/reg-fx diff --git a/src/status_im/accounts/logout/core.cljs b/src/status_im/accounts/logout/core.cljs index 93babfdb1c..4b385d96e1 100644 --- a/src/status_im/accounts/logout/core.cljs +++ b/src/status_im/accounts/logout/core.cljs @@ -2,20 +2,21 @@ (:require [re-frame.core :as re-frame] [status-im.i18n :as i18n] [status-im.transport.core :as transport] - [status-im.ui.screens.navigation :as navigation] [status-im.utils.fx :as fx] - [status-im.models.transactions :as transactions])) + [status-im.models.transactions :as transactions] + [status-im.node.core :as node] + [status-im.init.core :as init])) (fx/defn logout [{:keys [db] :as cofx}] - (let [{:transport/keys [chats]} db] - (fx/merge cofx - {:keychain/clear-user-password (get-in db [:account/account :address]) - :dev-server/stop nil - :keychain/get-encryption-key [:init.callback/get-encryption-key-success]} - (transactions/stop-sync) - (navigation/navigate-to-clean :login {}) - (transport/stop-whisper)))) + (fx/merge cofx + {:keychain/clear-user-password (get-in db [:account/account :address]) + :dev-server/stop nil} + (transactions/stop-sync) + (transport/stop-whisper) + (init/initialize-app-db) + (init/load-accounts-and-initialize-views) + (node/stop))) (fx/defn show-logout-confirmation [_] {:ui/show-confirmation diff --git a/src/status_im/accounts/recover/core.cljs b/src/status_im/accounts/recover/core.cljs index 068bfde0cc..d4f28228a4 100644 --- a/src/status_im/accounts/recover/core.cljs +++ b/src/status_im/accounts/recover/core.cljs @@ -10,7 +10,8 @@ [status-im.utils.identicon :as identicon] [status-im.utils.security :as security] [status-im.utils.types :as types] - [status-im.utils.fx :as fx])) + [status-im.utils.fx :as fx] + [status-im.node.core :as node])) (defn check-password-errors [password] (cond (string/blank? password) :required-field @@ -80,10 +81,15 @@ {:db (dissoc db :accounts/recover)} (validate-recover-result data password)))) -(fx/defn recover-account [{:keys [db]}] - (let [{:keys [password passphrase]} (:accounts/recover db)] - {:db (assoc-in db [:accounts/recover :processing?] true) - :accounts.recover/recover-account [(security/mask-data passphrase) password]})) +(fx/defn recover-account + [{:keys [db random-guid-generator] :as cofx}] + (fx/merge + cofx + {:db (-> db + (assoc-in [:accounts/recover :processing?] true) + (assoc :node/on-ready :recover-account) + (assoc :accounts/new-installation-id (random-guid-generator)))} + (node/initialize nil))) (fx/defn recover-account-with-checks [{:keys [db] :as cofx}] (let [{:keys [passphrase processing?]} (:accounts/recover db)] diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index ad6467e1f6..9ed7fbd476 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -118,10 +118,13 @@ (re-frame/inject-cofx :data-store/all-browsers) (re-frame/inject-cofx :data-store/all-dapp-permissions)] (fn [{:keys [db] :as cofx} [_ address]] - (fx/merge - cofx - (node/initialize (get-in db [:accounts/login :address])) - (init/initialize-account address)))) + (let [{:node/keys [status]} db] + (fx/merge + cofx + (if (= status :started) + (accounts.login/login) + (node/initialize (get-in db [:accounts/login :address]))) + (init/initialize-account address))))) (handlers/register-handler-fx :init.callback/keychain-reset @@ -176,6 +179,7 @@ (handlers/register-handler-fx :accounts.create.ui/next-step-pressed + [(re-frame/inject-cofx :random-guid-generator)] (fn [cofx [_ step password password-confirm]] (accounts.create/next-step cofx step password password-confirm))) @@ -231,11 +235,13 @@ (handlers/register-handler-fx :accounts.recover.ui/sign-in-button-pressed + [(re-frame/inject-cofx :random-guid-generator)] (fn [cofx _] (accounts.recover/recover-account-with-checks cofx))) (handlers/register-handler-fx :accounts.recover.ui/recover-account-confirmed + [(re-frame/inject-cofx :random-guid-generator)] (fn [cofx _] (accounts.recover/recover-account cofx))) @@ -252,7 +258,7 @@ (handlers/register-handler-fx :accounts.login.ui/password-input-submitted (fn [cofx _] - (accounts.login/user-login cofx))) + (accounts.login/user-login cofx false))) (handlers/register-handler-fx :accounts.login.callback/login-success @@ -293,6 +299,7 @@ (handlers/register-handler-fx :accounts.logout.ui/logout-confirmed + [(re-frame/inject-cofx :data-store/get-all-accounts)] (fn [cofx _] (accounts.logout/logout cofx))) @@ -300,6 +307,7 @@ (handlers/register-handler-fx :accounts.update.callback/save-settings-success + [(re-frame/inject-cofx :data-store/get-all-accounts)] (fn [cofx _] (accounts.logout/logout cofx))) diff --git a/src/status_im/init/core.cljs b/src/status_im/init/core.cljs index e1ab7bb575..1b3511d2e5 100644 --- a/src/status_im/init/core.cljs +++ b/src/status_im/init/core.cljs @@ -99,8 +99,7 @@ [cofx encryption-key] (fx/merge cofx {:init/init-store encryption-key} - (initialize-app-db) - (node/initialize nil))) + (initialize-app-db))) (fx/defn set-device-uuid [{:keys [db]} device-uuid] diff --git a/src/status_im/node/core.cljs b/src/status_im/node/core.cljs index ae1b47c98f..f9597e1793 100644 --- a/src/status_im/node/core.cljs +++ b/src/status_im/node/core.cljs @@ -77,11 +77,13 @@ current-fleet-key (fleet/current-fleet db address) current-fleet (get fleet/fleets current-fleet-key) rendezvous-nodes (pick-nodes 3 (vals (:rendezvous current-fleet))) - {:keys [network - installation-id - settings - bootnodes - networks]} (get accounts address) + {:keys [network installation-id settings bootnodes networks]} + (merge + {:network config/default-network + :networks (:networks/networks db) + :settings (constants/default-account-settings) + :installation-id (get db :accounts/new-installation-id)} + (get accounts address)) use-custom-bootnodes (get-in settings [:bootnodes network]) log-level (or (:log-level settings) config/log-level-status-go)] @@ -122,13 +124,6 @@ :always (add-log-level log-level)))) -(defn get-node-config [db network] - (-> (get-in (:networks/networks db) [network :config]) - (get-base-node-config) - (assoc :PFSEnabled false - :NoDiscovery true) - (add-log-level config/log-level-status-go))) - (fx/defn update-sync-state [{:keys [db]} error sync-state] {:db (assoc db :node/chain-sync-state @@ -146,9 +141,7 @@ (let [network (if address (get-account-network db address) (:network db)) - node-config (if address - (get-account-node-config db address) - (get-node-config db network)) + node-config (get-account-node-config db address) node-config-json (types/clj->json node-config)] (log/info "Node config: " node-config-json) {:db (assoc db @@ -156,7 +149,7 @@ :node/status :starting) :node/start node-config-json})) -(defn stop +(fx/defn stop [{:keys [db]}] {:db (assoc db :node/status :stopping) :node/stop nil}) @@ -165,7 +158,7 @@ [{{:node/keys [status] :as db} :db :as cofx} address] (let [restart {:db (assoc db :node/restart? true :node/address address)}] (case status - :started (stop cofx) + :started nil :starting (do (when utils.platform/desktop? (status/stop-node)) diff --git a/src/status_im/signals/core.cljs b/src/status_im/signals/core.cljs index 5b79b20af7..30b4b35ca8 100644 --- a/src/status_im/signals/core.cljs +++ b/src/status_im/signals/core.cljs @@ -8,11 +8,13 @@ [status-im.transport.message.core :as transport.message] [status-im.utils.fx :as fx] [status-im.utils.types :as types] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [status-im.utils.security :as security])) (fx/defn status-node-started [{db :db :as cofx}] - (let [{:node/keys [restart? address]} db + (let [{:node/keys [restart? address on-ready] + :accounts/keys [create]} db can-login? (and (not restart?) (:password (accounts.db/credentials cofx)))] (fx/merge cofx @@ -24,14 +26,19 @@ (when restart? (node/initialize address)) (when can-login? - (accounts.login/login))))) + (accounts.login/login)) + (when (= :create-account on-ready) + (fn [_] + {:accounts.create/create-account (:password create)})) + (when (= :recover-account on-ready) + (fn [{:keys [db]}] + (let [{:keys [password passphrase]} (:accounts/recover db)] + {:accounts.recover/recover-account + [(security/mask-data passphrase) password]})))))) (fx/defn status-node-stopped - [{db :db :as cofx}] - (let [{:keys [address]} (accounts.db/credentials cofx)] - (fx/merge cofx - {:db (assoc db :node/status :stopped)} - (node/start address)))) + [{db :db}] + {:db (assoc db :node/status :stopped)}) (fx/defn status-module-initialized [{:keys [db]}] {:db (assoc db :status-module-initialized? true) diff --git a/test/cljs/status_im/test/accounts/recover/core.cljs b/test/cljs/status_im/test/accounts/recover/core.cljs index aee2c57a8b..393cd949ab 100644 --- a/test/cljs/status_im/test/accounts/recover/core.cljs +++ b/test/cljs/status_im/test/accounts/recover/core.cljs @@ -95,29 +95,24 @@ (models/validate-password {:db {:accounts/recover {:password "thisisapaswoord"}}})))) (deftest recover-account - (let [new-cofx (models/recover-account {:db {:accounts/recover + (let [new-cofx (models/recover-account {:random-guid-generator (constantly "random") + :db {:accounts/recover {:passphrase "game buzz method pretty zeus fat quit display velvet unveil marine crater" :password "thisisapaswoord"}}})] - (is (= {:accounts/recover {:passphrase "game buzz method pretty zeus fat quit display velvet unveil marine crater" - :password "thisisapaswoord" - :processing? true}} - (:db new-cofx))) - (is (= security/MaskedData - (-> new-cofx :accounts.recover/recover-account first type))) - (is (= "thisisapaswoord" (-> new-cofx :accounts.recover/recover-account second))))) + (is (contains? new-cofx :node/start)) + (is (= "random" (get-in new-cofx [:db :accounts/new-installation-id]))) + (is (= :recover-account (get-in new-cofx [:db :node/on-ready]))))) (deftest recover-account-with-checks - (let [new-cofx (models/recover-account-with-checks {:db {:accounts/recover + (let [new-cofx (models/recover-account-with-checks {:random-guid-generator (constantly "random") + :db {:accounts/recover {:passphrase "game buzz method pretty olympic fat quit display velvet unveil marine crater" :password "thisisapaswoord"}}})] - (is (= {:accounts/recover {:passphrase "game buzz method pretty olympic fat quit display velvet unveil marine crater" - :password "thisisapaswoord" - :processing? true}} - (:db new-cofx))) - (is (= security/MaskedData - (-> new-cofx :accounts.recover/recover-account first type))) - (is (= "thisisapaswoord" (-> new-cofx :accounts.recover/recover-account second)))) - (let [new-cofx (models/recover-account-with-checks {:db {:accounts/recover + (is (contains? new-cofx :node/start)) + (is (= "random" (get-in new-cofx [:db :accounts/new-installation-id]))) + (is (= :recover-account (get-in new-cofx [:db :node/on-ready])))) + (let [new-cofx (models/recover-account-with-checks {:random-guid-generator (constantly "random") + :db {:accounts/recover {:passphrase "game buzz method pretty zeus fat quit display velvet unveil marine crater" :password "thisisapaswoord"}}})] (is (= (i18n/label :recovery-typo-dialog-title) (-> new-cofx :ui/show-confirmation :title))) diff --git a/test/cljs/status_im/test/node/core.cljs b/test/cljs/status_im/test/node/core.cljs index ab48ae6e40..27f2680631 100644 --- a/test/cljs/status_im/test/node/core.cljs +++ b/test/cljs/status_im/test/node/core.cljs @@ -13,34 +13,18 @@ (let [address "a" cofx {:db {:accounts/accounts {address {:installation-id "id"}}}}] (testing "installation-id" - (testing "the user is not logged in" - (let [actual (parse-node-config (node/start cofx nil))] - (is (not (:InstallationID actual))))) - (testing "the user is logged in" - (let [actual (parse-node-config (node/start cofx address))] - (is (= "id" (:InstallationID actual)))))) + (let [actual (parse-node-config (node/start cofx address))] + (is (= "id" (:InstallationID actual))))) (testing "pfs & group chats disabled" (with-redefs [config/pfs-encryption-enabled? false config/group-chats-enabled? false] - (testing "the user is not logged in" - (let [actual (parse-node-config (node/start cofx nil))] - (is (not (:PFSEnabled actual))))) - (testing "the user is logged in" - (let [actual (parse-node-config (node/start cofx address))] - (is (not (:PFSEnabled actual)))))) + (let [actual (parse-node-config (node/start cofx address))] + (is (not (:PFSEnabled actual))))) (testing "pfs is enabled" (with-redefs [config/pfs-encryption-enabled? true] - (testing "the user is not logged in" - (let [actual (parse-node-config (node/start cofx nil))] - (is (not (:PFSEnabled actual))))) - (testing "the user is logged in" - (let [actual (parse-node-config (node/start cofx address))] - (is (:PFSEnabled actual)))))) + (let [actual (parse-node-config (node/start cofx address))] + (is (:PFSEnabled actual))))) (testing "group chats is enabled" (with-redefs [config/group-chats-enabled? true] - (testing "the user is not logged in" - (let [actual (parse-node-config (node/start cofx nil))] - (is (not (:PFSEnabled actual))))) - (testing "the user is logged in" - (let [actual (parse-node-config (node/start cofx address))] - (is (:PFSEnabled actual))))))))) + (let [actual (parse-node-config (node/start cofx address))] + (is (:PFSEnabled actual))))))))