Handle cross-network /send and /receive commands

If network mismatch:
* Disable on-press action for /receive
* Show warning message with network name command originated from

Signed-off-by: Dmitry Novotochinov <trybeee@gmail.com>
This commit is contained in:
Dmitry Novotochinov 2018-05-18 16:29:42 +03:00
parent 971d780c1c
commit 9dc689db67
No known key found for this signature in database
GPG Key ID: 267674DCC86628D9
9 changed files with 80 additions and 33 deletions

View File

@ -563,13 +563,15 @@ var paramsGroupRequest = [recipientRequestParam, amountRequestParam];
function handlePersonalRequest(params, context) {
var val = params["amount"].replace(",", ".");
var network = context["network"];
return {
event: "request",
request: {
command: "send",
params: {
amount: val
network: network,
amount: val,
},
prefill: [val]
}
@ -578,6 +580,7 @@ function handlePersonalRequest(params, context) {
function handleGroupRequest(params, context) {
var val = params["amount"].replace(",", ".");
var network = context["network"];
return {
event: "request",
@ -585,6 +588,7 @@ function handleGroupRequest(params, context) {
command: "send",
params: {
recipient: context["current-account"]["name"],
network: network,
amount: val
},
prefill: [context["current-account"]["name"], val],

View File

@ -2,6 +2,7 @@
(:require [re-frame.core :as re-frame]
[taoensso.timbre :as log]
[status-im.chat.models.message :as models.message]
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.handlers :as handlers]
[status-im.i18n :as i18n]
[status-im.utils.platform :as platform]))
@ -10,8 +11,9 @@
(defn- generate-context
"Generates context for jail call"
[current-account-id chat-id group-chat? to]
[current-account-id chat-id group-chat? to network]
(merge {:platform platform/os
:network (ethereum/network-names network)
:from current-account-id
:to to
:chat {:chat-id chat-id
@ -20,7 +22,7 @@
(defn request-command-message-data
"Requests command message data from jail"
[{:contacts/keys [contacts] :as db}
[{:contacts/keys [contacts] :keys [network] :as db}
{{:keys [command command-scope-bitmask bot params type]} :content
:keys [chat-id group-id] :as message}
{:keys [data-type] :as opts}]
@ -33,7 +35,7 @@
to (get-in contacts [chat-id :address])
address (get-in db [:account/account :address])
jail-params {:parameters params
:context (generate-context address chat-id (models.message/group-message? message) to)}]
:context (generate-context address chat-id (models.message/group-message? message) to network)}]
{:db db
:call-jail [{:jail-id bot
:path path

View File

@ -2,6 +2,7 @@
(:require [re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.utils.core :as utils]
[status-im.utils.ethereum.core :as ethereum]
[status-im.chat.events.console :as console-events]
[status-im.chat.events.requests :as requests-events]
[status-im.chat.models :as chat-model]
@ -247,7 +248,8 @@
request-command :command
:keys [prefill prefillBotDb]
:as request}
{:keys [params command handler-data content-type]}]
{:keys [params command handler-data content-type]}
network]
(let [content (if request
{:request-command request-command
;; TODO janherich this is technically not correct, but works for now
@ -255,7 +257,9 @@
:params (assoc request-params :bot-db (:bot-db params))
:prefill prefill
:prefill-bot-db prefillBotDb}
{:params params})
{:params (cond-> params
(= (:name command) "send")
(assoc :network (ethereum/network-names network)))})
content' (assoc content
:command (:name command)
:handler-data handler-data
@ -284,13 +288,13 @@
(vector :chat-received-message/add))})
(defn send-command
[{{:keys [current-public-key chats] :as db} :db :keys [now] :as cofx} params]
[{{:keys [current-public-key chats network] :as db} :db :keys [now] :as cofx} params]
(let [{{:keys [handler-data to-message command] :as content} :command chat-id :chat-id} params
;; We send commands to deleted chats as well, i.e. signed later transactions
chat (or (get chats chat-id) {:chat-id chat-id})
request (:request handler-data)]
(handlers-macro/merge-fx cofx
(upsert-and-send (prepare-command-message current-public-key chat now request content))
(upsert-and-send (prepare-command-message current-public-key chat now request content network))
(add-console-responses command handler-data)
(requests-events/request-answered chat-id to-message))))
@ -305,7 +309,7 @@
(merge {:dispatch-n dn} (dissoc fx :dispatch-n) (dissoc command :dispatch-n)))))
(defn invoke-command-handlers
[{{:contacts/keys [contacts] :as db} :db}
[{{:contacts/keys [contacts] :keys [network] :as db} :db}
{{:keys [command params id]} :command
:keys [chat-id address]
:as orig-params}]
@ -319,6 +323,7 @@
:context (cond-> {:from address
:to to
:current-account (get db :account/account)
:network (ethereum/network-names network)
:message-id id}
(:async-handler command)
(assoc :orig-params orig-params))}]

View File

@ -83,9 +83,12 @@
:padding-right 10
:align-items align}))
(def delivery-status
{:align-self :flex-end
:padding-right 22})
(defn delivery-status [outgoing]
(if outgoing
{:align-self :flex-end
:padding-right 22}
{:align-self :flex-start
:padding-left 16}))
(def message-author
{:width photos/photo-size

View File

@ -60,9 +60,12 @@
(defview message-content-command
[{:keys [content params] :as message}]
(letsubs [command [:get-command (:command-ref content)]]
(letsubs [command [:get-command (:command-ref content)]
network [:network-name]]
(let [preview (:preview content)
{:keys [color] icon-path :icon} command]
{:keys [color] icon-path :icon} command
send-network (get-in content [:params :network])
network-mismatch? (and (seq send-network) (not= network send-network))]
[react/view style/content-command-view
(when color
[react/view style/command-container
@ -75,7 +78,8 @@
[react/icon icon-path style/command-image]])
(if (:markup preview)
;; Markup was defined for command in jail, generate hiccup and render it
(commands.utils/generate-hiccup (:markup preview))
(cond-> (commands.utils/generate-hiccup (:markup preview))
network-mismatch? (conj [react/text send-network]))
;; Display preview if it's defined (as a string), in worst case, render params
[react/text {:style style/command-text
:font :default}
@ -278,6 +282,15 @@
[react/view style/not-sent-icon
[vector-icons/icon :icons/warning {:color colors/red}]]]])
(defview command-status [{{:keys [network]} :params}]
(letsubs [current-network [:network-name]]
(when (and network (not= current-network network))
[react/view style/not-sent-view
[react/text {:style style/not-sent-text}
(i18n/label :network-mismatch)]
[react/view style/not-sent-icon
[vector-icons/icon :icons/warning {:color colors/red}]]])))
(defn message-delivery-status
[{:keys [chat-id message-id current-public-key user-statuses content last-outgoing? outgoing message-type] :as message}]
(let [outgoing-status (or (get user-statuses current-public-key) :not-sent)
@ -291,11 +304,14 @@
(case status
:sending [message-activity-indicator]
:not-sent [message-not-sent-text chat-id message-id]
(when last-outgoing?
(if (= message-type :group-user-message)
[group-message-delivery-status message]
(when outgoing
[text-status status]))))))
(if (and (not outgoing)
(:command content))
[command-status content]
(when last-outgoing?
(if (= message-type :group-user-message)
[group-message-delivery-status message]
(if outgoing
[text-status status])))))))
(defview message-author-name [from message-username]
(letsubs [username [:get-contact-name-by-identity from]]
@ -323,7 +339,7 @@
[message-author-name from username])
[react/view {:style (style/timestamp-content-wrapper message)}
content]]]
[react/view style/delivery-status
[react/view (style/delivery-status outgoing)
[message-delivery-status message]]])
(defn chat-message [{:keys [outgoing group-chat current-public-key content-type content] :as message}]

View File

@ -48,7 +48,7 @@
[command-name]
(keyword (str "request-" (name command-name))))
(defn request-button [message-id _ _]
(defn request-button [message-id _ on-press-handler]
(let [scale-anim-val (anim/create-value min-scale)
answered? (subscribe [:is-request-answered? message-id])
loop? (r/atom true)
@ -59,7 +59,7 @@
(r/create-class
{:display-name "request-button"
:component-did-mount
(if @answered? (fn []) #(request-button-animation-logic context))
(if (or (nil? on-press-handler) @answered?) (fn []) #(request-button-animation-logic context))
:component-will-unmount
#(reset! loop? false)
:reagent-render
@ -77,23 +77,28 @@
[{:keys [message-id content] :as message}]
(letsubs [command [:get-command (:request-command-ref content)]
answered? [:is-request-answered? message-id]
status-initialized? [:get :status-module-initialized?]]
status-initialized? [:get :status-module-initialized?]
network [:network-name]]
(let [{:keys [prefill prefill-bot-db prefillBotDb params preview]
text-content :text} content
command (if (and params command)
(merge command {:prefill prefill
:prefill-bot-db (or prefill-bot-db prefillBotDb)})
command)
on-press-handler (if (:execute-immediately? command)
#(dispatch [:execute-command-immediately command])
(when (and (not answered?) status-initialized?)
#(set-chat-command message-id command)))]
command (if (and params command)
(merge command {:prefill prefill
:prefill-bot-db (or prefill-bot-db prefillBotDb)})
command)
request-network (:network params)
network-mismatch? (and request-network (not= request-network network))
on-press-handler (cond
network-mismatch? nil
(:execute-immediately? command) #(dispatch [:execute-command-immediately command])
(and (not answered?) status-initialized?) #(set-chat-command message-id command))]
[view st/command-request-view
[touchable-highlight
{:on-press on-press-handler}
[view st/command-request-message-view
(if (:markup preview)
[view (commands-utils/generate-hiccup (:markup preview))]
[view (commands-utils/generate-hiccup (:markup preview))
(when network-mismatch?
[text request-network])]
[text {:style st/style-message-text
:font :default}
(or preview text-content (:content content))])]]

View File

@ -91,6 +91,7 @@
:suggestions-commands "Commands"
:faucet-success "Faucet request has been received"
:faucet-error "Faucet request error"
:network-mismatch "Network mismatch"
;;sync
:sync-in-progress "Syncing..."

View File

@ -1,5 +1,6 @@
(ns status-im.ui.screens.subs
(:require [re-frame.core :refer [reg-sub subscribe]]
[status-im.utils.ethereum.core :as ethereum]
status-im.chat.subs
status-im.commands.subs
status-im.ui.screens.accounts.subs
@ -36,6 +37,8 @@
(fn [current-account]
(get (:networks current-account) (:network current-account))))
(reg-sub :network-name (comp ethereum/network-names :network))
(reg-sub :sync-state :sync-state)
(reg-sub :network-status :network-status)
(reg-sub :peers-count :peers-count)

View File

@ -13,6 +13,14 @@
:testnet {:id 3 :name "Ropsten"}
:rinkeby {:id 4 :name "Rinkeby"}})
(def network-names
{"mainnet" "mainnet"
"mainnet_rpc" "mainnet"
"testnet" "testnet"
"testnet_rpc" "testnet"
"rinkeby" "rinkeby"
"rinkeby_rpc" "rinkeby"})
(defn chain-id->chain-keyword [i]
(some #(when (= i (:id (val %))) (key %)) chains))