[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": {
"version": "github:status-im/web3.js#17b13f26044e60ac824d1b97052bfad1be5af9cc",
"version": "github:status-im/web3.js#aca66029d7ffac8ed2803b2fc7f0fec01e335ca3",
"requires": {
"bignumber.js": "github:status-im/bignumber.js#cc066a0a3d6bfe0c436c9957f4ea8344bf963c89",
"crypto-js": "3.1.8",

View File

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

View File

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