diff --git a/electrum/invoices.py b/electrum/invoices.py index 2791258bc..59065162e 100644 --- a/electrum/invoices.py +++ b/electrum/invoices.py @@ -107,8 +107,16 @@ class BaseInvoice(StoredObject): #bip70_requestor = attr.ib(type=str, kw_only=True) # type: Optional[str] - def is_lightning(self): - return self.lightning_invoice is not None + def is_lightning(self) -> bool: + raise NotImplementedError() + + def get_address(self) -> Optional[str]: + """returns the first address, to be displayed in GUI""" + raise NotImplementedError() + + @property + def rhash(self) -> str: + raise NotImplementedError() def get_status_str(self, status): status_str = pr_tooltips[status] @@ -240,8 +248,10 @@ class Invoice(BaseInvoice): lightning_invoice = attr.ib(type=str, kw_only=True) # type: Optional[str] __lnaddr = None + def is_lightning(self): + return self.lightning_invoice is not None + def get_address(self) -> Optional[str]: - """returns the first address, to be displayed in GUI""" address = None if self.outputs: address = self.outputs[0].address if len(self.outputs) > 0 else None @@ -257,6 +267,7 @@ class Invoice(BaseInvoice): @property def rhash(self) -> str: + assert self.is_lightning() return self._lnaddr.paymenthash.hex() @lightning_invoice.validator @@ -295,7 +306,6 @@ class Request(BaseInvoice): return self.payment_hash is not None def get_address(self) -> Optional[str]: - """returns the first address, to be displayed in GUI""" address = None if self.outputs: address = self.outputs[0].address if len(self.outputs) > 0 else None @@ -303,9 +313,10 @@ class Request(BaseInvoice): @property def rhash(self) -> str: + assert self.is_lightning() return self.payment_hash.hex() -def get_id_from_onchain_outputs(outputs: List[PartialTxOutput], *, timestamp: int) -> str: +def get_id_from_onchain_outputs(outputs: Sequence[PartialTxOutput], *, timestamp: int) -> str: outputs_str = "\n".join(f"{txout.scriptpubkey.hex()}, {txout.value}" for txout in outputs) return sha256d(outputs_str + "%d" % timestamp).hex()[0:10] diff --git a/electrum/lnworker.py b/electrum/lnworker.py index 5ecaaabb7..eae2ac43a 100644 --- a/electrum/lnworker.py +++ b/electrum/lnworker.py @@ -30,6 +30,7 @@ from . import constants, util from . import keystore from .util import profiler, chunks, OldTaskGroup from .invoices import Invoice, PR_UNPAID, PR_EXPIRED, PR_PAID, PR_INFLIGHT, PR_FAILED, PR_ROUTING, LN_EXPIRY_NEVER +from .invoices import BaseInvoice from .util import NetworkRetryManager, JsonRPCClient, NotEnoughFunds from .util import EventListener, event_listener from .lnutil import LN_MAX_FUNDING_SAT @@ -1899,7 +1900,7 @@ class LNWallet(LNWorker): info = self.get_payment_info(payment_hash) return info.status if info else PR_UNPAID - def get_invoice_status(self, invoice: Invoice) -> int: + def get_invoice_status(self, invoice: BaseInvoice) -> int: invoice_id = invoice.rhash if invoice_id in self.inflight_payments: return PR_INFLIGHT @@ -2291,7 +2292,7 @@ class LNWallet(LNWorker): return await self.pay_invoice( invoice, channels=[chan1]) - def can_receive_invoice(self, invoice: Invoice) -> bool: + def can_receive_invoice(self, invoice: BaseInvoice) -> bool: assert invoice.is_lightning() return (invoice.get_amount_sat() or 0) <= self.num_sats_can_receive() diff --git a/electrum/wallet.py b/electrum/wallet.py index bc6661ebf..a1d607518 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -2361,11 +2361,11 @@ class Abstract_Wallet(ABC, Logger, EventListener): def delete_address(self, address: str) -> None: raise Exception("this wallet cannot delete addresses") - def get_request_URI(self, req: Invoice) -> Optional[str]: + def get_request_URI(self, req: Request) -> Optional[str]: include_lightning = bool(self.config.get('bip21_lightning', False)) return req.get_bip21_URI(include_lightning=include_lightning) - def check_expired_status(self, r: Invoice, status): + def check_expired_status(self, r: BaseInvoice, status): #if r.is_lightning() and r.exp == 0: # status = PR_EXPIRED # for BOLT-11 invoices, exp==0 means 0 seconds if status == PR_UNPAID and r.has_expired(): @@ -2850,7 +2850,7 @@ class Abstract_Wallet(ABC, Logger, EventListener): else: return allow_send, long_warning, short_warning - def get_help_texts_for_receive_request(self, req: Invoice) -> ReceiveRequestHelp: + def get_help_texts_for_receive_request(self, req: Request) -> ReceiveRequestHelp: key = req.get_id() addr = req.get_address() or '' amount_sat = req.get_amount_sat() or 0