[Fix #2809] Support requestMessages in web3.js

Signed-off-by: Dmitry Novotochinov <trybeee@gmail.com>
This commit is contained in:
Foo Pang 2017-12-28 20:58:09 +08:00 committed by Dmitry Novotochinov
parent 29657a08de
commit fe697bfcc8
No known key found for this signature in database
GPG Key ID: 267674DCC86628D9
3 changed files with 31 additions and 33 deletions

2
package-lock.json generated
View File

@ -9485,7 +9485,7 @@
} }
}, },
"web3": { "web3": {
"version": "github:status-im/web3.js#17b13f26044e60ac824d1b97052bfad1be5af9cc", "version": "github:status-im/web3.js#aca66029d7ffac8ed2803b2fc7f0fec01e335ca3",
"requires": { "requires": {
"bignumber.js": "github:status-im/bignumber.js#cc066a0a3d6bfe0c436c9957f4ea8344bf963c89", "bignumber.js": "github:status-im/bignumber.js#cc066a0a3d6bfe0c436c9957f4ea8344bf963c89",
"crypto-js": "3.1.8", "crypto-js": "3.1.8",

View File

@ -240,8 +240,9 @@
(re-frame/reg-fx (re-frame/reg-fx
::request-messages ::request-messages
(fn [{:keys [wnode topic sym-key-id]}] (fn [{:keys [wnode topic sym-key-id web3]}]
(inbox/request-messages wnode (inbox/request-messages web3
wnode
topic topic
sym-key-id sym-key-id
#(re-frame/dispatch [::request-messages-success %]) #(re-frame/dispatch [::request-messages-success %])

View File

@ -3,7 +3,8 @@
[taoensso.timbre :as log] [taoensso.timbre :as log]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[clojure.string :as string] [clojure.string :as string]
[status-im.protocol.web3.keys :as keys])) [status-im.protocol.web3.keys :as keys]
[status-im.protocol.web3.utils :as utils]))
(def peers (atom #{})) (def peers (atom #{}))
(def trusted-peers (atom #{})) (def trusted-peers (atom #{}))
@ -12,20 +13,28 @@
;; {"error": "msg"} or {"result": true} ;; {"error": "msg"} or {"result": true}
(defn- parse-json [s] (defn- parse-json [s]
(try (try
(-> s (let [res (-> s
js/JSON.parse js/JSON.parse
(js->clj :keywordize-keys true)) (js->clj :keywordize-keys true))]
;; NOTE(dmitryn): AddPeer() may return {"error": ""}
;; assuming empty error is a success response
;; by transforming {"error": ""} to {:result true}
(if (and (:error res)
(= (:error res) ""))
{:result true}
res))
(catch :default e (catch :default e
{:error (.-message e)}))) {:error (.-message e)})))
(defn- response-handler [error-fn success-fn] (defn- response-handler [error-fn success-fn]
(fn [response] (fn handle-response
(let [{:keys [error result]} (parse-json response)] ([response]
;; NOTE(dmitryn): AddPeer() may return {"error": ""} (let [{:keys [error result]} (parse-json response)]
;; assuming empty error is a success response (handle-response error result)))
(if (seq error) ([error result]
(error-fn error) (if error
(success-fn result))))) (error-fn error)
(success-fn result)))))
(defn add-peer [enode success-fn error-fn] (defn add-peer [enode success-fn error-fn]
(if (@peers enode) (if (@peers enode)
@ -49,27 +58,15 @@
(swap! trusted-peers conj enode) (swap! trusted-peers conj enode)
(success-fn result))))))) (success-fn result)))))))
;; TODO(oskarth): Use web3 binding instead of raw RPC above, pending binding and deps: (defn request-messages [web3 wnode topic sym-key-id success-fn error-fn]
;; (.requestMessages (utils/shh web3)
;; (clj->js opts)
;; callback
;; #(log/warn :request-messages-error
;; (.stringify js/JSON (clj->js opts)) %))
(defn request-messages [wnode topic sym-key-id success-fn error-fn]
(log/info "offline inbox: sym-key-id" sym-key-id) (log/info "offline inbox: sym-key-id" sym-key-id)
(let [args {:jsonrpc "2.0" (let [opts {:mailServerPeer wnode
:id 2 :topic topic
:method "shh_requestMessages" :symKeyID sym-key-id}]
;; NOTE: "from" and "to" parameters omitted here
;; by default "from" is 24 hours ago and "to" is time now
:params [{:mailServerPeer wnode
:topic topic
:symKeyID sym-key-id}]}
payload (.stringify js/JSON (clj->js args))]
(log/info "offline inbox: request-messages request") (log/info "offline inbox: request-messages request")
(log/info "offline inbox: request-messages args" (pr-str args)) (log/info "offline inbox: request-messages args" (pr-str opts))
(log/info "offline inbox: request-messages payload" (pr-str payload)) (.requestMessages (utils/shh web3)
(status/call-web3 payload (clj->js opts)
(response-handler error-fn success-fn)))) (response-handler error-fn success-fn))))
(defn initialize! [web3] (defn initialize! [web3]