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.
This commit is contained in:
Ulises Manuel 2024-05-30 12:17:11 -06:00 committed by GitHub
parent c37a2a5d6f
commit dafab10582
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 16 deletions

View File

@ -40,9 +40,21 @@
(rf/reg-event-fx :wallet/store-new-seed-phrase store-new-seed-phrase) (rf/reg-event-fx :wallet/store-new-seed-phrase store-new-seed-phrase)
(defn seed-phrase-validated (defn seed-phrase-validated
[{:keys [db]} [seed-phrase]] [{:keys [db]} [seed-phrase key-uid on-error]]
{:db (assoc-in db [:wallet :ui :create-account :seed-phrase] seed-phrase) (let [keypair-already-added? (->> db
:fx [[:dispatch [:navigate-to :screen/wallet.keypair-name]]]}) :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) (rf/reg-event-fx :wallet/seed-phrase-validated seed-phrase-validated)
@ -51,7 +63,7 @@
{:fx [[:multiaccount/validate-mnemonic {:fx [[:multiaccount/validate-mnemonic
[seed-phrase [seed-phrase
(fn [mnemonic key-uid] (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]]]}) on-error]]]})
(rf/reg-event-fx :wallet/seed-phrase-entered seed-phrase-entered) (rf/reg-event-fx :wallet/seed-phrase-entered seed-phrase-entered)

View File

@ -8,6 +8,7 @@
[status-im.common.validation.general :as validators] [status-im.common.validation.general :as validators]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.contexts.wallet.add-account.create-account.key-pair-name.style :as style] [status-im.contexts.wallet.add-account.create-account.key-pair-name.style :as style]
[taoensso.timbre :as log]
[utils.i18n :as i18n] [utils.i18n :as i18n]
[utils.re-frame :as rf])) [utils.re-frame :as rf]))
@ -17,10 +18,25 @@
:emoji (i18n/label :t/key-name-error-emoji) :emoji (i18n/label :t/key-name-error-emoji)
:special-char (i18n/label :t/key-name-error-special-char)}) :special-char (i18n/label :t/key-name-error-special-char)})
(defn navigate-back (defn- navigate-back
[] []
(rf/dispatch [: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 (defn view
[] []
(let [{:keys [workflow]} (rf/sub [:get-screen-params]) (let [{:keys [workflow]} (rf/sub [:get-screen-params])
@ -45,17 +61,7 @@
:else (set-error nil)))) :else (set-error nil))))
on-continue (rn/use-callback on-continue (rn/use-callback
(fn [_] #(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
(rf/dispatch [:wallet/generate-account-for-keypair
{:keypair-name key-pair-name}])
(js/alert "Unknown workflow")))
[workflow key-pair-name]) [workflow key-pair-name])
disabled? (or (some? error) (string/blank? key-pair-name))] disabled? (or (some? error) (string/blank? key-pair-name))]
[rn/view {:style {:flex 1}} [rn/view {:style {:flex 1}}