From c878fba6f13f458d4918ea02cab19e03f9d0b3fe Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Thu, 30 Jun 2016 18:06:05 +0300 Subject: [PATCH] created new accounts realm object and fixed new create account flow --- .../statusim/geth/service/GethService.java | 2 +- src/status_im/accounts/handlers.cljs | 32 +++++++++++++++++++ src/status_im/chat/handlers.cljs | 6 +--- src/status_im/db.cljs | 1 + src/status_im/handlers.cljs | 7 ++-- src/status_im/models/accounts.cljs | 22 +++++++++++++ src/status_im/persistence/realm.cljs | 4 +++ 7 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 src/status_im/accounts/handlers.cljs create mode 100644 src/status_im/models/accounts.cljs diff --git a/android/app/src/main/java/com/statusim/geth/service/GethService.java b/android/app/src/main/java/com/statusim/geth/service/GethService.java index 8a62fcc55f..c0af872fe6 100644 --- a/android/app/src/main/java/com/statusim/geth/service/GethService.java +++ b/android/app/src/main/java/com/statusim/geth/service/GethService.java @@ -186,7 +186,7 @@ public class GethService extends Service { String password = data.getString("password"); // TODO: remove second argument Log.d(TAG, "Creating account: " + password + " - " + dataFolder); - String jsonData = Statusgo.CreateAccount(password, dataFolder); + String jsonData = Statusgo.CreateAccount(password, dataFolder + "/keystore"); Log.d(TAG, "Created account: " + jsonData); Bundle replyData = new Bundle(); diff --git a/src/status_im/accounts/handlers.cljs b/src/status_im/accounts/handlers.cljs new file mode 100644 index 0000000000..e70b48e1d9 --- /dev/null +++ b/src/status_im/accounts/handlers.cljs @@ -0,0 +1,32 @@ +(ns status-im.accounts.handlers + (:require [status-im.models.accounts :as accounts] + [re-frame.core :refer [register-handler after dispatch debug]] + [status-im.utils.logging :as log] + [status-im.components.react :refer [geth]] + [status-im.utils.types :refer [json->clj]] + [clojure.string :as str])) + + +(defn save-account [_ [_ account]] + (accounts/save-accounts [account])) + +(register-handler :add-account + (-> (fn [db [_ {:keys [address] :as account}]] + (update db :accounts assoc address account)) + ((after save-account)))) + +(defn account-created [result] + (let [data (json->clj result) + public-key (:pubkey data) + address (:address data)] + (log/debug "Created account: " result) + (when (not (str/blank? public-key)) + (do + (dispatch [:initialize-protocol public-key]) + (dispatch [:add-account {:address address + :public-key public-key}]))))) + +(register-handler :create-account + (-> (fn [db [_ password]] + (.createAccount geth password (fn [result] (account-created result))) + db))) \ No newline at end of file diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 1f05a181bd..803e09095e 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -262,11 +262,7 @@ (register-handler :save-password (fn [db [_ password]] - (.createAccount geth password (fn [result] - (let [data (json->clj result) - public-key (:pubkey data)] - (log/debug "Created account: " result) - (when (not (str/blank? public-key)) (dispatch [:initialize-protocol public-key]))))) + (dispatch [:create-account password]) (sign-up-service/save-password password) (assoc db :password-saved true))) diff --git a/src/status_im/db.cljs b/src/status_im/db.cljs index 7c07c77636..963e27f342 100644 --- a/src/status_im/db.cljs +++ b/src/status_im/db.cljs @@ -11,6 +11,7 @@ ;; initial state of app-db (def app-db {:identity-password "replace-me-with-user-entered-password" :identity "me" + :accounts {} :contacts [] :contacts-ids #{} :selected-contacts #{} diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index fdfc7f64ff..f98862ff93 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -19,6 +19,7 @@ status-im.new-group.handlers status-im.participants.handlers status-im.qr-scanner.handlers + status-im.accounts.handlers status-im.protocol.handlers)) ;; -- Middleware ------------------------------------------------------------ @@ -77,9 +78,9 @@ (dispatch [:crypt-initialized])))))))) (register-handler :initialize-geth (u/side-effect! - (fn [_ _] - (log/debug "Starting node") - (.startNode geth (fn [result] (log/debug "Started Node: " result)))))) + (fn [_ _] + (log/debug "Starting node") + (.startNode geth (fn [result] (log/debug "Started Node: " result)))))) (register-handler :crypt-initialized (u/side-effect! diff --git a/src/status_im/models/accounts.cljs b/src/status_im/models/accounts.cljs new file mode 100644 index 0000000000..a9f333c3c7 --- /dev/null +++ b/src/status_im/models/accounts.cljs @@ -0,0 +1,22 @@ +(ns status-im.models.accounts + (:require [status-im.persistence.realm :as r])) + +(defn get-accounts [] + (-> (r/get-all :accounts) + r/collection->map)) + +(defn create-account [{:keys [address public-key] :as account}] + (->> account + (r/create :accounts))) + +(defn save-accounts [accounts] + (r/write #(mapv create-account accounts))) + + +;;;;;;;;;;;;;;;;;;;;---------------------------------------------- + +(defn accounts-list [] + (r/get-all :accounts)) + +(defn account-by-address [address] + (r/single-cljs (r/get-by-field :accounts :address address))) diff --git a/src/status_im/persistence/realm.cljs b/src/status_im/persistence/realm.cljs index 5d304828fe..941cd0f1b0 100644 --- a/src/status_im/persistence/realm.cljs +++ b/src/status_im/persistence/realm.cljs @@ -15,6 +15,10 @@ :optional true} :photo-path {:type "string" :optinal true}}} + {:name :accounts + :primaryKey :address + :properties {:address "string" + :public-key "string"}} {:name :kv-store :primaryKey :key :properties {:key "string"