Browse Source

Merge #577: Raise ValueError on invalid Bitcoin amount strings

bfe9f35 Raise ValueError on invalid Bitcoin amount strings (Kristaps Kaupe)
master
Adam Gibson 6 years ago
parent
commit
cd37b1f766
No known key found for this signature in database
GPG Key ID: 141001A1AF77F20B
  1. 6
      jmbitcoin/jmbitcoin/amount.py
  2. 20
      jmbitcoin/test/test_amounts.py
  3. 12
      scripts/joinmarket-qt.py

6
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"):

20
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():

12
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())

Loading…
Cancel
Save