From bfe9f3531e904a6744f510f4b684c8fe99299194 Mon Sep 17 00:00:00 2001 From: Kristaps Kaupe Date: Thu, 14 May 2020 18:26:32 +0300 Subject: [PATCH] Raise ValueError on invalid Bitcoin amount strings --- jmbitcoin/jmbitcoin/amount.py | 6 +++++- jmbitcoin/test/test_amounts.py | 20 ++++++++++++++++++++ scripts/joinmarket-qt.py | 12 ++++++++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/jmbitcoin/jmbitcoin/amount.py b/jmbitcoin/jmbitcoin/amount.py index 15e0de1..2f0d982 100644 --- a/jmbitcoin/jmbitcoin/amount.py +++ b/jmbitcoin/jmbitcoin/amount.py @@ -1,4 +1,5 @@ from decimal import Decimal +import re def btc_to_sat(btc): @@ -19,7 +20,10 @@ def sat_to_btc(sat): def amount_to_sat(amount_str): - amount_str = str(amount_str) + amount_str = str(amount_str).strip() + if re.compile("^[0-9]{1,8}(\.)?([0-9]{1,8})?(btc|sat)?$").match( + amount_str.lower()) is None: + raise ValueError("Invalid BTC amount string " + amount_str) if amount_str.lower().endswith("btc"): return int(btc_to_sat(amount_str[:-3])) elif amount_str.lower().endswith("sat"): diff --git a/jmbitcoin/test/test_amounts.py b/jmbitcoin/test/test_amounts.py index 1944eec..ebe6041 100644 --- a/jmbitcoin/test/test_amounts.py +++ b/jmbitcoin/test/test_amounts.py @@ -24,6 +24,10 @@ def test_amount_to_sat(): assert(btc.amount_to_sat("1.12300000sat") == 1) assert(btc.amount_to_sat("1btc") == 100000000) assert(btc.amount_to_sat("1BTC") == 100000000) + with pytest.raises(ValueError): + btc.amount_to_sat("") + btc.amount_to_sat("invalidamount") + btc.amount_to_sat("123inv") def test_amount_to_btc(): @@ -37,6 +41,10 @@ def test_amount_to_btc(): assert(btc.amount_to_btc("1.12300000sat") == Decimal("0.00000001")) assert(btc.amount_to_btc("1btc") == 1) assert(btc.amount_to_btc("1BTC") == 1) + with pytest.raises(ValueError): + btc.amount_to_btc("") + btc.amount_to_btc("invalidamount") + btc.amount_to_btc("123inv") def test_amount_to_sat_str(): @@ -50,6 +58,10 @@ def test_amount_to_sat_str(): assert(btc.amount_to_sat_str("1.12300000sat") == "1 sat") assert(btc.amount_to_sat_str("1btc") == "100000000 sat") assert(btc.amount_to_sat_str("1BTC") == "100000000 sat") + with pytest.raises(ValueError): + btc.amount_to_sat_str("") + btc.amount_to_sat_str("invalidamount") + btc.amount_to_sat_str("123inv") def test_amount_to_btc_str(): @@ -63,6 +75,10 @@ def test_amount_to_btc_str(): assert(btc.amount_to_btc_str("1.12300000sat") == "0.00000001 BTC") assert(btc.amount_to_btc_str("1btc") == "1.00000000 BTC") assert(btc.amount_to_btc_str("1BTC") == "1.00000000 BTC") + with pytest.raises(ValueError): + btc.amount_to_btc_str("") + btc.amount_to_btc_str("invalidamount") + btc.amount_to_btc_str("123inv") def test_amount_to_str(): @@ -76,6 +92,10 @@ def test_amount_to_str(): assert(btc.amount_to_str("1.12300000sat") == "0.00000001 BTC (1 sat)") assert(btc.amount_to_str("1btc") == "1.00000000 BTC (100000000 sat)") assert(btc.amount_to_str("1BTC") == "1.00000000 BTC (100000000 sat)") + with pytest.raises(ValueError): + btc.amount_to_str("") + btc.amount_to_str("invalidamount") + btc.amount_to_str("123inv") def test_sat_to_str(): diff --git a/scripts/joinmarket-qt.py b/scripts/joinmarket-qt.py index 3b88272..55f07b4 100755 --- a/scripts/joinmarket-qt.py +++ b/scripts/joinmarket-qt.py @@ -128,7 +128,11 @@ def checkAddress(parent, addr): def checkAmount(parent, amount_str): if not amount_str: return False - amount_sat = btc.amount_to_sat(amount_str) + try: + amount_sat = btc.amount_to_sat(amount_str) + except ValueError as e: + JMQtMessageBox(parent, e.args[0], title="Error", mbtype="warn") + return False if amount_sat < DUST_THRESHOLD: JMQtMessageBox(parent, "Amount " + btc.amount_to_str(amount_sat) + @@ -644,7 +648,11 @@ class SpendTab(QWidget): return destaddr = str(self.addressInput.text().strip()) - amount = btc.amount_to_sat(self.amountInput.text()) + try: + amount = btc.amount_to_sat(self.amountInput.text()) + except ValueError as e: + JMQtMessageBox(parent, e.args[0], title="Error", mbtype="warn") + return makercount = int(self.numCPInput.text()) mixdepth = int(self.mixdepthInput.text())