From 1155575f113e7ab63c43e644fe28a4aba22d2a5b Mon Sep 17 00:00:00 2001 From: michaelr Date: Thu, 17 Mar 2016 19:11:45 +0200 Subject: [PATCH] support new geth methods to create and update identity --- protocol/src/cljs/syng_im/protocol/api.cljs | 16 ++++++---- protocol/src/cljs/syng_im/protocol/web3.cljs | 32 +++++++++++++++++++ protocol/src/cljs/syng_im/utils/logging.cljs | 17 ++++++---- .../src/cljs/syng_im/core.cljs | 7 +++- 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/protocol/src/cljs/syng_im/protocol/api.cljs b/protocol/src/cljs/syng_im/protocol/api.cljs index 9215526..f71a891 100644 --- a/protocol/src/cljs/syng_im/protocol/api.cljs +++ b/protocol/src/cljs/syng_im/protocol/api.cljs @@ -21,7 +21,8 @@ make-msg post-msg make-web3 - new-identity + create-identity + add-identity stop-listener]] [syng-im.protocol.handler :refer [handle-incoming-whisper-msg]] [syng-im.protocol.user-handler :refer [invoke-user-handler]] @@ -38,9 +39,6 @@ (defn create-connection [ethereum-rpc-url] (make-web3 ethereum-rpc-url)) -(defn create-identity [connection] - (new-identity connection)) - (defn my-identity [] (state/my-identity)) @@ -73,10 +71,14 @@ (set-handler handler) (go (let [connection (create-connection ethereum-rpc-url) - identity (or identity - (> (:private identity) + (add-identity connection))) + identity) + (js [{:jsonrpc "2.0" :method "shh_createIdentity" :params [] :id 99999999999}]) + (fn [error result] + (if error + (do + (log/error (str "Call to shh_createIdentity failed" ":") error) + (invoke-user-handler :error {:error-msg "Call to shh_createIdentity failed" + :details error})) + (let [[public private] (-> (js->clj result :keywordize-keys true) + (first) + :result)] + (put! result-channel {:public public + :private private}))) + (close! result-channel))) + result-channel)) + +(defn add-identity [web3 private-key] + (let [result-channel (chan)] + (.sendAsync (.-currentProvider web3) + (clj->js [{:jsonrpc "2.0" :method "shh_addIdentity" :params [private-key] :id 99999999999}]) + (fn [error result] + (if error + (do + (log/error (str "Call to shh_addIdentity failed" ":") error) + (invoke-user-handler :error {:error-msg "Call to shh_addIdentity failed" + :details error})) + (put! result-channel (js->clj result))) + (close! result-channel))) + result-channel)) + (defn post-msg [web3 msg] (let [js-msg (clj->js msg)] (log/info "Sending whisper message:" js-msg) diff --git a/protocol/src/cljs/syng_im/utils/logging.cljs b/protocol/src/cljs/syng_im/utils/logging.cljs index 7413954..fb7322d 100644 --- a/protocol/src/cljs/syng_im/utils/logging.cljs +++ b/protocol/src/cljs/syng_im/utils/logging.cljs @@ -5,17 +5,22 @@ (defn timestamp [] (tf/unparse (:hour-minute-second-fraction tf/formatters) (t/now))) +(defn to-js [args] + (->> (cons (timestamp) args) + (mapv clj->js) + (into-array))) + +(defn debug [& args] + (.apply (.-log js/console) js/console (to-js args))) + (defn info [& args] - (let [args (cons (timestamp) args)] - (.apply (.-log js/console) js/console (into-array args)))) + (.apply (.-log js/console) js/console (to-js args))) (defn warn [& args] - (let [args (cons (timestamp) args)] - (.apply (.-warn js/console) js/console (into-array args)))) + (.apply (.-warn js/console) js/console (to-js args))) (defn error [& args] - (let [args (cons (timestamp) args)] - (.apply (.-error js/console) js/console (into-array args)))) + (.apply (.-error js/console) js/console (to-js args))) (comment diff --git a/simple-whisper-chat/src/cljs/syng_im/core.cljs b/simple-whisper-chat/src/cljs/syng_im/core.cljs index 6b4dc0b..d0bb9ee 100644 --- a/simple-whisper-chat/src/cljs/syng_im/core.cljs +++ b/simple-whisper-chat/src/cljs/syng_im/core.cljs @@ -217,11 +217,16 @@ ;(p/make-whisper-msg web3-2 user2-ident user1-ident "Hello World!") - (require '[syng-im.protocol.whisper :as w]) + (require '[syng-im.protocol.web3 :as w]) (def web3 (w/make-web3 "http://localhost:4546")) (.newIdentity (w/whisper web3) (fn [error result] (println error result))) + (.sendAsync (.-currentProvider web3) + (clj->js [{:jsonrpc "2.0" :method "shh_addIdentity" :params ["0x585493cda18f2b4314afb51224e9c1e913780642783ca11e683a66cfaa9eec94"] :id 99999999999}]) + (fn [error result] + (println error result))) + ) (comment