From dafab10582c726c0223fb22b03b53f9fb489035f Mon Sep 17 00:00:00 2001 From: Ulises Manuel <90291778+ulisesmac@users.noreply.github.com> Date: Thu, 30 May 2024 12:17:11 -0600 Subject: [PATCH] fix(wallet): add support to import keypairs using a seed phrase (#20218) * Add support to import keypairs by using a seed phrase with validation for existing keypairs. --- .../add_account/create_account/events.cljs | 20 ++++++++++--- .../create_account/key_pair_name/view.cljs | 30 +++++++++++-------- 2 files changed, 34 insertions(+), 16 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 5673cf6a64..e8e9e83f74 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 @@ -40,9 +40,21 @@ (rf/reg-event-fx :wallet/store-new-seed-phrase store-new-seed-phrase) (defn seed-phrase-validated - [{:keys [db]} [seed-phrase]] - {:db (assoc-in db [:wallet :ui :create-account :seed-phrase] seed-phrase) - :fx [[:dispatch [:navigate-to :screen/wallet.keypair-name]]]}) + [{:keys [db]} [seed-phrase key-uid on-error]] + (let [keypair-already-added? (->> db + :wallet + :keypairs + (some #(= key-uid (:key-uid %))))] + (if keypair-already-added? + (do + (on-error) + {:fx [[:dispatch + [:toasts/upsert + {:id :already-existing-keypair + :type :negative + :text "This keypair already exists in the device"}]]]}) + {:db (assoc-in db [:wallet :ui :create-account :new-keypair :seed-phrase] seed-phrase) + :fx [[:dispatch [:navigate-to :screen/wallet.keypair-name {:workflow :recovery-phrase}]]]}))) (rf/reg-event-fx :wallet/seed-phrase-validated seed-phrase-validated) @@ -51,7 +63,7 @@ {:fx [[:multiaccount/validate-mnemonic [seed-phrase (fn [mnemonic key-uid] - (rf/dispatch [:wallet/seed-phrase-validated mnemonic key-uid])) + (rf/dispatch [:wallet/seed-phrase-validated mnemonic key-uid on-error])) on-error]]]}) (rf/reg-event-fx :wallet/seed-phrase-entered seed-phrase-entered) 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 18ffbfe03e..7d99d45887 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 @@ -8,6 +8,7 @@ [status-im.common.validation.general :as validators] [status-im.constants :as constants] [status-im.contexts.wallet.add-account.create-account.key-pair-name.style :as style] + [taoensso.timbre :as log] [utils.i18n :as i18n] [utils.re-frame :as rf])) @@ -17,10 +18,25 @@ :emoji (i18n/label :t/key-name-error-emoji) :special-char (i18n/label :t/key-name-error-special-char)}) -(defn navigate-back +(defn- navigate-back [] (rf/dispatch [:navigate-back])) +(defn- next-workflow-step + [workflow key-pair-name] + (case workflow + ;; TODO issue #19759. Implement creation account from private key + :import-private-key + (not-implemented/alert) + + (:new-keypair :recovery-phrase) + (rf/dispatch [:wallet/generate-account-for-keypair + {:keypair-name key-pair-name}]) + + (do + (log/error "Unknown workflow" workflow) + (js/alert "Unknown workflow")))) + (defn view [] (let [{:keys [workflow]} (rf/sub [:get-screen-params]) @@ -45,17 +61,7 @@ :else (set-error nil)))) on-continue (rn/use-callback - (fn [_] - (case workflow - ;; TODO issue #19759. Implement creation account from - ;; private key - :import-private-key - (not-implemented/alert) - - :new-keypair - (rf/dispatch [:wallet/generate-account-for-keypair - {:keypair-name key-pair-name}]) - (js/alert "Unknown workflow"))) + #(next-workflow-step workflow key-pair-name) [workflow key-pair-name]) disabled? (or (some? error) (string/blank? key-pair-name))] [rn/view {:style {:flex 1}}