-
Notifications
You must be signed in to change notification settings - Fork 3
/
RNEthDaemonModule.java
96 lines (72 loc) · 2.76 KB
/
RNEthDaemonModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.reactlibrary;
import android.util.Log;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import org.ethereum.geth.Geth;
import org.ethereum.geth.Node;
import org.ethereum.geth.NodeConfig;
import org.json.JSONException;
import java.io.File;
/**
* Created by Estarrona on 22/12/17.
*/
public class RNEthDaemonModule extends ReactContextBaseJavaModule {
private Node node;
final String TAG = "GethNode";
public RNEthDaemonModule (ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "RNEthDaemon";
}
@ReactMethod
public void start(ReadableMap jsonConfig, Promise promise) throws JSONException {
Log.d("GethNode", "Starting Geth process" );
NodeConfig nodeConfig = Geth.newNodeConfig();
Log.d("GethNode", "New node" );
nodeConfig.setEthereumEnabled(jsonConfig.getBoolean("enabledEthereum"));
nodeConfig.setEthereumDatabaseCache(jsonConfig.getInt("enodesNumber"));
nodeConfig.setEthereumNetworkID(jsonConfig.getInt("networkID"));
nodeConfig.setMaxPeers(jsonConfig.getInt("maxPeers"));
nodeConfig.setWhisperEnabled(jsonConfig.getBoolean("enabledWhisper"));
String genesis = Geth.testnetGenesis();
nodeConfig.setEthereumGenesis(genesis);
Log.d("GethNode", "Configured" );
final String root = getReactApplicationContext().getBaseContext().getApplicationInfo().dataDir;
final String dataFolder = root + "/.ethereum";
Log.d("GethNode", "Starting Geth node in folder: " + dataFolder);
Log.d("GethNode", "Have the root/folder" );
try {
final File newFile = new File(dataFolder);
newFile.mkdir();
} catch (Exception e) {
Log.e(TAG, "error making folder: " + dataFolder, e);
}
final String networkDir = dataFolder + "/$TMPDIR";
node = new Node(networkDir, nodeConfig);
Log.d(TAG, "Node config " + nodeConfig);
try {
node.start();
promise.resolve(null);
} catch (Exception e) {
e.printStackTrace();
promise.reject("node_error", e.getLocalizedMessage());
}
Log.d(TAG, "Geth node started");
}
@ReactMethod
public void stop(Promise promise) {
try {
node.stop();
promise.resolve(null);
} catch (Exception e) {
e.printStackTrace();
promise.reject("node_error", e.getLocalizedMessage());
}
Log.d(TAG, "Geth node stoped");
}
}