diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java index 2e81a7c0e8..a0e1503fe0 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java @@ -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(); + } } diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/Web3Bridge.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/Web3Bridge.java new file mode 100644 index 0000000000..f845719325 --- /dev/null +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/Web3Bridge.java @@ -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(); + } + } +} diff --git a/modules/react-native-status/ios/RCTStatus/RCTStatus.m b/modules/react-native-status/ios/RCTStatus/RCTStatus.m index 7ae413ac3b..2b0ae1d895 100644 --- a/modules/react-native-status/ios/RCTStatus/RCTStatus.m +++ b/modules/react-native-status/ios/RCTStatus/RCTStatus.m @@ -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){ diff --git a/package.json b/package.json index 8714502070..5b37d719c1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/resources/web3_init.js b/resources/web3_init.js index cd7c6cb3c3..536cd0e4db 100644 --- a/resources/web3_init.js +++ b/resources/web3_init.js @@ -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)); } } diff --git a/src/status_im/android/core.cljs b/src/status_im/android/core.cljs index 32c4fbb0c1..509cbdbfe5 100644 --- a/src/status_im/android/core.cljs +++ b/src/status_im/android/core.cljs @@ -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 diff --git a/src/status_im/components/react.cljs b/src/status_im/components/react.cljs index a1ad6e1dfc..e2cafc8cbc 100644 --- a/src/status_im/components/react.cljs +++ b/src/status_im/components/react.cljs @@ -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")) diff --git a/src/status_im/components/status.cljs b/src/status_im/components/status.cljs index 02b69d4808..8301c9d639 100644 --- a/src/status_im/components/status.cljs +++ b/src/status_im/components/status.cljs @@ -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)))) diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index ba459c6d0a..19e6a9feb2 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -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 [_]] diff --git a/src/status_im/protocol/web3/utils.cljs b/src/status_im/protocol/web3/utils.cljs index 7ccd3f2f6f..3e2f102d16 100644 --- a/src/status_im/protocol/web3/utils.cljs +++ b/src/status_im/protocol/web3/utils.cljs @@ -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 [] diff --git a/src/status_im/utils/ethereum_network.cljs b/src/status_im/utils/ethereum_network.cljs index 5a73547b27..084aad1cb6 100644 --- a/src/status_im/utils/ethereum_network.cljs +++ b/src/status_im/utils/ethereum_network.cljs @@ -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 diff --git a/src/status_im/utils/web_provider.cljs b/src/status_im/utils/web_provider.cljs new file mode 100644 index 0000000000..dd83bca474 --- /dev/null +++ b/src/status_im/utils/web_provider.cljs @@ -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))))))})