diff --git a/src/status_im/contexts/settings/wallet/effects.cljs b/src/status_im/contexts/settings/wallet/effects.cljs new file mode 100644 index 000000000000..1ba4a2d9588f --- /dev/null +++ b/src/status_im/contexts/settings/wallet/effects.cljs @@ -0,0 +1,45 @@ +(ns status-im.contexts.settings.wallet.effects + (:require [native-module.core :as native-module] + [promesa.core :as promesa] + [status-im.contexts.syncing.events :as syncing-events] + [status-im.contexts.syncing.utils :as sync-utils] + [utils.re-frame :as rf] + [utils.security.core :as security] + [utils.transforms :as transforms])) + +(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 (throw (js/Error. + "generic-error: failed to get connection string")))))) + (promesa/catch on-fail))))) + +(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] + (if-let [error (when (syncing-events/extract-error res) + (str "generic-error: " res))] + (on-fail error) + (on-success keypairs-key-uids)))) + (promesa/catch on-fail))))) diff --git a/src/status_im/contexts/settings/wallet/events.cljs b/src/status_im/contexts/settings/wallet/events.cljs index 97adb73d105d..cce3b5fdf220 100644 --- a/src/status_im/contexts/settings/wallet/events.cljs +++ b/src/status_im/contexts/settings/wallet/events.cljs @@ -1,15 +1,10 @@ (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] [utils.i18n :as i18n] - [utils.re-frame :as rf] - [utils.security.core :as security] - [utils.transforms :as transforms])) + [utils.re-frame :as rf])) (rf/reg-event-fx :wallet/rename-keypair-success @@ -34,22 +29,6 @@ (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-keypair-export-connection [{:keys [db]} [{:keys [sha3-pwd keypair-key-uid callback]}]] (let [key-uid (get-in db [:profile/profile :key-uid])] @@ -63,7 +42,7 @@ :on-fail (fn [error] (rf/dispatch [:toasts/upsert {:type :negative - :text error}]))}]]})) + :text (.-message error)}]))}]]})) (rf/reg-event-fx :wallet/get-keypair-export-connection get-keypair-export-connection) @@ -85,7 +64,7 @@ [{:method "accounts_deleteKeypair" :params [key-uid] :on-success [:wallet/remove-keypair-success key-uid] - :on-error #(log/info "failed to remove keypair " %)}]]]}) + :on-error #(log/error "failed to remove keypair " {:error %})}]]]}) (rf/reg-event-fx :wallet/remove-keypair remove-keypair) @@ -107,27 +86,6 @@ (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-import-keypair [{:keys [db]} [{:keys [sha3-pwd keypairs-key-uids connection-string]}]] (let [key-uid (get-in db [:profile/profile :key-uid])] @@ -136,8 +94,7 @@ :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-success #(rf/dispatch [:wallet/make-keypairs-accounts-fully-operable %]) :on-fail #(rf/dispatch [:toasts/upsert {:type :negative :text %}])}]]})) diff --git a/src/status_im/contexts/settings/wallet/events_test.cljs b/src/status_im/contexts/settings/wallet/events_test.cljs index 62793973f36d..f74b20e7c26e 100644 --- a/src/status_im/contexts/settings/wallet/events_test.cljs +++ b/src/status_im/contexts/settings/wallet/events_test.cljs @@ -7,44 +7,54 @@ (def mock-key-uid "key-1") (defn mock-db [keypairs accounts] - {:wallet {:keypairs keypairs - :accounts accounts} - :profile {:profile {:key-uid "test-key-uid"}}}) + {:wallet {:keypairs keypairs + :accounts accounts} + :profile/profile {:key-uid "test-key-uid"}}) -(deftest test-rename-keypair +(deftest rename-keypair-test (let [new-keypair-name "key pair new" - cofx {:db {}} - expected {:fx [[:json-rpc/call - [{:method "accounts_updateKeypairName" - :params [mock-key-uid new-keypair-name] - :on-success [:wallet/rename-keypair-success mock-key-uid - new-keypair-name] - :on-error fn?}]]]}] - (is (match? expected - (sut/rename-keypair cofx - [{:key-uid mock-key-uid - :keypair-name new-keypair-name}]))))) + cofx {:db {}}] + (testing "rename-keypair" + (let [expected {:fx [[:json-rpc/call + [{:method "accounts_updateKeypairName" + :params [mock-key-uid new-keypair-name] + :on-success [:wallet/rename-keypair-success mock-key-uid + new-keypair-name] + :on-error fn?}]]]}] + (is (match? expected + (sut/rename-keypair cofx + [{:key-uid mock-key-uid + :keypair-name new-keypair-name}]))))))) -(deftest test-get-keypair-export-connection - (let [db (mock-db [] {}) - sha3-pwd "test-password" - keypair-key-uid "test-keypair-uid" - callback (fn [connect-string] (println "callback" connect-string))] - (testing "test-get-keypair-export-connection" - (let [effects (sut/get-keypair-export-connection - {:db db} - [{:sha3-pwd sha3-pwd :keypair-key-uid keypair-key-uid :callback callback}]) - fx (:fx effects)] - (is (some? fx)))))) +(deftest get-keypair-export-connection-test + (let [cofx {:db (mock-db [] {})} + sha3-pwd "test-password" + user-key-uid "test-key-uid" + callback (fn [connect-string] (println "callback" connect-string))] + (testing "get-keypair-export-connection" + (let [expected {:fx [[:effects.connection-string/export-keypair + {:key-uid user-key-uid + :sha3-pwd sha3-pwd + :keypair-key-uid mock-key-uid + :on-success fn? + :on-fail fn?}]]}] + (is (match? expected + (sut/get-keypair-export-connection + cofx + [{:sha3-pwd sha3-pwd :keypair-key-uid mock-key-uid :callback callback}]))))))) -(deftest test-remove-keypair - (let [db (mock-db [{:key-uid mock-key-uid :name "Key 1"}] {})] +(deftest remove-keypair-test + (let [cofx {:db {}}] (testing "remove-keypair" - (let [effects (sut/remove-keypair {:db db} [{:key-uid mock-key-uid}]) - fx (:fx effects)] - (is (some? fx)))))) + (let [expected {:fx [[:json-rpc/call + [{:method "accounts_deleteKeypair" + :params [mock-key-uid] + :on-success [:wallet/remove-keypair-success mock-key-uid] + :on-error fn?}]]]}] + (is (match? expected + (sut/remove-keypair cofx [mock-key-uid]))))))) -(deftest test-make-keypairs-accounts-fully-operable +(deftest make-keypairs-accounts-fully-operable-test (let [db (mock-db [{:key-uid mock-key-uid :accounts [{:key-uid mock-key-uid :operable "no"}]}] {"0x1" {:key-uid mock-key-uid :operable "no"}}) @@ -58,21 +68,27 @@ (is (= (keyword (-> updated-keypair :accounts first :operable)) :fully)) (is (= (keyword (:operable updated-account)) :fully)))))) -(deftest test-connection-string-for-import-keypair - (let [db (mock-db [] {}) +(deftest connection-string-for-import-keypair-test + (let [cofx {:db (mock-db [] {})} sha3-pwd "test-password" - keypairs-key-uids ["test-keypair-uid"] + user-key-uid "test-key-uid" connection-string "test-connection-string"] (testing "connection-string-for-import-keypair" - (let [effects (sut/connection-string-for-import-keypair - {:db db} - [{:sha3-pwd sha3-pwd - :keypairs-key-uids keypairs-key-uids - :connection-string connection-string}]) - fx (:fx effects)] - (is (some? fx)))))) + (let [expected {:fx [[:effects.connection-string/import-keypair + {:key-uid user-key-uid + :sha3-pwd sha3-pwd + :keypairs-key-uids [mock-key-uid] + :connection-string connection-string + :on-success fn? + :on-fail fn?}]]}] + (is (match? expected + (sut/connection-string-for-import-keypair cofx + [{:sha3-pwd sha3-pwd + :keypairs-key-uids [mock-key-uid] + :connection-string + connection-string}]))))))) -(deftest test-success-keypair-qr-scan +(deftest success-keypair-qr-scan-test (let [connection-string "valid-connection-string" keypairs-key-uids ["keypair-uid"]] (testing "success-keypair-qr-scan" diff --git a/src/status_im/contexts/wallet/events.cljs b/src/status_im/contexts/wallet/events.cljs index c22b95c55643..5ca07040eb79 100644 --- a/src/status_im/contexts/wallet/events.cljs +++ b/src/status_im/contexts/wallet/events.cljs @@ -4,6 +4,7 @@ [clojure.string :as string] [react-native.platform :as platform] [status-im.constants :as constants] + [status-im.contexts.settings.wallet.effects] [status-im.contexts.settings.wallet.events] [status-im.contexts.wallet.common.utils.networks :as network-utils] [status-im.contexts.wallet.data-store :as data-store]