3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2014 Thomas Voegtlin
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
33 import paymentrequest_pb2
35 sys.exit("Error: could not find paymentrequest_pb2.py. Create it with 'protoc --proto_path=lib/ --python_out=lib/ lib/paymentrequest.proto'")
40 sys.exit("Error: requests does not seem to be installed. Try 'sudo pip install requests'")
49 REQUEST_HEADERS = {'Accept': 'application/bitcoin-paymentrequest', 'User-Agent': 'Electrum'}
50 ACK_HEADERS = {'Content-Type':'application/bitcoin-payment','Accept':'application/bitcoin-paymentack','User-Agent':'Electrum'}
54 ca_path = os.path.expanduser("~/.electrum/ca/ca-bundle.crt")
59 def load_certificates():
61 ca_f = open(ca_path, 'r')
63 print "ERROR: Could not open %s"%ca_path
64 print "ca-bundle.crt file should be placed in ~/.electrum/ca/ca-bundle.crt"
65 print "Documentation on how to download or create the file here: http://curl.haxx.se/docs/caextract.html"
66 print "Payment will continue with manual verification."
70 if line == "-----BEGIN CERTIFICATE-----\n":
74 if line == "-----END CERTIFICATE-----\n":
78 except Exception as e:
79 util.print_error("cannot parse cert:", e)
80 ca_list[x.getFingerprint()] = x
82 util.print_error("%d certificates"%len(ca_list))
90 def __init__(self, config):
94 self.dir_path = os.path.join( self.config.path, 'requests')
95 if not os.path.exists(self.dir_path):
96 os.mkdir(self.dir_path)
100 u = urlparse.urlparse(url)
101 self.domain = u.netloc
103 connection = httplib.HTTPConnection(u.netloc) if u.scheme == 'http' else httplib.HTTPSConnection(u.netloc)
104 connection.request("GET",u.geturl(), headers=REQUEST_HEADERS)
105 response = connection.getresponse()
107 self.error = "cannot read url"
113 self.error = "cannot read"
116 self.id = bitcoin.sha256(r)[0:16].encode('hex')
117 filename = os.path.join(self.dir_path, self.id)
118 with open(filename,'w') as f:
124 def get_status(self):
131 def read_file(self, key):
132 filename = os.path.join(self.dir_path, key)
133 with open(filename,'r') as f:
136 assert key == bitcoin.sha256(r)[0:16].encode('hex')
143 self.data = paymentrequest_pb2.PaymentRequest()
144 self.data.ParseFromString(r)
146 self.error = "cannot parse payment request"
153 self.error = "Trusted certificate authorities list not found"
156 paymntreq = self.data
157 if not paymntreq.signature:
158 self.error = "No signature"
161 cert = paymentrequest_pb2.X509Certificates()
162 cert.ParseFromString(paymntreq.pki_data)
163 cert_num = len(cert.certificate)
166 for i in range(cert_num):
168 x.parseBinary(bytearray(cert.certificate[i]))
172 if not x.check_name(self.domain):
173 self.error = "Certificate Domain Mismatch"
177 self.error = "ERROR: Supplied CA Certificate Error"
181 self.error = "ERROR: CA Certificate Chain Not Provided by Payment Processor"
184 for i in range(1, cert_num):
186 prev_x = x509_chain[i-1]
188 algo, sig, data = prev_x.extract_sig()
189 if algo.getComponentByName('algorithm') != x509.ALGO_RSA_SHA1:
190 self.error = "Algorithm not suported"
193 sig = bytearray(sig[5:])
195 verify = pubkey.hashAndVerify(sig, data)
197 self.error = "Certificate not Signed by Provided CA Certificate Chain"
200 ca = x509_chain[cert_num-1]
201 supplied_CA_fingerprint = ca.getFingerprint()
202 supplied_CA_names = ca.extract_names()
203 CA_OU = supplied_CA_names['OU']
205 x = ca_list.get(supplied_CA_fingerprint)
208 names = x.extract_names()
210 if names['CN'] != supplied_CA_names['CN']:
211 print "ERROR: Trusted CA CN Mismatch; however CA has trusted fingerprint"
212 print "Payment will continue with manual verification."
216 pubkey0 = x509_chain[0].publicKey
217 sig = paymntreq.signature
218 paymntreq.signature = ''
219 s = paymntreq.SerializeToString()
220 sigBytes = bytearray(sig)
221 msgBytes = bytearray(s)
223 if paymntreq.pki_type == "x509+sha256":
224 hashBytes = bytearray(hashlib.sha256(msgBytes).digest())
225 prefixBytes = bytearray([0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05,0x00,0x04,0x20])
226 verify = pubkey0.verify(sigBytes, prefixBytes + hashBytes)
227 elif paymntreq.pki_type == "x509+sha1":
228 verify = pubkey0.hashAndVerify(sigBytes, msgBytes)
230 self.error = "ERROR: Unsupported PKI Type for Message Signature"
234 self.error = "ERROR: Invalid Signature for Payment Request Data"
238 self.details = pay_det = paymentrequest_pb2.PaymentDetails()
239 self.details.ParseFromString(paymntreq.serialized_payment_details)
241 for o in pay_det.outputs:
242 addr = transaction.get_address_from_output_script(o.script)[1]
243 self.outputs.append( (addr, o.amount) )
245 self.memo = self.details.memo
248 self.status = 'Signed by Trusted CA:\n' + CA_OU
250 self.status = "Supplied CA Not Found in Trusted CA Store."
252 self.payment_url = self.details.payment_url
256 def has_expired(self):
257 return self.details.expires and self.details.expires < int(time.time())
259 def get_expiration_date(self):
260 return self.details.expires
262 def get_amount(self):
263 return sum(map(lambda x:x[1], self.outputs))
265 def get_domain(self):
274 def get_outputs(self):
275 return self.outputs[:]
277 def send_ack(self, raw_tx, refund_addr):
279 pay_det = self.details
280 if not self.details.payment_url:
281 return False, "no url"
283 paymnt = paymentrequest_pb2.Payment()
284 paymnt.merchant_data = pay_det.merchant_data
285 paymnt.transactions.append(raw_tx)
287 ref_out = paymnt.refund_to.add()
288 ref_out.script = transaction.Transaction.pay_script(refund_addr)
289 paymnt.memo = "Paid using Electrum"
290 pm = paymnt.SerializeToString()
292 payurl = urlparse.urlparse(pay_det.payment_url)
294 r = requests.post(payurl.geturl(), data=pm, headers=ACK_HEADERS, verify=ca_path)
295 except requests.exceptions.SSLError:
296 print "Payment Message/PaymentACK verify Failed"
298 r = requests.post(payurl.geturl(), data=pm, headers=ACK_HEADERS, verify=False)
299 except Exception as e:
301 return False, "Payment Message/PaymentACK Failed"
303 if r.status_code >= 500:
304 return False, r.reason
307 paymntack = paymentrequest_pb2.PaymentACK()
308 paymntack.ParseFromString(r.content)
310 return False, "PaymentACK could not be processed. Payment was sent; please manually verify that payment was received."
312 print "PaymentACK message received: %s" % paymntack.memo
313 return True, paymntack.memo
317 if __name__ == "__main__":
322 print "usage: %s url"%sys.argv[0]
323 print "example url: \"bitcoin:mpu3yTLdqA1BgGtFUwkVJmhnU3q5afaFkf?r=https%3A%2F%2Fbitcoincore.org%2F%7Egavin%2Ff.php%3Fh%3D2a828c05b8b80dc440c80a5d58890298&amount=1\""
326 address, amount, label, message, request_url, url = util.parse_url(uri)
327 pr = PaymentRequest(request_url)
331 print 'Payment Request Verified Domain: ', pr.domain
332 print 'outputs', pr.outputs
333 print 'Payment Memo: ', pr.details.memo
336 pr.send_ack(tx, refund_addr = "1vXAXUnGitimzinpXrqDWVU4tyAAQ34RA")