Skip to content

Commit

Permalink
[#20035] fix: remove unnecessary checks and manage errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ghafouri committed May 30, 2024
1 parent 04062b6 commit ff4a73e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 46 deletions.
25 changes: 9 additions & 16 deletions src/status_im/contexts/settings/wallet/effects.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(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]
Expand All @@ -20,26 +19,20 @@
(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"))))))
(on-fail (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}}))]
(let [config-map (transforms/clj->json {: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/then #(on-success keypairs-key-uids))
(promesa/catch on-fail)))))
36 changes: 15 additions & 21 deletions src/status_im/contexts/settings/wallet/events.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns status-im.contexts.settings.wallet.events
(:require
[status-im.contexts.settings.wallet.data-store :as data-store]
[status-im.contexts.syncing.utils :as sync-utils]
[taoensso.timbre :as log]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))
Expand Down Expand Up @@ -96,30 +95,25 @@
:connection-string connection-string
:on-success #(rf/dispatch [:wallet/make-keypairs-accounts-fully-operable %])
:on-fail #(rf/dispatch [:toasts/upsert
{:type :negative
:text %}])}]]}))
{:type :negative
:theme :dark
:text %}])}]]}))

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

(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-import-keypair
{: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)}]])]})
{:fx [[: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-import-keypair
{:connection-string connection-string
:keypairs-key-uids keypairs-key-uids
:sha3-pwd password}]))}]]]})

(rf/reg-event-fx :wallet/success-keypair-qr-scan success-keypair-qr-scan)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[react-native.core :as rn]
[status-im.common.scan-qr-code.view :as scan-qr-code]
[status-im.contexts.communities.events]
[status-im.contexts.syncing.utils :as sync-utils]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))

Expand All @@ -17,4 +18,6 @@
{:title (i18n/label :t/scan-key-pairs-qr-code)
:subtitle (i18n/label :t/find-it-in-setting)
:share-button? false
:validate-fn sync-utils/valid-connection-string?
:error-message (i18n/label :t/invalid-qr)
:on-success-scan on-success-scan}]))
9 changes: 1 addition & 8 deletions src/status_im/contexts/syncing/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,12 @@
:VerifyTransactionChainID config/verify-transaction-chain-id}}
log-config)))

(defn extract-error
[json-str]
(-> json-str
transforms/json->clj
(get :error "")
not-empty))

(defn- input-connection-string-callback
[res]
(log/info "[local-pairing] input-connection-string-for-bootstrapping callback"
{:response res
:event :syncing/input-connection-string-for-bootstrapping})
(let [error (when (extract-error res)
(let [error (when (sync-utils/extract-error res)
(str "generic-error: " res))]
(when (some? error)
(rf/dispatch [:toasts/upsert
Expand Down
10 changes: 9 additions & 1 deletion src/status_im/contexts/syncing/utils.cljs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
(ns status-im.contexts.syncing.utils
(:require
[clojure.string :as string]
[status-im.constants :as constants]))
[status-im.constants :as constants]
[utils.transforms :as transforms]))

(defn valid-connection-string?
[connection-string]
(when connection-string
(string/starts-with?
connection-string
constants/local-pairing-connection-string-identifier)))

(defn extract-error
[json-str]
(-> json-str
transforms/json->clj
(get :error "")
not-empty))

0 comments on commit ff4a73e

Please sign in to comment.