Browse Source

qt send tab: show friendlier error on mistyped bitcoin address

master
SomberNight 5 years ago
parent
commit
b95525896f
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 9
      electrum/gui/qt/main_window.py
  2. 23
      electrum/gui/qt/paytoedit.py

9
electrum/gui/qt/main_window.py

@ -1486,8 +1486,15 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
if not pr: if not pr:
errors = self.payto_e.get_errors() errors = self.payto_e.get_errors()
if errors: if errors:
if len(errors) == 1 and not errors[0].is_multiline:
err = errors[0]
self.show_warning(_("Failed to parse 'Pay to' line") + ":\n" +
f"{err.line_content[:40]}...\n\n"
f"{err.exc!r}")
else:
self.show_warning(_("Invalid Lines found:") + "\n\n" + self.show_warning(_("Invalid Lines found:") + "\n\n" +
'\n'.join([_("Line #") + f"{err.idx+1}: {err.line_content[:40]}... ({repr(err.exc)})" '\n'.join([_("Line #") +
f"{err.idx+1}: {err.line_content[:40]}... ({err.exc!r})"
for err in errors])) for err in errors]))
return True return True

23
electrum/gui/qt/paytoedit.py

@ -52,9 +52,10 @@ normal_style = "QPlainTextEdit { }"
class PayToLineError(NamedTuple): class PayToLineError(NamedTuple):
idx: int # index of line
line_content: str line_content: str
exc: Exception exc: Exception
idx: int = 0 # index of line
is_multiline: bool = False
class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger): class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
@ -93,7 +94,10 @@ class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
self.setStyleSheet(util.ColorScheme.RED.as_stylesheet(True)) self.setStyleSheet(util.ColorScheme.RED.as_stylesheet(True))
def parse_address_and_amount(self, line) -> PartialTxOutput: def parse_address_and_amount(self, line) -> PartialTxOutput:
try:
x, y = line.split(',') x, y = line.split(',')
except ValueError:
raise Exception("expected two comma-separated values: (address, amount)") from None
scriptpubkey = self.parse_output(x) scriptpubkey = self.parse_output(x)
amount = self.parse_amount(y) amount = self.parse_amount(y)
return PartialTxOutput(scriptpubkey=scriptpubkey, value=amount) return PartialTxOutput(scriptpubkey=scriptpubkey, value=amount)
@ -102,9 +106,14 @@ class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
try: try:
address = self.parse_address(x) address = self.parse_address(x)
return bfh(bitcoin.address_to_script(address)) return bfh(bitcoin.address_to_script(address))
except: except Exception:
pass
try:
script = self.parse_script(x) script = self.parse_script(x)
return bfh(script) return bfh(script)
except Exception:
pass
raise Exception("Invalid address or script.")
def parse_script(self, x): def parse_script(self, x):
script = '' script = ''
@ -150,25 +159,27 @@ class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
try: try:
self.win.parse_lightning_invoice(bolt11_invoice) self.win.parse_lightning_invoice(bolt11_invoice)
except LnDecodeException as e: except LnDecodeException as e:
self.errors.append(PayToLineError(idx=0, line_content=data, exc=e)) self.errors.append(PayToLineError(line_content=data, exc=e))
else: else:
self.lightning_invoice = bolt11_invoice self.lightning_invoice = bolt11_invoice
return return
try: try:
self.payto_scriptpubkey = self.parse_output(data) self.payto_scriptpubkey = self.parse_output(data)
except: except Exception as e:
pass self.errors.append(PayToLineError(line_content=data, exc=e))
if self.payto_scriptpubkey: if self.payto_scriptpubkey:
self.win.set_onchain(True) self.win.set_onchain(True)
self.win.lock_amount(False) self.win.lock_amount(False)
return return
# there are multiple lines
is_max = False is_max = False
for i, line in enumerate(lines): for i, line in enumerate(lines):
try: try:
output = self.parse_address_and_amount(line) output = self.parse_address_and_amount(line)
except Exception as e: except Exception as e:
self.errors.append(PayToLineError(idx=i, line_content=line.strip(), exc=e)) self.errors.append(PayToLineError(
idx=i, line_content=line.strip(), exc=e, is_multiline=True))
continue continue
outputs.append(output) outputs.append(output)
if output.value == '!': if output.value == '!':

Loading…
Cancel
Save