From dd37223b5807f6ec73c7705a87e2a22bf75fd647 Mon Sep 17 00:00:00 2001 From: Goran Jovic Date: Mon, 28 May 2018 16:51:56 +0200 Subject: [PATCH] bug #4485 - shows warning when scanned qr code changes pre-selected asset type or amount Signed-off-by: Goran Jovic --- src/status_im/translations/en.cljs | 2 ++ .../wallet/choose_recipient/events.cljs | 34 ++++++++++++++++--- .../wallet/choose_recipient/views.cljs | 2 +- .../ui/screens/wallet/components/views.cljs | 33 ++++++++++-------- src/status_im/ui/screens/wallet/send/db.cljs | 3 +- .../ui/screens/wallet/send/events.cljs | 1 + .../ui/screens/wallet/send/views.cljs | 3 +- 7 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/status_im/translations/en.cljs b/src/status_im/translations/en.cljs index 33d5b365d0..cfa89a7f48 100644 --- a/src/status_im/translations/en.cljs +++ b/src/status_im/translations/en.cljs @@ -573,6 +573,8 @@ :wallet-address-from-clipboard "Use Address From Clipboard" :wallet-invalid-address "Invalid address: \n {{data}}" :wallet-invalid-chain-id "Network does not match: \n {{data}} but current chain is {{chain}}" + :changed-asset-warning "Asset was changed from {{old}} to {{new}}" + :changed-amount-warning "Amount was changed from {{old}} to {{new}}" :wallet-browse-photos "Browse Photos" :wallet-advanced "Advanced" :wallet-transaction-fee "Transaction Fee" diff --git a/src/status_im/ui/screens/wallet/choose_recipient/events.cljs b/src/status_im/ui/screens/wallet/choose_recipient/events.cljs index c9e242ed43..ddaa9b314a 100644 --- a/src/status_im/ui/screens/wallet/choose_recipient/events.cljs +++ b/src/status_im/ui/screens/wallet/choose_recipient/events.cljs @@ -35,17 +35,43 @@ (when (ethereum/address? s) {:address s :chain-id chain-id}))) +(defn changed-asset-warning [cofx old-symbol new-symbol] + (assoc-in cofx [:db :wallet :send-transaction :asset-error] + (i18n/label :t/changed-asset-warning {:old old-symbol :new new-symbol}))) + +(defn changed-amount-warning [cofx old-amount new-amount] + (assoc-in cofx [:db :wallet :send-transaction :amount-error] + (i18n/label :t/changed-amount-warning {:old old-amount :new new-amount}))) + +(defn use-default-eth-gas [cofx] + (assoc-in cofx [:db :wallet :send-transaction :gas] + ethereum/default-transaction-gas)) + (handlers/register-handler-fx :wallet/fill-request-from-url - (fn [{{:keys [network] :as db} :db} [_ data]] - (let [{:keys [view-id]} db - current-chain-id (get-in constants/default-networks [network :config :NetworkId]) + (fn [{{:keys [network] :as db} :db} [_ data origin]] + (let [{:keys [view-id]} db + current-chain-id (get-in constants/default-networks [network :config :NetworkId]) {:keys [address chain-id] :as details} (extract-details data current-chain-id) - valid-network? (boolean (= current-chain-id chain-id))] + valid-network? (boolean (= current-chain-id chain-id)) + previous-state (get-in db [:wallet :send-transaction]) + old-symbol (:symbol previous-state) + new-symbol (:symbol details) + old-amount (:amount previous-state) + new-amount (:value details) + new-gas (:gas details)] (cond-> {:db db :dispatch [:navigate-back]} (and address (= :choose-recipient view-id)) (assoc :dispatch [:navigate-back]) (and address valid-network?) (update :db #(fill-request-details % details)) + (and old-symbol new-symbol (not= old-symbol new-symbol)) (changed-asset-warning old-symbol new-symbol) + (and old-amount new-amount (not= old-amount new-amount)) (changed-amount-warning old-amount new-amount) + ;; NOTE(goranjovic) - the next line is there is because QR code scanning switches the amount to ETH + ;; automatically, so we need to update the gas limit accordingly. The check for origin screen is there + ;; so that we wouldn't also switch gas limit to ETH specific if the user pastes address as text. + ;; We need to check if address is defined so that we wouldn't trigger this behavior when invalid QR is scanned + ;; (e.g. whisper-id) + (and address (= origin :qr) (not new-gas)) (use-default-eth-gas) (not address) (assoc :show-error (i18n/label :t/wallet-invalid-address {:data data})) (and address (not valid-network?)) (assoc :show-error (i18n/label :t/wallet-invalid-chain-id {:data data :chain current-chain-id})))))) diff --git a/src/status_im/ui/screens/wallet/choose_recipient/views.cljs b/src/status_im/ui/screens/wallet/choose_recipient/views.cljs index 91fa601da1..9c28569455 100644 --- a/src/status_im/ui/screens/wallet/choose_recipient/views.cljs +++ b/src/status_im/ui/screens/wallet/choose_recipient/views.cljs @@ -63,7 +63,7 @@ :torchMode (camera/set-torch camera-flashlight) :onBarCodeRead #(when-not @read-once? (reset! read-once? true) - (re-frame/dispatch [:wallet/fill-request-from-url (camera/get-qr-code-data %) nil]))}]] + (re-frame/dispatch [:wallet/fill-request-from-url (camera/get-qr-code-data %) :qr]))}]] [viewfinder dimensions (size dimensions)]] [bottom-buttons/bottom-button [button/button {:disabled? false diff --git a/src/status_im/ui/screens/wallet/components/views.cljs b/src/status_im/ui/screens/wallet/components/views.cljs index 8acf0b1430..d5d6b5fd3f 100644 --- a/src/status_im/ui/screens/wallet/components/views.cljs +++ b/src/status_im/ui/screens/wallet/components/views.cljs @@ -75,23 +75,26 @@ :request :wallet-request-assets (throw (str "Unknown type: " k)))) -(views/defview asset-selector [{:keys [disabled? type symbol]}] +(views/defview asset-selector [{:keys [disabled? type symbol error]}] (views/letsubs [balance [:balance] network [:network]] (let [{:keys [name icon decimals]} (tokens/asset-for (ethereum/network->chain-keyword network) symbol)] - [components/cartouche {:disabled? disabled? :on-press #(re-frame/dispatch [:navigate-to (type->view type)])} - (i18n/label :t/wallet-asset) - [react/view {:style styles/asset-content-container - :accessibility-label :choose-asset-button} - [list/item-image (assoc icon :style styles/asset-icon :image-style {:width 32 :height 32})] - [react/view styles/asset-text-content - [react/view styles/asset-label-content - [react/text {:style (merge styles/text-content styles/asset-label)} - name] - [react/text {:style styles/text-secondary-content} - (clojure.core/name symbol)]] - [react/text {:style (merge styles/text-secondary-content styles/asset-label)} - (str (wallet.utils/format-amount (symbol balance) decimals))]]]]))) + [react/view + [components/cartouche {:disabled? disabled? :on-press #(re-frame/dispatch [:navigate-to (type->view type)])} + (i18n/label :t/wallet-asset) + [react/view {:style styles/asset-content-container + :accessibility-label :choose-asset-button} + [list/item-image (assoc icon :style styles/asset-icon :image-style {:width 32 :height 32})] + [react/view styles/asset-text-content + [react/view styles/asset-label-content + [react/text {:style (merge styles/text-content styles/asset-label)} + name] + [react/text {:style styles/text-secondary-content} + (clojure.core/name symbol)]] + [react/text {:style (merge styles/text-secondary-content styles/asset-label)} + (str (wallet.utils/format-amount (symbol balance) decimals))]]]] + (when error + [tooltip/tooltip error {}])]))) (defn- recipient-address [address] [react/text {:style (merge styles/recipient-address (when-not address styles/recipient-no-address)) @@ -150,7 +153,7 @@ :accessibility-label :recipient-address-input}]] [bottom-buttons/bottom-button [button/button {:disabled? (string/blank? @content) - :on-press #(re-frame/dispatch [:wallet/fill-request-from-url @content]) + :on-press #(re-frame/dispatch [:wallet/fill-request-from-url @content :code]) :fit-to-text? false} (i18n/label :t/done)]]]]))) diff --git a/src/status_im/ui/screens/wallet/send/db.cljs b/src/status_im/ui/screens/wallet/send/db.cljs index 1ec76e7603..af781b240b 100644 --- a/src/status_im/ui/screens/wallet/send/db.cljs +++ b/src/status_im/ui/screens/wallet/send/db.cljs @@ -8,6 +8,7 @@ (spec/def ::to (spec/nilable string?)) (spec/def ::to-name (spec/nilable string?)) (spec/def ::amount-error (spec/nilable string?)) +(spec/def ::asset-error (spec/nilable string?)) (spec/def ::amount-text (spec/nilable string?)) (spec/def ::password (spec/nilable #(instance? security/MaskedData %))) (spec/def ::wrong-password? (spec/nilable boolean?)) @@ -28,7 +29,7 @@ (spec/def ::method (spec/nilable string?)) (spec/def :wallet/send-transaction (allowed-keys - :opt-un [::amount ::to ::to-name ::amount-error ::amount-text ::password + :opt-un [::amount ::to ::to-name ::amount-error ::asset-error ::amount-text ::password ::waiting-signal? ::signing? ::id ::later? ::camera-flashlight ::in-progress? ::wrong-password? ::from-chat? ::symbol ::advanced? diff --git a/src/status_im/ui/screens/wallet/send/events.cljs b/src/status_im/ui/screens/wallet/send/events.cljs index bed05fd449..9cf27dd525 100644 --- a/src/status_im/ui/screens/wallet/send/events.cljs +++ b/src/status_im/ui/screens/wallet/send/events.cljs @@ -89,6 +89,7 @@ (assoc-in [:wallet :send-transaction :symbol] symbol) (assoc-in [:wallet :send-transaction :amount] nil) (assoc-in [:wallet :send-transaction :amount-text] nil) + (assoc-in [:wallet :send-transaction :asset-error] nil) (assoc-in [:wallet :send-transaction :gas] (ethereum/estimate-gas symbol)))})) (handlers/register-handler-fx diff --git a/src/status_im/ui/screens/wallet/send/views.cljs b/src/status_im/ui/screens/wallet/send/views.cljs index cedad36f00..9f23aaffd5 100644 --- a/src/status_im/ui/screens/wallet/send/views.cljs +++ b/src/status_im/ui/screens/wallet/send/views.cljs @@ -198,7 +198,7 @@ [advanced-cartouche transaction modal?])]) (defn- send-transaction-panel [{:keys [modal? transaction scroll advanced? network]}] - (let [{:keys [amount amount-text amount-error signing? to to-name sufficient-funds? in-progress? from-chat? symbol]} transaction + (let [{:keys [amount amount-text amount-error asset-error signing? to to-name sufficient-funds? in-progress? from-chat? symbol]} transaction {:keys [decimals] :as token} (tokens/asset-for (ethereum/network->chain-keyword network) symbol) timeout (atom nil)] [wallet.components/simple-screen {:avoid-keyboard? (not modal?) @@ -216,6 +216,7 @@ :address to :name to-name}] [components/asset-selector {:disabled? (or from-chat? modal?) + :error asset-error :type :send :symbol symbol}] [components/amount-selector {:disabled? (or from-chat? modal?)