1
0

invoice: fail gracefully with large amount

This commit is contained in:
bitromortac
2021-05-06 15:30:34 +02:00
parent 6ce96306ca
commit 853e912885
4 changed files with 99 additions and 79 deletions

View File

@@ -22,6 +22,10 @@ if TYPE_CHECKING:
from .lnutil import LnFeatures
class LnAddressError(Exception):
pass
# BOLT #11:
#
# A writer MUST encode `amount` as a positive decimal integer with no
@@ -265,6 +269,7 @@ def lnencode(addr: 'LnAddr', privkey) -> str:
return bech32_encode(segwit_addr.Encoding.BECH32, hrp, bitarray_to_u5(data))
class LnAddr(object):
def __init__(self, *, paymenthash: bytes = None, amount=None, currency=None, tags=None, date=None,
payment_secret: bytes = None):
@@ -286,16 +291,16 @@ class LnAddr(object):
@amount.setter
def amount(self, value):
if not (isinstance(value, Decimal) or value is None):
raise ValueError(f"amount must be Decimal or None, not {value!r}")
raise LnAddressError(f"amount must be Decimal or None, not {value!r}")
if value is None:
self._amount = None
return
assert isinstance(value, Decimal)
if value.is_nan() or not (0 <= value <= TOTAL_COIN_SUPPLY_LIMIT_IN_BTC):
raise ValueError(f"amount is out-of-bounds: {value!r} BTC")
raise LnAddressError(f"amount is out-of-bounds: {value!r} BTC")
if value * 10**12 % 10:
# max resolution is millisatoshi
raise ValueError(f"Cannot encode {value!r}: too many decimal places")
raise LnAddressError(f"Cannot encode {value!r}: too many decimal places")
self._amount = value
def get_amount_sat(self) -> Optional[Decimal]: