replace last remains of web3.js by json-rpc

This commit is contained in:
yenda 2019-08-20 11:53:10 +02:00
parent d61fffb021
commit 1c475e5b1e
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
22 changed files with 203 additions and 282 deletions

View File

@ -103,7 +103,7 @@
chat-props)]
(fx/merge cofx
{:db (update-in db [:chats chat-id] merge chat)}
(chats-store/save-chat-rpc chat))))
(chats-store/save-chat chat))))
(fx/defn add-public-chat
"Adds new public group chat to db & realm"
@ -149,7 +149,7 @@
:unviewed-messages-count 0
:deleted-at-clock-value last-message-clock-value})}
(messages-store/delete-messages-by-chat-id chat-id)
#(chats-store/save-chat-rpc % (get-in % [:db :chats chat-id])))))
#(chats-store/save-chat % (get-in % [:db :chats chat-id])))))
(fx/defn deactivate-chat
[{:keys [db now] :as cofx} chat-id]
@ -157,7 +157,7 @@
{:db (-> db
(assoc-in [:chats chat-id :is-active] false)
(assoc-in [:current-chat-id] nil))}
#(chats-store/save-chat-rpc % (get-in % [:db :chats chat-id]))))
#(chats-store/save-chat % (get-in % [:db :chats chat-id]))))
(fx/defn remove-chat
"Removes chat completely from app, producing all necessary effects for that"

View File

@ -145,7 +145,7 @@
(fx/defn add-received-message
[{:keys [db] :as cofx}
{:keys [from message-id chat-id js-obj content metadata] :as raw-message}]
(let [{:keys [web3 current-chat-id view-id]} db
(let [{:keys [current-chat-id view-id]} db
current-public-key (multiaccounts.model/current-public-key cofx)
current-chat? (and (or (= :chat view-id)
(= :chat-modal view-id))

View File

@ -50,11 +50,9 @@
(fx/defn notify-user
"Send an empty message to the user, which will carry device information"
[cofx public-key]
(let [{:keys [web3]} (:db cofx)
current-public-key (multiaccounts.model/current-public-key cofx)]
(let [current-public-key (multiaccounts.model/current-public-key cofx)]
{:shh/send-direct-message
[{:web3 web3
:src current-public-key
[{:src current-public-key
:dst public-key
:payload ""}]}))

View File

@ -141,7 +141,7 @@
(assoc :group-chat-local-version 1) ;; TODO(cammellos): this can be removed
(dissoc :chatType :members)))
(fx/defn save-chat-rpc [cofx {:keys [chat-id] :as chat}]
(fx/defn save-chat [cofx {:keys [chat-id] :as chat}]
{::json-rpc/call [{:method "shhext_saveChat"
:params [(->rpc chat)]
:on-success #(log/debug "saved chat" chat-id "successfuly")

View File

@ -17,7 +17,7 @@
(defn supports?
"Calls cb with true if `supportsInterface` is supported by this contract.
See EIP for details."
[web3 contract cb]
[contract cb]
(supports-interface?
contract
supports-interface-hash

View File

@ -26,6 +26,13 @@
"eth_syncing" {}
"net_version" {}
"web3_clientVersion" {}
"shhext_post" {}
"shh_generateSymKeyFromPassword" {}
"shh_getSymKey" {}
"shh_markTrustedPeer" {}
"shhext_requestMessages" {}
"shhext_sendDirectMessage" {}
"shhext_sendPublicMessage" {}
"shhext_enableInstallation" {}
"shhext_disableInstallation" {}
"shhext_getOurInstallations" {}

View File

@ -65,7 +65,6 @@
[status-im.wallet.core :as wallet]
[status-im.wallet.custom-tokens.core :as custom-tokens]
[status-im.wallet.db :as wallet.db]
[status-im.web3.core :as web3]
[taoensso.timbre :as log]
[status-im.chat.commands.sending :as commands.sending]
[status-im.utils.money :as money]

View File

@ -120,7 +120,6 @@
creator (extract-creator chat)
members (clojure.set/union (get-in cofx [:db :chats chat-id :contacts])
removed-members)
{:keys [web3]} (:db cofx)
current-public-key (multiaccounts.model/current-public-key cofx)
members-allowed (filter
(fn [pk]
@ -134,8 +133,7 @@
(fx/merge
cofx
{:shh/send-group-message
{:web3 web3
:src current-public-key
{:src current-public-key
:dsts destinations
:success-event [:transport/message-sent
chat-id

View File

@ -12,6 +12,7 @@
[status-im.transport.db :as transport.db]
[status-im.transport.message.protocol :as protocol]
[clojure.string :as string]
[status-im.ethereum.json-rpc :as json-rpc]
[status-im.mailserver.topics :as mailserver.topics]
[status-im.mailserver.constants :as constants]
[status-im.data-store.mailservers :as data-store.mailservers]
@ -158,18 +159,15 @@
(reset! limit (decrease-limit))
(reset! success-counter 0)))
(defn mark-trusted-peer! [web3 enode]
(.markTrustedPeer (transport.utils/shh web3)
enode
(fn [error response]
(if error
(re-frame/dispatch [:mailserver.callback/mark-trusted-peer-error error])
(re-frame/dispatch [:mailserver.callback/mark-trusted-peer-success response])))))
(defn mark-trusted-peer! [enode]
(json-rpc/call {:method "shh_markTrustedPeer"
:params [enode]
:on-success #(re-frame/dispatch [:mailserver.callback/mark-trusted-peer-success %])
:on-error #(re-frame/dispatch [:mailserver.callback/mark-trusted-peer-error %])}))
(re-frame/reg-fx
:mailserver/mark-trusted-peer
(fn [{:keys [address web3]}]
(mark-trusted-peer! web3 address)))
mark-trusted-peer!)
(fx/defn generate-mailserver-symkey
[{:keys [db] :as cofx} {:keys [password id] :as mailserver}]
@ -177,7 +175,6 @@
{:db (assoc-in db [:mailserver/mailservers current-fleet id :generating-sym-key?] true)
:shh/generate-sym-key-from-password
[{:password password
:web3 (:web3 db)
:on-success (fn [_ sym-key-id]
(re-frame/dispatch [:mailserver.callback/generate-mailserver-symkey-success mailserver sym-key-id]))
:on-error #(log/error "mailserver: get-sym-key error" %)}]}))
@ -196,8 +193,7 @@
(let [{:keys [address sym-key-id generating-sym-key?] :as mailserver} (fetch-current cofx)]
(fx/merge cofx
{:db (update-mailserver-state db :added)
:mailserver/mark-trusted-peer {:web3 (:web3 db)
:address address}}
:mailserver/mark-trusted-peer address}
(when-not (or sym-key-id generating-sym-key?)
(generate-mailserver-symkey mailserver)))))
@ -297,7 +293,8 @@
(assoc-in [:mailserver/current-request :request-id] request-id))}
{:db (assoc-in db [:mailserver/current-request :request-id] request-id)}))))
(defn request-messages! [web3 {:keys [sym-key-id address]} {:keys [topics cursor to from force-to?] :as request}]
(defn request-messages!
[{:keys [sym-key-id address]} {:keys [topics cursor to from force-to?] :as request}]
;; Add some room to from, unless we break day boundaries so that messages that have
;; been received after the last request are also fetched
(let [actual-from (adjust-request-for-transit-time from)
@ -311,30 +308,28 @@
" range " (- to from)
" cursor " cursor
" limit " actual-limit)
(.requestMessages (transport.utils/shh web3)
(clj->js (cond-> {:topics topics
:mailServerPeer address
:symKeyID sym-key-id
:timeout constants/request-timeout
:limit actual-limit
:cursor cursor
:from actual-from}
force-to?
(assoc :to to)))
(fn [error request-id]
(if-not error
(do
(log/info "mailserver: messages request success for topic " topics "from" from "to" to)
(re-frame/dispatch [:mailserver.callback/request-success {:request-id request-id :topics topics}]))
(do
(log/error "mailserver: messages request error for topic " topics ": " error)
(utils/set-timeout #(re-frame/dispatch [:mailserver.callback/resend-request {:request-id nil}])
constants/backoff-interval-ms)))))))
(json-rpc/call {:method "shhext_requestMessages"
:params [(cond-> {:topics topics
:mailServerPeer address
:symKeyID sym-key-id
:timeout constants/request-timeout
:limit actual-limit
:cursor cursor
:from actual-from}
force-to?
(assoc :to to))]
:on-success (fn [request-id]
(log/info "mailserver: messages request success for topic " topics "from" from "to" to)
(re-frame/dispatch [:mailserver.callback/request-success {:request-id request-id :topics topics}]))
:on-error (fn [error]
(log/error "mailserver: messages request error for topic " topics ": " error)
(utils/set-timeout #(re-frame/dispatch [:mailserver.callback/resend-request {:request-id nil}])
constants/backoff-interval-ms))})))
(re-frame/reg-fx
:mailserver/request-messages
(fn [{:keys [web3 mailserver request]}]
(request-messages! web3 mailserver request)))
(fn [{:keys [mailserver request]}]
(request-messages! mailserver request)))
(defn get-mailserver-when-ready
"return the mailserver if the mailserver is ready"
@ -399,16 +394,14 @@
(when-let [mailserver (get-mailserver-when-ready cofx)]
(let [request-to (or (:mailserver/request-to db)
(quot now 1000))
requests (prepare-messages-requests cofx request-to)
web3 (:web3 db)]
requests (prepare-messages-requests cofx request-to)]
(log/debug "Mailserver: planned requests " requests)
(if-let [request (first requests)]
{:db (assoc db
:mailserver/pending-requests (count requests)
:mailserver/current-request request
:mailserver/request-to request-to)
:mailserver/request-messages {:web3 web3
:mailserver mailserver
:mailserver/request-messages {:mailserver mailserver
:request request}}
{:db (dissoc db
:mailserver/pending-requests
@ -735,8 +728,7 @@
(when-let [mailserver (get-mailserver-when-ready cofx)]
(let [request-with-cursor (assoc request :cursor cursor)]
{:db (assoc db :mailserver/current-request request-with-cursor)
:mailserver/request-messages {:web3 (:web3 db)
:mailserver mailserver
:mailserver/request-messages {:mailserver mailserver
:request request-with-cursor}}))
(let [{:keys [gap chat-id]} request]
(fx/merge cofx
@ -870,8 +862,7 @@
(not current-request)
(-> (assoc-in [:db :mailserver/current-request] first-request)
(assoc :mailserver/request-messages
{:web3 (:web3 db)
:mailserver mailserver
{:mailserver mailserver
:request first-request})))))
(fx/defn resend-request
@ -905,13 +896,11 @@
(dissoc :mailserver/planned-gap-requests))}
mailserver
(let [{:keys [topics from to cursor limit] :as request} current-request
web3 (:web3 db)]
(let [{:keys [topics from to cursor limit] :as request} current-request]
(log/info "mailserver: message request " request-id "expired for mailserver topic" topics "from" from "to" to "cursor" cursor "limit" (decrease-limit))
{:db (update-in db [:mailserver/current-request :attempts] inc)
:mailserver/decrease-limit []
:mailserver/request-messages {:web3 web3
:mailserver mailserver
:mailserver/request-messages {:mailserver mailserver
:request (assoc request :limit (decrease-limit))}})
:else

View File

@ -5,6 +5,7 @@
[status-im.chat.models :as chat-model]
[status-im.chat.models.loading :as chat.loading]
[status-im.constants :as constants]
[status-im.data-store.core :as data-store]
[status-im.contact.core :as contact]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.json-rpc :as json-rpc]
@ -27,7 +28,6 @@
[status-im.utils.types :as types]
[status-im.utils.universal-links.core :as universal-links]
[status-im.wallet.core :as wallet]
[status-im.web3.core :as web3]
[taoensso.timbre :as log]))
(def rpc-endpoint "https://goerli.infura.io/v3/f315575765b14720b32382a61a89341a")
@ -155,6 +155,7 @@
(let [stored-pns (:push-notifications/stored db)]
(fx/merge cofx
{:db (assoc db :chats/loading? true)
::data-store/change-multiaccount [address password]
::json-rpc/call
[{:method "browsers_getBrowsers"
:on-success #(re-frame/dispatch [::initialize-browsers %])}
@ -177,7 +178,7 @@
(boolean (get-in cofx [:db :multiaccounts/recover])))
(fx/defn create-only-events
[{:keys [db] :as cofx}]
[{:keys [db] :as cofx} address password]
(let [{:keys [multiaccount]} db]
(fx/merge cofx
{:db (assoc db
@ -191,6 +192,7 @@
:filters/initialized 1
:network constants/default-network
:networks/networks constants/default-networks)
::data-store/create-multiaccount [address password]
:filters/load-filters []
::json-rpc/call
[{:method "settings_saveConfig"
@ -222,9 +224,6 @@
:card-read-in-progress?
:pin
:multiaccount))
;;TODO remove once web3 has been replaced by json-rpc in protocol
;; after this call realm and the protocol are initialized as a callback
::web3/initialize [address password login-only?]
::json-rpc/call
[{:method "web3_clientVersion"
:on-success #(re-frame/dispatch [::initialize-web3-client-version %])}]
@ -235,7 +234,7 @@
(initialize-multiaccount-db address)
(if login-only?
(login-only-events address password save-password?)
(create-only-events))
(create-only-events address password))
(when recovering?
(navigation/navigate-to-cofx :home nil)))))

View File

@ -7,7 +7,7 @@
;; Publish updates every 48 hours
(def publish-updates-interval (* 48 60 60 1000))
(defn publish-update! [{:keys [db now web3]}]
(defn publish-update! [{:keys [db now]}]
(let [my-public-key (get-in db [:multiaccount :public-key])
peers-count (:peers-count db)
last-updated (get-in
@ -21,21 +21,17 @@
payload (multiaccounts/multiaccount-update-message {:db db})
sync-message (pairing/sync-installation-multiaccount-message {:db db})]
(doseq [pk public-keys]
(shh/send-direct-message!
web3
{:pubKey pk
:sig my-public-key
:chat constants/contact-discovery
:payload payload}
[:multiaccounts.update.callback/published]
[:multiaccounts.update.callback/failed-to-publish]
1))
(shh/send-direct-message!
web3
{:pubKey my-public-key
:sig my-public-key
:chat constants/contact-discovery
:payload sync-message}
[:multiaccounts.update.callback/published]
[:multiaccounts.update.callback/failed-to-publish]
1)))))
(shh/send-direct-message! {:pubKey pk
:sig my-public-key
:chat constants/contact-discovery
:payload payload}
[:multiaccounts.update.callback/published]
[:multiaccounts.update.callback/failed-to-publish]
1))
(shh/send-direct-message! {:pubKey my-public-key
:sig my-public-key
:chat constants/contact-discovery
:payload sync-message}
[:multiaccounts.update.callback/published]
[:multiaccounts.update.callback/failed-to-publish]
1)))))

View File

@ -193,23 +193,23 @@
(re-frame/reg-fx
:node/les-show-debug-info
(fn [[web3 multiaccount chain-sync-state]]
(.getBalance
(.-eth web3)
(:address multiaccount)
(fn [error-balance balance]
(.getBlockNumber
(.-eth web3)
(fn
[error-block block]
(utils/show-popup
"LES sync status"
(str
"* multiaccount=" (:address multiaccount) "\n"
"* latest block=" (or error-block block) "\n"
"* balance=" (or error-balance balance) "\n"
"* eth_getSyncing=" (or chain-sync-state "false")))))))))
(fn [[multiaccount chain-sync-state]]
#_(.getBalance
(.-eth eb3)
(:address multiaccount)
(fn [error-balance balance]
(.getBlockNumber
(.-eth eb3)
(fn
[error-block block]
(utils/show-popup
"LES sync status"
(str
"* multiaccount=" (:address multiaccount) "\n"
"* latest block=" (or error-block block) "\n"
"* balance=" (or error-balance balance) "\n"
"* eth_getSyncing=" (or chain-sync-state "false")))))))))
(defn display-les-debug-info
[{{:keys [web3 multiaccount] :node/keys [chain-sync-state]} :db}]
{:node/les-show-debug-info [web3 multiaccount]})
[{{:keys [multiaccount] :node/keys [chain-sync-state]} :db}]
{:node/les-show-debug-info [multiaccount]})

View File

@ -73,14 +73,13 @@
(protocol/send (transport.pairing/PairInstallation. installation-id device-type installation-name fcm-token) nil cofx)))
(fx/defn confirm-message-processed
[{:keys [db]} confirmation]
[cofx confirmation]
{:transport/confirm-messages-processed [confirmation]})
(defn send-pair-installation [cofx payload]
(let [{:keys [web3]} (:db cofx)
current-public-key (multiaccounts.model/current-public-key cofx)]
{:shh/send-pairing-message {:web3 web3
:src current-public-key
(defn send-pair-installation
[cofx payload]
(let [current-public-key (multiaccounts.model/current-public-key cofx)]
{:shh/send-pairing-message {:src current-public-key
:payload payload}}))
(defn merge-contact [local remote]
@ -263,12 +262,11 @@
:pairing/get-our-installations
get-our-installations)
(fx/defn send-sync-installation [cofx payload]
(let [{:keys [web3]} (:db cofx)
current-public-key (multiaccounts.model/current-public-key cofx)]
(fx/defn send-sync-installation
[cofx payload]
(let [current-public-key (multiaccounts.model/current-public-key cofx)]
{:shh/send-direct-message
[{:web3 web3
:src current-public-key
[{:src current-public-key
:dst current-public-key
:payload payload}]}))

View File

@ -37,7 +37,7 @@
(fx/defn init-whisper
"Initialises whisper protocol by:
- (optionally) initializing mailserver"
[{:keys [db web3] :as cofx}]
[cofx]
(fx/merge cofx
(fetch-node-info-fx)
(pairing/init)

View File

@ -13,13 +13,8 @@
[status-im.transport.utils :as transport.utils]
[status-im.utils.config :as config]
[status-im.utils.fx :as fx]
[taoensso.timbre :as log]))
(defn confirm-messages [confirmations]
(json-rpc/call {:method "shhext_confirmMessagesProcessedByID"
:params [confirmations]
:on-success #(log/debug "successfully confirmed messages")
:on-failure #(log/error "failed to confirm messages" %)}))
[taoensso.timbre :as log]
[status-im.ethereum.json-rpc :as json-rpc]))
(defn add-raw-payload
"Add raw payload for id calculation"
@ -218,4 +213,7 @@
:transport/confirm-messages-processed
(fn [confirmations]
(when (seq confirmations)
(confirm-messages confirmations))))
(json-rpc/call {:method "shhext_confirmMessagesProcessedByID"
:params [confirmations]
:on-success #(log/debug "successfully confirmed messages")
:on-failure #(log/error "failed to confirm messages" %)}))))

View File

@ -49,32 +49,27 @@
(defn send-public-message
"Sends the payload to topic"
[{:keys [db] :as cofx} chat-id success-event payload]
(let [{:keys [web3]} db]
{:shh/send-public-message [{:web3 web3
:success-event success-event
:src (multiaccounts.model/current-public-key cofx)
:chat chat-id
:payload payload}]}))
[cofx chat-id success-event payload]
{:shh/send-public-message [{:success-event success-event
:src (multiaccounts.model/current-public-key cofx)
:chat chat-id
:payload payload}]})
(fx/defn send-direct-message
"Sends the payload using to dst"
[{:keys [db] :as cofx} dst success-event payload]
(let [{:keys [web3]} db]
{:shh/send-direct-message [{:web3 web3
:success-event success-event
:src (multiaccounts.model/current-public-key cofx)
:dst dst
:payload payload}]}))
[cofx dst success-event payload]
{:shh/send-direct-message [{:success-event success-event
:src (multiaccounts.model/current-public-key cofx)
:dst dst
:payload payload}]})
(fx/defn send-with-pubkey
"Sends the payload using asymetric key (multiaccount `:public-key` in db) and fixed discovery topic"
[{:keys [db] :as cofx} {:keys [payload chat-id success-event]}]
(let [{:keys [web3]} db]
(send-direct-message cofx
chat-id
success-event
payload)))
[cofx {:keys [payload chat-id success-event]}]
(send-direct-message cofx
chat-id
success-event
payload))
(defrecord Message [content content-type message-type clock-value timestamp]
StatusMessage

View File

@ -4,115 +4,106 @@
[status-im.ethereum.core :as ethereum]
[status-im.transport.message.transit :as transit]
[status-im.transport.utils :as transport.utils]
[taoensso.timbre :as log]))
[taoensso.timbre :as log]
[status-im.ethereum.json-rpc :as json-rpc]))
(defn generate-sym-key-from-password
[{:keys [web3 password on-success on-error]}]
(.. web3
-shh
(generateSymKeyFromPassword password (fn [err resp]
(if-not err
(on-success resp)
(on-error err))))))
[{:keys [password on-success on-error]}]
(json-rpc/call {:method "shh_generateSymKeyFromPassword"
:params [password]
:on-success on-success
:on-error on-error}))
(defn post-message
[{:keys [web3 whisper-message on-success on-error]}]
(.. web3
-shh
(extPost (clj->js whisper-message) (fn [err resp]
(if-not err
(on-success resp)
(on-error err))))))
[{:keys [whisper-message on-success on-error]}]
(json-rpc/call {:method "shh_generateSymKeyFromPassword"
:params [whisper-message]
:on-success on-success
:on-error on-error}))
(defn handle-response [success-event error-event messages-count]
(fn [err resp]
(if-not err
(if success-event
(re-frame/dispatch (conj success-event resp messages-count))
(log/debug :shh/post-success))
(re-frame/dispatch [error-event err resp]))))
(defn send-direct-message! [web3 direct-message success-event error-event count]
(.. web3
-shh
(sendDirectMessage
(clj->js (update direct-message :payload (comp ethereum/utf8-to-hex
transit/serialize)))
(handle-response success-event error-event count))))
(defn send-direct-message!
[direct-message success-event error-event messages-count]
(json-rpc/call {:method "shhext_sendDirectMessage"
:params [(update direct-message :payload (comp ethereum/utf8-to-hex
transit/serialize))]
:on-success #(if success-event
(re-frame/dispatch (conj success-event % messages-count))
(log/debug :shh/post-success))
:on-error #(re-frame/dispatch [error-event %])}))
(re-frame/reg-fx
:shh/send-direct-message
(fn [post-calls]
(doseq [{:keys [web3 payload src dst success-event error-event]
(doseq [{:keys [payload src dst success-event error-event]
:or {error-event :transport/send-status-message-error}} post-calls]
(let [direct-message {:pubKey dst
:sig src
:payload payload}]
(send-direct-message! web3 direct-message success-event error-event 1)))))
(send-direct-message! direct-message success-event error-event 1)))))
(re-frame/reg-fx
:shh/send-pairing-message
(fn [params]
(let [{:keys [web3 payload src success-event error-event]
:or {error-event :transport/send-status-message-error}} params
message (clj->js {:sig src
:pubKey src
;; Send to any device
:DH true
:payload (-> payload
transit/serialize
ethereum/utf8-to-hex)})]
(.. web3
-shh
(sendDirectMessage
message
(handle-response success-event error-event 1))))))
(let [{:keys [payload src success-event error-event]
:or {error-event :transport/send-status-message-error}} params]
(json-rpc/call {:method "shhext_sendDirectMessage"
:params [{:sig src
:pubKey src
;; Send to any device
:DH true
:payload (-> payload
transit/serialize
ethereum/utf8-to-hex)}]
:on-success #(if success-event
(re-frame/dispatch (conj success-event % 1))
(log/debug :shh/post-success))
:on-error #(re-frame/dispatch [error-event %])}))))
(re-frame/reg-fx
:shh/send-group-message
(fn [params]
(let [{:keys [web3 payload src dsts success-event error-event]
:or {error-event :transport/send-status-message-error}} params]
(let [{:keys [payload src dsts success-event error-event]
:or {error-event :transport/send-status-message-error}} params
payload (-> payload
transit/serialize
ethereum/utf8-to-hex)]
(doseq [{:keys [public-key chat]} dsts]
(let [message
(clj->js {:pubKey public-key
:sig src
:payload (-> payload
transit/serialize
ethereum/utf8-to-hex)})]
(let [message {:pubKey public-key
:sig src
:payload payload}]
(json-rpc/call {:method "shhext_sendDirectMessage"
:params [message]
:on-success #(if success-event
(re-frame/dispatch (conj success-event % (count dsts)))
(log/debug :shh/post-success))
:on-error #(re-frame/dispatch [error-event %])}))))))
(.. web3
-shh
(sendDirectMessage
message
(handle-response success-event error-event (count dsts)))))))))
(defn send-public-message! [web3 message success-event error-event]
(.. web3
-shh
(sendPublicMessage
(clj->js message)
(handle-response success-event error-event 1))))
(defn send-public-message! [message success-event error-event]
(json-rpc/call {:method "shhext_sendPublicMessage"
:params [message]
:on-success #(if success-event
(re-frame/dispatch (conj success-event % 1))
(log/debug :shh/post-success))
:on-error #(re-frame/dispatch [error-event %])}))
(re-frame/reg-fx
:shh/send-public-message
(fn [post-calls]
(doseq [{:keys [web3 payload src chat success-event error-event]
(doseq [{:keys [payload src chat success-event error-event]
:or {error-event :transport/send-status-message-error}} post-calls]
(let [message {:chat chat
:sig src
:payload (-> payload
transit/serialize
ethereum/utf8-to-hex)}]
(send-public-message! web3 message success-event error-event)))))
(send-public-message! message success-event error-event)))))
(re-frame/reg-fx
:shh/post
(fn [post-calls]
(doseq [{:keys [web3 message success-event error-event]
(doseq [{:keys [message success-event error-event]
:or {error-event :transport/send-status-message-error}} post-calls]
(post-message {:web3 web3
:whisper-message (update message :payload (comp ethereum/utf8-to-hex
(post-message {:whisper-message (update message :payload (comp ethereum/utf8-to-hex
transit/serialize))
:on-success (if success-event
#(re-frame/dispatch (conj success-event % 1))
@ -120,13 +111,11 @@
:on-error #(re-frame/dispatch [error-event %])}))))
(defn get-sym-key
[{:keys [web3 sym-key-id on-success on-error]}]
(.. web3
-shh
(getSymKey sym-key-id (fn [err resp]
(if-not err
(on-success resp)
(on-error err))))))
[{:keys [sym-key-id on-success on-error]}]
(json-rpc/call {:method "shh_getSymKey"
:params [sym-key-id]
:on-success on-success
:on-error on-error}))
(defn log-error [error]
(log/error :shh/get-new-sym-key-error error))
@ -134,12 +123,10 @@
(re-frame/reg-fx
:shh/generate-sym-key-from-password
(fn [args]
(doseq [{:keys [web3 password on-success]} args]
(generate-sym-key-from-password {:web3 web3
:password password
(doseq [{:keys [password on-success]} args]
(generate-sym-key-from-password {:password password
:on-success (fn [sym-key-id]
(get-sym-key {:web3 web3
:sym-key-id sym-key-id
(get-sym-key {:sym-key-id sym-key-id
:on-success (fn [sym-key]
(on-success sym-key sym-key-id))
:on-error log-error}))

View File

@ -20,9 +20,6 @@
[chat-id]
(subs (ethereum/sha3 chat-id) 0 10))
(defn shh [web3]
(.-shh web3))
(defn extract-enode-id [enode]
(-> enode
(string/split #"/")

View File

@ -10,14 +10,13 @@
(def sync-interval-ms 120000)
(def sync-timeout-ms 20000)
(defn- start-publisher! [web3]
(defn- start-publisher! []
(when @polling-executor
(async-util/async-periodic-stop! @polling-executor))
(reset! polling-executor
(async-util/async-periodic-exec
(fn [done-fn]
(let [cofx {:web3 web3
:now (datetime/timestamp)
(let [cofx {:now (datetime/timestamp)
:db @re-frame.db/app-db}]
(multiaccounts/publish-update! cofx)
(done-fn)))
@ -26,15 +25,17 @@
(re-frame/reg-fx
::start-publisher
#(start-publisher! %))
start-publisher!)
(re-frame/reg-fx
::stop-publisher
#(when @polling-executor
(async-util/async-periodic-stop! @polling-executor)))
(fx/defn start-fx [{:keys [web3]}]
{::start-publisher web3})
(fx/defn start-fx
[cofx]
{::start-publisher nil})
(fx/defn stop-fx [cofx]
(fx/defn stop-fx
[cofx]
{::stop-publisher []})

View File

@ -1,40 +0,0 @@
(ns status-im.web3.core
(:require [status-im.ethereum.core :as ethereum]
[status-im.js-dependencies :as dependencies]
[status-im.native-module.core :as status]
[taoensso.timbre :as log]
[status-im.data-store.core :as data-store]
[re-frame.core :as re-frame]
[status-im.utils.handlers :as handlers]
[status-im.utils.fx :as fx]))
(defn make-internal-web3
"This Web3 object will allow access to private RPC calls
It should be only used for internal application needs and never provided to any
3rd parties (DApps, etc)"
[]
(let [Web3 (dependencies/Web3)]
(Web3.
#js {:sendAsync (fn [payload callback]
(status/call-private-rpc
(.stringify js/JSON payload)
(fn [response]
(if (= "" response)
(log/warn :web3-response-error)
(callback nil (.parse js/JSON response))))))})))
(fx/defn initialization-success
{:events [::initialization-success]}
[{:keys [db]} web3 address password login?]
{:db (assoc db :web3 web3)
(if login?
::data-store/change-multiaccount
::data-store/create-multiaccount) [address password]})
(re-frame/reg-fx
::initialize
(fn [[address password login?]]
(let [web3 (make-internal-web3)]
(set! (.-defaultAccount (.-eth web3))
(ethereum/normalized-address address))
(re-frame/dispatch [::initialization-success web3 address password login?]))))

View File

@ -29,9 +29,9 @@
(deftest test-send-message!
(testing "send contact request & message"
(run-test-async
(let [web3 (make-web3)
shh (transport.utils/shh web3)
from (create-keys shh)]
(let [web3 (make-web3)
shh (.-shh web3)
from (create-keys shh)]
(reset! re-frame.db/app-db {:web3 web3
:multiaccount {:public-key from}})
@ -52,7 +52,7 @@
(testing "Whisper version supported"
(async done
(let [web3 (make-web3)
shh (transport.utils/shh web3)]
shh (.-shh web3)]
(.version shh
(fn [& args]
(is (= "6.0" (second args)))

View File

@ -7,7 +7,6 @@
[status-im.events :as events]
[status-im.data-store.core :as data-store]
[status-im.multiaccounts.login.core :as login.core]
[status-im.web3.core :as web3]
[status-im.signals.core :as signals]
[status-im.test.sign-in.data :as data]))
@ -42,7 +41,7 @@
(testing ":accounts/login cleared."
(is (not (contains? new-db :multiaccounts/login))))
(testing "Check the rest of effects."
(is (contains? efx ::web3/initialize))
(is (contains? efx ::data-store/change-multiaccount))
(is (json-rpc "web3_clientVersion"))))))
;;TODO re-enable when keycard is fixed