From af644e5b113e3c4db7cbd8130fc0ce85c7b0d3b1 Mon Sep 17 00:00:00 2001 From: Sean Hagstrom Date: Thu, 13 Jun 2024 10:29:10 +0100 Subject: [PATCH] feat: add support for importing key-pair with private-key --- .../add_account/create_account/events.cljs | 70 ++++++++++++------- .../create_account/events_test.cljs | 4 +- .../create_account/key_pair_name/view.cljs | 2 +- .../add_account/create_account/utils.cljs | 14 ++-- .../add_account/create_account/view.cljs | 10 +-- 5 files changed, 61 insertions(+), 39 deletions(-) diff --git a/src/status_im/contexts/wallet/add_account/create_account/events.cljs b/src/status_im/contexts/wallet/add_account/create_account/events.cljs index 1bbe0ae5e4..8ba32e4306 100644 --- a/src/status_im/contexts/wallet/add_account/create_account/events.cljs +++ b/src/status_im/contexts/wallet/add_account/create_account/events.cljs @@ -68,7 +68,7 @@ (rf/reg-event-fx :wallet/seed-phrase-entered seed-phrase-entered) -(defn store-account-generated +(defn store-account-generated-with-mnemonic [{:keys [db]} [{:keys [new-account-data keypair-name]}]] (let [new-account (update new-account-data :mnemonic security/mask-data)] {:db (-> db @@ -82,21 +82,21 @@ :random-phrase)) :fx [[:dispatch [:navigate-back-to :screen/wallet.create-account]]]})) -(rf/reg-event-fx :wallet/store-account-generated store-account-generated) +(rf/reg-event-fx :wallet/store-account-generated-with-mnemonic store-account-generated-with-mnemonic) -(defn generate-account-for-keypair - [{:keys [db]} [{:keys [keypair-name workflow]}]] +(defn generate-account-for-keypair-with-mnemonic + [{:keys [db]} [{:keys [keypair-name]}]] (let [seed-phrase (-> db :wallet :ui :create-account :new-keypair :seed-phrase)] - {:db (assoc-in db [:wallet :ui :create-account :new-keypair :workflow] workflow) - :fx [[:effects.wallet/create-account-from-mnemonic + {:fx [[:effects.wallet/create-account-from-mnemonic {:mnemonic-phrase (security/safe-unmask-data seed-phrase) :paths [constants/path-default-wallet] :on-success (fn [new-account-data] - (rf/dispatch [:wallet/store-account-generated + (rf/dispatch [:wallet/store-account-generated-with-mnemonic {:new-account-data new-account-data :keypair-name keypair-name}]))}]]})) -(rf/reg-event-fx :wallet/generate-account-for-keypair generate-account-for-keypair) +(rf/reg-event-fx :wallet/generate-account-for-keypair-with-mnemonic + generate-account-for-keypair-with-mnemonic) (defn clear-create-account-data [{:keys [db]}] @@ -157,14 +157,19 @@ (rf/reg-event-fx :wallet/create-keypair-with-account (fn [{db :db} [password account-preferences]] - (let [{:keys [keypair-name + (let [{:keys [workflow + keypair-name new-account-data]} (-> db :wallet :ui :create-account :new-keypair) + keypair-type (if (= workflow :workflow-new-keypair/import-private-key) + :key + :seed) keypair-with-account (create-account.utils/prepare-new-account {:keypair-name keypair-name + :keypair-type keypair-type :account-data new-account-data :account-preferences account-preferences}) new-address (some-> new-account-data - (create-account.utils/first-derived-account) + (create-account.utils/first-derived-account keypair-type) (:address) (string/lower-case)) unmasked-password (security/safe-unmask-data password)] @@ -174,7 +179,7 @@ :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 +(defn import-mnemonic-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)) @@ -185,7 +190,8 @@ :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) +(rf/reg-event-fx :wallet/import-mnemonic-and-create-keypair-with-account + import-mnemonic-and-create-keypair-with-account) (rf/reg-event-fx :wallet/derive-address-and-add-account @@ -217,24 +223,38 @@ {:json-rpc/call [{:method "accounts_importPrivateKey" :params [unmasked-private-key unmasked-password] - :on-success [:wallet/import-and-create-keypair-with-account - {:passord password - :account-preferences account-preferences}] + :on-success [:wallet/create-keypair-with-account password account-preferences] :on-error #(log/error "Failed to import private key" %)}]})) (rf/reg-event-fx :wallet/import-private-key-and-create-keypair-with-account import-private-key-and-create-keypair-with-account) +(defn store-account-generated-with-private-key + [{:keys [db]} [{:keys [new-account-data keypair-name]}]] + {:db (-> db + (update-in [:wallet :ui :create-account :new-keypair] + assoc + :new-account-data new-account-data + :keypair-name keypair-name) + (update-in [:wallet :ui :create-account :new-keypair] + dissoc + :private-key)) + :fx [[:dispatch [:navigate-back-to :screen/wallet.create-account]]]}) + +(rf/reg-event-fx :wallet/store-account-generated-with-private-key + store-account-generated-with-private-key) + (rf/reg-event-fx :wallet/import-private-key-and-generate-account-for-keypair - (fn [_ [{:keys [keypair-name]}]] - {:effects.wallet/get-random-mnemonic - {:on-success - #(rf/dispatch - [:wallet/set-seed-phrase - {:seed-phrase % - :on-success [:dispatch - [:wallet/generate-account-for-keypair - {:keypair-name keypair-name - :workflow :workflow-new-keypair/import-private-key}]]}])}})) + (fn [{:keys [db]} [{:keys [keypair-name]}]] + (let [private-key (get-in db [:wallet :ui :create-account :private-key])] + {:db (assoc-in db + [:wallet :ui :create-account :new-keypair :workflow] + :workflow-new-keypair/import-private-key) + :fx [[:effects.wallet/create-account-from-private-key + {:private-key private-key + :on-success (fn [new-account-data] + (rf/dispatch [:wallet/store-account-generated-with-mnemonic + {:keypair-name keypair-name + :new-account-data new-account-data}]))}]]}))) diff --git a/src/status_im/contexts/wallet/add_account/create_account/events_test.cljs b/src/status_im/contexts/wallet/add_account/create_account/events_test.cljs index dcc30d0a98..efdf6b3969 100644 --- a/src/status_im/contexts/wallet/add_account/create_account/events_test.cljs +++ b/src/status_im/contexts/wallet/add_account/create_account/events_test.cljs @@ -37,7 +37,7 @@ {:new-account-data {"test" "data" :mnemonic masked-mnemonic} :keypair-name "new-keypair-name"}}}}} - effects (events/store-account-generated {:db db} props) + effects (events/store-account-generated-with-mnemonic {:db db} props) result-db (:db effects) remove-mnemonic #(update-in % [:wallet :ui :create-account :new-keypair :new-account-data] @@ -61,7 +61,7 @@ expected-effects [[:effects.wallet/create-account-from-mnemonic {:mnemonic-phrase "test-secret" :paths [constants/path-default-wallet]}]] - effects (events/generate-account-for-keypair {:db db} props)] + effects (events/generate-account-for-keypair-with-mnemonic {:db db} props)] (is (match? (update-in effects [:fx 0 1] dissoc :on-success) {:fx expected-effects})) diff --git a/src/status_im/contexts/wallet/add_account/create_account/key_pair_name/view.cljs b/src/status_im/contexts/wallet/add_account/create_account/key_pair_name/view.cljs index f2a6af54f1..08ca7ffcf7 100644 --- a/src/status_im/contexts/wallet/add_account/create_account/key_pair_name/view.cljs +++ b/src/status_im/contexts/wallet/add_account/create_account/key_pair_name/view.cljs @@ -29,7 +29,7 @@ {:keypair-name key-pair-name}]) (:new-keypair :recovery-phrase) - (rf/dispatch [:wallet/generate-account-for-keypair + (rf/dispatch [:wallet/generate-account-for-keypair-with-mnemonic {:keypair-name key-pair-name}]) (do diff --git a/src/status_im/contexts/wallet/add_account/create_account/utils.cljs b/src/status_im/contexts/wallet/add_account/create_account/utils.cljs index 121cbc34a6..704aa4c722 100644 --- a/src/status_im/contexts/wallet/add_account/create_account/utils.cljs +++ b/src/status_im/contexts/wallet/add_account/create_account/utils.cljs @@ -2,19 +2,21 @@ (:require [status-im.constants :as constants])) (defn first-derived-account - [account-data] - (-> account-data :derived first val)) + [account-data keypair-type] + (if (= keypair-type :seed) + (some-> account-data :derived first val) + account-data)) (defn prepare-new-account - [{keypair-name :keypair-name + [{:keys [keypair-name keypair-type] {:keys [keyUid address] :as account} :account-data {:keys [account-name color emoji]} :account-preferences}] - (let [account-to-create (first-derived-account account) + (let [account-to-create (first-derived-account account keypair-type) account-config {:address (:address account-to-create) :key-uid keyUid :wallet false :chat false - :type :seed + :type keypair-type :path constants/path-default-wallet :public-key (:publicKey account-to-create) :name account-name @@ -23,7 +25,7 @@ :hidden false}] {:key-uid keyUid :name keypair-name - :type :seed + :type keypair-type :derived-from address :last-used-derivation-index 0 :accounts [account-config]})) diff --git a/src/status_im/contexts/wallet/add_account/create_account/view.cljs b/src/status_im/contexts/wallet/add_account/create_account/view.cljs index 3a36477b8b..19ad8a9143 100644 --- a/src/status_im/contexts/wallet/add_account/create_account/view.cljs +++ b/src/status_im/contexts/wallet/add_account/create_account/view.cljs @@ -159,15 +159,15 @@ [{:keys [on-change-text set-account-color set-emoji] {:keys [account-name account-color emoji]} :state}] - (let [on-auth-success-mnemonic + (let [on-auth-success--mnemonic (fn [password] (rf/dispatch - [:wallet/import-and-create-keypair-with-account + [:wallet/import-mnemonic-and-create-keypair-with-account {:password password :account-preferences {:account-name @account-name :color @account-color :emoji @emoji}}])) - on-auth-success-import-private-key + on-auth-success--import-private-key (fn [password] (rf/dispatch [:wallet/import-private-key-and-create-keypair-with-account @@ -180,8 +180,8 @@ derivation-path (when (not= workflow :workflow-new-keypair/import-private-key) constants/path-default-wallet) on-auth-success (if (= workflow :workflow-new-keypair/import-private-key) - on-auth-success-import-private-key - on-auth-success-mnemonic)] + on-auth-success--import-private-key + on-auth-success--mnemonic)] [floating-button {:account-color @account-color :slide-button-props {:on-auth-success on-auth-success