From a2ca191de17a5aa19a32f0515faf789f544682d1 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Wed, 31 May 2023 20:54:30 +0200 Subject: [PATCH] pass wallet to PaymentIdentifier instead of config and contacts --- electrum/gui/qt/paytoedit.py | 4 ++-- electrum/gui/qt/send_tab.py | 8 ++++---- electrum/payment_identifier.py | 24 ++++++++++++++---------- run_electrum | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/electrum/gui/qt/paytoedit.py b/electrum/gui/qt/paytoedit.py index 5076b5292..18ac029b7 100644 --- a/electrum/gui/qt/paytoedit.py +++ b/electrum/gui/qt/paytoedit.py @@ -213,9 +213,9 @@ class PayToEdit(Logger, GenericInputHandler): self.previous_payto = text if self.disable_checks: return - pi = PaymentIdentifier(self.config, self.win.contacts, text) + pi = PaymentIdentifier(self.send_tab.wallet, text) self.is_multiline = bool(pi.multiline_outputs) - print('is_multiline', self.is_multiline) + self.logger.debug(f'is_multiline {self.is_multiline}') self.send_tab.handle_payment_identifier(pi, can_use_network=full_check) def handle_multiline(self, outputs): diff --git a/electrum/gui/qt/send_tab.py b/electrum/gui/qt/send_tab.py index bde4847f2..7473884ad 100644 --- a/electrum/gui/qt/send_tab.py +++ b/electrum/gui/qt/send_tab.py @@ -194,7 +194,7 @@ class SendTab(QWidget, MessageBoxMixin, Logger): self.set_payment_identifier(text) def set_payment_identifier(self, text): - pi = PaymentIdentifier(self.config, self.window.contacts, text) + pi = PaymentIdentifier(self.wallet, text) if pi.error: self.show_error(_('Clipboard text is not a valid payment identifier') + '\n' + pi.error) return @@ -356,7 +356,7 @@ class SendTab(QWidget, MessageBoxMixin, Logger): w.setReadOnly(False) def update_fields(self, pi): - recipient, amount, description, comment, validated = pi.get_fields_for_GUI(self.wallet) + recipient, amount, description, comment, validated = pi.get_fields_for_GUI() if recipient: self.payto_e.setTextNoCheck(recipient) elif pi.multiline_outputs: @@ -408,7 +408,7 @@ class SendTab(QWidget, MessageBoxMixin, Logger): self.show_error(_('No amount')) return - invoice = self.payment_identifier.get_invoice(self.wallet, amount_sat, self.get_message()) + invoice = self.payment_identifier.get_invoice(amount_sat, self.get_message()) #except Exception as e: if not invoice: self.show_error('error getting invoice' + self.payment_identifier.error) @@ -449,7 +449,7 @@ class SendTab(QWidget, MessageBoxMixin, Logger): self.do_clear() return self.update_fields(pi) - invoice = pi.get_invoice(self.wallet, self.get_amount(), self.get_message()) + invoice = pi.get_invoice(self.get_amount(), self.get_message()) self.pending_invoice = invoice self.do_pay_invoice(invoice) diff --git a/electrum/payment_identifier.py b/electrum/payment_identifier.py index dce52282a..d1b69f66b 100644 --- a/electrum/payment_identifier.py +++ b/electrum/payment_identifier.py @@ -2,7 +2,7 @@ import asyncio import urllib import re from decimal import Decimal, InvalidOperation -from typing import NamedTuple, Optional, Callable, Any, Sequence, List +from typing import NamedTuple, Optional, Callable, Any, Sequence, List, TYPE_CHECKING from urllib.parse import urlparse from . import bitcoin @@ -16,6 +16,9 @@ from .bitcoin import COIN, TOTAL_COIN_SUPPLY_LIMIT_IN_BTC, opcodes, construct_sc from .lnaddr import lndecode, LnDecodeException, LnInvoiceException from .lnutil import IncompatibleOrInsaneFeatures +if TYPE_CHECKING: + from .wallet import Abstract_Wallet + def maybe_extract_lightning_payment_identifier(data: str) -> Optional[str]: data = data.strip() # whitespaces @@ -184,12 +187,14 @@ class PaymentIdentifier(Logger): * lightning-URI (containing bolt11 or lnurl) * bolt11 invoice * lnurl + * TODO: lightning address """ - def __init__(self, config, contacts, text): + def __init__(self, wallet: 'Abstract_Wallet', text): Logger.__init__(self) - self.contacts = contacts - self.config = config + self.wallet = wallet + self.contacts = wallet.contacts if wallet is not None else None + self.config = wallet.config if wallet is not None else None self.text = text self._type = None self.error = None # if set, GUI should show error and stop @@ -307,7 +312,7 @@ class PaymentIdentifier(Logger): total += output.value if is_multiline and errors: self.error = str(errors) if errors else None - print(outputs, self.error) + self.logger.debug(f'multiline: {outputs!r}, {self.error}') return outputs def parse_address_and_amount(self, line) -> 'PartialTxOutput': @@ -364,7 +369,7 @@ class PaymentIdentifier(Logger): assert bitcoin.is_address(address) return address - def get_fields_for_GUI(self, wallet): + def get_fields_for_GUI(self): """ sets self.error as side effect""" recipient = None amount = None @@ -429,7 +434,7 @@ class PaymentIdentifier(Logger): if label and not description: description = label lightning = self.bip21.get('lightning') - if lightning and wallet.has_lightning(): + if lightning and self.wallet.has_lightning(): # maybe set self.bolt11? recipient, amount, description = self.get_bolt11_fields(lightning) if not amount: @@ -540,8 +545,7 @@ class PaymentIdentifier(Logger): self.logger.info(f"Payment ACK: {ack_status}. Ack message: {ack_msg}") on_success(self) - def get_invoice(self, wallet, amount_sat, message): - # fixme: wallet not really needed, only height + def get_invoice(self, amount_sat, message): from .invoices import Invoice if self.is_lightning(): invoice_str = self.bolt11 @@ -555,7 +559,7 @@ class PaymentIdentifier(Logger): outputs = self.get_onchain_outputs(amount_sat) message = self.bip21.get('message') if self.bip21 else message bip70_data = self.bip70_data if self.bip70 else None - return wallet.create_invoice( + return self.wallet.create_invoice( outputs=outputs, message=message, pr=bip70_data, diff --git a/run_electrum b/run_electrum index a0c95c728..46aca8a67 100755 --- a/run_electrum +++ b/run_electrum @@ -367,7 +367,7 @@ def main(): # check if we received a valid payment identifier uri = config_options.get('url') - if uri and not PaymentIdentifier(None, None, uri).is_valid(): + if uri and not PaymentIdentifier(None, uri).is_valid(): print_stderr('unknown command:', uri) sys.exit(1)