From ec234105a5bfb9415e9f54ac10090556a908c4ff Mon Sep 17 00:00:00 2001 From: ThomasV Date: Wed, 7 May 2014 18:10:14 +0200 Subject: [PATCH] send error messages for payment requests --- gui/qt/__init__.py | 28 ++++++++++++++-------------- gui/qt/main_window.py | 16 +++++++++++++--- lib/paymentrequest.py | 24 ++++++++++++------------ 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/gui/qt/__init__.py b/gui/qt/__init__.py index 1848220f2..818400e66 100644 --- a/gui/qt/__init__.py +++ b/gui/qt/__init__.py @@ -172,22 +172,22 @@ class ElectrumGui: from electrum import paymentrequest except: print "cannot import paymentrequest" - return - def payment_request(): - pr = paymentrequest.PaymentRequest(request_url) - if pr.verify() or 1: - self.main_window.payment_request = pr - self.main_window.emit(SIGNAL('payment_request_ok')) - else: - self.main_window.emit(SIGNAL('payment_request_failed')) - - threading.Thread(target=payment_request).start() - self.main_window.tabs.setCurrentIndex(1) - else: - self.main_window.set_send(address, amount, label, message) + request_url = None - if self.lite_window: + if not request_url: + self.main_window.set_send(address, amount, label, message) self.lite_window.set_payment_fields(address, amount) + return + + def payment_request(): + self.payment_request = paymentrequest.PaymentRequest(request_url) + if self.payment_request.verify(): + self.main_window.emit(SIGNAL('payment_request_ok')) + else: + self.main_window.emit(SIGNAL('payment_request_error')) + + threading.Thread(target=payment_request).start() + self.main_window.prepare_for_payment_request() def main(self, url): diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py index d56a0900e..0f9c0f831 100644 --- a/gui/qt/main_window.py +++ b/gui/qt/main_window.py @@ -107,6 +107,7 @@ class ElectrumWindow(QMainWindow): self.config = config self.network = network + self.gui_object = gui_object self.tray = gui_object.tray self.go_lite = gui_object.go_lite self.lite = None @@ -157,6 +158,7 @@ class ElectrumWindow(QMainWindow): self.connect(self, QtCore.SIGNAL('send_tx2'), self.send_tx2) self.connect(self, QtCore.SIGNAL('send_tx3'), self.send_tx3) self.connect(self, QtCore.SIGNAL('payment_request_ok'), self.payment_request_ok) + self.connect(self, QtCore.SIGNAL('payment_request_error'), self.payment_request_error) self.history_list.setFocus(True) @@ -896,14 +898,22 @@ class ElectrumWindow(QMainWindow): - def payment_request_ok(self): + def prepare_for_payment_request(self): style = "QWidget { background-color:none;border:none;}" - self.payto_e.setText(self.payment_request.domain) + self.tabs.setCurrentIndex(1) self.payto_e.setReadOnly(True) self.payto_e.setStyleSheet(style) - self.amount_e.setText(self.format_amount(self.payment_request.get_amount())) self.amount_e.setReadOnly(True) + self.payto_e.setText(_("please wait...")) self.amount_e.setStyleSheet(style) + return True + + def payment_request_ok(self): + self.payto_e.setText(self.gui_object.payment_request.domain) + self.amount_e.setText(self.format_amount(self.gui_object.payment_request.get_amount())) + + def payment_request_error(self): + self.payto_e.setText(self.gui_object.payment_request.error) def set_send(self, address, amount, label, message): diff --git a/lib/paymentrequest.py b/lib/paymentrequest.py index 63cbeb79f..5df5d6bfa 100644 --- a/lib/paymentrequest.py +++ b/lib/paymentrequest.py @@ -55,6 +55,7 @@ class PaymentRequest: def __init__(self, url): self.url = url self.outputs = [] + self.error = "" def get_amount(self): return sum(map(lambda x:x[1], self.outputs)) @@ -74,7 +75,7 @@ class PaymentRequest: sig = paymntreq.signature if not sig: - print "No signature" + self.error = "No signature" return cert = paymentrequest_pb2.X509Certificates() @@ -101,8 +102,7 @@ class PaymentRequest: print "ERROR: No SAN data" if not validcert: ###TODO: check for wildcards - print "ERROR: Certificate Subject Domain Mismatch and SAN Mismatch" - print self.domain, x509_1.get_subject().CN + self.error = "ERROR: Certificate Subject Domain Mismatch and SAN Mismatch" return x509 = [] @@ -112,16 +112,16 @@ class PaymentRequest: for i in range(cert_num - 1): x509.append(X509.load_cert_der_string(cert.certificate[i+1])) if x509[i].check_ca() == 0: - print "ERROR: Supplied CA Certificate Error" + self.error = "ERROR: Supplied CA Certificate Error" return for i in range(cert_num - 1): if i == 0: if x509_1.verify(x509[i].get_pubkey()) != 1: - print "ERROR: Certificate not Signed by Provided CA Certificate Chain" + self.error = "ERROR: Certificate not Signed by Provided CA Certificate Chain" return else: if x509[i-1].verify(x509[i].get_pubkey()) != 1: - print "ERROR: CA Certificate not Signed by Provided CA Certificate Chain" + self.error = "ERROR: CA Certificate not Signed by Provided CA Certificate Chain" return supplied_CA_fingerprint = x509[cert_num-2].get_fingerprint() @@ -139,7 +139,7 @@ class PaymentRequest: print "ERROR: Supplied CA Not Found in Trusted CA Store." print "Payment will continue with manual verification." else: - print "ERROR: CA Certificate Chain Not Provided by Payment Processor" + self.error = "ERROR: CA Certificate Chain Not Provided by Payment Processor" return False paymntreq.signature = '' @@ -151,13 +151,13 @@ class PaymentRequest: elif paymntreq.pki_type == "x509+sha1": pubkey_1.reset_context(md="sha1") else: - print "ERROR: Unsupported PKI Type for Message Signature" + self.error = "ERROR: Unsupported PKI Type for Message Signature" return False pubkey_1.verify_init() pubkey_1.verify_update(s) if pubkey_1.verify_final(sig) != 1: - print "ERROR: Invalid Signature for Payment Request Data" + self.error = "ERROR: Invalid Signature for Payment Request Data" return False ### SIG Verified @@ -166,8 +166,8 @@ class PaymentRequest: pay_det.ParseFromString(paymntreq.serialized_payment_details) if pay_det.expires and pay_det.expires < int(time.time()): - print "ERROR: Payment Request has Expired." - #return False + self.error = "ERROR: Payment Request has Expired." + return False for o in pay_det.outputs: addr = transaction.get_address_from_output_script(o.script)[1] @@ -176,7 +176,7 @@ class PaymentRequest: if CA_match: print 'Signed By Trusted CA: ', CA_OU - return pay_det + return True