changed add account flow to use new binding system

Former-commit-id: ec9cfe8f8c
This commit is contained in:
Adrian Tiberius 2016-06-30 13:37:36 +03:00
parent 5a1c2c9bb3
commit 232259c4ba
11 changed files with 113 additions and 57 deletions

View File

@ -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"])
}

View File

@ -25,8 +25,7 @@
<service
android:name=".geth.service.GethService"
android:enabled="true"
android:exported="true"
android:process=":geth_process"/>
android:exported="true"/>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>

View File

@ -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

View File

@ -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) {

View File

@ -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

View File

@ -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<GethService> 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<Void, Void, Void> {
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);
}
}
}

View File

@ -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);
}

View File

@ -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)

View File

@ -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)))

View File

@ -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!

View File

@ -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]]