Skip to content

Commit

Permalink
WIP: keypair cleaned
Browse files Browse the repository at this point in the history
  • Loading branch information
ulisesmac committed May 20, 2024
1 parent c3fd425 commit 693d175
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 110 deletions.
3 changes: 1 addition & 2 deletions src/status_im/common/enter_seed_phrase/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@
(swap! seed-phrase clean-seed-phrase)
(if recovering-keypair?
(rf/dispatch [:wallet/seed-phrase-entered
(security/mask-data
@seed-phrase)
(security/mask-data @seed-phrase)
set-invalid-seed-phrase])
(rf/dispatch [:onboarding/seed-phrase-entered
(security/mask-data @seed-phrase)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns status-im.contexts.wallet.add-account.create-account.events
(:require [camel-snake-kebab.extras :as cske]
[clojure.string :as string]
[status-im.contexts.wallet.add-account.create-account.utils :as create-account.utils]
[status-im.contexts.wallet.data-store :as data-store]
[utils.re-frame :as rf]
[utils.security.core :as security]
Expand All @@ -23,15 +25,15 @@

(rf/reg-event-fx :wallet/confirm-account-origin confirm-account-origin)

(defn store-seed-phrase
(defn store-new-seed-phrase
[{:keys [db]} [{:keys [seed-phrase random-phrase]}]]
{:db (-> db
(assoc-in [:wallet :ui :generate-keypair :seed-phrase] seed-phrase)
(assoc-in [:wallet :ui :generate-keypair :random-phrase] random-phrase))
{:db (update-in db [:wallet :ui :create-account :new-keypair] assoc
:seed-phrase seed-phrase
:random-phrase random-phrase)
:fx [[:dispatch-later [{:ms 20
:dispatch [:navigate-to :screen/wallet.confirm-backup]}]]]})

(rf/reg-event-fx :wallet/store-seed-phrase store-seed-phrase)
(rf/reg-event-fx :wallet/store-new-seed-phrase store-new-seed-phrase)

(defn seed-phrase-validated
[{:keys [db]} [seed-phrase]]
Expand All @@ -50,34 +52,37 @@

(rf/reg-event-fx :wallet/seed-phrase-entered seed-phrase-entered)

(defn store-keypair-generated
[{:keys [db]} [{:keys [new-keypair keypair-name]}]]
{:db (-> db
(assoc-in [:wallet :ui :generate-keypair :new-keypair] new-keypair)
(assoc-in [:wallet :ui :generate-keypair :keypair-name] keypair-name)
(update-in [:wallet :ui :generate-keypair] dissoc :seed-phrase :random-phrase))
:fx [[:dispatch [:navigate-back-to :screen/wallet.create-account]]]})
(defn store-account-generated
[{:keys [db]} [{:keys [new-account-data keypair-name]}]]
(let [new-account (update new-account-data :mnemonic security/mask-data)]
{:db (-> db
(update-in [:wallet :ui :create-account :new-keypair] assoc
:new-account-data new-account
:keypair-name keypair-name)
(update-in [:wallet :ui :create-account :new-keypair] dissoc
:seed-phrase :random-phrase))
:fx [[:dispatch [:navigate-back-to :screen/wallet.create-account]]]}))

(rf/reg-event-fx :wallet/store-keypair-generated store-keypair-generated)
(rf/reg-event-fx :wallet/store-account-generated store-account-generated)

(defn generate-new-keypair
(defn generate-account-for-keypair
[{:keys [db]} [{:keys [keypair-name]}]]
(let [seed-phrase (-> db :wallet :ui :generate-keypair :seed-phrase)]
(let [seed-phrase (-> db :wallet :ui :create-account :new-keypair :seed-phrase)]
{:fx [[:effects.wallet/create-account-from-mnemonic
{:mnemonic-phrase (security/safe-unmask-data seed-phrase)
:paths ["m/44'/60'/0'/0/0"]
:on-success (fn [new-keypair]
(rf/dispatch [:wallet/store-keypair-generated
{:new-keypair new-keypair
:keypair-name keypair-name}]))}]]}))
:paths ["m/44'/60'/0'/0/0"] ;; TODO: move to a constant
:on-success (fn [new-account-data]
(rf/dispatch [:wallet/store-account-generated
{:new-account-data new-account-data
:keypair-name keypair-name}]))}]]}))

(rf/reg-event-fx :wallet/generate-new-keypair generate-new-keypair)
(rf/reg-event-fx :wallet/generate-account-for-keypair generate-account-for-keypair)

(defn clear-new-keypair
(defn clear-create-account-data
[{:keys [db]}]
{:db (update-in db [:wallet :ui] dissoc :generate-keypair)})
{:db (update-in db [:wallet :ui :create-account] dissoc :new-keypair)})

(rf/reg-event-fx :wallet/clear-new-keypair clear-new-keypair)
(rf/reg-event-fx :wallet/clear-create-account clear-create-account-data)

(defn get-derived-addresses
[{:keys [db]} [{:keys [password derived-from paths]}]]
Expand Down Expand Up @@ -129,3 +134,36 @@
:wallet/clear-private-key-data
(fn [{:keys [db]} _]
{:db (update-in db [:wallet :ui :create-account] dissoc :private-key :public-address)}))

(rf/reg-event-fx
:wallet/create-keypair-with-account
(fn [{db :db} [password account-preferences]]
(let [{:keys [keypair-name
new-account-data]} (-> db :wallet :ui :create-account :new-keypair)
keypair-with-account (create-account.utils/prepare-new-account
{:keypair-name keypair-name
:account-data new-account-data
:account-preferences account-preferences})
new-address (some-> new-account-data
(create-account.utils/first-derived-account)
(:address)
(string/lower-case))
unmasked-password (security/safe-unmask-data password)]
{:fx [[:json-rpc/call
[{:method "accounts_addKeypair"
:params [unmasked-password keypair-with-account]
:on-success [:wallet/add-account-success new-address]
:on-error #(log/error "Failed to add Keypair and create account" %)}]]]})))

(defn import-and-create-keypair-with-account
[{db :db} [{:keys [password account-preferences]}]]
(let [account-data (-> db :wallet :ui :create-account :new-keypair :new-account-data)
unmasked-mnemonic (security/safe-unmask-data (:mnemonic account-data))
unmasked-password (security/safe-unmask-data password)]
{:fx [[:json-rpc/call
[{:method "accounts_importMnemonic"
:params [unmasked-mnemonic unmasked-password]
:on-success #(rf/dispatch
[:wallet/create-keypair-with-account password account-preferences])}]]]}))

(rf/reg-event-fx :wallet/import-and-create-keypair-with-account import-and-create-keypair-with-account)
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
:import-private-key
(not-implemented/alert)

:new-key-pair
(rf/dispatch [:wallet/generate-new-keypair
:new-keypair
(rf/dispatch [:wallet/generate-account-for-keypair
{:keypair-name key-pair-name}])

(js/alert "Unknown workflow")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@
:button-one-label (i18n/label :t/i-have-written)
:button-one-props {:disabled? (some false? (vals @checked?))
:customization-color customization-color
:on-press #(rf/dispatch [:wallet/store-seed-phrase
{:seed-phrase (security/mask-data
@seed-phrase)
:on-press #(rf/dispatch [:wallet/store-new-seed-phrase
{:seed-phrase (security/mask-data @seed-phrase)
:random-phrase @random-phrase}])}}]
[quo/text
{:size :paragraph-2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
quiz-index (reagent/atom 0)
incorrect-count (reagent/atom 0)
show-error? (reagent/atom false)
{:keys [seed-phrase random-phrase]} (rf/sub [:wallet/generate-keypair])
{:keys [seed-phrase random-phrase]} (rf/sub [:wallet/create-account-new-keypair])
unmasked-seed-phrase (security/safe-unmask-data seed-phrase)]
(fn []
(let [current-word-index (get random-indices
Expand All @@ -83,7 +83,7 @@
(rf/dispatch
[:navigate-to
:screen/wallet.keypair-name
{:workflow :new-key-pair}])))
{:workflow :new-keypair}])))
(do
(when (> @incorrect-count 0)
(rf/dispatch [:show-bottom-sheet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(ns status-im.contexts.wallet.add-account.create-account.utils)
(ns status-im.contexts.wallet.add-account.create-account.utils
(:require [clojure.string :as string]))

(defn prepare-new-keypair
[{{:keys [keyUid derived address]} :new-keypair
keypair-name :keypair-name
{:keys [account-name account-color emoji]} :new-account}]
(defn prepare-new-account
[{keypair-name :keypair-name
{:keys [keyUid derived address]} :account-data
{:keys [account-name account-color emoji]} :account-preferences}]
(let [account-to-create (val (first derived))]
{:key-uid keyUid
:name keypair-name
Expand Down Expand Up @@ -39,6 +40,9 @@
:path "m/44'/60'/0'/0/0"
:address (:address new-keypair)}]))

(defn first-derived-account [account-data]
(-> account-data :derived first val))

{"privateKey" "0x61c4d1b891ebee9af38e5e40ef589871b7d18b1bf844f497948045b8479490b3",
"publicKey" "0x044cd08b14ac81e4ad3b1f5ac95c8c68ade831f8a33c9026e5564f572b837a73c85ae5c953f336a3904cfcfd73ae2eb906a1906bd51199168d1269bb195a8a5c35"
"address" "0x073Fd5b6f7cd988dAa93ae227d92F1AD2e5f59a7",
Expand Down
77 changes: 41 additions & 36 deletions src/status_im/contexts/wallet/add_account/create_account/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@
:description-props {:text formatted-path}}]))

(defn- create-new-keypair-account
[{:keys [password new-account-data]}]
(rf/dispatch [:wallet/add-keypair-and-create-account
{:password password
:new-account-data new-account-data}]))
[{:keys [password account-preferences]}]
(rf/dispatch [:wallet/import-and-create-keypair-with-account
{:password password
:account-preferences account-preferences}]))

(defn create-existing-keypair-account
[{password :password
selected-keypair :selected-keypair
derivation-path :derivation-path
{:keys [account-name account-color emoji]} :new-account-data}]
{:keys [account-name account-color emoji]} :account-preferences}]
(rf/dispatch
[:wallet/derive-address-and-add-account {:password password
:address (:derived-from selected-keypair)
Expand All @@ -88,38 +88,43 @@
derivation-path (reagent/atom "")
set-derivation-path #(reset! derivation-path %)]
(fn []
(let [theme (quo.theme/use-theme)
customization-color (rf/sub [:profile/customization-color])
{:keys [new-keypair-name
new-keypair]} (rf/sub [:wallet/generated-keypair])
selected-keypair (rf/sub [:wallet/selected-keypair])
primary? (rf/sub [:wallet/selected-primary-keypair?])
placeholder (i18n/label :t/default-account-placeholder)
keypair-title (or new-keypair-name
(if primary?
(i18n/label :t/keypair-title
{:name (:name selected-keypair)})
(:name selected-keypair)))
on-auth-success (fn [password]
(let [new-account-data {:account-name @account-name
:account-color @account-color
:emoji @emoji}]
(if new-keypair
(create-new-keypair-account
{:password password
:new-account-data new-account-data})
(create-existing-keypair-account
{:password password
:selected-keypair selected-keypair
:derivation-path @derivation-path
:new-account-data new-account-data}))))]
(let [theme (quo.theme/use-theme)
customization-color (rf/sub [:profile/customization-color])
{:keys [new-account-data
keypair-name]} (rf/sub [:wallet/create-account-new-keypair])
selected-keypair (rf/sub [:wallet/selected-keypair])
primary? (rf/sub [:wallet/selected-primary-keypair?])
placeholder (i18n/label :t/default-account-placeholder)
keypair-title (or keypair-name
(if primary?
(i18n/label :t/keypair-title
{:name (:name selected-keypair)})
(:name selected-keypair)))
adding-new-keypair? (some? new-account-data)
on-auth-success (fn [password]
(let [account-preferences {:account-name @account-name
:account-color @account-color
:emoji @emoji}]
(if adding-new-keypair?
(create-new-keypair-account
{:password password
:account-preferences account-preferences})
(create-existing-keypair-account
{:password password
:selected-keypair selected-keypair
:derivation-path @derivation-path
:account-preferences account-preferences}))))]
(rn/use-effect
#(rf/dispatch
[:wallet/new-derivation-path {:on-success set-derivation-path
:keypair-uid (:key-uid selected-keypair)}])
(fn []
(if adding-new-keypair?
;; TODO: reference constant
(set-derivation-path "m/44'/60'/0'/0/0")
(rf/dispatch
[:wallet/new-derivation-path {:on-success set-derivation-path
:keypair-uid (:key-uid selected-keypair)}])))
[(:key-uid selected-keypair)])

(rn/use-unmount #(rf/dispatch [:wallet/clear-new-keypair]))
#_(rn/use-unmount #(rf/dispatch [:wallet/clear-create-account]))

[floating-button-page/view
{:gradient-cover? true
Expand All @@ -142,7 +147,7 @@
:disabled? (or (empty? @account-name)
(= "" @derivation-path))
:container-style (style/slide-button-container bottom)
:dependencies [new-keypair]}]}
:dependencies [new-account-data]}]}
[rn/view {:style style/account-avatar-container}
[quo/account-avatar
{:customization-color @account-color
Expand Down Expand Up @@ -187,6 +192,6 @@
:label (i18n/label :t/origin)
:data (get-keypair-data {:title keypair-title
:primary-keypair? primary?
:new-keypair? (boolean new-keypair)
:new-keypair? adding-new-keypair?
:derivation-path @derivation-path
:customization-color customization-color})}]]))))
Loading

0 comments on commit 693d175

Please sign in to comment.