From 0ce43593aacb01c13837631be8fafe0c351cbf29 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 17 Nov 2016 19:33:38 +0200 Subject: [PATCH] closes #446 --- src/status_im/accounts/handlers.cljs | 9 ++++--- .../realm/schemas/account/v4/chat.cljs | 2 +- .../data_store/realm/schemas/base/core.cljs | 8 ++++-- .../realm/schemas/base/v2/account.cljs | 26 +++++++++++++++++++ .../realm/schemas/base/v2/core.cljs | 11 ++++++++ src/status_im/handlers.cljs | 19 +++++++++----- src/status_im/protocol/handlers.cljs | 5 ++-- src/status_im/utils/ethereum_network.cljs | 23 ++++++++++++++++ 8 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 src/status_im/data_store/realm/schemas/base/v2/account.cljs create mode 100644 src/status_im/data_store/realm/schemas/base/v2/core.cljs create mode 100644 src/status_im/utils/ethereum_network.cljs diff --git a/src/status_im/accounts/handlers.cljs b/src/status_im/accounts/handlers.cljs index cbace9d4d4..f31a647c4b 100644 --- a/src/status_im/accounts/handlers.cljs +++ b/src/status_im/accounts/handlers.cljs @@ -21,14 +21,15 @@ [status-im.navigation.handlers :as nav])) -(defn save-account [_ [_ account]] - (accounts-store/save account true)) +(defn save-account [{:keys [network]} [_ account]] + (accounts-store/save (assoc account :network network) true)) (register-handler :add-account ((after save-account) - (fn [db [_ {:keys [address] :as account}]] - (update db :accounts assoc address account)))) + (fn [{:keys [network] :as db} [_ {:keys [address] :as account}]] + (let [account' (assoc account :network network)] + (update db :accounts assoc address account'))))) (defn account-created [result password] (let [data (json->clj result) diff --git a/src/status_im/data_store/realm/schemas/account/v4/chat.cljs b/src/status_im/data_store/realm/schemas/account/v4/chat.cljs index 1ce8a098a8..41e1458d68 100644 --- a/src/status_im/data_store/realm/schemas/account/v4/chat.cljs +++ b/src/status_im/data_store/realm/schemas/account/v4/chat.cljs @@ -34,5 +34,5 @@ (defn migration [_ new-realm] (let [new-objs (.objects new-realm "chat")] - (dotimes [i (range (.-length new-objs))] + (dotimes [i (.-length new-objs)] (aset (aget new-objs i) "pending-contact?" false)))) diff --git a/src/status_im/data_store/realm/schemas/base/core.cljs b/src/status_im/data_store/realm/schemas/base/core.cljs index 5a5b22934e..b96a0788e9 100644 --- a/src/status_im/data_store/realm/schemas/base/core.cljs +++ b/src/status_im/data_store/realm/schemas/base/core.cljs @@ -1,7 +1,11 @@ (ns status-im.data-store.realm.schemas.base.core - (:require [status-im.data-store.realm.schemas.base.v1.core :as v1])) + (:require [status-im.data-store.realm.schemas.base.v1.core :as v1] + [status-im.data-store.realm.schemas.base.v2.core :as v2])) ; put schemas ordered by version (def schemas [{:schema v1/schema :schemaVersion 1 - :migration v1/migration}]) \ No newline at end of file + :migration v1/migration} + {:schema v2/schema + :schemaVersion 2 + :migration v2/migration}]) diff --git a/src/status_im/data_store/realm/schemas/base/v2/account.cljs b/src/status_im/data_store/realm/schemas/base/v2/account.cljs new file mode 100644 index 0000000000..1f677b85ee --- /dev/null +++ b/src/status_im/data_store/realm/schemas/base/v2/account.cljs @@ -0,0 +1,26 @@ +(ns status-im.data-store.realm.schemas.base.v2.account + (:require [taoensso.timbre :as log])) + +(def schema {:name :account + :primaryKey :address + :properties {:address :string + :public-key :string + :updates-public-key {:type :string + :optional true} + :updates-private-key {:type :string + :optional true} + :name {:type :string :optional true} + :phone {:type :string :optional true} + :email {:type :string :optional true} + :status {:type :string :optional true} + :photo-path :string + :last-updated {:type :int :default 0} + :signed-up? {:type :bool + :default false} + :network {:type :string}}}) + +(defn migration [old-realm new-realm] + (log/debug "migrating account schema v2") + (let [new-objs (.objects new-realm "account")] + (dotimes [i (.-length new-objs)] + (aset (aget new-objs i) "network" "testnet")))) diff --git a/src/status_im/data_store/realm/schemas/base/v2/core.cljs b/src/status_im/data_store/realm/schemas/base/v2/core.cljs new file mode 100644 index 0000000000..d0362f2cf5 --- /dev/null +++ b/src/status_im/data_store/realm/schemas/base/v2/core.cljs @@ -0,0 +1,11 @@ +(ns status-im.data-store.realm.schemas.base.v2.core + (:require [status-im.data-store.realm.schemas.base.v2.account :as account] + [status-im.data-store.realm.schemas.base.v1.kv-store :as kv-store] + [taoensso.timbre :as log])) + +(def schema [account/schema + kv-store/schema]) + +(defn migration [old-realm new-realm] + (log/debug "migrating v2 base database: " old-realm new-realm) + (account/migration old-realm new-realm)) diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index 5afb55d51f..da08a8111b 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -23,7 +23,8 @@ status-im.transactions.handlers status-im.network.handlers [status-im.utils.types :as t] - [status-im.constants :refer [console-chat-id]])) + [status-im.constants :refer [console-chat-id]] + [status-im.utils.ethereum-network :as enet])) ;; -- Common -------------------------------------------------------------- @@ -42,12 +43,15 @@ (assoc-in db [:animations k] v))) (register-handler :initialize-db - (fn [{:keys [status-module-initialized?]} _] + (fn [{:keys [status-module-initialized? network]} _] (data-store/init) - (let [db' (assoc app-db :current-account-id nil)] - (if status-module-initialized? - (assoc db' :status-module-initialized? true) - db')))) + (cond-> (assoc app-db :current-account-id nil) + + status-module-initialized? + (assoc :status-module-initialized? true) + + true + (assoc :network network)))) (register-handler :initialize-account-db (fn [db _] @@ -97,7 +101,8 @@ (dispatch [:crypt-initialized])))))))) (defn node-started [db result] - (log/debug "Started Node: ")) + (log/debug "Started Node") + (enet/get-network #(dispatch [:set :network %]))) (register-handler :initialize-geth (u/side-effect! diff --git a/src/status_im/protocol/handlers.cljs b/src/status_im/protocol/handlers.cljs index b950cd7039..5432cee89d 100644 --- a/src/status_im/protocol/handlers.cljs +++ b/src/status_im/protocol/handlers.cljs @@ -11,7 +11,8 @@ blocks-per-hour]] [status-im.i18n :refer [label]] [status-im.utils.random :as random] - [taoensso.timbre :as log :refer-macros [debug]])) + [taoensso.timbre :as log :refer-macros [debug]] + [status-im.constants :as c])) (register-handler :initialize-protocol (fn [db [_ current-account-id]] @@ -20,7 +21,7 @@ (get-in db [:accounts current-account-id])] (let [groups (chats/get-active-group-chats) w3 (protocol/init-whisper! - {:rpc-url "http://localhost:8545" + {:rpc-url c/ethereum-rpc-url :identity public-key :groups groups :callback #(dispatch [:incoming-message %1 %2]) diff --git a/src/status_im/utils/ethereum_network.cljs b/src/status_im/utils/ethereum_network.cljs new file mode 100644 index 0000000000..2fb3ca707c --- /dev/null +++ b/src/status_im/utils/ethereum_network.cljs @@ -0,0 +1,23 @@ +(ns status-im.utils.ethereum-network + (:require [status-im.constants :as c])) + +(def Web3 (js/require "web3")) + +(defn web3 [] + (->> (Web3.providers.HttpProvider. c/ethereum-rpc-url) + (Web3.))) + +(def networks + {"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" :mainnet + "0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303" :testnet + ;; Ropsten + "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d" :testnet}) + +(defn- on-block [callback] + (fn [error block] + (when-not error + (let [hash (.-hash block)] + (callback (networks hash :unknown)))))) + +(defn get-network [callback] + (.eth.getBlock (web3) 0 false (on-block callback)))