From 232259c4ba0df47d7c047dd7dad2260ee1948aa1 Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Thu, 30 Jun 2016 13:37:36 +0300 Subject: [PATCH] changed add account flow to use new binding system Former-commit-id: ec9cfe8f8c32d73f04a3cb37a788b8f156f6b212 --- android/app/build.gradle | 4 +- android/app/src/main/AndroidManifest.xml | 3 +- .../com/statusim/geth/module/GethModule.java | 24 +++-- .../statusim/geth/service/GethConnector.java | 11 ++- .../statusim/geth/service/GethMessages.java | 4 +- .../statusim/geth/service/GethService.java | 88 ++++++++++++------- .../geth/service/ServiceConnector.java | 14 +-- src/status_im/android/core.cljs | 4 +- src/status_im/chat/handlers.cljs | 8 ++ src/status_im/handlers.cljs | 6 ++ src/status_im/protocol/handlers.cljs | 4 +- 11 files changed, 113 insertions(+), 57 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 301a07656c..c60d4eb2d1 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -132,8 +132,8 @@ dependencies { compile project(':ReactNativeAndroidSmsListener') compile project(':react-native-camera') compile project(':react-native-orientation') -// compile(name:'geth', ext:'aar') - compile(group: 'status-im', name: 'status-go', version: '0.1.0-test-callback', ext: 'aar') + compile(name:'statusgo-android-16', ext:'aar') + //compile(group: 'status-im', name: 'status-go', version: '0.1.0-test-callback', ext: 'aar') compile fileTree(dir: "node_modules/realm/android/libs", include: ["*.jar"]) } diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index a7b7a31725..05008651d2 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -25,8 +25,7 @@ + android:exported="true"/> diff --git a/android/app/src/main/java/com/statusim/geth/module/GethModule.java b/android/app/src/main/java/com/statusim/geth/module/GethModule.java index a4bb3e12d0..058ab03667 100644 --- a/android/app/src/main/java/com/statusim/geth/module/GethModule.java +++ b/android/app/src/main/java/com/statusim/geth/module/GethModule.java @@ -8,12 +8,15 @@ import com.statusim.geth.service.ConnectorHandler; import com.statusim.geth.service.GethConnector; import com.statusim.geth.service.GethMessages; import com.statusim.geth.service.GethService; +import android.util.Log; import java.util.HashMap; import java.util.UUID; public class GethModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ConnectorHandler { + private static final String TAG = "GethModule"; + protected GethConnector geth = null; protected String handlerIdentifier = createIdentifier(); @@ -81,15 +84,20 @@ public class GethModule extends ReactContextBaseJavaModule implements LifecycleE @Override public boolean handleMessage(Message message) { + Log.d(TAG, "Received message: " + message.toString()); boolean isClaimed = true; Bundle data = message.getData(); String callbackIdentifier = data.getString(GethConnector.CALLBACK_IDENTIFIER); + Log.d(TAG, "callback identifier: " + callbackIdentifier); Callback callback = null; switch (message.what) { case GethMessages.MSG_NODE_STARTED: + Log.d(TAG, "handle startNodeCallbacks size: " + startNodeCallbacks.size()); callback = startNodeCallbacks.remove(callbackIdentifier); if (callback != null) { - callback.invoke(null); + callback.invoke(true); + } else { + Log.d(TAG, "Could not find callback: " + callbackIdentifier); } break; case GethMessages.MSG_NODE_STOPPED: @@ -97,7 +105,7 @@ public class GethModule extends ReactContextBaseJavaModule implements LifecycleE case GethMessages.MSG_ACCOUNT_CREATED: callback = createAccountCallbacks.remove(callbackIdentifier); if (callback != null) { - callback.invoke(null, "{ \"address\": \"" + data.getString("address") + "\"}"); + callback.invoke(data.getString("data")); } break; case GethMessages.MSG_ACCOUNT_ADDED: @@ -106,7 +114,7 @@ public class GethModule extends ReactContextBaseJavaModule implements LifecycleE callback.invoke(null, "{ \"address\": \"" + data.getString("address") + "\"}"); } break; - case GethMessages.MSG_ACCOUNT_UNLOCKED: + case GethMessages.MSG_LOGGED_IN: callback = unlockAccountCallbacks.remove(callbackIdentifier); if (callback != null) { callback.invoke(null, "{ \"result\": \"" + data.getString("result") + "\"}"); @@ -135,13 +143,15 @@ public class GethModule extends ReactContextBaseJavaModule implements LifecycleE } String callbackIdentifier = createIdentifier(); + Log.d(TAG, "Created callback identifier: " + callbackIdentifier); startNodeCallbacks.put(callbackIdentifier, callback); + Log.d(TAG, "startNodeCallbacks size: " + startNodeCallbacks.size()); geth.startNode(callbackIdentifier); } @ReactMethod - public void unlockAccount(String address, String password, Callback callback) { + public void login(String address, String password, Callback callback) { Activity currentActivity = getCurrentActivity(); @@ -158,11 +168,11 @@ public class GethModule extends ReactContextBaseJavaModule implements LifecycleE String callbackIdentifier = createIdentifier(); unlockAccountCallbacks.put(callbackIdentifier, callback); - geth.unlockAccount(callbackIdentifier, address, password); + geth.login(callbackIdentifier, address, password); } @ReactMethod - public void createAccount(Callback callback) { + public void createAccount(String password, Callback callback) { Activity currentActivity = getCurrentActivity(); @@ -179,7 +189,7 @@ public class GethModule extends ReactContextBaseJavaModule implements LifecycleE String callbackIdentifier = createIdentifier(); createAccountCallbacks.put(callbackIdentifier, callback); - geth.createAccount(callbackIdentifier); + geth.createAccount(callbackIdentifier, password); } @ReactMethod diff --git a/android/app/src/main/java/com/statusim/geth/service/GethConnector.java b/android/app/src/main/java/com/statusim/geth/service/GethConnector.java index 73a1fd74d4..7083dff2d5 100644 --- a/android/app/src/main/java/com/statusim/geth/service/GethConnector.java +++ b/android/app/src/main/java/com/statusim/geth/service/GethConnector.java @@ -42,13 +42,13 @@ public class GethConnector extends ServiceConnector { } } - public void unlockAccount(String callbackIdentifier, String address, String password) { + public void login(String callbackIdentifier, String address, String password) { if (checkBound()) { Bundle data = new Bundle(); data.putString("address", address); data.putString("password", password); - Message msg = createMessage(callbackIdentifier, GethMessages.MSG_UNLOCK_ACCOUNT, data); + Message msg = createMessage(callbackIdentifier, GethMessages.MSG_LOGIN, data); try { serviceMessenger.send(msg); } catch (RemoteException e) { @@ -57,10 +57,12 @@ public class GethConnector extends ServiceConnector { } } - public void createAccount(String callbackIdentifier) { + public void createAccount(String callbackIdentifier, String password) { if (checkBound()) { - Message msg = createMessage(callbackIdentifier, GethMessages.MSG_CREATE_ACCOUNT, null); + Bundle data = new Bundle(); + data.putString("password", password); + Message msg = createMessage(callbackIdentifier, GethMessages.MSG_CREATE_ACCOUNT, data); try { serviceMessenger.send(msg); } catch (RemoteException e) { @@ -95,6 +97,7 @@ public class GethConnector extends ServiceConnector { protected Message createMessage(String callbackIdentifier, int idMessage, Bundle data) { + Log.d(TAG, "Client messenger: " + clientMessenger.toString()); Message msg = Message.obtain(null, idMessage, 0, 0); msg.replyTo = clientMessenger; if (data == null) { diff --git a/android/app/src/main/java/com/statusim/geth/service/GethMessages.java b/android/app/src/main/java/com/statusim/geth/service/GethMessages.java index 080725bca2..f6c3e24737 100644 --- a/android/app/src/main/java/com/statusim/geth/service/GethMessages.java +++ b/android/app/src/main/java/com/statusim/geth/service/GethMessages.java @@ -26,12 +26,12 @@ public class GethMessages { /** * Unlock an account */ - public static final int MSG_UNLOCK_ACCOUNT = 5; + public static final int MSG_LOGIN = 5; /** * Account unlocked event */ - public static final int MSG_ACCOUNT_UNLOCKED = 6; + public static final int MSG_LOGGED_IN = 6; /** * Create an account 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 638c4c3657..8a62fcc55f 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 @@ -20,6 +20,8 @@ public class GethService extends Service { private static boolean isGethInitialized = false; private final Handler handler = new Handler(); + private static String dataFolder; + static class IncomingHandler extends Handler { private final WeakReference service; @@ -80,6 +82,7 @@ public class GethService extends Service { switch (message.what) { case GethMessages.MSG_START_NODE: + Log.d(TAG, "Received start node message." + message.toString()); startNode(message); break; @@ -95,8 +98,8 @@ public class GethService extends Service { addAccount(message); break; - case GethMessages.MSG_UNLOCK_ACCOUNT: - unlockAccount(message); + case GethMessages.MSG_LOGIN: + login(message); break; default: @@ -109,16 +112,22 @@ public class GethService extends Service { protected void startNode(Message message) { if (!isGethInitialized) { isGethInitialized = true; - new StartTask(message).execute(); + Log.d(TAG, "Client messenger1: " + message.replyTo.toString()); + Bundle data = message.getData(); + String callbackIdentifier = data.getString(GethConnector.CALLBACK_IDENTIFIER); + Log.d(TAG, "Callback identifier: " + callbackIdentifier); + new StartTask(message.replyTo, callbackIdentifier).execute(); } } protected class StartTask extends AsyncTask { - protected Message message; + protected String callbackIdentifier; + protected Messenger messenger; - public StartTask(Message message) { - this.message = message; + public StartTask(Messenger messenger, String callbackIdentifier) { + this.messenger = messenger; + this.callbackIdentifier = callbackIdentifier; } protected Void doInBackground(Void... args) { @@ -127,30 +136,41 @@ public class GethService extends Service { } protected void onPostExecute(Void results) { - onGethStarted(message); + onGethStarted(messenger, callbackIdentifier); } } - protected void onGethStarted(Message message) { + protected void onGethStarted(Messenger messenger, String callbackIdentifier) { Log.d(TAG, "Geth Service started"); isGethStarted = true; - - sendReply(message, GethMessages.MSG_NODE_STARTED, null); + Message replyMessage = Message.obtain(null, GethMessages.MSG_NODE_STARTED, 0, 0, null); + Bundle replyData = new Bundle(); + Log.d(TAG, "Callback identifier: " + callbackIdentifier); + replyData.putString(GethConnector.CALLBACK_IDENTIFIER, callbackIdentifier); + replyMessage.setData(replyData); + sendReply(messenger, replyMessage); } protected void startGeth() { - Log.d(TAG, "Starting background Geth Service"); + File extStore = Environment.getExternalStorageDirectory(); - final String dataFolder = extStore.exists() ? - extStore.getAbsolutePath() : - getApplicationInfo().dataDir; + dataFolder = extStore.exists() ? + extStore.getAbsolutePath() + "/ethereum" : + getApplicationInfo().dataDir + "/ethereum"; + Log.d(TAG, "Starting background Geth Service in folder: " + dataFolder); + try { + final File newFile = new File(dataFolder); + newFile.mkdir(); + } catch (Exception e) { + Log.e(TAG, "error making folder: " + dataFolder, e); + } new Thread(new Runnable() { public void run() { - Statusgo.doStartNode(dataFolder); + Statusgo.StartNode(dataFolder); } }).start(); } @@ -158,19 +178,20 @@ public class GethService extends Service { protected void stopNode(Message message) { // TODO: stop node - sendReply(message, GethMessages.MSG_NODE_STOPPED, null); + createAndSendReply(message, GethMessages.MSG_NODE_STOPPED, null); } protected void createAccount(Message message) { Bundle data = message.getData(); String password = data.getString("password"); // TODO: remove second argument - String address = Statusgo.doCreateAccount(password, ""); - Log.d(TAG, "Created account: " + address); + Log.d(TAG, "Creating account: " + password + " - " + dataFolder); + String jsonData = Statusgo.CreateAccount(password, dataFolder); + Log.d(TAG, "Created account: " + jsonData); Bundle replyData = new Bundle(); - replyData.putString("address", address); - sendReply(message, GethMessages.MSG_ACCOUNT_CREATED, replyData); + replyData.putString("data", jsonData); + createAndSendReply(message, GethMessages.MSG_ACCOUNT_CREATED, replyData); } protected void addAccount(Message message) { @@ -184,27 +205,27 @@ public class GethService extends Service { Bundle replyData = new Bundle(); replyData.putString("address", address); - sendReply(message, GethMessages.MSG_ACCOUNT_ADDED, replyData); + createAndSendReply(message, GethMessages.MSG_ACCOUNT_ADDED, replyData); } - protected void unlockAccount(Message message) { + protected void login(Message message) { Bundle data = message.getData(); String address = data.getString("address"); String password = data.getString("password"); // TODO: remove third argument - String result = Statusgo.doUnlockAccount(address, password, 0); + String result = Statusgo.Login(address, password); Log.d(TAG, "Unlocked account: " + result); Bundle replyData = new Bundle(); replyData.putString("result", result); - sendReply(message, GethMessages.MSG_ACCOUNT_UNLOCKED, replyData); + createAndSendReply(message, GethMessages.MSG_LOGGED_IN, replyData); } public static boolean isRunning() { return isGethInitialized; } - protected void sendReply(Message message, int replyIdMessage, Bundle replyData) { + protected void createAndSendReply(Message message, int replyIdMessage, Bundle replyData) { if (message == null) { return; @@ -214,14 +235,21 @@ public class GethService extends Service { replyData = new Bundle(); } Bundle data = message.getData(); - String callbackIdentifier = data.getString("callbackIdentifier"); - replyData.putString("callbackIdentifier", callbackIdentifier); + String callbackIdentifier = data.getString(GethConnector.CALLBACK_IDENTIFIER); + Log.d(TAG, "Callback identifier: " + callbackIdentifier); + replyData.putString(GethConnector.CALLBACK_IDENTIFIER, callbackIdentifier); replyMessage.setData(replyData); + sendReply(message.replyTo, replyMessage); + } + + protected void sendReply(Messenger messenger, Message message) { try { - message.replyTo.send(replyMessage); - } catch (RemoteException e) { - Log.e(TAG, "Exception sending message id: " + replyIdMessage, e); + messenger.send(message); + + } catch (Exception e) { + + Log.e(TAG, "Exception sending message id: " + message.what, e); } } } diff --git a/android/app/src/main/java/com/statusim/geth/service/ServiceConnector.java b/android/app/src/main/java/com/statusim/geth/service/ServiceConnector.java index 1c70e92992..f20e1fcf66 100644 --- a/android/app/src/main/java/com/statusim/geth/service/ServiceConnector.java +++ b/android/app/src/main/java/com/statusim/geth/service/ServiceConnector.java @@ -52,17 +52,17 @@ public class ServiceConnector { public void handleMessage(Message message) { boolean isClaimed = false; - if (message.obj != null) { - String identifier = ((Bundle) message.obj).getString("identifier"); - if (identifier != null) { + //if (message.obj != null) { + // String identifier = ((Bundle) message.obj).getString("identifier"); + //if (identifier != null) { for (ConnectorHandler handler : handlers) { - if (identifier.equals(handler.getID())) { + // if (identifier.equals(handler.getID())) { isClaimed = handler.handleMessage(message); - } + // } } - } - } + // } + //} if (!isClaimed) { super.handleMessage(message); } diff --git a/src/status_im/android/core.cljs b/src/status_im/android/core.cljs index 38b60a5b77..db4c13c2e0 100644 --- a/src/status_im/android/core.cljs +++ b/src/status_im/android/core.cljs @@ -78,8 +78,10 @@ (defn init [] (dispatch-sync [:initialize-db]) (dispatch [:initialize-crypt]) + (dispatch [:initialize-geth]) (dispatch [:initialize-chats]) - (dispatch [:initialize-protocol]) + ;protocol must be initialized after user enters password and we create account + ;(dispatch [:initialize-protocol]) (dispatch [:load-user-phone-number]) (dispatch [:load-contacts]) ;; load commands from remote server (todo: uncomment) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index ef8ae2d24f..1f05a181bd 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -19,6 +19,9 @@ [status-im.handlers.content-suggestions :refer [get-content-suggestions]] [status-im.utils.phone-number :refer [format-phone-number]] [status-im.utils.datetime :as time] + [status-im.components.react :refer [geth]] + [status-im.utils.logging :as log] + [status-im.utils.types :refer [json->clj]] [status-im.chat.handlers.animation :refer [update-response-height get-response-height]])) @@ -259,6 +262,11 @@ (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]))))) (sign-up-service/save-password password) (assoc db :password-saved true))) diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index ebf835a9c3..fdfc7f64ff 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -10,6 +10,7 @@ [status-im.utils.logging :as log] [status-im.utils.crypt :refer [gen-random-bytes]] [status-im.utils.handlers :as u] + [status-im.components.react :refer [geth]] status-im.chat.handlers status-im.group-settings.handlers status-im.navigation.handlers @@ -74,6 +75,11 @@ (.toBits (.. js/ecc -sjcl -codec -hex)) (.addEntropy (.. js/ecc -sjcl -random))) (dispatch [:crypt-initialized])))))))) +(register-handler :initialize-geth + (u/side-effect! + (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/protocol/handlers.cljs b/src/status_im/protocol/handlers.cljs index a9b94e5541..14690d6f47 100644 --- a/src/status_im/protocol/handlers.cljs +++ b/src/status_im/protocol/handlers.cljs @@ -17,8 +17,8 @@ (register-handler :initialize-protocol (u/side-effect! - (fn [db [_]] - (init-protocol (make-handler db))))) + (fn [db [_ public-key]] + (init-protocol public-key (make-handler db))))) (register-handler :protocol-initialized (fn [db [_ identity]]