diff --git a/STATUS_GO_SHA256 b/STATUS_GO_SHA256 index 9484c62978cc..bb09a335f82b 100644 --- a/STATUS_GO_SHA256 +++ b/STATUS_GO_SHA256 @@ -1,3 +1,3 @@ ## DO NOT EDIT THIS FILE BY HAND. USE `scripts/update-status-go.sh ` instead -0049i6znvl45hc651bqyzwgmzlv0fp40maggfjsrv13q5avd0g6d +0kxiw130lf4yf9kk0zcs60k58ycd88khwqy1wqvlskj0yqsbmy8k diff --git a/STATUS_GO_VERSION b/STATUS_GO_VERSION index 6162c3d599eb..daad1495b4da 100644 --- a/STATUS_GO_VERSION +++ b/STATUS_GO_VERSION @@ -1,3 +1,3 @@ ## DO NOT EDIT THIS FILE BY HAND. USE `scripts/update-status-go.sh ` instead -v0.25.0-beta.1 +v0.26.0-alpha.38 diff --git a/env/dev/env/config.cljs b/env/dev/env/config.cljs index af77efd8127b..ef57eb3c3fa6 100644 --- a/env/dev/env/config.cljs +++ b/env/dev/env/config.cljs @@ -1,6 +1,6 @@ (ns env.config) -(def figwheel-urls {:android "ws://localhost:3449/figwheel-ws", - :ios "ws://192.168.0.9:3449/figwheel-ws", +(def figwheel-urls {:android "ws://10.0.3.2:3449/figwheel-ws", + :ios "ws://192.168.56.1:3449/figwheel-ws", :desktop "ws://localhost:3449/figwheel-ws"} ) \ No newline at end of file diff --git a/mobile_files/package.json.orig b/mobile_files/package.json.orig index 84704bd15272..f984817669b1 100644 --- a/mobile_files/package.json.orig +++ b/mobile_files/package.json.orig @@ -70,7 +70,7 @@ "string_decoder": "0.10.31", "text-encoding": "^0.6.4", "url": "0.10.3", - "web3": "git+https://github.com/status-im/web3.js.git#0.20.2-status", + "web3": "git+https://github.com/status-im/web3.js.git#0.20.3-status.alpha.3", "web3-utils": "1.0.0-beta.36" } } diff --git a/mobile_files/yarn.lock b/mobile_files/yarn.lock index 8a7430c1ef05..8a0d436e117d 100644 --- a/mobile_files/yarn.lock +++ b/mobile_files/yarn.lock @@ -7081,9 +7081,9 @@ web3-utils@1.0.0-beta.36: underscore "1.8.3" utf8 "2.1.1" -"web3@git+https://github.com/status-im/web3.js.git#0.20.2-status": +"web3@git+https://github.com/status-im/web3.js.git#0.20.3-status.alpha.3": version "0.20.1" - resolved "git+https://github.com/status-im/web3.js.git#958dbabff2c77615e23f5de678a6fae0b0d70147" + resolved "git+https://github.com/status-im/web3.js.git#29677754c8b87c55c828df2694e35b0b760dca5f" dependencies: bignumber.js "git+https://github.com/status-im/bignumber.js.git#v4.0.2-status" crypto-js "^3.1.4" diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java index d37453e180f7..fc943ae579cc 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java @@ -1133,4 +1133,64 @@ Map getConstants() { public void isDeviceRooted(final Callback callback) { callback.invoke(rootedDevice); } + + @ReactMethod + public void loadFilters(final String chats, final Callback callback) { + Log.d(TAG, "loadFilters"); + if (!checkAvailability()) { + callback.invoke(false); + return; + } + + Runnable r = new Runnable() { + @Override + public void run() { + String res = Statusgo.loadFilters(chats); + + callback.invoke(res); + } + }; + + StatusThreadPoolExecutor.getInstance().execute(r); + } + + @ReactMethod + public void loadFilter(final String chat, final Callback callback) { + Log.d(TAG, "loadFilter"); + if (!checkAvailability()) { + callback.invoke(false); + return; + } + + Runnable r = new Runnable() { + @Override + public void run() { + String res = Statusgo.loadFilter(chat); + + callback.invoke(res); + } + }; + + StatusThreadPoolExecutor.getInstance().execute(r); + } + + @ReactMethod + public void removeFilter(final String chat, final Callback callback) { + Log.d(TAG, "removeFilter"); + if (!checkAvailability()) { + callback.invoke(false); + return; + } + + Runnable r = new Runnable() { + @Override + public void run() { + String res = Statusgo.removeFilter(chat); + + callback.invoke(res); + } + }; + + StatusThreadPoolExecutor.getInstance().execute(r); + } } diff --git a/src/status_im/chat/models.cljs b/src/status_im/chat/models.cljs index d86dfa5067f5..c8d547c6688c 100644 --- a/src/status_im/chat/models.cljs +++ b/src/status_im/chat/models.cljs @@ -169,15 +169,7 @@ (log/error "can't remove a chat:" error))}]} (navigation/navigate-to-cofx :home {})) (fx/merge cofx - ;; TODO: There's a race condition here, as the removal of the filter (async) - ;; is done at the same time as the removal of the chat, so a message - ;; might come between and restore the chat. Multiple way to handle this - ;; (remove chat only after the filter has been removed, probably the safest, - ;; flag the chat to ignore new messages, change receive method for public/group chats) - ;; For now to keep the code simplier and avoid significant changes, best to leave as it is. #(when (public-chat? % chat-id) - (transport.chat/unsubscribe-from-chat % chat-id)) - #(when (group-chat? % chat-id) (mailserver/remove-chat-from-mailserver-topic % chat-id)) (mailserver/remove-gaps chat-id) (mailserver/remove-range chat-id) @@ -185,6 +177,8 @@ (clear-history chat-id) #(when (one-to-one-chat? % chat-id) (contact-code/stop-listening % chat-id)) + #(when (public-chat? % chat-id) + (transport.chat/unsubscribe-from-chat % chat-id)) (navigation/navigate-to-cofx :home {})))) (defn- unread-messages-number [chats] diff --git a/src/status_im/contact/core.cljs b/src/status_im/contact/core.cljs index fb445ec9f426..03edca81f079 100644 --- a/src/status_im/contact/core.cljs +++ b/src/status_im/contact/core.cljs @@ -87,24 +87,6 @@ {:db (assoc-in db [:contacts/new-identity] "")} (upsert-contact contact))))) -(fx/defn add-contacts-filter [{:keys [db]} public-key action] - (when (not= (get-in db [:account/account :public-key]) public-key) - (let [current-public-key (get-in db [:account/account :public-key])] - {:db - (cond-> db - config/partitioned-topic-enabled? - (assoc :filters/after-adding-discovery-filter - {:action action - :public-key public-key})) - - :shh/add-discovery-filters - {:web3 (:web3 db) - :private-key-id current-public-key - :topics [{:topic (transport.topic/partitioned-topic-hash public-key) - :chat-id public-key - :minPow 1 - :callback (constantly nil)}]}}))) - (defn handle-contact-update [public-key timestamp diff --git a/src/status_im/contact_code/core.cljs b/src/status_im/contact_code/core.cljs index f5c438762d80..9b07745ec826 100644 --- a/src/status_im/contact_code/core.cljs +++ b/src/status_im/contact_code/core.cljs @@ -17,9 +17,9 @@ (str pk "-contact-code")) (fx/defn listen [cofx chat-id] - (transport.public-chat/join-public-chat + (transport.public-chat/join-one-to-one-chat cofx - (topic chat-id))) + chat-id)) (fx/defn listen-to-chat "For a one-to-one chat, listen to the pk of the user, for a group chat @@ -47,52 +47,12 @@ (and is-active (contains? members-joined my-public-key) (contains? members their-public-key))) - (vals (:chats db))) - their-topic (topic their-public-key)] + (vals (:chats db)))] (when (and (not (contact.db/active? db their-public-key)) (not= my-public-key their-public-key) (not (get-in db [:chats their-public-key :is-active])) (empty? active-group-chats)) (fx/merge cofx - (mailserver/remove-gaps their-topic) - (mailserver/remove-range their-topic) - (transport.chat/unsubscribe-from-chat their-topic))))) + (transport.chat/unsubscribe-from-chat their-public-key))))) -;; Publish contact code every 12hrs -(def publish-contact-code-interval (* 12 60 60 1000)) - -(fx/defn init [cofx] - (log/debug "initializing contact-code") - (let [current-public-key (accounts.db/current-public-key cofx)] - (listen cofx current-public-key))) - -(defn publish! [{:keys [web3 now] :as cofx}] - (let [current-public-key (accounts.db/current-public-key cofx) - chat-id (topic current-public-key) - peers-count (:peers-count @re-frame.db/app-db) - last-published (get-in - @re-frame.db/app-db - [:account/account :last-published-contact-code])] - (when (and (pos? peers-count) - (< publish-contact-code-interval - (- now last-published))) - - (let [message {:chat chat-id - :sig current-public-key - :payload ""}] - (shh/send-public-message! - web3 - message - [:contact-code.callback/contact-code-published] - :contact-code.callback/contact-code-publishing-failed))))) - -(fx/defn published [{:keys [now db] :as cofx}] - (let [new-account (assoc (:account/account db) - :last-published-contact-code - now)] - {:db (assoc db :account/account new-account) - :data-store/base-tx [(data-store.accounts/save-account-tx new-account)]})) - -(fx/defn publishing-failed [cofx] - (log/warn "failed to publish contact-code")) diff --git a/src/status_im/data_store/realm/schemas/account/transport.cljs b/src/status_im/data_store/realm/schemas/account/transport.cljs index da7cda00d1d8..06b9964b845a 100644 --- a/src/status_im/data_store/realm/schemas/account/transport.cljs +++ b/src/status_im/data_store/realm/schemas/account/transport.cljs @@ -72,3 +72,4 @@ (def v9 (update v8 :properties dissoc :ack :seen :pending-ack :pending-send)) +(def v10 (update v9 :properties dissoc :sym-key-id :sym-key)) diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index c552dd378a80..522866152a81 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -17,7 +17,6 @@ [status-im.chat.models.input :as chat.input] [status-im.chat.models.loading :as chat.loading] [status-im.chat.models.message :as chat.message] - [status-im.contact-code.core :as contact-code] [status-im.contact-recovery.core :as contact-recovery] [status-im.contact.block :as contact.block] [status-im.contact.core :as contact] @@ -1712,9 +1711,7 @@ :contact.ui/add-to-contact-pressed [(re-frame/inject-cofx :random-id-generator)] (fn [cofx [_ public-key]] - (if config/partitioned-topic-enabled? - (contact/add-contacts-filter cofx public-key :add-contact) - (contact/add-contact cofx public-key)))) + (contact/add-contact cofx public-key))) (handlers/register-handler-fx :contact.ui/block-contact-pressed @@ -1745,9 +1742,7 @@ :on-dismiss #(re-frame/dispatch [:navigate-to-clean :home])}} (fx/merge cofx fx - (if config/partitioned-topic-enabled? - (contact/add-contacts-filter contact-identity :open-chat) - (chat/start-chat contact-identity {:navigation-reset? true}))))))) + (chat/start-chat contact-identity {:navigation-reset? true})))))) (handlers/register-handler-fx :contact/filters-added @@ -1800,16 +1795,6 @@ (fn [cofx [_ initial-props]] {:db (assoc (:db cofx) :initial-props initial-props)})) -(handlers/register-handler-fx - :contact-code.callback/contact-code-published - (fn [cofx arg] - (contact-code/published cofx))) - -(handlers/register-handler-fx - :contact-code.callback/contact-code-publishing-failed - (fn [cofx _] - (contact-code/publishing-failed cofx))) - (handlers/register-handler-fx :pairing.ui/enable-installation-pressed (fn [cofx [_ installation-id]] diff --git a/src/status_im/mailserver/core.cljs b/src/status_im/mailserver/core.cljs index aadff3eba1c8..575e250f9c9c 100644 --- a/src/status_im/mailserver/core.cljs +++ b/src/status_im/mailserver/core.cljs @@ -34,7 +34,8 @@ ;; as soon as the mailserver becomes available -(def one-day (* 24 3600)) +(def one-hour 3600) +(def one-day (* 24 one-hour)) (def seven-days (* 7 one-day)) (def max-gaps-range (* 30 one-day)) (def max-request-range one-day) @@ -406,7 +407,6 @@ [{:keys [db now] :as cofx}] (when (and (mobile-network-utils/syncing-allowed? cofx) - (transport.db/all-filters-added? cofx) (not (:mailserver/current-request db))) (when-let [mailserver (get-mailserver-when-ready cofx)] (let [request-to (or (:mailserver/request-to db) @@ -517,11 +517,14 @@ [{:keys [db] :as cofx} chat-id] (let [{:keys [public?] :as chat} (get-in db [:chats chat-id]) topic (if (and chat (not public?)) + ;; TODO: look for all the topics for this chat and remove them (transport.topic/discovery-topic-hash) (get-in db [:transport/chats chat-id :topic])) {:keys [chat-ids] :as mailserver-topic} (update (get-in db [:mailserver/topics topic]) :chat-ids disj chat-id)] + + (log/info "mailserver: removing topic: " topic " chat-id: " chat-id " chat-ids: " chat-ids) (if (empty? chat-ids) (fx/merge cofx {:db (update db :mailserver/topics dissoc topic) diff --git a/src/status_im/native_module/core.cljs b/src/status_im/native_module/core.cljs index 0f09fa09f14b..256dc7f2f847 100644 --- a/src/status_im/native_module/core.cljs +++ b/src/status_im/native_module/core.cljs @@ -103,6 +103,12 @@ (def enable-installation native-module/enable-installation) +(def load-filters native-module/load-filters) + +(def load-filter native-module/load-filter) + +(def remove-filter native-module/remove-filter) + (def disable-installation native-module/disable-installation) (def update-mailservers native-module/update-mailservers) diff --git a/src/status_im/native_module/impl/module.cljs b/src/status_im/native_module/impl/module.cljs index 5d505949d16e..f0a1a39c5b9e 100644 --- a/src/status_im/native_module/impl/module.cljs +++ b/src/status_im/native_module/impl/module.cljs @@ -152,6 +152,30 @@ (when (status) (.disableInstallation (status) installation-id callback))) +(defn load-filters [chats callback] + (let [params {:jsonrpc "2.0" + :id 2 + :method "shhext_loadFilters" + :params [chats]} + payload (.stringify js/JSON (clj->js params))] + (call-private-rpc payload callback))) + +(defn load-filter [chat callback] + (let [params {:jsonrpc "2.0" + :id 2 + :method "shhext_loadFilter" + :params [chat]} + payload (.stringify js/JSON (clj->js params))] + (call-private-rpc payload callback))) + +(defn remove-filter [chat callback] + (let [params {:jsonrpc "2.0" + :id 2 + :method "shhext_removeFilter" + :params [chat]} + payload (.stringify js/JSON (clj->js params))] + (call-private-rpc payload callback))) + (defn is24Hour [] (when (status) (.-is24Hour (status)))) diff --git a/src/status_im/signals/core.cljs b/src/status_im/signals/core.cljs index b5d2acc95e61..aea546ea03fe 100644 --- a/src/status_im/signals/core.cljs +++ b/src/status_im/signals/core.cljs @@ -9,6 +9,7 @@ [status-im.node.core :as node] [status-im.pairing.core :as pairing] [status-im.transport.message.core :as transport.message] + [status-im.transport.filters :as transport.filters] [status-im.utils.fx :as fx] [status-im.utils.security :as security] [status-im.utils.types :as types] @@ -77,4 +78,11 @@ "subscriptions.data" (ethereum.subscriptions/handle-signal cofx event) "subscriptions.error" (ethereum.subscriptions/handle-error cofx event) "status.chats.did-change" (chat.loading/load-chats-from-rpc cofx) + "whisper.filter.added" {:shh/add-raw-filters + {:web3 (get-in cofx [:db :web3]) + :filters (map #(hash-map :sym-key-id (:symKeyId %) + :chat-id (str "0x" (:identity %)) + :topic (:topic %) + :filter-id (:filterId %)) + (:filters event))}} (log/debug "Event " type " not handled")))) diff --git a/src/status_im/transport/core.cljs b/src/status_im/transport/core.cljs index 6d2b59c52b59..a89eb4ed4912 100644 --- a/src/status_im/transport/core.cljs +++ b/src/status_im/transport/core.cljs @@ -1,33 +1,18 @@ (ns ^{:doc "API to init and stop whisper messaging"} status-im.transport.core - (:require status-im.transport.filters - [re-frame.core :as re-frame] - [status-im.native-module.core :as status] - [status-im.mailserver.core :as mailserver] - [status-im.transport.message.core :as message] - [status-im.transport.partitioned-topic :as transport.topic] - [status-im.contact-code.core :as contact-code] - [status-im.utils.publisher :as publisher] - [status-im.utils.fx :as fx] - [status-im.utils.handlers :as handlers] - [taoensso.timbre :as log] - status-im.transport.shh - [status-im.utils.config :as config])) - -(defn get-public-key-topics [chats] - (keep (fn [[chat-id {:keys [topic sym-key one-to-one]}]] - (cond (and (not sym-key) topic) - {:topic topic - :chat-id chat-id} - - ;; we have to listen the topic to which we are going to send - ;; a message, otherwise the message will not match bloom - (and config/partitioned-topic-enabled? one-to-one) - {:topic (transport.topic/partitioned-topic-hash chat-id) - :chat-id chat-id - :minPow 1 - :callback (constantly nil)})) - chats)) + (:require + [re-frame.core :as re-frame] + [status-im.transport.filters :as transport.filters] + [status-im.native-module.core :as status] + [status-im.mailserver.core :as mailserver] + [status-im.transport.message.core :as message] + [status-im.transport.partitioned-topic :as transport.topic] + [status-im.utils.publisher :as publisher] + [status-im.utils.fx :as fx] + [status-im.utils.handlers :as handlers] + [taoensso.timbre :as log] + status-im.transport.shh + [status-im.utils.config :as config])) (defn set-node-info [{:keys [db]} node-info] {:db (assoc db :node-info node-info)}) @@ -46,77 +31,46 @@ (fn [] (fetch-node-info))) +(defn to-filter [{:keys [oneToOne filterId chatId symKeyId topic identity]}] + {:chat-id (if (not= identity "") (str "0x" identity) chatId) + :filter-id filterId + :sym-key-id symKeyId + :topic topic}) + +(defn process-filters [web3 filters] + (println filters) + (transport.filters/process-raw-filters + web3 + (map to-filter filters))) + +(re-frame/reg-fx + :filters/load-filters + (fn [[web3 chats]] + (println "LOADING" chats) + (status/load-filters + chats + (handlers/response-handler (partial process-filters web3) + #(log/error "load-filters: failed error" %))))) + +(defn chat->filter [{:keys [group-chat chat-id]}] + {:ChatID chat-id + :OneToOne (not group-chat) + :Identity (when (not group-chat) (subs chat-id 2))}) + +(defn chats->filters [chats] + (map chat->filter chats)) + (fx/defn init-whisper "Initialises whisper protocol by: - adding fixed shh discovery filter - restoring existing symetric keys along with their unique filters - (optionally) initializing mailserver" [{:keys [db web3] :as cofx}] - (when-let [public-key (get-in db [:account/account :public-key])] - (let [public-key-topics (get-public-key-topics (:transport/chats db)) - discovery-topics (transport.topic/discovery-topics public-key)] - (fx/merge cofx - {:shh/add-discovery-filters - {:web3 web3 - :private-key-id public-key - :topics (concat public-key-topics - (map - (fn [discovery-topic] - {:topic discovery-topic - :chat-id :discovery-topic}) - discovery-topics))} - - ::fetch-node-info [] - :shh/restore-sym-keys-batch - {:web3 web3 - :transport (keep (fn [[chat-id {:keys [topic sym-key] - :as chat}]] - (when (and topic sym-key) - (assoc chat :chat-id chat-id))) - (:transport/chats db)) - :on-success #(re-frame/dispatch [::sym-keys-added %])}} - (publisher/start-fx) - (contact-code/init) - (mailserver/connect-to-mailserver) - (message/resend-contact-messages []))))) - -;;TODO (yenda) remove once go implements persistence -;;Since symkeys are not persisted, we restore them via add sym-keys, -;;this is the callback that is called when a key has been restored for a particular chat. -;;it saves the sym-key-id in app-db to send messages later -;;and starts a filter to receive messages -(handlers/register-handler-fx - ::sym-keys-added - (fn [{:keys [db]} [_ keys]] - (log/debug "PERF" ::sym-keys-added (count keys)) - (let [web3 (:web3 db) - chats (:transport/chats db) - {:keys [updated-chats filters]} - (reduce - (fn [{:keys [updated-chats filters]} chat] - (let [{:keys [chat-id sym-key-id]} chat - {:keys [topic one-to-one]} (get updated-chats chat-id)] - {:updated-chats (assoc-in updated-chats - [chat-id :sym-key-id] sym-key-id) - :filters (conj filters {:sym-key-id sym-key-id - :topic topic - :chat-id chat-id - :one-to-one one-to-one})})) - {:updated-chats chats - :filters []} - keys)] - {:db (assoc db :transport/chats updated-chats) - :shh/add-filters {:web3 web3 - :filters filters}}))) - -;;TODO (yenda) uncomment and rework once go implements persistence -#_(doseq [[chat-id {:keys [sym-key-id topic] :as chat}] transport] - (when sym-key-id - (filters/add-filter! web3 - {:symKeyID sym-key-id - :topics [topic]} - (fn [js-error js-message] - (re-frame/dispatch [:protocol/receive-whisper-message js-error js-message chat-id]))))) + (fx/merge cofx + {:filters/load-filters [web3 (chats->filters (vals (get-in cofx [:db :chats])))]} + (publisher/start-fx) + (mailserver/connect-to-mailserver) + (message/resend-contact-messages []))) (fx/defn stop-whisper "Stops whisper protocol by removing all existing shh filters diff --git a/src/status_im/transport/db.cljs b/src/status_im/transport/db.cljs index 5589bfd9a214..9807279da6ae 100644 --- a/src/status_im/transport/db.cljs +++ b/src/status_im/transport/db.cljs @@ -126,15 +126,3 @@ (spec/keys :req-un [:message.text/content]))) (spec/def :message/message (spec/multi-spec content-type :content-type)) - -(defn all-filters-added? - [{:keys [db]}] - (let [filters (set (keys (get db :transport/filters))) - chats (into #{:discovery-topic} - (keys (filter (fn [[chat-id {:keys [topic one-to-one]}]] - (if one-to-one - (and config/partitioned-topic-enabled? - chat-id) - topic)) - (get db :transport/chats))))] - (empty? (sets/difference chats filters)))) diff --git a/src/status_im/transport/filters.cljs b/src/status_im/transport/filters.cljs index 55bce5091781..2bae2c97a152 100644 --- a/src/status_im/transport/filters.cljs +++ b/src/status_im/transport/filters.cljs @@ -22,14 +22,14 @@ (re-frame/dispatch [:shh.callback/filter-removed chat-id]))))) (log/debug :stop-watching filter)) -(defn add-filters! +(defn add-raw-filters! [web3 filters] - (log/debug "PERF" :add-filters (first filters)) + (log/debug "PERF" :add-raw-filters filters) (re-frame/dispatch [:shh.callback/filters-added (keep (fn [{:keys [options callback chat-id]}] - (when-let [filter (.newMessageFilter + (when-let [filter (.newRawMessageFilter (utils/shh web3) (clj->js (assoc options :allowP2P true)) callback @@ -40,36 +40,29 @@ :filter filter})) filters)])) -(re-frame/reg-fx - :shh/add-filters - (fn [{:keys [web3 filters]}] - (log/debug "PERF" :shh/add-filters) - (let [filters - (reduce - (fn [acc {:keys [sym-key-id topic chat-id]}] - (conj acc - {:options {:topics [topic] - :symKeyID sym-key-id} - :callback (partial receive-message chat-id) - :chat-id chat-id})) - [] - filters)] - (add-filters! web3 filters)))) +(defn process-raw-filters [web3 raw-filters] + (log/debug "processing filters" web3 raw-filters) + (let [filters + (reduce + (fn [acc {:keys [sym-key-id + topic + filter-id + chat-id]}] + (conj acc + {:options {:topics [topic] + :filterId filter-id + :symKeyID sym-key-id} + :callback (partial receive-message chat-id) + :chat-id chat-id})) + [] + raw-filters)] + (add-raw-filters! web3 filters))) (re-frame/reg-fx - :shh/add-discovery-filters - (fn [{:keys [web3 private-key-id topics]}] - (let [params {:privateKeyID private-key-id}] - (add-filters! - web3 - (map (fn [{:keys [chat-id topic callback minPow]}] - {:options (cond-> (assoc params :topics [topic]) - minPow - (assoc :minPow minPow)) - ;; We don't pass a chat id on discovery-filters as we might receive - ;; messages for multiple chats - :callback (or callback (partial receive-message nil)) - :chat-id chat-id}) topics))))) + :shh/add-raw-filters + (fn [{:keys [web3 filters]}] + (log/debug "PERF" :shh/add-raw-filters) + (process-raw-filters web3 filters))) (fx/defn add-filter [{:keys [db]} chat-id filter] diff --git a/src/status_im/transport/message/contact.cljs b/src/status_im/transport/message/contact.cljs index c9ce2920591f..e32ce4d4ce6c 100644 --- a/src/status_im/transport/message/contact.cljs +++ b/src/status_im/transport/message/contact.cljs @@ -21,11 +21,3 @@ (validate [this] (when (spec/valid? :message/contact-update this) this))) - -(fx/defn remove-chat-filter - "Stops the filter for the given chat-id" - [{:keys [db]} chat-id] - (when-let [filters (get-in db [:transport/filters chat-id])] - {:shh/remove-filters - {:filters (map (fn [filter] [chat-id filter]) filters)}})) - diff --git a/src/status_im/transport/message/protocol.cljs b/src/status_im/transport/message/protocol.cljs index 73b982c5ea35..dfdc7c218822 100644 --- a/src/status_im/transport/message/protocol.cljs +++ b/src/status_im/transport/message/protocol.cljs @@ -47,21 +47,6 @@ :chat chat-id :payload payload}]})) -(fx/defn send-with-sym-key - "Sends the payload using symetric key and topic from db (looked up by `chat-id`)" - [{:keys [db] :as cofx} {:keys [payload chat-id success-event]}] - ;; we assume that the chat contains the contact public-key - (let [{:keys [web3]} db - {:keys [sym-key-id topic]} (get-in db [:transport/chats chat-id])] - {:shh/post [{:web3 web3 - :success-event success-event - :message (merge {:sig (accounts.db/current-public-key cofx) - :symKeyID sym-key-id - :payload payload - :topic (or topic - (transport.topic/public-key->discovery-topic-hash chat-id))} - whisper-opts)}]})) - (fx/defn send-direct-message "Sends the payload using to dst" [{:keys [db] :as cofx} dst success-event payload] @@ -108,11 +93,11 @@ message-type]}] (case message-type :public-group-user-message - (send-with-sym-key cofx params) + (send-public-message cofx chat-id (:success-event params) this) :user-message (fx/merge cofx - (send-direct-message current-public-key nil this) + #_(send-direct-message current-public-key nil this) (send-with-pubkey params))))) (receive [this chat-id signature timestamp cofx] (let [received-message-fx {:chat-received-message/add-fx diff --git a/src/status_im/transport/message/public_chat.cljs b/src/status_im/transport/message/public_chat.cljs index 8c21a38fe61b..ef05e1c0fc8b 100644 --- a/src/status_im/transport/message/public_chat.cljs +++ b/src/status_im/transport/message/public_chat.cljs @@ -3,6 +3,7 @@ (:require [re-frame.core :as re-frame] [status-im.data-store.transport :as transport-store] [status-im.transport.message.protocol :as protocol] + [status-im.transport.utils :as transport.utils] [status-im.utils.fx :as fx] [status-im.utils.handlers :as handlers])) @@ -10,34 +11,33 @@ (defn- has-already-joined? [{:keys [db]} chat-id] (get-in db [:transport/chats chat-id])) +(fx/defn store-transport [{:keys [db]} chat-id] + {:data-store/tx [(transport-store/save-transport-tx + {:chat-id chat-id + :chat (get-in db [:transport/chats chat-id])})]}) + +(defn chat->filter [{:keys [group-chat chat-id]}] + {:ChatID chat-id + :OneToOne (not group-chat) + :Identity (when (not group-chat) (subs chat-id 2))}) + (fx/defn join-public-chat "Function producing all protocol level effects necessary for joining public chat identified by chat-id" [{:keys [db] :as cofx} chat-id] (when-not (has-already-joined? cofx chat-id) - (let [on-success (fn [sym-key sym-key-id] - (re-frame/dispatch [::add-new-sym-key {:chat-id chat-id - :sym-key sym-key - :sym-key-id sym-key-id}]))] - (fx/merge cofx - {:shh/generate-sym-key-from-password [{:web3 (:web3 db) - :password chat-id - :on-success on-success}]} - (protocol/init-chat {:chat-id chat-id - :topic (transport.utils/get-topic chat-id)}))))) + (fx/merge cofx + {:filters/load-filters [(:web3 db) [(chat->filter {:chat-id chat-id + :group-chat true})]]} + (protocol/init-chat {:chat-id chat-id + :topic (transport.utils/get-topic chat-id)}) + (store-transport chat-id)))) -(handlers/register-handler-fx - ::add-new-sym-key - (fn [{:keys [db]} [_ {:keys [sym-key-id sym-key chat-id]}]] - (let [{:keys [web3]} db - topic (transport.utils/get-topic chat-id)] - {:db (assoc-in db [:transport/chats chat-id :sym-key-id] sym-key-id) - :shh/add-filters {:web3 web3 - :filters [{:sym-key-id sym-key-id - :topic topic - :chat-id chat-id}]} - :data-store/tx [(transport-store/save-transport-tx - {:chat-id chat-id - :chat (-> (get-in db [:transport/chats chat-id]) - (assoc :sym-key-id sym-key-id) - ;;TODO (yenda) remove once go implements persistence - (assoc :sym-key sym-key))})]}))) +(fx/defn join-one-to-one-chat + "Function producing all protocol level effects necessary for joining public chat identified by chat-id" + [{:keys [db] :as cofx} chat-id] + (when-not (has-already-joined? cofx chat-id) + (fx/merge cofx + {:filters/load-filters [(:web3 db) [(chat->filter {:chat-id chat-id})]]} + (protocol/init-chat {:chat-id chat-id + :topic (transport.utils/get-topic chat-id)}) + (store-transport chat-id)))) diff --git a/src/status_im/transport/shh.cljs b/src/status_im/transport/shh.cljs index aac2270328cf..3fa8213e5865 100644 --- a/src/status_im/transport/shh.cljs +++ b/src/status_im/transport/shh.cljs @@ -7,41 +7,6 @@ [status-im.transport.utils :as transport.utils] [taoensso.timbre :as log])) -(defn get-new-key-pair [{:keys [web3 on-success on-error]}] - (if web3 - (.. web3 - -shh - (newKeyPair (fn [err resp] - (if-not err - (on-success resp) - (on-error err))))) - (on-error "web3 not available."))) - -(re-frame/reg-fx - :shh/get-new-key-pair - (fn [{:keys [web3 success-event error-event]}] - (get-new-key-pair {:web3 web3 - :on-success #(re-frame/dispatch [success-event %]) - :on-error #(re-frame/dispatch [error-event %])}))) - -(defn get-public-key [{:keys [web3 key-pair-id on-success on-error]}] - (if (and web3 key-pair-id) - (.. web3 - -shh - (getPublicKey key-pair-id (fn [err resp] - (if-not err - (on-success resp) - (on-error err))))) - (on-error "web3 or key-pair id not available."))) - -(re-frame/reg-fx - :shh/get-public-key - (fn [{:keys [web3 key-pair-id success-event error-event]}] - (get-public-key {:web3 web3 - :key-pair-id key-pair-id - :on-success #(re-frame/dispatch [success-event %]) - :on-error #(re-frame/dispatch [error-event %])}))) - (defn generate-sym-key-from-password [{:keys [web3 password on-success on-error]}] (.. web3 @@ -62,6 +27,7 @@ (defn handle-response [success-event error-event messages-count] (fn [err resp] + (println err) (if-not err (if success-event (re-frame/dispatch (conj success-event resp messages-count)) @@ -81,13 +47,8 @@ (fn [post-calls] (doseq [{:keys [web3 payload src dst success-event error-event topics] :or {error-event :transport/send-status-message-error}} post-calls] - (let [part-topic-hash (transport.topic/public-key->discovery-topic-hash dst) - topic (if (contains? topics part-topic-hash) - (transport.topic/public-key->discovery-topic dst) - transport.topic/discovery-topic) - direct-message {:pubKey dst + (let [direct-message {:pubKey dst :sig src - :chat topic :payload payload}] (send-direct-message! web3 direct-message success-event error-event 1))))) @@ -97,7 +58,6 @@ (let [{:keys [web3 payload src success-event error-event] :or {error-event :protocol/send-status-message-error}} params message (clj->js {:sig src - :chat (transport.topic/public-key->discovery-topic src) :payload (-> payload transit/serialize ethereum/utf8-to-hex)})] @@ -113,12 +73,8 @@ (let [{:keys [web3 payload src dsts success-event error-event available-topics] :or {error-event :transport/send-status-message-error}} params] (doseq [{:keys [public-key chat]} dsts] - (let [topic (if (transport.topic/contains-topic? available-topics chat) - chat - transport.topic/discovery-topic) - message + (let [message (clj->js {:pubKey public-key - :chat topic :sig src :payload (-> payload transit/serialize @@ -162,40 +118,6 @@ #(log/debug :shh/post-success)) :on-error #(re-frame/dispatch [error-event %])})))) -(defn add-sym-key - [{:keys [web3 sym-key on-success on-error]}] - (.. web3 - -shh - (addSymKey sym-key (fn [err resp] - (if-not err - (on-success resp) - (on-error err)))))) - -(defn add-sym-keys-batch - [{:keys [web3 keys on-success on-error]}] - (let [batch (.createBatch web3) - results (atom []) - total (count keys) - counter (atom 0) - callback (fn [chat-id sym-key err resp] - (swap! counter inc) - (if err - (on-error err) - (swap! results conj {:chat-id chat-id - :sym-key sym-key - :sym-key-id resp})) - (when (= @counter total) - (on-success @results)))] - (log/debug "PERF" :add-sym-key-batch total) - (doseq [{:keys [chat-id sym-key]} keys] - (let [request (.. web3 - -shh - -addSymKey - (request sym-key - (partial callback chat-id sym-key)))] - (.add batch request))) - (.execute batch))) - (defn get-sym-key [{:keys [web3 sym-key-id on-success on-error]}] (.. web3 @@ -205,54 +127,9 @@ (on-success resp) (on-error err)))))) -(defn new-sym-key - [{:keys [web3 on-success on-error]}] - (.. web3 - -shh - (newSymKey (fn [err resp] - (if-not err - (on-success resp) - (on-error err)))))) - (defn log-error [error] (log/error :shh/get-new-sym-key-error error)) -;;TODO (yenda) remove once go implements persistence -(re-frame/reg-fx - :shh/restore-sym-keys-batch - (fn [{:keys [web3 transport on-success]}] - (log/debug "PERF" :shh/restore-sym-keys-batch (.now js/Date)) - (add-sym-keys-batch {:web3 web3 - :keys transport - :on-success on-success - :on-error log-error}))) - -(defn add-new-sym-key [{:keys [web3 sym-key on-success]}] - (add-sym-key {:web3 web3 - :sym-key sym-key - :on-success (fn [sym-key-id] - (on-success sym-key sym-key-id)) - :on-error log-error})) - -(re-frame/reg-fx - :shh/add-new-sym-keys - (fn [args] - (doseq [add-new-sym-key-params args] - (add-new-sym-key add-new-sym-key-params)))) - -(re-frame/reg-fx - :shh/get-new-sym-keys - (fn [args] - (doseq [{:keys [web3 on-success]} args] - (new-sym-key {:web3 web3 - :on-success (fn [sym-key-id] - (get-sym-key {:web3 web3 - :sym-key-id sym-key-id - :on-success (fn [sym-key] - (on-success sym-key sym-key-id)) - :on-error log-error})) - :on-error log-error})))) - (re-frame/reg-fx :shh/generate-sym-key-from-password (fn [args] diff --git a/src/status_im/utils/config.cljs b/src/status_im/utils/config.cljs index 77b981c0b883..403a69a9fe50 100644 --- a/src/status_im/utils/config.cljs +++ b/src/status_im/utils/config.cljs @@ -53,5 +53,4 @@ (def default-network (get-config :DEFAULT_NETWORK)) (def pow-target (js/parseFloat (get-config :POW_TARGET "0.002"))) (def pow-time (js/parseInt (get-config :POW_TIME "1"))) -(def use-sym-key (enabled? (get-config :USE_SYM_KEY 0))) (def max-installations 2) diff --git a/src/status_im/utils/fx.cljs b/src/status_im/utils/fx.cljs index 1f7e5494ef6f..be7e4a89ce31 100644 --- a/src/status_im/utils/fx.cljs +++ b/src/status_im/utils/fx.cljs @@ -11,7 +11,7 @@ (def ^:private mergable-keys #{:data-store/tx :data-store/base-tx :chat-received-message/add-fx - :shh/add-new-sym-keys :shh/get-new-sym-keys :shh/post + :shh/post :shh/send-direct-message :shh/remove-filter :shh/generate-sym-key-from-password :transport/confirm-messages-processed :group-chats/extract-membership-signature :utils/dispatch-later :json-rpc/call}) diff --git a/src/status_im/utils/publisher.cljs b/src/status_im/utils/publisher.cljs index cae8201f1fdc..259a850bc1c3 100644 --- a/src/status_im/utils/publisher.cljs +++ b/src/status_im/utils/publisher.cljs @@ -2,7 +2,6 @@ (:require [re-frame.core :as re-frame] [re-frame.db] [status-im.accounts.update.publisher :as accounts] - [status-im.contact-code.core :as contact-code] [status-im.utils.async :as async-util] [status-im.utils.datetime :as datetime] [status-im.utils.fx :as fx])) @@ -21,7 +20,6 @@ :now (datetime/timestamp) :db @re-frame.db/app-db}] (accounts/publish-update! cofx) - (contact-code/publish! cofx) (done-fn))) sync-interval-ms sync-timeout-ms)))