|
| 1 | +# create a BIP70 payment request signed with a certificate |
| 2 | + |
| 3 | +import tlslite |
| 4 | +import time |
| 5 | +import hashlib |
| 6 | + |
| 7 | +from electrum import paymentrequest_pb2 as pb2 |
| 8 | +from electrum.transaction import Transaction |
| 9 | +from electrum import bitcoin |
| 10 | +from electrum import x509 |
| 11 | + |
| 12 | + |
| 13 | +chain_file = 'mychain.pem' |
| 14 | +cert_file = 'mycert.pem' |
| 15 | +amount = 1000000 |
| 16 | +address = "18U5kpCAU4s8weFF8Ps5n8HAfpdUjDVF64" |
| 17 | +memo = "blah" |
| 18 | +out_file = "payreq" |
| 19 | + |
| 20 | + |
| 21 | +with open(chain_file, 'r') as f: |
| 22 | + chain = tlslite.X509CertChain() |
| 23 | + chain.parsePemList(f.read()) |
| 24 | + |
| 25 | +certificates = pb2.X509Certificates() |
| 26 | +certificates.certificate.extend(map(lambda x: str(x.bytes), chain.x509List)) |
| 27 | + |
| 28 | +with open(cert_file, 'r') as f: |
| 29 | + rsakey = tlslite.utils.python_rsakey.Python_RSAKey.parsePEM(f.read()) |
| 30 | + |
| 31 | + |
| 32 | +def make_payment_request(amount, script, memo): |
| 33 | + """Generates a http PaymentRequest object""" |
| 34 | + pd = pb2.PaymentDetails() |
| 35 | + pd.outputs.add(amount=amount, script=script) |
| 36 | + now = int(time.time()) |
| 37 | + pd.time = now |
| 38 | + pd.expires = now + 15*60 |
| 39 | + pd.memo = memo |
| 40 | + pd.payment_url = 'http://payment_ack.url' |
| 41 | + pr = pb2.PaymentRequest() |
| 42 | + pr.serialized_payment_details = pd.SerializeToString() |
| 43 | + pr.pki_type = 'x509+sha256' |
| 44 | + pr.pki_data = certificates.SerializeToString() |
| 45 | + pr.signature = '' |
| 46 | + msgBytes = bytearray(pr.SerializeToString()) |
| 47 | + hashBytes = bytearray(hashlib.sha256(msgBytes).digest()) |
| 48 | + sig = rsakey.sign(x509.PREFIX_RSA_SHA256 + hashBytes) |
| 49 | + pr.signature = bytes(sig) |
| 50 | + return pr.SerializeToString() |
| 51 | + |
| 52 | + |
| 53 | +script = Transaction.pay_script('address', address).decode('hex') |
| 54 | + |
| 55 | +pr_string = make_payment_request(amount, script, memo) |
| 56 | +with open(out_file,'wb') as f: |
| 57 | + f.write(pr_string) |
| 58 | + |
| 59 | +print "Payment request was written to file '%s'"%out_file |
0 commit comments