feat: add support for importing key-pair with private-key
This commit is contained in:
parent
4e9edf7c23
commit
af644e5b11
|
@ -68,7 +68,7 @@
|
||||||
|
|
||||||
(rf/reg-event-fx :wallet/seed-phrase-entered seed-phrase-entered)
|
(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]}]]
|
[{:keys [db]} [{:keys [new-account-data keypair-name]}]]
|
||||||
(let [new-account (update new-account-data :mnemonic security/mask-data)]
|
(let [new-account (update new-account-data :mnemonic security/mask-data)]
|
||||||
{:db (-> db
|
{:db (-> db
|
||||||
|
@ -82,21 +82,21 @@
|
||||||
:random-phrase))
|
:random-phrase))
|
||||||
:fx [[:dispatch [:navigate-back-to :screen/wallet.create-account]]]}))
|
: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 workflow]}]]
|
[{:keys [db]} [{:keys [keypair-name]}]]
|
||||||
(let [seed-phrase (-> db :wallet :ui :create-account :new-keypair :seed-phrase)]
|
(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)
|
{:mnemonic-phrase (security/safe-unmask-data seed-phrase)
|
||||||
:paths [constants/path-default-wallet]
|
:paths [constants/path-default-wallet]
|
||||||
:on-success (fn [new-account-data]
|
: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
|
{:new-account-data new-account-data
|
||||||
:keypair-name keypair-name}]))}]]}))
|
: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
|
(defn clear-create-account-data
|
||||||
[{:keys [db]}]
|
[{:keys [db]}]
|
||||||
|
@ -157,14 +157,19 @@
|
||||||
(rf/reg-event-fx
|
(rf/reg-event-fx
|
||||||
:wallet/create-keypair-with-account
|
:wallet/create-keypair-with-account
|
||||||
(fn [{db :db} [password account-preferences]]
|
(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)
|
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-with-account (create-account.utils/prepare-new-account
|
||||||
{:keypair-name keypair-name
|
{:keypair-name keypair-name
|
||||||
|
:keypair-type keypair-type
|
||||||
:account-data new-account-data
|
:account-data new-account-data
|
||||||
:account-preferences account-preferences})
|
:account-preferences account-preferences})
|
||||||
new-address (some-> new-account-data
|
new-address (some-> new-account-data
|
||||||
(create-account.utils/first-derived-account)
|
(create-account.utils/first-derived-account keypair-type)
|
||||||
(:address)
|
(:address)
|
||||||
(string/lower-case))
|
(string/lower-case))
|
||||||
unmasked-password (security/safe-unmask-data password)]
|
unmasked-password (security/safe-unmask-data password)]
|
||||||
|
@ -174,7 +179,7 @@
|
||||||
:on-success [:wallet/add-account-success new-address]
|
:on-success [:wallet/add-account-success new-address]
|
||||||
:on-error #(log/error "Failed to add Keypair and create account" %)}]]]})))
|
: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]}]]
|
[{db :db} [{:keys [password account-preferences]}]]
|
||||||
(let [account-data (-> db :wallet :ui :create-account :new-keypair :new-account-data)
|
(let [account-data (-> db :wallet :ui :create-account :new-keypair :new-account-data)
|
||||||
unmasked-mnemonic (security/safe-unmask-data (:mnemonic account-data))
|
unmasked-mnemonic (security/safe-unmask-data (:mnemonic account-data))
|
||||||
|
@ -185,7 +190,8 @@
|
||||||
:on-success #(rf/dispatch
|
:on-success #(rf/dispatch
|
||||||
[:wallet/create-keypair-with-account password account-preferences])}]]]}))
|
[: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
|
(rf/reg-event-fx
|
||||||
:wallet/derive-address-and-add-account
|
:wallet/derive-address-and-add-account
|
||||||
|
@ -217,24 +223,38 @@
|
||||||
{:json-rpc/call
|
{:json-rpc/call
|
||||||
[{:method "accounts_importPrivateKey"
|
[{:method "accounts_importPrivateKey"
|
||||||
:params [unmasked-private-key unmasked-password]
|
:params [unmasked-private-key unmasked-password]
|
||||||
:on-success [:wallet/import-and-create-keypair-with-account
|
:on-success [:wallet/create-keypair-with-account password account-preferences]
|
||||||
{:passord password
|
|
||||||
:account-preferences account-preferences}]
|
|
||||||
:on-error #(log/error "Failed to import private key" %)}]}))
|
:on-error #(log/error "Failed to import private key" %)}]}))
|
||||||
|
|
||||||
(rf/reg-event-fx
|
(rf/reg-event-fx
|
||||||
:wallet/import-private-key-and-create-keypair-with-account
|
:wallet/import-private-key-and-create-keypair-with-account
|
||||||
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
|
(rf/reg-event-fx
|
||||||
:wallet/import-private-key-and-generate-account-for-keypair
|
:wallet/import-private-key-and-generate-account-for-keypair
|
||||||
(fn [_ [{:keys [keypair-name]}]]
|
(fn [{:keys [db]} [{:keys [keypair-name]}]]
|
||||||
{:effects.wallet/get-random-mnemonic
|
(let [private-key (get-in db [:wallet :ui :create-account :private-key])]
|
||||||
{:on-success
|
{:db (assoc-in db
|
||||||
#(rf/dispatch
|
[:wallet :ui :create-account :new-keypair :workflow]
|
||||||
[:wallet/set-seed-phrase
|
:workflow-new-keypair/import-private-key)
|
||||||
{:seed-phrase %
|
:fx [[:effects.wallet/create-account-from-private-key
|
||||||
:on-success [:dispatch
|
{:private-key private-key
|
||||||
[:wallet/generate-account-for-keypair
|
:on-success (fn [new-account-data]
|
||||||
|
(rf/dispatch [:wallet/store-account-generated-with-mnemonic
|
||||||
{:keypair-name keypair-name
|
{:keypair-name keypair-name
|
||||||
:workflow :workflow-new-keypair/import-private-key}]]}])}}))
|
:new-account-data new-account-data}]))}]]})))
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
{:new-account-data {"test" "data"
|
{:new-account-data {"test" "data"
|
||||||
:mnemonic masked-mnemonic}
|
:mnemonic masked-mnemonic}
|
||||||
:keypair-name "new-keypair-name"}}}}}
|
: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)
|
result-db (:db effects)
|
||||||
remove-mnemonic #(update-in %
|
remove-mnemonic #(update-in %
|
||||||
[:wallet :ui :create-account :new-keypair :new-account-data]
|
[:wallet :ui :create-account :new-keypair :new-account-data]
|
||||||
|
@ -61,7 +61,7 @@
|
||||||
expected-effects [[:effects.wallet/create-account-from-mnemonic
|
expected-effects [[:effects.wallet/create-account-from-mnemonic
|
||||||
{:mnemonic-phrase "test-secret"
|
{:mnemonic-phrase "test-secret"
|
||||||
:paths [constants/path-default-wallet]}]]
|
: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?
|
(is (match?
|
||||||
(update-in effects [:fx 0 1] dissoc :on-success)
|
(update-in effects [:fx 0 1] dissoc :on-success)
|
||||||
{:fx expected-effects}))
|
{:fx expected-effects}))
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
{:keypair-name key-pair-name}])
|
{:keypair-name key-pair-name}])
|
||||||
|
|
||||||
(:new-keypair :recovery-phrase)
|
(: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}])
|
{:keypair-name key-pair-name}])
|
||||||
|
|
||||||
(do
|
(do
|
||||||
|
|
|
@ -2,19 +2,21 @@
|
||||||
(:require [status-im.constants :as constants]))
|
(:require [status-im.constants :as constants]))
|
||||||
|
|
||||||
(defn first-derived-account
|
(defn first-derived-account
|
||||||
[account-data]
|
[account-data keypair-type]
|
||||||
(-> account-data :derived first val))
|
(if (= keypair-type :seed)
|
||||||
|
(some-> account-data :derived first val)
|
||||||
|
account-data))
|
||||||
|
|
||||||
(defn prepare-new-account
|
(defn prepare-new-account
|
||||||
[{keypair-name :keypair-name
|
[{:keys [keypair-name keypair-type]
|
||||||
{:keys [keyUid address] :as account} :account-data
|
{:keys [keyUid address] :as account} :account-data
|
||||||
{:keys [account-name color emoji]} :account-preferences}]
|
{: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)
|
account-config {:address (:address account-to-create)
|
||||||
:key-uid keyUid
|
:key-uid keyUid
|
||||||
:wallet false
|
:wallet false
|
||||||
:chat false
|
:chat false
|
||||||
:type :seed
|
:type keypair-type
|
||||||
:path constants/path-default-wallet
|
:path constants/path-default-wallet
|
||||||
:public-key (:publicKey account-to-create)
|
:public-key (:publicKey account-to-create)
|
||||||
:name account-name
|
:name account-name
|
||||||
|
@ -23,7 +25,7 @@
|
||||||
:hidden false}]
|
:hidden false}]
|
||||||
{:key-uid keyUid
|
{:key-uid keyUid
|
||||||
:name keypair-name
|
:name keypair-name
|
||||||
:type :seed
|
:type keypair-type
|
||||||
:derived-from address
|
:derived-from address
|
||||||
:last-used-derivation-index 0
|
:last-used-derivation-index 0
|
||||||
:accounts [account-config]}))
|
:accounts [account-config]}))
|
||||||
|
|
|
@ -159,15 +159,15 @@
|
||||||
[{:keys [on-change-text set-account-color set-emoji]
|
[{:keys [on-change-text set-account-color set-emoji]
|
||||||
{:keys [account-name account-color emoji]}
|
{:keys [account-name account-color emoji]}
|
||||||
:state}]
|
:state}]
|
||||||
(let [on-auth-success-mnemonic
|
(let [on-auth-success--mnemonic
|
||||||
(fn [password]
|
(fn [password]
|
||||||
(rf/dispatch
|
(rf/dispatch
|
||||||
[:wallet/import-and-create-keypair-with-account
|
[:wallet/import-mnemonic-and-create-keypair-with-account
|
||||||
{:password password
|
{:password password
|
||||||
:account-preferences {:account-name @account-name
|
:account-preferences {:account-name @account-name
|
||||||
:color @account-color
|
:color @account-color
|
||||||
:emoji @emoji}}]))
|
:emoji @emoji}}]))
|
||||||
on-auth-success-import-private-key
|
on-auth-success--import-private-key
|
||||||
(fn [password]
|
(fn [password]
|
||||||
(rf/dispatch
|
(rf/dispatch
|
||||||
[:wallet/import-private-key-and-create-keypair-with-account
|
[:wallet/import-private-key-and-create-keypair-with-account
|
||||||
|
@ -180,8 +180,8 @@
|
||||||
derivation-path (when (not= workflow :workflow-new-keypair/import-private-key)
|
derivation-path (when (not= workflow :workflow-new-keypair/import-private-key)
|
||||||
constants/path-default-wallet)
|
constants/path-default-wallet)
|
||||||
on-auth-success (if (= workflow :workflow-new-keypair/import-private-key)
|
on-auth-success (if (= workflow :workflow-new-keypair/import-private-key)
|
||||||
on-auth-success-import-private-key
|
on-auth-success--import-private-key
|
||||||
on-auth-success-mnemonic)]
|
on-auth-success--mnemonic)]
|
||||||
[floating-button
|
[floating-button
|
||||||
{:account-color @account-color
|
{:account-color @account-color
|
||||||
:slide-button-props {:on-auth-success on-auth-success
|
:slide-button-props {:on-auth-success on-auth-success
|
||||||
|
|
Loading…
Reference in New Issue