From 19590c8e549326c61bbca3937c710e089c5a3732 Mon Sep 17 00:00:00 2001 From: Dmitry Novotochinov Date: Wed, 29 Aug 2018 15:29:56 +0300 Subject: [PATCH] [fix #5303] Improve amount validation for /send and /request commands Signed-off-by: Dmitry Novotochinov --- .../chat/commands/impl/transactions.cljs | 6 ++++-- .../test/chat/commands/impl/transactions.cljs | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/status_im/chat/commands/impl/transactions.cljs b/src/status_im/chat/commands/impl/transactions.cljs index a23e8e64d3..bad92060d4 100644 --- a/src/status_im/chat/commands/impl/transactions.cljs +++ b/src/status_im/chat/commands/impl/transactions.cljs @@ -167,11 +167,13 @@ (let [sanitised-str (string/replace amount #"," ".") portions (string/split sanitised-str ".") decimals (count (get portions 1)) - amount (js/parseFloat sanitised-str)] + amount-string (str amount) + amount (js/Number sanitised-str)] (cond (or (js/isNaN amount) - (> (count portions) 2)) + (> (count portions) 2) + (re-matches #".+(\.|,)$" amount-string)) {:title "Amount" :description "Amount is not valid number"} diff --git a/test/cljs/status_im/test/chat/commands/impl/transactions.cljs b/test/cljs/status_im/test/chat/commands/impl/transactions.cljs index 2029ad3b28..6d981f4ea8 100644 --- a/test/cljs/status_im/test/chat/commands/impl/transactions.cljs +++ b/test/cljs/status_im/test/chat/commands/impl/transactions.cljs @@ -57,6 +57,21 @@ (is (= (protocol/validate personal-request-command {:asset "SNT" :amount "a"} cofx) {:title "Amount" :description "Amount is not valid number"})) + (is (= (protocol/validate personal-request-command {:asset "ETH" :amount "0,1Aaa"} cofx) + {:title "Amount" + :description "Amount is not valid number"})) + (is (= (protocol/validate personal-request-command {:asset "ETH" :amount "1-45"} cofx) + {:title "Amount" + :description "Amount is not valid number"})) + (is (= (protocol/validate personal-request-command {:asset "SNT" :amount "1$#@8"} cofx) + {:title "Amount" + :description "Amount is not valid number"})) + (is (= (protocol/validate personal-request-command {:asset "SNT" :amount "20,"} cofx) + {:title "Amount" + :description "Amount is not valid number"})) + (is (= (protocol/validate personal-request-command {:asset "SNT" :amount "20."} cofx) + {:title "Amount" + :description "Amount is not valid number"})) (is (= (protocol/validate personal-request-command {:asset "ETH" :amount "0.54354353454353453453454353453445345545"} cofx) {:title "Amount" :description "Max number of decimals is 18"}))