From f8c9bec38316b836dab084767f5d1fe9c1031fab Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Wed, 11 Jul 2018 21:52:53 +0200 Subject: [PATCH] [Fixes #5127] Move files to no-backup directory & hash accounts Signed-off-by: Andrea Maria Piana --- android/app/src/main/AndroidManifest.xml | 3 +- src/status_im/data_store/core.cljs | 12 ++- src/status_im/data_store/realm/core.cljs | 74 +++++++++++++++---- src/status_im/ui/screens/events.cljs | 12 ++- src/status_im/utils/fs.cljs | 12 ++- .../react_native/js_dependencies.cljs | 1 + .../status_im/test/data_store/realm/core.cljs | 31 ++++++++ 7 files changed, 116 insertions(+), 29 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index fdf0204d9a..4bf653238c 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -25,7 +25,8 @@ js {:path file-name}))) - -(defn- default-realm-dir [path] - (string/replace path #"default\.realm$" "")) +(defn- is-account-file? [n] + (re-matches #".*/[0-9a-f]{40}$" n)) (defn- is-realm-file? [n] - (or (re-matches #".*/default\.realm$" n) - (re-matches #".*/new-account$" n) - (re-matches #".*/[0-9a-f]{40}$" n))) + (or (re-matches #".*/default\.realm(\.management|\.lock|\.note)?$" n) + (re-matches #".*/new-account(\.management|\.lock|\.note)?$" n) + (re-matches #".*/[0-9a-f]{40}(\.management|\.lock|\.note)?$" n))) + +(defn- realm-management-file? [n] + (re-matches #".*(\.management|\.lock|\.note)$" n)) + +(def old-base-realm-path + (.-defaultPath rn-dependencies/realm)) + +(def realm-dir + (cond + utils.platform/android? (str + (.-DocumentDirectoryPath rn-dependencies/fs) + "/../no_backup/realm/") + utils.platform/ios? (str + (.-LibraryDirectoryPath rn-dependencies/fs) + "/realm/") + :else (.-defaultPath rn-dependencies/realm))) + +(def old-realm-dir + (string/replace old-base-realm-path #"default\.realm$" "")) + +(def accounts-realm-dir + (str realm-dir "accounts/")) + +(def base-realm-path + (str realm-dir + "default.realm")) (defn- delete-realms [] (log/warn "realm: deleting all realms") + (fs/unlink realm-dir)) + +(defn- ensure-directories [] (.. - (fs/read-dir (default-realm-dir (.-defaultPath rn-dependencies/realm))) + (fs/mkdir realm-dir) + (then #(fs/mkdir accounts-realm-dir)))) + +(defn- move-realm-to-library [path] + (let [filename (last (string/split path "/")) + new-path (if (is-account-file? path) + (str accounts-realm-dir (utils.ethereum/sha3 filename)) + (str realm-dir filename))] + (log/debug "realm: moving " path " to " new-path) + (if (realm-management-file? path) + (fs/unlink path) + (fs/move-file path new-path)))) + +(defn- move-realms [] + (log/info "realm: moving all realms") + (.. + (fs/read-dir old-realm-dir) (then #(->> (js->clj % :keywordize-keys true) (map :path) - (filter is-realm-file?) - (run! delete-realm))))) + (filter is-realm-file?))) + (then #(js/Promise.all (clj->js (map move-realm-to-library %)))))) (defn- close [realm] (when realm @@ -98,12 +141,13 @@ (log/debug "Opening base realm... (first run)") (when @base-realm (close @base-realm)) - (reset! base-realm (open-migrated-realm (.-defaultPath rn-dependencies/realm) base/schemas encryption-key)) + (reset! base-realm (open-migrated-realm base-realm-path base/schemas encryption-key)) (log/debug "Created @base-realm")) (defn change-account [address encryption-key] - (close-account-realm) - (reset! account-realm (open-migrated-realm address account/schemas encryption-key))) + (let [path (str accounts-realm-dir (utils.ethereum/sha3 address))] + (close-account-realm) + (reset! account-realm (open-migrated-realm path account/schemas encryption-key)))) (declare realm-obj->clj) diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index a8bb460b16..c4aec66606 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -143,13 +143,11 @@ (re-frame/reg-fx ::init-store (fn [encryption-key] - (try - (do - (data-store/init encryption-key) - (re-frame/dispatch [:after-decryption])) - (catch js/Error error - (log/warn "Could not decrypt database" error) - (re-frame/dispatch [:initialize-app encryption-key :decryption-failed]))))) + (.. (data-store/init encryption-key) + (then #(re-frame/dispatch [:after-decryption])) + (catch (fn [error] + (log/warn "Could not decrypt database" error) + (re-frame/dispatch [:initialize-app encryption-key :decryption-failed])))))) (re-frame/reg-fx :initialize-geth-fx diff --git a/src/status_im/utils/fs.cljs b/src/status_im/utils/fs.cljs index 30943f2746..10e17a57ec 100644 --- a/src/status_im/utils/fs.cljs +++ b/src/status_im/utils/fs.cljs @@ -1,10 +1,8 @@ (ns status-im.utils.fs (:require [status-im.react-native.js-dependencies :as rn-dependencies])) -(defn move-file [src dst handler] - (-> (.moveFile rn-dependencies/fs src dst) - (.then #(handler nil %)) - (.catch #(handler % nil)))) +(defn move-file [src dst] + (.moveFile rn-dependencies/fs src dst)) (defn read-file [path encoding on-read on-error] (-> (.readFile rn-dependencies/fs path encoding) @@ -13,3 +11,9 @@ (defn read-dir [path] (.readDir rn-dependencies/fs path)) + +(defn mkdir [path] + (.mkdir rn-dependencies/fs path)) + +(defn unlink [path] + (.unlink rn-dependencies/fs path)) diff --git a/test/cljs/status_im/react_native/js_dependencies.cljs b/test/cljs/status_im/react_native/js_dependencies.cljs index 51f97cba0a..30ba09cf02 100644 --- a/test/cljs/status_im/react_native/js_dependencies.cljs +++ b/test/cljs/status_im/react_native/js_dependencies.cljs @@ -24,6 +24,7 @@ :DeviceEventEmitter #js {:addListener (fn [])} :Dimensions #js {:get (fn [])}}) (def realm #js {:schemaVersion (fn []) + :defaultPath "/tmp/realm" :close (fn [])}) (def vector-icons #js {:default #js {}}) (def webview-bridge #js {:default #js {}}) diff --git a/test/cljs/status_im/test/data_store/realm/core.cljs b/test/cljs/status_im/test/data_store/realm/core.cljs index c8a973cd77..d1c38e0651 100644 --- a/test/cljs/status_im/test/data_store/realm/core.cljs +++ b/test/cljs/status_im/test/data_store/realm/core.cljs @@ -1,6 +1,7 @@ (ns status-im.test.data-store.realm.core (:require [cljs.test :refer-macros [deftest is testing use-fixtures]] [status-im.utils.utils :as utils] + [clojure.string :as string] [status-im.data-store.realm.core :as core])) (def migrated-realm? (atom nil)) @@ -9,6 +10,9 @@ (reset! migrated-realm? nil) (f)) +(def valid-account-path + (str "/some/" (string/join (repeat 40 "a")))) + (use-fixtures :each fixtures) (deftest migrate-realm @@ -24,6 +28,33 @@ (testing "it migrates the db" (is @migrated-realm?)))))) +(deftest is-account-file-test + (testing "not an account file" + (is (not (core/is-account-file? "not-one"))) + (is (not (core/is-account-file? "000000000000000009212102")))) + (testing "an account file" + (is (core/is-account-file? valid-account-path)))) + +(deftest is-realm-file-test + (testing "not a realm file" + (is (not (core/is-realm-file? "not-one"))) + (is (not (core/is-realm-file? "000000000000000009212102")))) + (testing "realm files" + (is (core/is-realm-file? "/some/new-account")) + (is (core/is-realm-file? "/some/new-account.lock")) + (is (core/is-realm-file? "/some/new-account.management")) + (is (core/is-realm-file? "/some/new-account.note")) + (is (core/is-realm-file? "/some/default.realm")) + (is (core/is-realm-file? valid-account-path)))) + +(deftest realm-management-file-test + (testing "not a management file" + (is (not (core/realm-management-file? "new-account")))) + (testing "management file" + (is (core/realm-management-file? "anything.management")) + (is (core/realm-management-file? "anything.lock")) + (is (core/realm-management-file? "anything.note")))) + (deftest serialization (is (nil? (core/deserialize ""))) (is (nil? (core/deserialize "giberrish")))