sendWeb3Request method in StatusModule

This commit is contained in:
Roman Volosovskyi 2017-04-18 17:22:05 +03:00 committed by Roman Volosovskyi
parent 441ca66e96
commit a9d1426426
12 changed files with 129 additions and 13 deletions

View File

@ -32,6 +32,7 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL
private ServiceConnector status = null;
private ExecutorService executor = null;
private boolean debug;
private Web3Bridge w3Bridge = new Web3Bridge();
StatusModule(ReactApplicationContext reactContext, boolean debug) {
super(reactContext);
@ -186,6 +187,7 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL
return;
}
this.w3Bridge = new Web3Bridge();
Thread thread = new Thread() {
@Override
public void run() {
@ -481,4 +483,16 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL
public void onConnectorDisconnected() {
}
@ReactMethod
public void sendWeb3Request(final String host, final String payload, final Callback callback) {
Thread thread = new Thread() {
@Override
public void run() {
w3Bridge.sendRequest(host, payload, callback);
}
};
thread.start();
}
}

View File

@ -0,0 +1,36 @@
package im.status.ethereum.module;
import android.os.Build;
import com.facebook.react.bridge.Callback;
import okhttp3.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class Web3Bridge {
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private OkHttpClient client;
public Web3Bridge() {
OkHttpClient.Builder b = new OkHttpClient.Builder();
b.readTimeout(310, TimeUnit.SECONDS);
client = b.build();
}
public void sendRequest(final String host, final String json, final Callback callback) {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(host)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
String rpcResponse = response.body().string().trim();
callback.invoke(rpcResponse);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -312,6 +312,39 @@ RCT_EXPORT_METHOD(clearStorageAPIs) {
}
}
RCT_EXPORT_METHOD(sendWeb3Request:(NSString *)host
password:(NSString *)payload
callback:(RCTResponseSenderBlock)callback) {
NSData *postData = [payload dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:host]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[request setTimeoutInterval:310];
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *strResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *trimmedResponse = [strResponse stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
callback(@[trimmedResponse]);
}];
[postDataTask resume];
}
+ (void)signalEvent:(const char *) signal
{
if(!signal){

View File

@ -74,7 +74,7 @@
"react-native-tcp": "^3.2.1",
"react-native-udp": "^2.0.0",
"react-native-vector-icons": "^4.0.1",
"react-native-webview-bridge": "github:status-im/react-native-webview-bridge#react-native-0.40",
"react-native-webview-bridge": "github:status-im/react-native-webview-bridge#0.33.12",
"readable-stream": "^1.0.33",
"realm": "^0.14.3",
"stream-browserify": "^1.0.0",

View File

@ -111,11 +111,8 @@ var protocol = window.location.protocol
var address = providerAddress || "http://localhost:8545";
console.log(protocol);
if (typeof web3 === "undefined") {
if (protocol == "https:") {
if (protocol == "https:" || protocol == "http:") {
console.log("StatusHttpProvider");
web3 = new Web3(new StatusHttpProvider(address));
} else if (protocol == "http:") {
console.log("HttpProvider");
web3 = new Web3(new Web3.providers.HttpProvider(address));
}
}

View File

@ -4,6 +4,7 @@
[status-im.handlers]
[status-im.subs]
[status-im.components.react :refer [app-registry
app-state
keyboard
orientation
back-android
@ -75,6 +76,9 @@
current-view
:chat))
(defn app-state-change-handler [state]
(dispatch [:app-state-change state]))
(defn app-root []
(let [signed-up? (subscribe [:signed-up?])
view-id (subscribe [:get :view-id])
@ -102,10 +106,12 @@
"keyboardDidHide"
#(when-not (= 0 @keyboard-height)
(dispatch [:set :keyboard-height 0])))
(.hide splash-screen))
(.hide splash-screen)
(.addEventListener app-state "change" app-state-change-handler))
:component-will-unmount
(fn []
(.stop http-bridge))
(.stop http-bridge)
(.removeEventListener app-state "change" app-state-change-handler))
:render
(fn []
(when @view-id

View File

@ -19,6 +19,7 @@
;; React Components
(def app-registry (get-react-property "AppRegistry"))
(def app-state (get-react-property "AppState"))
(def navigator (get-class "Navigator"))
(def view (get-class "View"))

View File

@ -158,3 +158,7 @@
(def adjust-resize 16)
(def adjust-pan 32)
(defn call-web3 [host payload callback]
(when status
(call-module #(.sendWeb3Request status host payload callback))))

View File

@ -125,9 +125,9 @@
(case type
"transaction.queued" (dispatch [:transaction-queued event])
"transaction.failed" (dispatch [:transaction-failed event])
"node.started" (dispatch [:status-node-started!])
"node.started" (dispatch [:status-node-started!])
"module.initialized" (dispatch [:status-module-initialized!])
"local_storage.set" (dispatch [:set-local-storage event])
"local_storage.set" (dispatch [:set-local-storage event])
(log/debug "Event " type " not handled"))))))
(register-handler :status-module-initialized!
@ -146,6 +146,16 @@
(fn [_ _]
(log/debug "crypt initialized"))))
(register-handler :app-state-change
(u/side-effect!
(fn [{:keys [webview-bridge] :as db} [_ state]]
(case state
"background" (status/stop-rpc-server)
"active" (do (status/start-rpc-server)
(when webview-bridge
(.resetOkHttpClient webview-bridge)))
nil))))
;; -- User data --------------------------------------------------------------
(register-handler :load-user-phone-number
(fn [db [_]]

View File

@ -1,6 +1,7 @@
(ns status-im.protocol.web3.utils
(:require [cljs-time.core :refer [now]]
[cljs-time.coerce :refer [to-long]]))
[cljs-time.coerce :refer [to-long]]
[status-im.utils.web-provider :as w3]))
(def web3 (js/require "web3"))
@ -18,7 +19,7 @@
(defn make-web3 [rpc-url]
(->> rpc-url
web3.providers.HttpProvider.
w3/get-provider
web3.))
(defn timestamp []

View File

@ -1,10 +1,11 @@
(ns status-im.utils.ethereum-network
(:require [status-im.constants :as c]))
(:require [status-im.constants :as c]
[status-im.utils.web-provider :as w3]))
(def Web3 (js/require "web3"))
(defn web3 []
(Web3. (Web3.providers.HttpProvider. c/ethereum-rpc-url)))
(Web3. (w3/get-provider c/ethereum-rpc-url)))
(def networks
{"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" :mainnet

View File

@ -0,0 +1,13 @@
(ns status-im.utils.web-provider
(:require [taoensso.timbre :as log]
[status-im.components.status :as status]))
(defn get-provider [rpc-url]
#js {:sendAsync (fn [payload callback]
(status/call-web3
rpc-url
(.stringify js/JSON payload)
(fn [response]
(if (= "" response)
(log/warn :web3-response-error)
(callback nil (.parse js/JSON response))))))})