From 659d6890d9b0957ea689505936dba59b4a8b39c3 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 11 Aug 2022 13:07:56 +0000 Subject: [PATCH] wallet.get_request_URI: rm code duplication --- electrum/gui/qt/receive_tab.py | 3 +-- electrum/gui/text.py | 2 +- electrum/invoices.py | 10 ++++++---- electrum/wallet.py | 19 +++---------------- 4 files changed, 11 insertions(+), 23 deletions(-) diff --git a/electrum/gui/qt/receive_tab.py b/electrum/gui/qt/receive_tab.py index f8c54e33f..293fed966 100644 --- a/electrum/gui/qt/receive_tab.py +++ b/electrum/gui/qt/receive_tab.py @@ -221,8 +221,7 @@ class ReceiveTab(QWidget, MessageBoxMixin, Logger): address_help = '' if addr else _('Amount too small to be received onchain') URI_help = '' lnaddr = req.lightning_invoice - bip21_lightning = lnaddr if self.config.get('bip21_lightning', False) else None - URI = req.get_bip21_URI(lightning=bip21_lightning) + URI = self.wallet.get_request_URI(req) or '' lightning_online = self.wallet.lnworker and self.wallet.lnworker.num_peers() > 0 can_receive_lightning = self.wallet.lnworker and amount_sat <= self.wallet.lnworker.num_sats_can_receive() has_expired = self.wallet.get_request_status(key) == PR_EXPIRED diff --git a/electrum/gui/text.py b/electrum/gui/text.py index f81525dc8..34e349c15 100644 --- a/electrum/gui/text.py +++ b/electrum/gui/text.py @@ -853,7 +853,7 @@ class ElectrumGui(BaseElectrumGui, EventListener): def show_request(self, key): req = self.wallet.get_request(key) addr = req.get_address() or '' - URI = req.get_bip21_URI() + URI = self.wallet.get_request_URI(req) or '' lnaddr = req.lightning_invoice or '' w = curses.newwin(self.maxy - 2, self.maxx - 2, 1, 1) pos = 4 diff --git a/electrum/invoices.py b/electrum/invoices.py index b15b6661d..461590680 100644 --- a/electrum/invoices.py +++ b/electrum/invoices.py @@ -155,7 +155,7 @@ class Invoice(StoredObject): return amount_msat return int(amount_msat // 1000) - def get_bip21_URI(self, lightning=None): + 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() @@ -164,13 +164,15 @@ class Invoice(StoredObject): message = self.message extra = {} if self.time and self.exp: - extra['time'] = str(self.time) - extra['exp'] = str(self.exp) - # only if we can receive + 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) diff --git a/electrum/wallet.py b/electrum/wallet.py index c0165a8d6..cddd34c05 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -2290,22 +2290,9 @@ 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) -> str: - # todo: should be a method of invoice? - addr = req.get_address() - message = self.get_label_for_address(addr) - amount = req.get_amount_sat() - extra_query_params = {} - if req.time and req.exp: - extra_query_params['time'] = str(int(req.time)) - extra_query_params['exp'] = str(int(req.exp)) - #if req.get('name') and req.get('sig'): - # sig = bfh(req.get('sig')) - # sig = bitcoin.base_encode(sig, base=58) - # extra_query_params['name'] = req['name'] - # extra_query_params['sig'] = sig - uri = create_bip21_uri(addr, amount, message, extra_query_params=extra_query_params) - return str(uri) + def get_request_URI(self, req: Invoice) -> 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): #if r.is_lightning() and r.exp == 0: