From 7b89adc8776a02fe41695b12be32514b80564e7a Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 29 Mar 2017 10:32:53 +0300 Subject: [PATCH] add customized logging on android and whisper ver5 support --- .../im/status/ethereum/MainApplication.java | 2 +- .../react-native-status/android/build.gradle | 2 +- .../status/ethereum/module/StatusModule.java | 26 +++++++++-- .../status/ethereum/module/StatusPackage.java | 8 +++- .../ios/RCTStatus/RCTStatus.m | 8 ---- .../react-native-status/ios/RCTStatus/pom.xml | 2 +- package.json | 2 +- src/status_im/protocol/listeners.cljs | 10 +++-- src/status_im/protocol/web3/delivery.cljs | 6 +-- src/status_im/protocol/web3/filtering.cljs | 45 +++++++++++++++---- src/status_im/protocol/web3/transport.cljs | 21 ++++++++- src/status_im/protocol/web3/utils.cljs | 8 +++- 12 files changed, 106 insertions(+), 34 deletions(-) diff --git a/android/app/src/main/java/im/status/ethereum/MainApplication.java b/android/app/src/main/java/im/status/ethereum/MainApplication.java index f13ae4538e..a48510d4d2 100644 --- a/android/app/src/main/java/im/status/ethereum/MainApplication.java +++ b/android/app/src/main/java/im/status/ethereum/MainApplication.java @@ -49,7 +49,7 @@ public class MainApplication extends Application implements ReactApplication { new NfcReactNativePackage(), new RNInstabugReactnativePackage("b239f82a9cb00464e4c72cc703e6821e",MainApplication.this,"shake"), new SplashScreenReactPackage(), - new StatusPackage(), + new StatusPackage(BuildConfig.DEBUG), new RealmReactPackage(), new VectorIconsPackage(), new ReactNativeContacts(), diff --git a/modules/react-native-status/android/build.gradle b/modules/react-native-status/android/build.gradle index f8229ccded..7da188793d 100644 --- a/modules/react-native-status/android/build.gradle +++ b/modules/react-native-status/android/build.gradle @@ -14,5 +14,5 @@ android { dependencies { compile 'com.facebook.react:react-native:+' - compile(group: 'status-im', name: 'status-go', version: '0.9.5-51-g8ef2206', ext: 'aar') + compile(group: 'status-im', name: 'status-go', version: '0.9.5-55-g01a1550', ext: 'aar') } 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 3fc7f3b9cd..6d22dfa36a 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 @@ -2,7 +2,6 @@ package im.status.ethereum.module; import android.app.Activity; import android.os.*; -import android.os.Process; import android.view.WindowManager; import android.util.Log; import android.webkit.CookieManager; @@ -20,6 +19,9 @@ import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import org.json.JSONObject; +import org.json.JSONException; + class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ConnectorHandler { private static final String TAG = "StatusModule"; @@ -29,12 +31,14 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL private static StatusModule module; private ServiceConnector status = null; private ExecutorService executor = null; + private boolean debug; - StatusModule(ReactApplicationContext reactContext) { + StatusModule(ReactApplicationContext reactContext, boolean debug) { super(reactContext); if (executor == null) { executor = Executors.newCachedThreadPool(); } + this.debug = debug; reactContext.addLifecycleEventListener(this); } @@ -133,7 +137,23 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL } } - Statusgo.StartNode(Statusgo.GenerateConfig(dataFolder, 3)); + String config; + String defaultConfig = Statusgo.GenerateConfig(dataFolder, 3); + try { + JSONObject jsonConfig = new JSONObject(defaultConfig); + jsonConfig.put("LogEnabled", this.debug); + jsonConfig.put("LogFile", "geth.log"); + jsonConfig.put("LogLevel", "DEBUG"); + + config = jsonConfig.toString(); + } catch (JSONException e) { + Log.d(TAG, "Something went wrong " + e.getMessage()); + Log.d(TAG, "Default configuration will be used"); + + config = defaultConfig; + } + + Statusgo.StartNode(config); Log.d(TAG, "Geth node started"); } diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusPackage.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusPackage.java index 5a6644ac00..9dd0448cfe 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusPackage.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusPackage.java @@ -12,12 +12,18 @@ import java.util.List; public class StatusPackage implements ReactPackage { + private boolean debug; + + public StatusPackage (boolean debug) { + this.debug = debug; + } + @Override public List createNativeModules(ReactApplicationContext reactContext) { List modules = new ArrayList<>(); System.loadLibrary("statusgoraw"); System.loadLibrary("statusgo"); - modules.add(new StatusModule(reactContext)); + modules.add(new StatusModule(reactContext, this.debug)); return modules; } diff --git a/modules/react-native-status/ios/RCTStatus/RCTStatus.m b/modules/react-native-status/ios/RCTStatus/RCTStatus.m index 238bf9bfc9..42b35415b3 100644 --- a/modules/react-native-status/ios/RCTStatus/RCTStatus.m +++ b/modules/react-native-status/ios/RCTStatus/RCTStatus.m @@ -162,14 +162,6 @@ RCT_EXPORT_METHOD(startNode:(RCTResponseSenderBlock)onResultCallback) { char *config = GenerateConfig([folderName.path UTF8String], 3); StartNode(config); }); - NSString *peer1 = @"enode://5f23bf4913dd005ce945648cb12d3ef970069818d8563a3fe054e5e1dc3898b9cb83e0af1f51b2dce75eaffc76e93f996caf538e21c5b64db5fa324958d59630@95.85.40.211:30303"; - NSString *peer2 = @"enode://b9de2532421f15ac55da9d9a7cddc0dc08b0d646d631fd7ab2a170bd2163fb86b095dd8bde66b857592812f7cd9539f2919b6c64bc1a784a1d1c6ec8137681ed@188.166.229.119:30303"; - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), - dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), - ^(void) { - AddPeer((char *) [peer1 UTF8String]); - AddPeer((char *) [peer2 UTF8String]); - }); onResultCallback(@[[NSNull null]]); //Screen lock notifications CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center diff --git a/modules/react-native-status/ios/RCTStatus/pom.xml b/modules/react-native-status/ios/RCTStatus/pom.xml index 4de79d7cb0..7f6dbea04c 100644 --- a/modules/react-native-status/ios/RCTStatus/pom.xml +++ b/modules/react-native-status/ios/RCTStatus/pom.xml @@ -25,7 +25,7 @@ status-im status-go-ios-simulator - 0.9.5-51-g8ef2206 + 0.9.5-55-g01a1550 zip true ./ diff --git a/package.json b/package.json index cb8f50047a..983f0ac503 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "tty-browserify": "0.0.0", "url": "^0.10.3", "vm-browserify": "0.0.4", - "web3": "0.16.0" + "web3": "github:status-im/web3.js#status-0.18.4" }, "browser": { "crypto": "react-native-crypto", diff --git a/src/status_im/protocol/listeners.cljs b/src/status_im/protocol/listeners.cljs index 36bc802544..7fd7d40524 100644 --- a/src/status_im/protocol/listeners.cljs +++ b/src/status_im/protocol/listeners.cljs @@ -9,7 +9,9 @@ (defn- parse-payload [payload] (debug :parse-payload) (try - {:payload (r/read-string (u/to-utf8 payload))} + ;; todo figure why we have to call to-utf8 twice + (let [read (comp r/read-string u/to-utf8 u/to-utf8)] + {:payload (read payload)}) (catch :default err (debug :parse-payload-error err) {:error err}))) @@ -36,7 +38,7 @@ (when error (debug :listener-error error)) (when-not error - (debug :message-received) + (debug :message-received (js->clj js-message)) (let [{:keys [from payload to] :as message} (js->clj js-message :keywordize-keys true) @@ -50,7 +52,9 @@ (= type :discover))) (let [{:keys [content error]} (parse-content (:private keypair) payload' - (not= "0x0" to))] + (and (not= "0x0" to) + (not= "" to) + (not (nil? to))))] (if error (debug :failed-to-handle-message error) (let [payload'' (assoc payload' :content content) diff --git a/src/status_im/protocol/web3/delivery.cljs b/src/status_im/protocol/web3/delivery.cljs index 862ee9d8d1..d5c4b7dd4e 100644 --- a/src/status_im/protocol/web3/delivery.cljs +++ b/src/status_im/protocol/web3/delivery.cljs @@ -128,10 +128,10 @@ :last-attempt (u/timestamp))))) (defn delivery-callback - [web3 post-error-callback {:keys [id requires-ack? to]}] + [web3 post-error-callback {:keys [id requires-ack? to]} message] (fn [error _] (when error - (log/warn :shh-post-error error) + (log/warn :shh-post-error error message) (when post-error-callback (post-error-callback error))) (when-not error @@ -207,7 +207,7 @@ (when (should-be-retransmitted? options data) (try (let [message' (check-ttl message type ttl-config default-ttl) - callback (delivery-callback web3 post-error-callback data)] + callback (delivery-callback web3 post-error-callback data message')] (t/post-message! web3 message' callback)) (catch :default err (log/error :post-message-error err)) diff --git a/src/status_im/protocol/web3/filtering.cljs b/src/status_im/protocol/web3/filtering.cljs index d4f3bfa49c..f6a60bbdd0 100644 --- a/src/status_im/protocol/web3/filtering.cljs +++ b/src/status_im/protocol/web3/filtering.cljs @@ -1,7 +1,7 @@ (ns status-im.protocol.web3.filtering (:require [status-im.protocol.web3.utils :as u] [cljs.spec :as s] - [taoensso.timbre :refer-macros [debug]])) + [taoensso.timbre :as log])) (def status-topic "status-dapp-topic") (defonce filters (atom {})) @@ -10,18 +10,45 @@ (defn remove-filter! [web3 options] (when-let [filter (get-in @filters [web3 options])] - (.stopWatching filter) - (debug :stop-watching options) + (.stopWatching filter + (fn [error _] + (when error + (log/warn :remove-filter-error options error)))) + (log/debug :stop-watching options) (swap! filters update web3 dissoc options))) -(defn add-filter! +(defn add-shh-filter! [web3 options callback] + (fn do-add-filter-fn + ([] (do-add-filter-fn nil)) + ([keyname] + (let [options' (if keyname + (assoc options :keyname keyname) + options) + filter (.filter (u/shh web3) (clj->js options') + callback + #(log/warn :add-filter-error options %))] + (swap! filters assoc-in [web3 options] filter))))) + +(defn add-filter! + [web3 {:keys [topics to] :as options} callback] (remove-filter! web3 options) - (debug :add-filter options) - (let [filter (.filter (u/shh web3) - (clj->js options) - callback)] - (swap! filters assoc-in [web3 options] filter))) + (log/debug :add-filter options) + (let [shh (u/shh web3) + encrypted? (boolean to) + do-add-filter (add-shh-filter! web3 options callback)] + (if encrypted? + (do-add-filter) + (let [topic (first topics)] + (.hasSymKey + shh topic + (fn [error res] + (if-not res + (.addSymKey + shh topic u/status-key-data + (fn [error res] + (when-not error (do-add-filter topic)))) + (do-add-filter topic)))))))) (defn remove-all-filters! [] (doseq [[web3 filters] @filters] diff --git a/src/status_im/protocol/web3/transport.cljs b/src/status_im/protocol/web3/transport.cljs index d913869370..f80da7e5f8 100644 --- a/src/status_im/protocol/web3/transport.cljs +++ b/src/status_im/protocol/web3/transport.cljs @@ -11,7 +11,24 @@ :opt-un [:message/to])) (defn post-message! - [web3 message callback] + [web3 {:keys [topics from to] :as message} callback] {:pre [(valid? :shh/message message)]} (debug :post-message message) - (.post (u/shh web3) (clj->js message) callback)) + (let [topic (first topics) + shh (u/shh web3) + encrypted? (boolean to) + message' (if encrypted? + message + (assoc message :keyname topic)) + do-post (fn [] (.post shh (clj->js message') callback))] + (if encrypted? + (do-post) + (.hasSymKey + shh topic + (fn [_ res] + (if-not res + (.addSymKey + shh topic u/status-key-data + (fn [error _] + (when-not error (do-post)))) + (do-post))))))) diff --git a/src/status_im/protocol/web3/utils.cljs b/src/status_im/protocol/web3/utils.cljs index 8d8bdd4b29..7ccd3f2f6f 100644 --- a/src/status_im/protocol/web3/utils.cljs +++ b/src/status_im/protocol/web3/utils.cljs @@ -7,8 +7,11 @@ (defn from-utf8 [s] (.fromUtf8 web3.prototype s)) +(defn to-ascii [s] + (.toAscii web3.prototype s)) + (defn to-utf8 [s] - (.toUtf8 web3.prototype s)) + (.toUtf8 web3.prototype (str s))) (defn shh [web3] (.-shh web3)) @@ -20,3 +23,6 @@ (defn timestamp [] (to-long (now))) + +(def status-key-data (.toHex web3.prototype "status-key-data")) +