[Fixes #5127] Move files to no-backup directory & hash accounts

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Andrea Maria Piana 2018-07-11 21:52:53 +02:00
parent 6ec40b3e21
commit f8c9bec383
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
7 changed files with 116 additions and 29 deletions

View File

@ -25,7 +25,8 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove"/> <uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove"/>
<application <application
android:allowBackup="true" tools:replace="android:allowBackup"
android:allowBackup="false"
android:label="@string/app_name" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme" android:theme="@style/AppTheme"

View File

@ -14,8 +14,16 @@
status-im.data-store.requests)) status-im.data-store.requests))
(defn init [encryption-key] (defn init [encryption-key]
(when-not @data-source/base-realm (if @data-source/base-realm
(data-source/open-base-realm encryption-key))) (js/Promise.resolve)
(..
(data-source/ensure-directories)
;; This can be removed when we are confident all the users
;; have migrated the data, introduced in 0.9.23
(then #(data-source/move-realms))
(catch (fn [error]
(log/error "Could not move realms" error)))
(then #(data-source/open-base-realm encryption-key)))))
(defn change-account [address encryption-key] (defn change-account [address encryption-key]
(log/debug "changing account to: " address) (log/debug "changing account to: " address)

View File

@ -7,6 +7,8 @@
[taoensso.timbre :as log] [taoensso.timbre :as log]
[status-im.utils.fs :as fs] [status-im.utils.fs :as fs]
[status-im.utils.async :as utils.async] [status-im.utils.async :as utils.async]
[status-im.utils.platform :as utils.platform]
[status-im.utils.ethereum.core :as utils.ethereum]
[cognitect.transit :as transit] [cognitect.transit :as transit]
[status-im.react-native.js-dependencies :as rn-dependencies] [status-im.react-native.js-dependencies :as rn-dependencies]
[status-im.utils.utils :as utils])) [status-im.utils.utils :as utils]))
@ -34,26 +36,67 @@
(when (exists? js/window) (when (exists? js/window)
(rn-dependencies/realm. options-js)))) (rn-dependencies/realm. options-js))))
(defn- delete-realm (defn- is-account-file? [n]
[file-name] (re-matches #".*/[0-9a-f]{40}$" n))
(.deleteFile rn-dependencies/realm (clj->js {:path file-name})))
(defn- default-realm-dir [path]
(string/replace path #"default\.realm$" ""))
(defn- is-realm-file? [n] (defn- is-realm-file? [n]
(or (re-matches #".*/default\.realm$" n) (or (re-matches #".*/default\.realm(\.management|\.lock|\.note)?$" n)
(re-matches #".*/new-account$" n) (re-matches #".*/new-account(\.management|\.lock|\.note)?$" n)
(re-matches #".*/[0-9a-f]{40}$" 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 [] (defn- delete-realms []
(log/warn "realm: deleting all 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) (then #(->> (js->clj % :keywordize-keys true)
(map :path) (map :path)
(filter is-realm-file?) (filter is-realm-file?)))
(run! delete-realm))))) (then #(js/Promise.all (clj->js (map move-realm-to-library %))))))
(defn- close [realm] (defn- close [realm]
(when realm (when realm
@ -98,12 +141,13 @@
(log/debug "Opening base realm... (first run)") (log/debug "Opening base realm... (first run)")
(when @base-realm (when @base-realm
(close @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")) (log/debug "Created @base-realm"))
(defn change-account [address encryption-key] (defn change-account [address encryption-key]
(close-account-realm) (let [path (str accounts-realm-dir (utils.ethereum/sha3 address))]
(reset! account-realm (open-migrated-realm address account/schemas encryption-key))) (close-account-realm)
(reset! account-realm (open-migrated-realm path account/schemas encryption-key))))
(declare realm-obj->clj) (declare realm-obj->clj)

View File

@ -143,13 +143,11 @@
(re-frame/reg-fx (re-frame/reg-fx
::init-store ::init-store
(fn [encryption-key] (fn [encryption-key]
(try (.. (data-store/init encryption-key)
(do (then #(re-frame/dispatch [:after-decryption]))
(data-store/init encryption-key) (catch (fn [error]
(re-frame/dispatch [:after-decryption])) (log/warn "Could not decrypt database" error)
(catch js/Error error (re-frame/dispatch [:initialize-app encryption-key :decryption-failed]))))))
(log/warn "Could not decrypt database" error)
(re-frame/dispatch [:initialize-app encryption-key :decryption-failed])))))
(re-frame/reg-fx (re-frame/reg-fx
:initialize-geth-fx :initialize-geth-fx

View File

@ -1,10 +1,8 @@
(ns status-im.utils.fs (ns status-im.utils.fs
(:require [status-im.react-native.js-dependencies :as rn-dependencies])) (:require [status-im.react-native.js-dependencies :as rn-dependencies]))
(defn move-file [src dst handler] (defn move-file [src dst]
(-> (.moveFile rn-dependencies/fs src dst) (.moveFile rn-dependencies/fs src dst))
(.then #(handler nil %))
(.catch #(handler % nil))))
(defn read-file [path encoding on-read on-error] (defn read-file [path encoding on-read on-error]
(-> (.readFile rn-dependencies/fs path encoding) (-> (.readFile rn-dependencies/fs path encoding)
@ -13,3 +11,9 @@
(defn read-dir [path] (defn read-dir [path]
(.readDir rn-dependencies/fs path)) (.readDir rn-dependencies/fs path))
(defn mkdir [path]
(.mkdir rn-dependencies/fs path))
(defn unlink [path]
(.unlink rn-dependencies/fs path))

View File

@ -24,6 +24,7 @@
:DeviceEventEmitter #js {:addListener (fn [])} :DeviceEventEmitter #js {:addListener (fn [])}
:Dimensions #js {:get (fn [])}}) :Dimensions #js {:get (fn [])}})
(def realm #js {:schemaVersion (fn []) (def realm #js {:schemaVersion (fn [])
:defaultPath "/tmp/realm"
:close (fn [])}) :close (fn [])})
(def vector-icons #js {:default #js {}}) (def vector-icons #js {:default #js {}})
(def webview-bridge #js {:default #js {}}) (def webview-bridge #js {:default #js {}})

View File

@ -1,6 +1,7 @@
(ns status-im.test.data-store.realm.core (ns status-im.test.data-store.realm.core
(:require [cljs.test :refer-macros [deftest is testing use-fixtures]] (:require [cljs.test :refer-macros [deftest is testing use-fixtures]]
[status-im.utils.utils :as utils] [status-im.utils.utils :as utils]
[clojure.string :as string]
[status-im.data-store.realm.core :as core])) [status-im.data-store.realm.core :as core]))
(def migrated-realm? (atom nil)) (def migrated-realm? (atom nil))
@ -9,6 +10,9 @@
(reset! migrated-realm? nil) (reset! migrated-realm? nil)
(f)) (f))
(def valid-account-path
(str "/some/" (string/join (repeat 40 "a"))))
(use-fixtures :each fixtures) (use-fixtures :each fixtures)
(deftest migrate-realm (deftest migrate-realm
@ -24,6 +28,33 @@
(testing "it migrates the db" (testing "it migrates the db"
(is @migrated-realm?)))))) (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 (deftest serialization
(is (nil? (core/deserialize ""))) (is (nil? (core/deserialize "")))
(is (nil? (core/deserialize "giberrish"))) (is (nil? (core/deserialize "giberrish")))