From 5935d29d7dcef538bf54ea630ac4ae007876f5f8 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Thu, 21 Jun 2018 20:08:10 +0200 Subject: [PATCH] Allow user to access account on invalid key Signed-off-by: Julien Eluard --- src/status_im/data_store/realm/core.cljs | 20 +++++++++++----- src/status_im/translations/en.cljs | 6 ++--- src/status_im/ui/screens/accounts/events.cljs | 2 +- .../ui/screens/accounts/login/events.cljs | 3 +-- src/status_im/ui/screens/events.cljs | 24 ++++++++++++------- src/status_im/utils/fs.cljs | 3 +++ src/status_im/utils/keychain/events.cljs | 5 ++-- 7 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/status_im/data_store/realm/core.cljs b/src/status_im/data_store/realm/core.cljs index 9169ccec4b..d7a1ec7026 100644 --- a/src/status_im/data_store/realm/core.cljs +++ b/src/status_im/data_store/realm/core.cljs @@ -41,14 +41,22 @@ [file-name] (.deleteFile rn-dependencies/realm (clj->js {:path file-name}))) +(defn- default-realm-dir [path] + (string/replace path #"default\.realm$" "")) + +(defn- is-realm-file? [n] + (or (re-matches #".*/default\.realm$" n) + (re-matches #".*/new-account$" n) + (re-matches #".*/[0-9a-f]{40}$" n))) + (defn- delete-realms [] (log/warn "realm: deleting all realms") - (try - (do - (delete-realm (.-defaultPath rn-dependencies/realm)) - (delete-realm new-account-filename)) - (catch :default ex - (log/warn "failed to delete realm" ex)))) + (.. + (fs/read-dir (default-realm-dir (.-defaultPath rn-dependencies/realm))) + (then #(->> (js->clj % :keywordize-keys true) + (map :path) + (filter is-realm-file?) + (run! delete-realm))))) (defn- close [realm] (when realm diff --git a/src/status_im/translations/en.cljs b/src/status_im/translations/en.cljs index 6665dcadf5..017aa4f000 100644 --- a/src/status_im/translations/en.cljs +++ b/src/status_im/translations/en.cljs @@ -682,9 +682,9 @@ ;; invalid-key - :invalid-key-title "Invalid key detected" - :invalid-key-content "The key used to encrypt your data is invalid. Clicking on 'Reset database' will delete any existing realm and create a new one with a stronger key. If you would like to backup you data please click on cancel and the app will quit. Ensure the data directory of the app is clean and make sure no data is backed up by your cloud provider." - :invalid-key-confirm "Reset database" + :invalid-key-title "We detected a problem with the encryption key" + :invalid-key-content "To protect yourself, you need to create new account and erase your old data by tapping “Apply”. If you have an existing account and would like to save your seed phrase then choose “Cancel”, back it up, and restart the app. We strongly recommend creating new account because the old one is stored unencrypted." + :invalid-key-confirm "Apply" ;; browser :browser "Browser" diff --git a/src/status_im/ui/screens/accounts/events.cljs b/src/status_im/ui/screens/accounts/events.cljs index bf623664a2..f047e34497 100644 --- a/src/status_im/ui/screens/accounts/events.cljs +++ b/src/status_im/ui/screens/accounts/events.cljs @@ -198,4 +198,4 @@ (handlers-macro/merge-fx cofx (wallet-set-up-passed db) - (accounts.utils/account-update {:wallet-set-up-passed? true})))) \ No newline at end of file + (accounts.utils/account-update {:wallet-set-up-passed? true})))) diff --git a/src/status_im/ui/screens/accounts/login/events.cljs b/src/status_im/ui/screens/accounts/login/events.cljs index ff4435efdb..20f8298277 100644 --- a/src/status_im/ui/screens/accounts/login/events.cljs +++ b/src/status_im/ui/screens/accounts/login/events.cljs @@ -44,8 +44,7 @@ (catch (fn [{:keys [error key]}] ;; no need of further error handling as already taken care ;; when starting the app - (when (= error :weak-key) - (change-account address key))))))) + (change-account address (or key ""))))))) ;;;; Handlers diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index 84ed7a093f..d547f318ae 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -223,22 +223,30 @@ (fn [_ _] {:get-encryption-key [:initialize-app]})) -(def handle-invalid-key-parameters +(defn- reset-keychain [] + (.. (keychain/reset) + (then + #(re-frame/dispatch [:initialize-keychain])))) + +(defn handle-invalid-key-parameters [encryption-key] {:title (i18n/label :invalid-key-title) :content (i18n/label :invalid-key-content) :confirm-button-text (i18n/label :invalid-key-confirm) - :on-cancel #(.exitApp react/back-handler) + ;; On cancel we initialize the app with the invalid key, to allow the user + ;; to recover the seed phrase + :on-cancel #(do + (log/warn "initializing app with invalid key") + (re-frame/dispatch [:initialize-app encryption-key])) :on-accept (fn [] - (realm/delete-realms) - (.. (keychain/reset) - (then - #(re-frame/dispatch [:initialize-keychain]))))}) + (.. (realm/delete-realms) + (then reset-keychain) + (catch reset-keychain)))}) (handlers/register-handler-fx :initialize-app (fn [_ [_ encryption-key error]] - (if (= error :invalid-key) - {:show-confirmation handle-invalid-key-parameters} + (if (= :invalid-key error) + {:show-confirmation (handle-invalid-key-parameters encryption-key)} {::init-device-UUID nil ::testfairy-alert nil :dispatch-n [[:initialize-db encryption-key] diff --git a/src/status_im/utils/fs.cljs b/src/status_im/utils/fs.cljs index 38b02db7be..30943f2746 100644 --- a/src/status_im/utils/fs.cljs +++ b/src/status_im/utils/fs.cljs @@ -10,3 +10,6 @@ (-> (.readFile rn-dependencies/fs path encoding) (.then on-read) (.catch on-error))) + +(defn read-dir [path] + (.readDir rn-dependencies/fs path)) diff --git a/src/status_im/utils/keychain/events.cljs b/src/status_im/utils/keychain/events.cljs index e290ecff4e..4d4f672126 100644 --- a/src/status_im/utils/keychain/events.cljs +++ b/src/status_im/utils/keychain/events.cljs @@ -6,8 +6,9 @@ (defn handle-key-error [event {:keys [error key]}] (if (= :weak-key error) (log/warn "weak key used, database might not be encrypted properly") - (log/error "invalid key detected")) - (re-frame/dispatch (into [] (concat event [key error])))) + (log/warn "invalid key detected")) + (re-frame/dispatch (into [] (concat event [(or key "") + (or error :invalid-key)])))) (re-frame/reg-fx :get-encryption-key