Skip to content

Commit

Permalink
register distinction between address and script for SPK type payment …
Browse files Browse the repository at this point in the history
…identifiers and allow zero amount for

script destinations.

This is mainly to support OP_RETURN outputs, which typically have a zero amount output value,
but as we don't special case OP_RETURN, this is currently done for all non-address scripts

Also, it's probably good to add a warning popup for OP_RETURN outputs with a non-zero output value, but this
would also need special casing for OP_RETURN.

Saving of script output payment identifiers is disabled for now, as reading the script from the stored invoice
back into human-readable form is currently not implemented, and currently only lightning invoices or address output
is supported.
  • Loading branch information
accumulator committed Sep 4, 2023
1 parent 0d96bc1 commit 307cf25
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
49 changes: 31 additions & 18 deletions electrum/gui/qt/send_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ def showSpinner(self, b):

def on_amount_changed(self, text):
# FIXME: implement full valid amount check to enable/disable Pay button
pi_valid = self.payto_e.payment_identifier.is_valid() if self.payto_e.payment_identifier else False
pi_error = self.payto_e.payment_identifier.is_error() if pi_valid else False
self.send_button.setEnabled(bool(self.amount_e.get_amount()) and pi_valid and not pi_error)
pi = self.payto_e.payment_identifier
if not pi:
self.send_button.setEnabled(False)
return
pi_error = pi.is_error() if pi.is_valid() else False
is_spk_script = pi.type == PaymentIdentifierType.SPK and not pi.spk_is_address
valid_amount = is_spk_script or bool(self.amount_e.get_amount())
self.send_button.setEnabled(pi.is_valid() and not pi_error and valid_amount)

def do_paste(self):
self.logger.debug('do_paste')
Expand All @@ -224,16 +229,20 @@ def set_payment_identifier(self, text):
self.show_error(_('Invalid payment identifier'))

def spend_max(self):
if self.payto_e.payment_identifier is None:
pi = self.payto_e.payment_identifier

if pi is None or pi.type == PaymentIdentifierType.UNKNOWN:
return

assert self.payto_e.payment_identifier.type in [PaymentIdentifierType.SPK, PaymentIdentifierType.MULTILINE,
PaymentIdentifierType.BIP21, PaymentIdentifierType.OPENALIAS]
assert not self.payto_e.payment_identifier.is_amount_locked()
assert pi.type in [PaymentIdentifierType.SPK, PaymentIdentifierType.MULTILINE,
PaymentIdentifierType.BIP21, PaymentIdentifierType.OPENALIAS]

if pi.type == PaymentIdentifierType.BIP21:
assert 'amount' not in pi.bip21

if run_hook('abort_send', self):
return
outputs = self.payto_e.payment_identifier.get_onchain_outputs('!')
outputs = pi.get_onchain_outputs('!')
if not outputs:
return
make_tx = lambda fee_est, *, confirmed_only=False: self.wallet.make_unsigned_transaction(
Expand Down Expand Up @@ -446,10 +455,13 @@ def update_fields(self):
self.spend_max()

pi_unusable = pi.is_error() or (not self.wallet.has_lightning() and not pi.is_onchain())
is_spk_script = pi.type == PaymentIdentifierType.SPK and not pi.spk_is_address

amount_valid = is_spk_script or bool(self.amount_e.get_amount())

self.send_button.setEnabled(not pi_unusable and bool(self.amount_e.get_amount()) and not pi.has_expired())
self.save_button.setEnabled(not pi_unusable and pi.type not in [PaymentIdentifierType.LNURLP,
PaymentIdentifierType.LNADDR])
self.send_button.setEnabled(not pi_unusable and amount_valid and not pi.has_expired())
self.save_button.setEnabled(not pi_unusable and not is_spk_script and \
pi.type not in [PaymentIdentifierType.LNURLP, PaymentIdentifierType.LNADDR])

def _handle_payment_identifier(self):
self.update_fields()
Expand Down Expand Up @@ -479,11 +491,8 @@ def get_message(self):
def read_invoice(self) -> Optional[Invoice]:
if self.check_payto_line_and_show_errors():
return
amount_sat = self.read_amount()
if not amount_sat:
self.show_error(_('No amount'))
return

amount_sat = self.read_amount()
invoice = invoice_from_payment_identifier(
self.payto_e.payment_identifier, self.wallet, amount_sat, self.get_message())
if not invoice:
Expand Down Expand Up @@ -551,15 +560,19 @@ def pay_multiple_invoices(self, invoices):
def do_edit_invoice(self, invoice: 'Invoice'): # FIXME broken
assert not bool(invoice.get_amount_sat())
text = invoice.lightning_invoice if invoice.is_lightning() else invoice.get_address()
self.payto_e._on_input_btn(text)
self.set_payment_identifier(text)
self.amount_e.setFocus()
# disable save button, because it would create a new invoice
self.save_button.setEnabled(False)

def do_pay_invoice(self, invoice: 'Invoice'):
if not bool(invoice.get_amount_sat()):
self.show_error(_('No amount'))
return
pi = self.payto_e.payment_identifier
if pi.type == PaymentIdentifierType.SPK and not pi.spk_is_address:
pass
else:
self.show_error(_('No amount'))
return
if invoice.is_lightning():
self.pay_lightning_invoice(invoice)
else:
Expand Down
18 changes: 11 additions & 7 deletions electrum/payment_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def __init__(self, wallet: Optional['Abstract_Wallet'], text: str):
self.bolt11 = None # type: Optional[Invoice]
self.bip21 = None
self.spk = None
self.spk_is_address = False
#
self.emaillike = None
self.domainlike = None
Expand Down Expand Up @@ -258,9 +259,11 @@ def parse(self, text: str):
except InvoiceError as e:
self.logger.debug(self._get_error_from_invoiceerror(e))
self.set_state(PaymentIdentifierState.AVAILABLE)
elif scriptpubkey := self.parse_output(text):
elif self.parse_output(text)[0]:
scriptpubkey, is_address = self.parse_output(text)
self._type = PaymentIdentifierType.SPK
self.spk = scriptpubkey
self.spk_is_address = is_address
self.set_state(PaymentIdentifierState.AVAILABLE)
elif self.contacts and (contact := self.contacts.by_name(text)):
if contact['type'] == 'address':
Expand Down Expand Up @@ -464,7 +467,8 @@ def get_onchain_outputs(self, amount):
return [PartialTxOutput(scriptpubkey=self.spk, value=amount)]
elif self.bip21:
address = self.bip21.get('address')
scriptpubkey = self.parse_output(address)
scriptpubkey, is_address = self.parse_output(address)
assert is_address # unlikely, but make sure it is an address, not a script
return [PartialTxOutput(scriptpubkey=scriptpubkey, value=amount)]
else:
raise Exception('not onchain')
Expand Down Expand Up @@ -499,25 +503,25 @@ def parse_address_and_amount(self, line: str) -> 'PartialTxOutput':
x, y = line.split(',')
except ValueError:
raise Exception("expected two comma-separated values: (address, amount)") from None
scriptpubkey = self.parse_output(x)
scriptpubkey, is_address = self.parse_output(x)
if not scriptpubkey:
raise Exception('Invalid address')
amount = self.parse_amount(y)
return PartialTxOutput(scriptpubkey=scriptpubkey, value=amount)

def parse_output(self, x: str) -> bytes:
def parse_output(self, x: str) -> Tuple[bytes, bool]:
try:
address = self.parse_address(x)
return bytes.fromhex(bitcoin.address_to_script(address))
return bytes.fromhex(bitcoin.address_to_script(address)), True
except Exception as e:
pass
try:
script = self.parse_script(x)
return bytes.fromhex(script)
return bytes.fromhex(script), False
except Exception as e:
pass

# raise Exception("Invalid address or script.")
return None, False

def parse_script(self, x: str):
script = ''
Expand Down

0 comments on commit 307cf25

Please sign in to comment.