Skip to content

Commit

Permalink
Fix account creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Vlasov committed Jun 10, 2019
1 parent 6687913 commit 5960522
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 63 deletions.
106 changes: 60 additions & 46 deletions src/status_im/accounts/create/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@
(assoc cofx :status (rand-nth statuses/data)))

(defn create-account! [password]
(log/info "#create-account!" password)
(status/create-account
password
#(re-frame/dispatch [:accounts.create.callback/create-account-success (types/json->clj %) password])))

;;;; Handlers
(defn create-account
[{:keys [db random-guid-generator] :as cofx}]
[{:keys [db] :as cofx}]
(log/info "creating account with password:" (get-in db [:intro-wizard :key-code]))
(fx/merge
cofx
{:db (-> db
(update :accounts/create assoc
:password (get-in db [:intro-wizard :key-code])
:step :account-creating
:error nil)
(assoc :node/on-ready :create-account
:accounts/new-installation-id (random-guid-generator)))}
:accounts/new-installation-id (random/guid)))}
(node/initialize nil)))

(fx/defn add-account
Expand All @@ -60,44 +63,6 @@
{:db (assoc-in db [:accounts/accounts address] enriched-account)
:data-store/base-tx [(accounts-store/save-account-tx enriched-account)]}))

(fx/defn on-account-created
[{:keys [signing-phrase
status
db] :as cofx}
{:keys [pubkey address mnemonic installation-id
keycard-instance-uid keycard-key-uid keycard-pairing keycard-paired-on]}
password
{:keys [seed-backed-up? login? new-account?] :or {login? true}}]
(let [normalized-address (utils.hex/normalize-hex address)
account {:public-key pubkey
:installation-id (or installation-id (get-in db [:accounts/new-installation-id]))
:address normalized-address
:name (gfycat/generate-gfy pubkey)
:status status
:signed-up? true
:desktop-notifications? false
:photo-path (identicon/identicon pubkey)
:signing-phrase signing-phrase
:seed-backed-up? seed-backed-up?
:mnemonic mnemonic
:keycard-instance-uid keycard-instance-uid
:keycard-key-uid keycard-key-uid
:keycard-pairing keycard-pairing
:keycard-paired-on keycard-paired-on
:settings (constants/default-account-settings)
:syncing-on-mobile-network? false
:remember-syncing-choice? false
:new-account? new-account?}]
(log/debug "account-created")
(when-not (string/blank? pubkey)
(fx/merge cofx
{:db (assoc db :accounts/login {:address normalized-address
:password password
:processing true})}
(add-account account)
(when login?
(accounts.login/user-login true))))))

(defn reset-account-creation [{db :db}]
{:db (update db :accounts/create assoc
:step :enter-password
Expand Down Expand Up @@ -166,16 +131,22 @@
(navigation/navigate-to-clean :intro nil)))))

(fx/defn intro-step-forward [{:keys [db] :as cofx} {:keys [skip?] :as opts}]
(let [step (get-in db [:intro-wizard :step])]
(let [step (get-in db [:intro-wizard :step])
_ (log/info "#step-forward" step)]

(cond (= step 7)
(fx/merge {:db (dissoc db :intro-wizard)}
(navigation/navigate-to-cofx :welcome nil))
(create-account cofx)
#_(fx/merge cofx
(navigation/navigate-to-cofx :welcome nil)
create-account)
#_(fx/merge {:db (dissoc db :intro-wizard)}
(navigation/navigate-to-cofx :welcome nil))
(= step 1)
{:db (assoc-in db [:intro-wizard :generating-keys?] true)
:intro-wizard/new-onboarding {:n 5 :mnemonic-length 12}}

(and (= step 5)
(not (:accounts/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])))
{:db (assoc-in db [:intro-wizard :confirm-failure?] true)}
Expand All @@ -185,9 +156,52 @@
(assoc-in [:intro-wizard :stored-key-code] (get-in db [:intro-wizard :key-code]))
(assoc-in [:intro-wizard :key-code] nil)
(assoc-in [:intro-wizard :step] 5))}
:else (fx/merge {:db (assoc-in db [:intro-wizard :step]
(inc step))}
(navigation/navigate-to-cofx :intro-wizard nil)))))
:else (do
(log/info "step-forward else")
(fx/merge {:db (assoc-in db [:intro-wizard :step]
(inc step))}
(navigation/navigate-to-cofx :intro-wizard nil))))))

(fx/defn on-account-created
[{:keys [signing-phrase
status
db] :as cofx}
{:keys [pubkey address mnemonic installation-id
keycard-instance-uid keycard-key-uid keycard-pairing keycard-paired-on] :as result}
password
{:keys [seed-backed-up? login? new-account?] :or {login? true}}]
(log/info "#on-account-created" result)
(let [normalized-address (utils.hex/normalize-hex address)
account {:public-key pubkey
:installation-id (or installation-id (get-in db [:accounts/new-installation-id]))
:address normalized-address
:name (gfycat/generate-gfy pubkey)
:status status
:signed-up? true
:desktop-notifications? false
:photo-path (identicon/identicon pubkey)
:signing-phrase signing-phrase
:seed-backed-up? seed-backed-up?
:mnemonic mnemonic
:keycard-instance-uid keycard-instance-uid
:keycard-key-uid keycard-key-uid
:keycard-pairing keycard-pairing
:keycard-paired-on keycard-paired-on
:settings (constants/default-account-settings)
:syncing-on-mobile-network? false
:remember-syncing-choice? false
:new-account? new-account?}]
(log/info "account-created" account)
(when-not (string/blank? pubkey)
(fx/merge cofx
{:db (assoc db :accounts/login {:address normalized-address
:password password
:processing true})}
(add-account account)
(when login?
(log/info "#before user-login" (:intro-wizard db))
(accounts.login/user-login true))))))

(re-frame/reg-fx
:intro-wizard/new-onboarding
(fn [{:keys [n mnemonic-length]}]
Expand Down
32 changes: 16 additions & 16 deletions src/status_im/init/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,22 @@
(fx/defn initialize-views
[cofx]
(let [{{:accounts/keys [accounts] :as db} :db} cofx]
(if true #_(empty? accounts)
(navigation/navigate-to-cofx 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)))))
(if (empty? accounts)
(navigation/navigate-to-cofx 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"
Expand Down
3 changes: 2 additions & 1 deletion src/status_im/ui/screens/intro/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,6 @@
4 [create-code wizard-state]
5 [confirm-code wizard-state]
6 [enable-fingerprint]
7 [enable-notifications])
7 [enable-notifications]
nil nil)
[bottom-bar wizard-state]]]))

0 comments on commit 5960522

Please sign in to comment.