From 9198b15380f8ada851d3acdf4950205f792c7da6 Mon Sep 17 00:00:00 2001 From: shamardy Date: Wed, 6 Feb 2019 14:36:23 +0200 Subject: [PATCH] Exposed whisper (shh) events Signed-off-by: Julien Eluard --- src/status_im/constants.cljs | 10 ++++ src/status_im/extensions/core.cljs | 54 ++++++++++++++++++++- src/status_im/extensions/ethereum.cljs | 66 ++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index cf591d7a3a..0827e6e5d8 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -210,6 +210,16 @@ (def ^:const web3-uninstall-filter "eth_uninstallFilter") (def ^:const web3-get-filter-changes "eth_getFilterChanges") +(def ^:const web3-shh-post "shh_post") +(def ^:const web3-shh-new-identity "shh_newIdentity") +(def ^:const web3-shh-has-identity "shh_hasIdentity") +(def ^:const web3-shh-new-group "shh_newGroup") +(def ^:const web3-shh-add-to-group "shh_addToGroup") +(def ^:const web3-shh-new-filter "shh_newFilter") +(def ^:const web3-shh-uninstall-filter "shh_uninstallFilter") +(def ^:const web3-shh-get-filter-changes "shh_getFilterChanges") +(def ^:const web3-shh-get-messages "shh_getMessages") + (def ^:const status-create-address "status_createaddress") (def ^:const event-transfer-hash diff --git a/src/status_im/extensions/core.cljs b/src/status_im/extensions/core.cljs index 8d3ac6eee5..cefaf61242 100644 --- a/src/status_im/extensions/core.cljs +++ b/src/status_im/extensions/core.cljs @@ -719,7 +719,59 @@ :params? :vector :outputs? :vector :on-success :event - :on-failure? :event}}} + :on-failure? :event}} + 'ethereum/shh_post + {:permissions [:read] + :value :extensions/shh-post + :arguments {:from? :string + :to? :string + :topics :vector + :payload :string + :priority :string + :ttl :string + :on-success :event + :on-failure? :event}} + 'ethereum/shh-new-identity + {:permissions [:read] + :value :extensions/shh-new-identity + :arguments {:on-success :event + :on-failure? :event}} + 'ethereum/shh-has-identity + {:permissions [:read] + :value :extensions/shh-has-identity + :arguments {:address :string + :on-success :event + :on-failure? :event}} + 'ethereum/shh-new-group + {:permissions [:read] + :value :extensions/shh-new-group + :arguments {:on-success :event + :on-failure? :event}} + 'ethereum/shh-add-to-group + {:permissions [:read] + :value :extensions/shh-add-to-group + :arguments {:address :string + :on-success :event + :on-failure? :event}} + 'ethereum/shh_new-filter + {:permissions [:read] + :value :extensions/shh-new-filter + :arguments {:to? :string + :topics :vector + :on-success :event + :on-failure? :event}} + 'ethereum/shh-uninstall-filter + {:permissions [:read] + :value :extensions/shh-uninstall-filter + :arguments {:id :string}} + 'ethereum/shh-get-filter-changes + {:permissions [:read] + :value :extensions/shh-get-filter-changes + :arguments {:id :string}} + 'ethereum/shh-get-messages + {:permissions [:read] + :value :extensions/shh-get-messages + :arguments {:id :string}}} :hooks {:chat.command commands/command-hook :wallet.settings settings/hook}}) diff --git a/src/status_im/extensions/ethereum.cljs b/src/status_im/extensions/ethereum.cljs index 77c6292ed4..b4fa11df38 100644 --- a/src/status_im/extensions/ethereum.cljs +++ b/src/status_im/extensions/ethereum.cljs @@ -394,3 +394,69 @@ :extensions/ethereum-create-filter (fn [_ [_ _ {:keys [type] :as m}]] (rpc-call (create-filter-method type) (create-filter-arguments type m) abi-spec/hex-to-number m))) + +(handlers/register-handler-fx + :extensions/shh-post + (fn [_ [_ _ {:keys [from to topics payload priority ttl] :as m}]] + (let [params [{:from from + :to to + :topics (generate-topic topics) + :payload payload + :priority (abi-spec/number-to-hex priority) + :ttl (abi-spec/number-to-hex ttl)}]] + (rpc-call constants/web3-shh-post params #(abi-spec/hex-to-value % "bool") m)))) + +(handlers/register-handler-fx + :extensions/shh-new-identity + (fn [_ [_ _ arguments]] + (let [params []] + (rpc-call constants/web3-shh-new-identity params #(identity %) arguments)))) + +(handlers/register-handler-fx + :extensions/shh-has-identity + (fn [_ [_ _ {:keys [address] :as m}]] + ((rpc-call constants/web3-shh-has-identity address #(abi-spec/hex-to-value % "bool") m)))) + +(handlers/register-handler-fx + :extensions/shh-new-group + (fn [_ [_ _ arguments]] + (let [params []] + (rpc-call constants/web3-shh-new-group params #(identity %) arguments)))) + +(handlers/register-handler-fx + :extensions/shh-add-to-group + (fn [_ [_ _ {:keys [address] :as m}]] + ((rpc-call constants/web3-shh-add-to-group address #(abi-spec/hex-to-value % "bool") m)))) + +(handlers/register-handler-fx + :extensions/shh-new-filter + (fn [_ [_ _ {:keys [to topics] :as m}]] + (let [params [{:to to + :topics (generate-topic topics)}]] + (rpc-call constants/web3-shh-new-filter params abi-spec/hex-to-number m)))) + +(handlers/register-handler-fx + :extensions/shh-uninstall-filter + (fn [_ [_ _ {:keys [id] :as m}]] + (rpc-call constants/web3-shh-uninstall-filter [(abi-spec/number-to-hex id)] #(abi-spec/hex-to-value % "bool") m))) + +(defn- parse-messages [{:keys [hash from to expiry ttl sent topics payload workProved]}] + {:hash hash + :from from + :to to + :expiry (abi-spec/hex-to-number expiry) + :ttl (abi-spec/hex-to-number ttl) + :sent (abi-spec/hex-to-number sent) + :topics topics + :payload payload + :workProved (abi-spec/hex-to-number workProved)}) + +(handlers/register-handler-fx + :extensions/shh-get-filter-changes + (fn [_ [_ _ {:keys [id] :as m}]] + (rpc-call constants/web3-shh-get-filter-changes [(abi-spec/number-to-hex id)] #(map parse-messages %) m))) + +(handlers/register-handler-fx + :extensions/shh-get-messages + (fn [_ [_ _ {:keys [id] :as m}]] + (rpc-call constants/web3-shh-get-messages [(abi-spec/number-to-hex id)] #(map parse-messages %) m)))