From 81bd6f7d1bfd2de93f2116106b33158263476031 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 3 Mar 2023 16:14:35 +0000 Subject: [PATCH] follow-up invoice changes: fix "Add lightning invoice to bitcoin URIs" follow-up 719b468eee8b3e13680f6e7b90194d618181fe0c --- electrum/invoices.py | 45 +++++++++++++++++++++++--------------------- electrum/wallet.py | 6 ++++-- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/electrum/invoices.py b/electrum/invoices.py index 59065162e..7aa2e0488 100644 --- a/electrum/invoices.py +++ b/electrum/invoices.py @@ -166,27 +166,6 @@ class BaseInvoice(StoredObject): return amount_msat return int(amount_msat // 1000) - def get_bip21_URI(self, *, include_lightning: bool = False) -> Optional[str]: - from electrum.util import create_bip21_uri - addr = self.get_address() - amount = self.get_amount_sat() - if amount is not None: - amount = int(amount) - message = self.message - extra = {} - if self.time and self.exp: - extra['time'] = str(int(self.time)) - extra['exp'] = str(int(self.exp)) - lightning = self.lightning_invoice if include_lightning else None - if lightning: - extra['lightning'] = lightning - if not addr and lightning: - return "bitcoin:?lightning="+lightning - if not addr and not lightning: - return None - uri = create_bip21_uri(addr, amount, message, extra_query_params=extra) - return str(uri) - @amount_msat.validator def _validate_amount(self, attribute, value): if value is None: @@ -316,6 +295,30 @@ class Request(BaseInvoice): assert self.is_lightning() return self.payment_hash.hex() + def get_bip21_URI( + self, + *, + lightning_invoice: Optional[str] = None, + ) -> Optional[str]: + from electrum.util import create_bip21_uri + addr = self.get_address() + amount = self.get_amount_sat() + if amount is not None: + amount = int(amount) + message = self.message + extra = {} + if self.time and self.exp: + extra['time'] = str(int(self.time)) + extra['exp'] = str(int(self.exp)) + if lightning_invoice: + extra['lightning'] = lightning_invoice + if not addr and lightning_invoice: + return "bitcoin:?lightning="+lightning_invoice + if not addr and not lightning_invoice: + return None + uri = create_bip21_uri(addr, amount, message, extra_query_params=extra) + return str(uri) + 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) diff --git a/electrum/wallet.py b/electrum/wallet.py index a1d607518..3ef44d9ba 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -2362,8 +2362,10 @@ class Abstract_Wallet(ABC, Logger, EventListener): raise Exception("this wallet cannot delete addresses") 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) + lightning_invoice = None + if self.config.get('bip21_lightning', False): + lightning_invoice = self.get_bolt11_invoice(req) + return req.get_bip21_URI(lightning_invoice=lightning_invoice) def check_expired_status(self, r: BaseInvoice, status): #if r.is_lightning() and r.exp == 0: