bug #4485 - shows warning when scanned qr code changes pre-selected asset type or amount

Signed-off-by: Goran Jovic <goranjovic@gmail.com>
This commit is contained in:
Goran Jovic 2018-05-28 16:51:56 +02:00
parent 446ae1b30d
commit dd37223b58
No known key found for this signature in database
GPG Key ID: D429D1A9B2EB8A8E
7 changed files with 56 additions and 22 deletions

View File

@ -573,6 +573,8 @@
:wallet-address-from-clipboard "Use Address From Clipboard" :wallet-address-from-clipboard "Use Address From Clipboard"
:wallet-invalid-address "Invalid address: \n {{data}}" :wallet-invalid-address "Invalid address: \n {{data}}"
:wallet-invalid-chain-id "Network does not match: \n {{data}} but current chain is {{chain}}" :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-browse-photos "Browse Photos"
:wallet-advanced "Advanced" :wallet-advanced "Advanced"
:wallet-transaction-fee "Transaction Fee" :wallet-transaction-fee "Transaction Fee"

View File

@ -35,17 +35,43 @@
(when (ethereum/address? s) (when (ethereum/address? s)
{:address s :chain-id chain-id}))) {: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 (handlers/register-handler-fx
:wallet/fill-request-from-url :wallet/fill-request-from-url
(fn [{{:keys [network] :as db} :db} [_ data]] (fn [{{:keys [network] :as db} :db} [_ data origin]]
(let [{:keys [view-id]} db (let [{:keys [view-id]} db
current-chain-id (get-in constants/default-networks [network :config :NetworkId]) current-chain-id (get-in constants/default-networks [network :config :NetworkId])
{:keys [address chain-id] :as details} (extract-details data current-chain-id) {: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 (cond-> {:db db
:dispatch [:navigate-back]} :dispatch [:navigate-back]}
(and address (= :choose-recipient view-id)) (assoc :dispatch [:navigate-back]) (and address (= :choose-recipient view-id)) (assoc :dispatch [:navigate-back])
(and address valid-network?) (update :db #(fill-request-details % details)) (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})) (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})))))) (and address (not valid-network?)) (assoc :show-error (i18n/label :t/wallet-invalid-chain-id {:data data :chain current-chain-id}))))))

View File

@ -63,7 +63,7 @@
:torchMode (camera/set-torch camera-flashlight) :torchMode (camera/set-torch camera-flashlight)
:onBarCodeRead #(when-not @read-once? :onBarCodeRead #(when-not @read-once?
(reset! read-once? true) (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)]] [viewfinder dimensions (size dimensions)]]
[bottom-buttons/bottom-button [bottom-buttons/bottom-button
[button/button {:disabled? false [button/button {:disabled? false

View File

@ -75,10 +75,11 @@
:request :wallet-request-assets :request :wallet-request-assets
(throw (str "Unknown type: " k)))) (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] (views/letsubs [balance [:balance]
network [:network]] network [:network]]
(let [{:keys [name icon decimals]} (tokens/asset-for (ethereum/network->chain-keyword network) symbol)] (let [{:keys [name icon decimals]} (tokens/asset-for (ethereum/network->chain-keyword network) symbol)]
[react/view
[components/cartouche {:disabled? disabled? :on-press #(re-frame/dispatch [:navigate-to (type->view type)])} [components/cartouche {:disabled? disabled? :on-press #(re-frame/dispatch [:navigate-to (type->view type)])}
(i18n/label :t/wallet-asset) (i18n/label :t/wallet-asset)
[react/view {:style styles/asset-content-container [react/view {:style styles/asset-content-container
@ -91,7 +92,9 @@
[react/text {:style styles/text-secondary-content} [react/text {:style styles/text-secondary-content}
(clojure.core/name symbol)]] (clojure.core/name symbol)]]
[react/text {:style (merge styles/text-secondary-content styles/asset-label)} [react/text {:style (merge styles/text-secondary-content styles/asset-label)}
(str (wallet.utils/format-amount (symbol balance) decimals))]]]]))) (str (wallet.utils/format-amount (symbol balance) decimals))]]]]
(when error
[tooltip/tooltip error {}])])))
(defn- recipient-address [address] (defn- recipient-address [address]
[react/text {:style (merge styles/recipient-address (when-not address styles/recipient-no-address)) [react/text {:style (merge styles/recipient-address (when-not address styles/recipient-no-address))
@ -150,7 +153,7 @@
:accessibility-label :recipient-address-input}]] :accessibility-label :recipient-address-input}]]
[bottom-buttons/bottom-button [bottom-buttons/bottom-button
[button/button {:disabled? (string/blank? @content) [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} :fit-to-text? false}
(i18n/label :t/done)]]]]))) (i18n/label :t/done)]]]])))

View File

@ -8,6 +8,7 @@
(spec/def ::to (spec/nilable string?)) (spec/def ::to (spec/nilable string?))
(spec/def ::to-name (spec/nilable string?)) (spec/def ::to-name (spec/nilable string?))
(spec/def ::amount-error (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 ::amount-text (spec/nilable string?))
(spec/def ::password (spec/nilable #(instance? security/MaskedData %))) (spec/def ::password (spec/nilable #(instance? security/MaskedData %)))
(spec/def ::wrong-password? (spec/nilable boolean?)) (spec/def ::wrong-password? (spec/nilable boolean?))
@ -28,7 +29,7 @@
(spec/def ::method (spec/nilable string?)) (spec/def ::method (spec/nilable string?))
(spec/def :wallet/send-transaction (allowed-keys (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? ::waiting-signal? ::signing? ::id ::later?
::camera-flashlight ::in-progress? ::camera-flashlight ::in-progress?
::wrong-password? ::from-chat? ::symbol ::advanced? ::wrong-password? ::from-chat? ::symbol ::advanced?

View File

@ -89,6 +89,7 @@
(assoc-in [:wallet :send-transaction :symbol] symbol) (assoc-in [:wallet :send-transaction :symbol] symbol)
(assoc-in [:wallet :send-transaction :amount] nil) (assoc-in [:wallet :send-transaction :amount] nil)
(assoc-in [:wallet :send-transaction :amount-text] 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)))})) (assoc-in [:wallet :send-transaction :gas] (ethereum/estimate-gas symbol)))}))
(handlers/register-handler-fx (handlers/register-handler-fx

View File

@ -198,7 +198,7 @@
[advanced-cartouche transaction modal?])]) [advanced-cartouche transaction modal?])])
(defn- send-transaction-panel [{:keys [modal? transaction scroll advanced? network]}] (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) {:keys [decimals] :as token} (tokens/asset-for (ethereum/network->chain-keyword network) symbol)
timeout (atom nil)] timeout (atom nil)]
[wallet.components/simple-screen {:avoid-keyboard? (not modal?) [wallet.components/simple-screen {:avoid-keyboard? (not modal?)
@ -216,6 +216,7 @@
:address to :address to
:name to-name}] :name to-name}]
[components/asset-selector {:disabled? (or from-chat? modal?) [components/asset-selector {:disabled? (or from-chat? modal?)
:error asset-error
:type :send :type :send
:symbol symbol}] :symbol symbol}]
[components/amount-selector {:disabled? (or from-chat? modal?) [components/amount-selector {:disabled? (or from-chat? modal?)