invoices/lnaddr: LNInvoice.from_bech32 now raises InvoiceError
rm LnAddressError fixes https://github.com/spesmilo/electrum/issues/7321 related https://github.com/spesmilo/electrum/pull/7234
This commit is contained in:
@@ -79,7 +79,7 @@ from electrum.exchange_rate import FxThread
|
|||||||
from electrum.simple_config import SimpleConfig
|
from electrum.simple_config import SimpleConfig
|
||||||
from electrum.logging import Logger
|
from electrum.logging import Logger
|
||||||
from electrum.lnutil import ln_dummy_address, extract_nodeid, ConnStringFormatError
|
from electrum.lnutil import ln_dummy_address, extract_nodeid, ConnStringFormatError
|
||||||
from electrum.lnaddr import lndecode, LnDecodeException, LnAddressError
|
from electrum.lnaddr import lndecode, LnDecodeException
|
||||||
|
|
||||||
from .exception_window import Exception_Hook
|
from .exception_window import Exception_Hook
|
||||||
from .amountedit import AmountEdit, BTCAmountEdit, FreezableLineEdit, FeerateEdit
|
from .amountedit import AmountEdit, BTCAmountEdit, FreezableLineEdit, FeerateEdit
|
||||||
@@ -1240,7 +1240,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
|
|||||||
if not key:
|
if not key:
|
||||||
return
|
return
|
||||||
self.address_list.update()
|
self.address_list.update()
|
||||||
except (InvoiceError, LnAddressError) as e:
|
except InvoiceError as e:
|
||||||
self.show_error(_('Error creating payment request') + ':\n' + str(e))
|
self.show_error(_('Error creating payment request') + ':\n' + str(e))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -212,7 +212,14 @@ class LNInvoice(Invoice):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_bech32(cls, invoice: str) -> 'LNInvoice':
|
def from_bech32(cls, invoice: str) -> 'LNInvoice':
|
||||||
amount_msat = lndecode(invoice).get_amount_msat()
|
"""Constructs LNInvoice object from BOLT-11 string.
|
||||||
|
Might raise InvoiceError.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
lnaddr = lndecode(invoice)
|
||||||
|
except Exception as e:
|
||||||
|
raise InvoiceError(e) from e
|
||||||
|
amount_msat = lnaddr.get_amount_msat()
|
||||||
return LNInvoice(
|
return LNInvoice(
|
||||||
type=PR_TYPE_LN,
|
type=PR_TYPE_LN,
|
||||||
invoice=invoice,
|
invoice=invoice,
|
||||||
|
|||||||
@@ -22,10 +22,6 @@ if TYPE_CHECKING:
|
|||||||
from .lnutil import LnFeatures
|
from .lnutil import LnFeatures
|
||||||
|
|
||||||
|
|
||||||
class LnAddressError(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# BOLT #11:
|
# BOLT #11:
|
||||||
#
|
#
|
||||||
# A writer MUST encode `amount` as a positive decimal integer with no
|
# A writer MUST encode `amount` as a positive decimal integer with no
|
||||||
@@ -291,16 +287,16 @@ class LnAddr(object):
|
|||||||
@amount.setter
|
@amount.setter
|
||||||
def amount(self, value):
|
def amount(self, value):
|
||||||
if not (isinstance(value, Decimal) or value is None):
|
if not (isinstance(value, Decimal) or value is None):
|
||||||
raise LnAddressError(f"amount must be Decimal or None, not {value!r}")
|
raise ValueError(f"amount must be Decimal or None, not {value!r}")
|
||||||
if value is None:
|
if value is None:
|
||||||
self._amount = None
|
self._amount = None
|
||||||
return
|
return
|
||||||
assert isinstance(value, Decimal)
|
assert isinstance(value, Decimal)
|
||||||
if value.is_nan() or not (0 <= value <= TOTAL_COIN_SUPPLY_LIMIT_IN_BTC):
|
if value.is_nan() or not (0 <= value <= TOTAL_COIN_SUPPLY_LIMIT_IN_BTC):
|
||||||
raise LnAddressError(f"amount is out-of-bounds: {value!r} BTC")
|
raise ValueError(f"amount is out-of-bounds: {value!r} BTC")
|
||||||
if value * 10**12 % 10:
|
if value * 10**12 % 10:
|
||||||
# max resolution is millisatoshi
|
# max resolution is millisatoshi
|
||||||
raise LnAddressError(f"Cannot encode {value!r}: too many decimal places")
|
raise ValueError(f"Cannot encode {value!r}: too many decimal places")
|
||||||
self._amount = value
|
self._amount = value
|
||||||
|
|
||||||
def get_amount_sat(self) -> Optional[Decimal]:
|
def get_amount_sat(self) -> Optional[Decimal]:
|
||||||
|
|||||||
Reference in New Issue
Block a user