Skip to content

Commit

Permalink
[#20035] fix: apply suggestion and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ghafouri committed May 28, 2024
1 parent d1cc9e6 commit ae982ed
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 170 deletions.
35 changes: 21 additions & 14 deletions src/native_module/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -604,20 +604,27 @@

(defn get-connection-string-for-exporting-keypairs-keystores
"Generates connection string form status-go for the purpose of exporting keypairs and keystores on sender side"
[config-json callback]
(log/info "[native-module] Fetching Export Keypairs Connection String"
{:fn :get-connection-string-for-exporting-keypairs-keystores
:config-json config-json})
(.getConnectionStringForExportingKeypairsKeystores ^js (network) config-json callback))
([config-json]
(native-utils/promisify-native-module-call get-connection-string-for-exporting-keypairs-keystores
config-json))
([config-json callback]
(log/info "[native-module] Fetching Export Keypairs Connection String"
{:fn :get-connection-string-for-exporting-keypairs-keystores
:config-json config-json})
(.getConnectionStringForExportingKeypairsKeystores ^js (network) config-json callback)))

(defn input-connection-string-for-importing-keypairs-keystores
"Provides connection string to status-go for the purpose of importing keypairs and keystores on the receiver side"
[connection-string config-json callback]
(log/info "[native-module] Sending Import Keypairs Connection String"
{:fn :input-connection-string-for-importing-keypairs-keystores
:config-json config-json
:connection-string connection-string})
(.inputConnectionStringForImportingKeypairsKeystores ^js (network)
connection-string
config-json
callback))
([connection-string config-json]
(native-utils/promisify-native-module-call input-connection-string-for-importing-keypairs-keystores
connection-string
config-json))
([connection-string config-json callback]
(log/info "[native-module] Sending Import Keypairs Connection String"
{:fn :input-connection-string-for-importing-keypairs-keystores
:config-json config-json
:connection-string connection-string})
(.inputConnectionStringForImportingKeypairsKeystores ^js (network)
connection-string
config-json
callback)))
4 changes: 2 additions & 2 deletions src/quo/components/wallet/missing_keypairs/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[utils.i18n :as i18n]))

(defn title-view
[{:keys [keypairs blur? on-import-click]}]
[{:keys [keypairs blur? on-import-press]}]
(let [theme (quo.theme/use-theme)]
[rn/view
{:accessibility-label :title
Expand All @@ -33,7 +33,7 @@
{:type :outline
:background :blur
:size 24
:on-press on-import-click}
:on-press on-import-press}
(i18n/label :t/import)]]
[text/text
{:size :paragraph-2
Expand Down
47 changes: 47 additions & 0 deletions src/status_im/contexts/settings/wallet/data_store.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(ns status-im.contexts.settings.wallet.data-store)

(defn extract-keypair-name
[db key-uids-set]
(when (= (count key-uids-set) 1)
(let [key-uid (first key-uids-set)
keypairs (get-in db [:wallet :keypairs])]
(->> (filter #(= (:key-uid %) key-uid) keypairs)
first
:name))))

(defn update-keypair
[keypairs key-uid update-fn]
(mapcat (fn [keypair]
(if (= (keypair :key-uid) key-uid)
(if-let [updated (update-fn keypair)]
[updated]
[])
[keypair]))
keypairs))

(defn make-accounts-fully-operable
[accounts key-uids-set]
(reduce-kv
(fn [acc k account]
(if (and (contains? key-uids-set (:key-uid account))
(= (keyword (:operable account)) :no))
(assoc acc k (assoc account :operable :fully))
(assoc acc k account)))
{}
accounts))

(defn- make-keypairs-accounts-fully-operable
[accounts]
(map (fn [account]
(assoc account :operable :fully))
accounts))

(defn make-keypairs-fully-operable
[keypairs key-uids-set]
(map (fn [keypair]
(if (contains? key-uids-set (:key-uid keypair))
(update keypair
:accounts
make-keypairs-accounts-fully-operable)
keypair))
keypairs))
210 changes: 105 additions & 105 deletions src/status_im/contexts/settings/wallet/events.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(ns status-im.contexts.settings.wallet.events
(:require
[native-module.core :as native-module]
[promesa.core :as promesa]
[status-im.contexts.settings.wallet.data-store :as data-store]
[status-im.contexts.syncing.events :as syncing-events]
[status-im.contexts.syncing.utils :as sync-utils]
[taoensso.timbre :as log]
Expand All @@ -9,22 +11,12 @@
[utils.security.core :as security]
[utils.transforms :as transforms]))

(defn- update-keypair
[keypairs key-uid update-fn]
(mapcat (fn [keypair]
(if (= (keypair :key-uid) key-uid)
(if-let [updated (update-fn keypair)]
[updated]
[])
[keypair]))
keypairs))

(rf/reg-event-fx
:wallet/rename-keypair-success
(fn [{:keys [db]} [key-uid name]]
{:db (update-in db
[:wallet :keypairs]
#(update-keypair % key-uid (fn [keypair] (assoc keypair :name name))))
#(data-store/update-keypair % key-uid (fn [keypair] (assoc keypair :name name))))
:fx [[:dispatch [:navigate-back]]
[:dispatch
[:toasts/upsert
Expand All @@ -42,31 +34,44 @@

(rf/reg-event-fx :wallet/rename-keypair rename-keypair)

(rf/reg-fx :effects.connection-string/export-keypair
(fn [{:keys [key-uid sha3-pwd keypair-key-uid on-success on-fail]}]
(let [config-map (transforms/clj->json {:senderConfig {:loggedInKeyUid key-uid
:keystorePath ""
:keypairsToExport [keypair-key-uid]
:password (security/safe-unmask-data
sha3-pwd)}
:serverConfig {:timeout 0}})]
(-> (native-module/get-connection-string-for-exporting-keypairs-keystores
config-map)
(promesa/then (fn [response]
(if (sync-utils/valid-connection-string? response)
(on-success response)
(on-fail "generic-error: failed to get connection string"))))
(promesa/catch on-fail)))))

(defn get-key-pair-export-connection
[{:keys [db]} [{:keys [sha3-pwd keypair-key-uid callback]}]]
(let [key-uid (get-in db [:profile/profile :key-uid])
config-map (transforms/clj->json {:senderConfig {:loggedInKeyUid key-uid
:keystorePath ""
:keypairsToExport [keypair-key-uid]
:password (security/safe-unmask-data
sha3-pwd)}
:serverConfig {:timeout 0}})
handle-connection (fn [response]
(when (sync-utils/valid-connection-string? response)
(callback response)
(rf/dispatch [:hide-bottom-sheet])))]
(native-module/get-connection-string-for-exporting-keypairs-keystores
config-map
handle-connection)))
(let [key-uid (get-in db [:profile/profile :key-uid])]
{:fx [[:effects.connection-string/export-keypair
{:key-uid key-uid
:sha3-pwd sha3-pwd
:keypair-key-uid keypair-key-uid
:on-success (fn [connect-string]
(callback connect-string)
(rf/dispatch [:hide-bottom-sheet]))
:on-fail (fn [error]
(rf/dispatch [:toasts/upsert
{:type :negative
:text error}]))}]]}))

(rf/reg-event-fx :wallet/get-key-pair-export-connection get-key-pair-export-connection)

(rf/reg-event-fx
:wallet/remove-keypair-success
(rf/reg-event-fx :wallet/remove-keypair-success
(fn [{:keys [db]} [key-uid]]
{:db (update-in db
[:wallet :keypairs]
#(update-keypair % key-uid (fn [_] nil)))
#(data-store/update-keypair % key-uid (fn [_] nil)))
:fx [[:dispatch [:hide-bottom-sheet]]
[:dispatch
[:toasts/upsert
Expand All @@ -84,85 +89,80 @@

(rf/reg-event-fx :wallet/remove-keypair remove-keypair)

(defn extract-keypair-name
[db key-uids-set]
(when (= (count key-uids-set) 1)
(let [key-uid (first key-uids-set)
keypairs (get-in db [:wallet :keypairs])]
(->> (filter #(= (:key-uid %) key-uid) keypairs)
first
:name))))

(defn update-accounts
[accounts key-uids-set]
(into {}
(map (fn [[k account]]
(if (and (contains? key-uids-set (:key-uid account))
(= (:operable account) :no))
[k (assoc account :operable :fully)]
[k account]))
accounts)))

(defn update-keypairs
[keypairs key-uids-set]
(map (fn [keypair]
(if (contains? key-uids-set (:key-uid keypair))
(update keypair
:accounts
(fn [accounts]
(map (fn [account]
(if (and (contains? key-uids-set (:key-uid account))
(= (:operable account) ":no"))
(assoc account :operable ":fully")
account))
accounts)))
keypair))
keypairs))

(rf/reg-event-fx :wallet/make-key-pairs-fully-operable
(fn [{:keys [db]} [key-uids-to-update]]
(let [key-uids-set (set key-uids-to-update)
keypair-name (extract-keypair-name db key-uids-set)]
{:db (-> db
(update-in [:wallet :accounts] update-accounts key-uids-set)
(update-in [:wallet :keypairs] update-keypairs key-uids-set))
:fx [[:dispatch
[:toasts/upsert
{:type :positive
:theme :dark
:text (if (= (count key-uids-to-update) 1)
(i18n/label :t/key-pair-imported-successfully {:name keypair-name})
(i18n/label :t/key-pairs-successfully-imported
{:count (count key-uids-to-update)}))}]]]})))

(defn- input-connection-string-for-importing-keypairs-keystores-callback
[res keypairs-key-uids]
(log/info "[local-pairing] input-connection-string-for-importing-keypairs-keystores callback"
{:response res
:event :settings/input-connection-string-for-importing-keypairs-keystores-callback})
(let [error (when (syncing-events/extract-error res)
(str "generic-error: " res))]
(when-not (some? error)
(rf/dispatch [:wallet/make-key-pairs-fully-operable keypairs-key-uids]))
(when (some? error)
(rf/dispatch [:toasts/upsert
{:type :negative
:text error}]))))
(defn make-keypairs-accounts-fully-operable
[{:keys [db]} [key-uids-to-update]]
(let [key-uids-set (set key-uids-to-update)
keypair-name (data-store/extract-keypair-name db key-uids-set)]
{:db (-> db
(update-in [:wallet :accounts] #(data-store/make-accounts-fully-operable % key-uids-set))
(update-in [:wallet :keypairs] #(data-store/make-keypairs-fully-operable % key-uids-set)))
:fx [[:dispatch
[:toasts/upsert
{:type :positive
:theme :dark
:text (if (= (count key-uids-to-update) 1)
(i18n/label :t/key-pair-imported-successfully {:name keypair-name})
(i18n/label :t/key-pairs-successfully-imported
{:count (count key-uids-to-update)}))}]]]}))

(rf/reg-event-fx :wallet/make-keypairs-accounts-fully-operable make-keypairs-accounts-fully-operable)

(rf/reg-fx :effects.connection-string/import-keypair
(fn [{:keys [key-uid sha3-pwd keypairs-key-uids connection-string on-success on-fail]}]
(let [config-map (.stringify js/JSON
(clj->js
{:receiverConfig
{:loggedInKeyUid key-uid
:keystorePath ""
:password (security/safe-unmask-data
sha3-pwd)
:keypairsToImport keypairs-key-uids}}))]
(-> (native-module/input-connection-string-for-importing-keypairs-keystores
connection-string
config-map)
(promesa/then (fn [res]
(let [error (when (syncing-events/extract-error res)
(str "generic-error: " res))]
(if-not (some? error)
(on-success)
(on-fail error)))))
(promesa/catch #(log/error "error import-keypair/connection-string " %))))))

(defn connection-string-for-key-pair-import
[{:keys [db]} [{:keys [sha3-pwd keypairs-key-uids connection-string]}]]
(let [key-uid (get-in db [:profile/profile :key-uid])
config-map (.stringify js/JSON
(clj->js
{:receiverConfig
{:loggedInKeyUid key-uid
:keystorePath ""
:password (security/safe-unmask-data
sha3-pwd)
:keypairsToImport keypairs-key-uids}}))]
(native-module/input-connection-string-for-importing-keypairs-keystores
connection-string
config-map
#(input-connection-string-for-importing-keypairs-keystores-callback % keypairs-key-uids))))
(let [key-uid (get-in db [:profile/profile :key-uid])]
{:fx [[:effects.connection-string/import-keypair
{:key-uid key-uid
:sha3-pwd sha3-pwd
:keypairs-key-uids keypairs-key-uids
:connection-string connection-string
:on-success #(rf/dispatch [:wallet/make-keypairs-accounts-fully-operable
keypairs-key-uids])
:on-fail #(rf/dispatch [:toasts/upsert
{:type :negative
:text %}])}]]}))

(rf/reg-event-fx :wallet/connection-string-for-key-pair-import connection-string-for-key-pair-import)

(defn success-keypair-qr-scan
[_ [connection-string keypairs-key-uids]]
{:fx [(if (sync-utils/valid-connection-string? connection-string)
[:dispatch
[:standard-auth/authorize-with-password
{:blur? true
:theme :dark
:auth-button-label (i18n/label :t/confirm)
:on-auth-success (fn [password]
(rf/dispatch [:hide-bottom-sheet])
(rf/dispatch
[:wallet/connection-string-for-key-pair-import
{:connection-string connection-string
:keypairs-key-uids keypairs-key-uids
:sha3-pwd password}]))}]]
[:dispatch
[:toasts/upsert
{:type :negative
:theme :dark
:text (i18n/label :t/invalid-qr)}]])]})

(rf/reg-event-fx :wallet/success-keypair-qr-scan success-keypair-qr-scan)

0 comments on commit ae982ed

Please sign in to comment.