commit b95525896f5e0e8d92294aa6a72ac8899dc27c87
parent 8e9d6a4c913df9759fc54f0829d8569f223b35aa
Author: SomberNight <somber.night@protonmail.com>
Date: Sun, 25 Oct 2020 04:24:31 +0100
qt send tab: show friendlier error on mistyped bitcoin address
Diffstat:
2 files changed, 29 insertions(+), 11 deletions(-)
diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py
@@ -1486,9 +1486,16 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
if not pr:
errors = self.payto_e.get_errors()
if errors:
- self.show_warning(_("Invalid Lines found:") + "\n\n" +
- '\n'.join([_("Line #") + f"{err.idx+1}: {err.line_content[:40]}... ({repr(err.exc)})"
- for err in 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" +
+ '\n'.join([_("Line #") +
+ f"{err.idx+1}: {err.line_content[:40]}... ({err.exc!r})"
+ for err in errors]))
return True
if self.payto_e.is_alias and self.payto_e.validated is False:
diff --git a/electrum/gui/qt/paytoedit.py b/electrum/gui/qt/paytoedit.py
@@ -52,9 +52,10 @@ normal_style = "QPlainTextEdit { }"
class PayToLineError(NamedTuple):
- idx: int # index of line
line_content: str
exc: Exception
+ idx: int = 0 # index of line
+ is_multiline: bool = False
class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
@@ -93,7 +94,10 @@ class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
self.setStyleSheet(util.ColorScheme.RED.as_stylesheet(True))
def parse_address_and_amount(self, line) -> PartialTxOutput:
- x, y = line.split(',')
+ try:
+ x, y = line.split(',')
+ except ValueError:
+ raise Exception("expected two comma-separated values: (address, amount)") from None
scriptpubkey = self.parse_output(x)
amount = self.parse_amount(y)
return PartialTxOutput(scriptpubkey=scriptpubkey, value=amount)
@@ -102,9 +106,14 @@ class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
try:
address = self.parse_address(x)
return bfh(bitcoin.address_to_script(address))
- except:
+ except Exception:
+ pass
+ try:
script = self.parse_script(x)
return bfh(script)
+ except Exception:
+ pass
+ raise Exception("Invalid address or script.")
def parse_script(self, x):
script = ''
@@ -150,25 +159,27 @@ class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
try:
self.win.parse_lightning_invoice(bolt11_invoice)
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:
self.lightning_invoice = bolt11_invoice
return
try:
self.payto_scriptpubkey = self.parse_output(data)
- except:
- pass
+ except Exception as e:
+ self.errors.append(PayToLineError(line_content=data, exc=e))
if self.payto_scriptpubkey:
self.win.set_onchain(True)
self.win.lock_amount(False)
- return
+ return
+ # there are multiple lines
is_max = False
for i, line in enumerate(lines):
try:
output = self.parse_address_and_amount(line)
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
outputs.append(output)
if output.value == '!':