Browse Source

invoices.py: small clean-up

master
SomberNight 3 years ago
parent
commit
a1a1fae4cc
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 21
      electrum/invoices.py
  2. 5
      electrum/lnworker.py
  3. 6
      electrum/wallet.py

21
electrum/invoices.py

@ -107,8 +107,16 @@ class BaseInvoice(StoredObject):
#bip70_requestor = attr.ib(type=str, kw_only=True) # type: Optional[str] #bip70_requestor = attr.ib(type=str, kw_only=True) # type: Optional[str]
def is_lightning(self): def is_lightning(self) -> bool:
return self.lightning_invoice is not None 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): def get_status_str(self, status):
status_str = pr_tooltips[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] lightning_invoice = attr.ib(type=str, kw_only=True) # type: Optional[str]
__lnaddr = None __lnaddr = None
def is_lightning(self):
return self.lightning_invoice is not None
def get_address(self) -> Optional[str]: def get_address(self) -> Optional[str]:
"""returns the first address, to be displayed in GUI"""
address = None address = None
if self.outputs: if self.outputs:
address = self.outputs[0].address if len(self.outputs) > 0 else None address = self.outputs[0].address if len(self.outputs) > 0 else None
@ -257,6 +267,7 @@ class Invoice(BaseInvoice):
@property @property
def rhash(self) -> str: def rhash(self) -> str:
assert self.is_lightning()
return self._lnaddr.paymenthash.hex() return self._lnaddr.paymenthash.hex()
@lightning_invoice.validator @lightning_invoice.validator
@ -295,7 +306,6 @@ class Request(BaseInvoice):
return self.payment_hash is not None return self.payment_hash is not None
def get_address(self) -> Optional[str]: def get_address(self) -> Optional[str]:
"""returns the first address, to be displayed in GUI"""
address = None address = None
if self.outputs: if self.outputs:
address = self.outputs[0].address if len(self.outputs) > 0 else None address = self.outputs[0].address if len(self.outputs) > 0 else None
@ -303,9 +313,10 @@ class Request(BaseInvoice):
@property @property
def rhash(self) -> str: def rhash(self) -> str:
assert self.is_lightning()
return self.payment_hash.hex() 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) outputs_str = "\n".join(f"{txout.scriptpubkey.hex()}, {txout.value}" for txout in outputs)
return sha256d(outputs_str + "%d" % timestamp).hex()[0:10] return sha256d(outputs_str + "%d" % timestamp).hex()[0:10]

5
electrum/lnworker.py

@ -30,6 +30,7 @@ from . import constants, util
from . import keystore from . import keystore
from .util import profiler, chunks, OldTaskGroup 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 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 NetworkRetryManager, JsonRPCClient, NotEnoughFunds
from .util import EventListener, event_listener from .util import EventListener, event_listener
from .lnutil import LN_MAX_FUNDING_SAT from .lnutil import LN_MAX_FUNDING_SAT
@ -1899,7 +1900,7 @@ class LNWallet(LNWorker):
info = self.get_payment_info(payment_hash) info = self.get_payment_info(payment_hash)
return info.status if info else PR_UNPAID 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 invoice_id = invoice.rhash
if invoice_id in self.inflight_payments: if invoice_id in self.inflight_payments:
return PR_INFLIGHT return PR_INFLIGHT
@ -2291,7 +2292,7 @@ class LNWallet(LNWorker):
return await self.pay_invoice( return await self.pay_invoice(
invoice, channels=[chan1]) invoice, channels=[chan1])
def can_receive_invoice(self, invoice: Invoice) -> bool: def can_receive_invoice(self, invoice: BaseInvoice) -> bool:
assert invoice.is_lightning() assert invoice.is_lightning()
return (invoice.get_amount_sat() or 0) <= self.num_sats_can_receive() return (invoice.get_amount_sat() or 0) <= self.num_sats_can_receive()

6
electrum/wallet.py

@ -2361,11 +2361,11 @@ class Abstract_Wallet(ABC, Logger, EventListener):
def delete_address(self, address: str) -> None: def delete_address(self, address: str) -> None:
raise Exception("this wallet cannot delete addresses") 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)) include_lightning = bool(self.config.get('bip21_lightning', False))
return req.get_bip21_URI(include_lightning=include_lightning) 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: #if r.is_lightning() and r.exp == 0:
# status = PR_EXPIRED # for BOLT-11 invoices, exp==0 means 0 seconds # status = PR_EXPIRED # for BOLT-11 invoices, exp==0 means 0 seconds
if status == PR_UNPAID and r.has_expired(): if status == PR_UNPAID and r.has_expired():
@ -2850,7 +2850,7 @@ class Abstract_Wallet(ABC, Logger, EventListener):
else: else:
return allow_send, long_warning, short_warning 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() key = req.get_id()
addr = req.get_address() or '' addr = req.get_address() or ''
amount_sat = req.get_amount_sat() or 0 amount_sat = req.get_amount_sat() or 0

Loading…
Cancel
Save