From a425ab030194c135b1f3b853073be1a7ec6ffc7c Mon Sep 17 00:00:00 2001
From: SomberNight <somber.night@protonmail.com>
Date: Mon, 7 Jun 2021 14:46:30 +0200
Subject: [PATCH] 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
---
 electrum/gui/qt/main_window.py |  4 ++--
 electrum/invoices.py           |  9 ++++++++-
 electrum/lnaddr.py             | 10 +++-------
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py
index de82be070..c45c21004 100644
--- a/electrum/gui/qt/main_window.py
+++ b/electrum/gui/qt/main_window.py
@@ -79,7 +79,7 @@ from electrum.exchange_rate import FxThread
 from electrum.simple_config import SimpleConfig
 from electrum.logging import Logger
 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 .amountedit import AmountEdit, BTCAmountEdit, FreezableLineEdit, FeerateEdit
@@ -1240,7 +1240,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
                 if not key:
                     return
                 self.address_list.update()
-        except (InvoiceError, LnAddressError) as e:
+        except InvoiceError as e:
             self.show_error(_('Error creating payment request') + ':\n' + str(e))
             return
 
diff --git a/electrum/invoices.py b/electrum/invoices.py
index 8dcb51f8b..abe5107dc 100644
--- a/electrum/invoices.py
+++ b/electrum/invoices.py
@@ -212,7 +212,14 @@ class LNInvoice(Invoice):
 
     @classmethod
     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(
             type=PR_TYPE_LN,
             invoice=invoice,
diff --git a/electrum/lnaddr.py b/electrum/lnaddr.py
index 0792e8526..cd6954ee8 100644
--- a/electrum/lnaddr.py
+++ b/electrum/lnaddr.py
@@ -22,10 +22,6 @@ 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
@@ -291,16 +287,16 @@ class LnAddr(object):
     @amount.setter
     def amount(self, value):
         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:
             self._amount = None
             return
         assert isinstance(value, Decimal)
         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:
             # 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
 
     def get_amount_sat(self) -> Optional[Decimal]: