From b957fce841ca9b5a3319ec9622a39b8540783013 Mon Sep 17 00:00:00 2001 From: Rende11 Date: Tue, 11 Jun 2024 00:05:33 +0300 Subject: [PATCH] [19759] Import private key - backend integration Fix --- .../add_account/create_account/events.cljs | 42 ++++++- .../import_private_key/view.cljs | 4 +- .../create_account/key_pair_name/view.cljs | 5 +- .../new_keypair/keypair_name/view.cljs | 0 .../create_account/select_keypair/view.cljs | 3 +- .../add_account/create_account/view.cljs | 106 ++++++++++-------- src/status_im/contexts/wallet/effects.cljs | 5 + 7 files changed, 112 insertions(+), 53 deletions(-) delete mode 100644 src/status_im/contexts/wallet/add_account/create_account/new_keypair/keypair_name/view.cljs 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 c0694b5f96..1bbe0ae5e4 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 @@ -85,9 +85,10 @@ (rf/reg-event-fx :wallet/store-account-generated store-account-generated) (defn generate-account-for-keypair - [{:keys [db]} [{:keys [keypair-name]}]] + [{:keys [db]} [{:keys [keypair-name workflow]}]] (let [seed-phrase (-> db :wallet :ui :create-account :new-keypair :seed-phrase)] - {:fx [[:effects.wallet/create-account-from-mnemonic + {:db (assoc-in db [:wallet :ui :create-account :new-keypair :workflow] workflow) + :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] @@ -99,7 +100,7 @@ (defn clear-create-account-data [{:keys [db]}] - {:db (update-in db [:wallet :ui :create-account] dissoc :new-keypair)}) + {:db (update-in db [:wallet :ui :create-account] dissoc :new-keypair :workflow)}) (rf/reg-event-fx :wallet/clear-create-account clear-create-account-data) @@ -202,3 +203,38 @@ derived-from-address %)}]]]})) +(rf/reg-event-fx + :wallet/set-seed-phrase + (fn [{:keys [db]} [{:keys [seed-phrase on-success]}]] + {:db (assoc-in db [:wallet :ui :create-account :new-keypair :seed-phrase] seed-phrase) + :fx [on-success]})) + +(defn import-private-key-and-create-keypair-with-account + [{:keys [db]} [{:keys [password account-preferences]}]] + (let [private-key (get-in db [:wallet :ui :create-account :private-key]) + unmasked-private-key (security/safe-unmask-data private-key) + unmasked-password (security/safe-unmask-data password)] + {: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-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) + +(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}]]}])}})) diff --git a/src/status_im/contexts/wallet/add_account/create_account/import_private_key/view.cljs b/src/status_im/contexts/wallet/add_account/create_account/import_private_key/view.cljs index e7f14a2e5f..b7d8d63050 100644 --- a/src/status_im/contexts/wallet/add_account/create_account/import_private_key/view.cljs +++ b/src/status_im/contexts/wallet/add_account/create_account/import_private_key/view.cljs @@ -90,7 +90,7 @@ :style style/indicator) (i18n/label message)]))) -(defn on-unmount +(defn on-mount [] (rf/dispatch [:wallet/clear-private-key-data])) @@ -112,7 +112,7 @@ public-address (rf/sub [:wallet/public-address]) [flow-state set-flow-state] (rn/use-state nil) error? (= :invalid-private-key flow-state)] - (rn/use-unmount on-unmount) + (rn/use-mount on-mount) [rn/view {:flex 1} [floating-button-page/view {:customization-color customization-color 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 7d99d45887..f2a6af54f1 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 @@ -4,7 +4,6 @@ [quo.core :as quo] [react-native.core :as rn] [status-im.common.floating-button-page.view :as floating-button-page] - [status-im.common.not-implemented :as not-implemented] [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] @@ -25,9 +24,9 @@ (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) + (rf/dispatch [:wallet/import-private-key-and-generate-account-for-keypair + {:keypair-name key-pair-name}]) (:new-keypair :recovery-phrase) (rf/dispatch [:wallet/generate-account-for-keypair diff --git a/src/status_im/contexts/wallet/add_account/create_account/new_keypair/keypair_name/view.cljs b/src/status_im/contexts/wallet/add_account/create_account/new_keypair/keypair_name/view.cljs deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/status_im/contexts/wallet/add_account/create_account/select_keypair/view.cljs b/src/status_im/contexts/wallet/add_account/create_account/select_keypair/view.cljs index dcf2331e5e..75751e750b 100644 --- a/src/status_im/contexts/wallet/add_account/create_account/select_keypair/view.cljs +++ b/src/status_im/contexts/wallet/add_account/create_account/select_keypair/view.cljs @@ -23,7 +23,8 @@ :add-divider? true :on-press #(rf/dispatch [:navigate-to :screen/wallet.enter-seed-phrase {:recovering-keypair? true}])} - (when (ff/enabled? ::wallet.import-private-key) + ;; TODO FIX before merge + (when (or true (ff/enabled? ::wallet.import-private-key)) {:icon :i/key :accessibility-label :import-private-key :label (i18n/label :t/import-private-key) 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 a0661c54e4..3a36477b8b 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 @@ -21,38 +21,40 @@ (defn- get-keypair-data [{:keys [title primary-keypair? new-keypair? derivation-path customization-color]}] - (let [formatted-path (string/replace derivation-path #"/" " / ") - on-auth-success (fn [password] - (rf/dispatch [:navigate-to - :screen/wallet.edit-derivation-path - {:password password - :current-derivation-path formatted-path}]))] - [{:title title - :image (if primary-keypair? :avatar :icon) - :image-props (if primary-keypair? - {:full-name (utils.string/get-initials title 1) - :size :xxs - :customization-color customization-color} - :i/seed) - :action (when-not new-keypair? :button) - :action-props {:on-press #(rf/dispatch [:navigate-to :screen/wallet.select-keypair]) - :button-text (i18n/label :t/edit) - :alignment :flex-start} - :description :text - :description-props {:text (i18n/label :t/on-device)}} - {:title (i18n/label :t/derivation-path) - :image :icon - :image-props :i/derivated-path - :action (if (ff/enabled? ::ff/wallet.edit-derivation-path) :button :none) - :action-props {:on-press #(rf/dispatch [:standard-auth/authorize - {:on-auth-success on-auth-success - :auth-button-label (i18n/label :t/continue)}]) + [{:title title + :image (if primary-keypair? :avatar :icon) + :image-props (if primary-keypair? + {:full-name (utils.string/get-initials title 1) + :size :xxs + :customization-color customization-color} + :i/seed) + :action (when-not new-keypair? :button) + :action-props {:on-press #(rf/dispatch [:navigate-to :screen/wallet.select-keypair]) + :button-text (i18n/label :t/edit) + :alignment :flex-start} + :description :text + :description-props {:text (i18n/label :t/on-device)}} - :button-text (i18n/label :t/edit) - :icon-left :i/face-id - :alignment :flex-start} - :description :text - :description-props {:text formatted-path}}])) + (when-not (string/blank? derivation-path) + (let [formatted-path (string/replace derivation-path #"/" " / ") + on-auth-success (fn [password] + (rf/dispatch [:navigate-to + :screen/wallet.edit-derivation-path + {:password password + :current-derivation-path formatted-path}]))] + {:title (i18n/label :t/derivation-path) + :image :icon + :image-props :i/derivated-path + :action (if (ff/enabled? ::ff/wallet.edit-derivation-path) :button :none) + :action-props {:on-press #(rf/dispatch [:standard-auth/authorize + {:on-auth-success on-auth-success + :auth-button-label (i18n/label :t/continue)}]) + + :button-text (i18n/label :t/edit) + :icon-left :i/face-id + :alignment :flex-start} + :description :text + :description-props {:text formatted-path}}))]) (defn- avatar [{:keys [account-color emoji on-select-emoji]}] @@ -157,15 +159,29 @@ [{:keys [on-change-text set-account-color set-emoji] {:keys [account-name account-color emoji]} :state}] - (let [on-auth-success (fn [password] - (rf/dispatch - [:wallet/import-and-create-keypair-with-account - {:password password - :account-preferences {:account-name @account-name - :color @account-color - :emoji @emoji}}]))] - (fn [{:keys [customization-color keypair-name]}] - (let [{:keys [new-account-data]} (rf/sub [:wallet/create-account-new-keypair])] + (let [on-auth-success-mnemonic + (fn [password] + (rf/dispatch + [:wallet/import-and-create-keypair-with-account + {:password password + :account-preferences {:account-name @account-name + :color @account-color + :emoji @emoji}}])) + on-auth-success-import-private-key + (fn [password] + (rf/dispatch + [:wallet/import-private-key-and-create-keypair-with-account + {:password password + :account-preferences {:account-name @account-name + :color @account-color + :emoji @emoji}}]))] + (fn [{:keys [customization-color keypair-name workflow]}] + (let [{:keys [new-account-data]} (rf/sub [:wallet/create-account-new-keypair]) + 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)] [floating-button {:account-color @account-color :slide-button-props {:on-auth-success on-auth-success @@ -183,7 +199,7 @@ {:account-color @account-color :set-account-color set-account-color}] [new-account-origin - {:derivation-path constants/path-default-wallet + {:derivation-path derivation-path :customization-color customization-color :keypair-title keypair-name}]])))) @@ -247,9 +263,9 @@ :account-color account-color :emoji emoji}] (fn [] - (let [customization-color (rf/sub [:profile/customization-color]) + (let [customization-color (rf/sub [:profile/customization-color]) ;; Having a keypair means the user is importing it or creating it. - {:keys [keypair-name]} (rf/sub [:wallet/create-account-new-keypair])] + {:keys [keypair-name workflow]} (rf/sub [:wallet/create-account-new-keypair])] (rn/use-unmount #(rf/dispatch [:wallet/clear-create-account])) @@ -260,7 +276,9 @@ :set-account-color set-account-color :set-emoji set-emoji :state state - :keypair-name keypair-name}] + :keypair-name keypair-name + :workflow workflow}] + [derive-account-variant {:customization-color customization-color :on-change-text on-change-text diff --git a/src/status_im/contexts/wallet/effects.cljs b/src/status_im/contexts/wallet/effects.cljs index 5f0d0d914b..b9440599a7 100644 --- a/src/status_im/contexts/wallet/effects.cljs +++ b/src/status_im/contexts/wallet/effects.cljs @@ -13,6 +13,11 @@ [kw] (-> kw symbol str)) +(rf/reg-fx + :effects.wallet/get-random-mnemonic + (fn [{:keys [on-success]}] + (native-module/get-random-mnemonic on-success))) + (rf/reg-fx :effects.wallet/create-account-from-mnemonic (fn [{:keys [mnemonic-phrase paths on-success]