chore(wallet): import private key - backend integration

Co-authored-by: Jamie Caprani <jamiecaprani@gmail.com>
This commit is contained in:
Nikolay 2024-07-09 13:49:31 +03:00 committed by GitHub
parent 7774c4eac1
commit a2178951d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 236 additions and 131 deletions

View File

@ -71,7 +71,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
@ -85,20 +85,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
(defn generate-account-for-keypair-with-mnemonic
[{:keys [db]} [{:keys [keypair-name]}]]
(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 [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]}]
@ -159,14 +160,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)]
@ -176,7 +182,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))
@ -187,7 +193,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
@ -206,3 +213,49 @@
:on-error [:wallet/log-rpc-error
{:event :wallet/derive-address-and-add-account
:params [derived-from-address [derivation-path]]}]}]]]}))
(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 :new-keypair
:new-account-data :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/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 [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-private-key
{:keypair-name keypair-name
:new-account-data new-account-data}]))}]]})))

View File

@ -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}))

View File

@ -63,31 +63,31 @@
(let [{:keys [message] :as props}
(case state
:scanning
{:type :info
{:status :default
:icon :i/scanning
:message :t/scanning-for-activity}
:inactive-address
{:type :warning
{:status :warning
:icon :i/info
:message :t/this-account-has-no-activity}
:active-address
{:type :success
{:status :success
:icon :i/done
:message :t/this-address-has-activity}
:invalid-private-key
{:type :error
{:status :error
:icon :i/info
:message :t/invalid-private-key}
nil)]
(when props
[quo/info-message
(assoc props
:size :default
:style style/indicator)
(-> props
(assoc :size :default :container-style style/indicator)
(dissoc :message))
(i18n/label message)])))
(defn on-unmount

View File

@ -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,12 +24,12 @@
(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
(rf/dispatch [:wallet/generate-account-for-keypair-with-mnemonic
{:keypair-name key-pair-name}])
(do

View File

@ -2,28 +2,57 @@
(: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 get-account-details
[keypair-type account account-to-create]
(if (= keypair-type :key)
{:key-uid (:key-uid account)
:public-key (:public-key account)
:account-address (:address account)}
{:key-uid (:keyUid account)
:public-key (:publicKey account-to-create)
:account-address (:address account-to-create)}))
(defn get-account-config
[{:keys [account-address key-uid keypair-type public-key account-name emoji color]}]
{:address account-address
:key-uid key-uid
:wallet false
:chat false
:type keypair-type
:path constants/path-default-wallet
:public-key public-key
:name account-name
:emoji emoji
:colorID color
:hidden false})
(defn prepare-new-account
[{keypair-name :keypair-name
{:keys [keyUid address] :as account} :account-data
{:keys [account-name color emoji]} :account-preferences}]
(let [account-to-create (first-derived-account account)
account-config {:address (:address account-to-create)
:key-uid keyUid
:wallet false
:chat false
:type :seed
:path constants/path-default-wallet
:public-key (:publicKey account-to-create)
:name account-name
:emoji emoji
:colorID color
:hidden false}]
{:key-uid keyUid
[{:keys [keypair-name keypair-type]
{:keys [address] :as account} :account-data
{:keys [account-name color emoji]} :account-preferences}]
(let [account-to-create (first-derived-account account keypair-type)
{:keys [key-uid public-key account-address]} (get-account-details keypair-type
account
account-to-create)
account-config (get-account-config {:account-address
account-address
:key-uid key-uid
:keypair-type keypair-type
:public-key public-key
:account-name account-name
:emoji emoji
:color color})]
{:key-uid key-uid
:name keypair-name
:type :seed
:type keypair-type
:derived-from address
:last-used-derivation-index 0
:accounts [account-config]}))

View File

@ -22,37 +22,38 @@
(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)}])
:button-text (i18n/label :t/edit)
:icon-left :i/face-id
:alignment :flex-start}
:description :text
:description-props {:text 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)}}
(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]}]
@ -167,38 +168,59 @@
:container-style (style/slide-button-container bottom))]}]
children))))
(defn- on-auth-success-mnemonic
[password account-preferences]
(rf/dispatch
[:wallet/import-mnemonic-and-create-keypair-with-account
{:password password
:account-preferences account-preferences}]))
(defn- on-auth-success-import-private-key
[password account-preferences]
(rf/dispatch
[:wallet/import-private-key-and-create-keypair-with-account
{:password password
:account-preferences account-preferences}]))
(defn add-new-keypair-variant
[{{: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 [on-change-text set-account-color set-emoji customization-color keypair-name error]}]
(let [{:keys [new-account-data]} (rf/sub [:wallet/create-account-new-keypair])]
[floating-button
{:account-color @account-color
:slide-button-props {:on-auth-success on-auth-success
:disabled? (empty? @account-name)
:dependencies [new-account-data]}}
[avatar
{:account-color @account-color
:emoji @emoji
:on-select-emoji set-emoji}]
[input
{:account-color @account-color
:account-name @account-name
:on-change-text on-change-text
:error error}]
[color-picker
{:account-color @account-color
:set-account-color set-account-color}]
[new-account-origin
{:derivation-path constants/path-default-wallet
:customization-color customization-color
:keypair-title keypair-name}]]))))
(fn [{:keys [on-change-text set-account-color set-emoji customization-color keypair-name error]}]
(let [{:keys [new-account-data workflow]} (rf/sub [:wallet/create-account-new-keypair])
derivation-path (when (not= workflow
:workflow/new-keypair.import-private-key)
constants/path-default-wallet)]
[floating-button
{:account-color @account-color
:slide-button-props {:on-auth-success (fn on-auth-success [password]
(let [account-preferences {:account-name @account-name
:color @account-color
:emoji @emoji}]
(if (= workflow
:workflow/new-keypair.import-private-key)
(on-auth-success-import-private-key
password
account-preferences)
(on-auth-success-mnemonic
password
account-preferences))))
:disabled? (empty? @account-name)
:dependencies [new-account-data]}}
[avatar
{:account-color @account-color
:emoji @emoji
:on-select-emoji set-emoji}]
[input
{:account-color @account-color
:account-name @account-name
:on-change-text on-change-text
:error error}]
[color-picker
{:account-color @account-color
:set-account-color set-account-color}]
[new-account-origin
{:derivation-path derivation-path
:customization-color customization-color
:keypair-title keypair-name}]])))
(defn derive-account-variant
[{{:keys [account-name account-color emoji]} :state}]
@ -262,33 +284,33 @@
:account-name-error account-name-error
:emoji-and-color-error? emoji-and-color-error?}]
(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])
accounts-names (rf/sub [:wallet/accounts-names])
accounts-emojis-and-colors (rf/sub [:wallet/accounts-emojis-and-colors])
on-change-text (rn/use-callback
(fn [new-text]
(reset! account-name new-text)
(reset! account-name-error
(common.utils/get-account-name-error new-text
accounts-names)))
[accounts-names accounts-emojis-and-colors])
check-emoji-and-color-error (fn [emoji color]
(let [repeated? (accounts-emojis-and-colors [emoji color])]
(reset! emoji-and-color-error?
(when repeated? :emoji-and-color))))
set-account-color (rn/use-callback
(fn [new-color]
(reset! account-color new-color)
(check-emoji-and-color-error @emoji new-color))
[accounts-emojis-and-colors @emoji])
set-emoji (rn/use-callback
(fn [new-emoji]
(reset! emoji new-emoji)
(check-emoji-and-color-error new-emoji @account-color))
[accounts-emojis-and-colors @account-color])
error (or @account-name-error @emoji-and-color-error?)]
{:keys [keypair-name workflow]} (rf/sub [:wallet/create-account-new-keypair])
accounts-names (rf/sub [:wallet/accounts-names])
accounts-emojis-and-colors (rf/sub [:wallet/accounts-emojis-and-colors])
on-change-text (rn/use-callback
(fn [new-text]
(reset! account-name new-text)
(reset! account-name-error
(common.utils/get-account-name-error new-text
accounts-names)))
[accounts-names accounts-emojis-and-colors])
check-emoji-and-color-error (fn [emoji color]
(let [repeated? (accounts-emojis-and-colors [emoji color])]
(reset! emoji-and-color-error?
(when repeated? :emoji-and-color))))
set-account-color (rn/use-callback
(fn [new-color]
(reset! account-color new-color)
(check-emoji-and-color-error @emoji new-color))
[accounts-emojis-and-colors @emoji])
set-emoji (rn/use-callback
(fn [new-emoji]
(reset! emoji new-emoji)
(check-emoji-and-color-error new-emoji @account-color))
[accounts-emojis-and-colors @account-color])
error (or @account-name-error @emoji-and-color-error?)]
(rn/use-mount #(check-emoji-and-color-error @emoji @account-color))
(rn/use-unmount #(rf/dispatch [:wallet/clear-create-account]))
@ -300,8 +322,10 @@
:on-change-text on-change-text
:set-account-color set-account-color
:set-emoji set-emoji
:keypair-name keypair-name
:error error}]
:error error
:workflow workflow}]
[derive-account-variant
{:state state
:customization-color customization-color

View File

@ -58,11 +58,11 @@
:emoji-hash emojiHash
:key-uid keyUid
:public-key publicKey
:private-key privateKey})))))
:private-key (security/mask-data privateKey)})))))
(rf/reg-fx
:effects.wallet/create-account-from-private-key
(fn [[private-key on-success on-error]]
(fn [{:keys [private-key on-success on-error]}]
(-> (create-account-from-private-key private-key)
(promesa/then (partial rf/call-continuation on-success))
(promesa/catch (partial rf/call-continuation on-error)))))