electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit be5731d463235d843b9a7d2194cb2171cc38a05f
parent 1a768427307f16cdff1801605a1c7c3d20eb4edd
Author: ThomasV <thomasv@gitorious>
Date:   Sun,  5 Apr 2015 18:57:00 +0200

move make_payment_request to lib

Diffstat:
Mlib/paymentrequest.py | 33++++++++++++++++++++++++++++-----
Mscripts/bip70 | 29++++-------------------------
2 files changed, 32 insertions(+), 30 deletions(-)

diff --git a/lib/paymentrequest.py b/lib/paymentrequest.py @@ -30,7 +30,7 @@ import urlparse import requests try: - import paymentrequest_pb2 + import paymentrequest_pb2 as pb2 except ImportError: sys.exit("Error: could not find paymentrequest_pb2.py. Create it with 'protoc --proto_path=lib/ --python_out=lib/ lib/paymentrequest.proto'") @@ -101,7 +101,7 @@ class PaymentRequest: def parse(self, r): try: - self.data = paymentrequest_pb2.PaymentRequest() + self.data = pb2.PaymentRequest() self.data.ParseFromString(r) except: self.error = "cannot parse payment request" @@ -119,7 +119,7 @@ class PaymentRequest: self.error = "No signature" return - cert = paymentrequest_pb2.X509Certificates() + cert = pb2.X509Certificates() cert.ParseFromString(paymntreq.pki_data) cert_num = len(cert.certificate) @@ -209,7 +209,7 @@ class PaymentRequest: return False ### SIG Verified - self.details = pay_det = paymentrequest_pb2.PaymentDetails() + self.details = pay_det = pb2.PaymentDetails() self.details.ParseFromString(paymntreq.serialized_payment_details) for o in pay_det.outputs: @@ -278,7 +278,7 @@ class PaymentRequest: return False, r.reason try: - paymntack = paymentrequest_pb2.PaymentACK() + paymntack = pb2.PaymentACK() paymntack.ParseFromString(r.content) except Exception: return False, "PaymentACK could not be processed. Payment was sent; please manually verify that payment was received." @@ -288,6 +288,29 @@ class PaymentRequest: +def make_payment_request(amount, script, memo, rsakey=None): + """Generates a http PaymentRequest object""" + pd = pb2.PaymentDetails() + pd.outputs.add(amount=amount, script=script) + now = int(time.time()) + pd.time = now + pd.expires = now + 15*60 + pd.memo = memo + #pd.payment_url = 'http://payment_ack.url' + pr = pb2.PaymentRequest() + pr.serialized_payment_details = pd.SerializeToString() + pr.signature = '' + if rsakey: + pr.pki_type = 'x509+sha256' + pr.pki_data = certificates.SerializeToString() + msgBytes = bytearray(pr.SerializeToString()) + hashBytes = bytearray(hashlib.sha256(msgBytes).digest()) + sig = rsakey.sign(x509.PREFIX_RSA_SHA256 + hashBytes) + pr.signature = bytes(sig) + return pr.SerializeToString() + + + if __name__ == "__main__": util.set_verbosity(True) diff --git a/scripts/bip70 b/scripts/bip70 @@ -4,11 +4,11 @@ import tlslite import time import hashlib -from electrum import paymentrequest_pb2 as pb2 from electrum.transaction import Transaction from electrum import bitcoin from electrum import x509 - +from electrum import paymentrequest +from electrum import paymentrequest_pb2 as pb2 chain_file = 'mychain.pem' cert_file = 'mycert.pem' @@ -28,31 +28,10 @@ certificates.certificate.extend(map(lambda x: str(x.bytes), chain.x509List)) with open(cert_file, 'r') as f: rsakey = tlslite.utils.python_rsakey.Python_RSAKey.parsePEM(f.read()) - -def make_payment_request(amount, script, memo): - """Generates a http PaymentRequest object""" - pd = pb2.PaymentDetails() - pd.outputs.add(amount=amount, script=script) - now = int(time.time()) - pd.time = now - pd.expires = now + 15*60 - pd.memo = memo - pd.payment_url = 'http://payment_ack.url' - pr = pb2.PaymentRequest() - pr.serialized_payment_details = pd.SerializeToString() - pr.pki_type = 'x509+sha256' - pr.pki_data = certificates.SerializeToString() - pr.signature = '' - msgBytes = bytearray(pr.SerializeToString()) - hashBytes = bytearray(hashlib.sha256(msgBytes).digest()) - sig = rsakey.sign(x509.PREFIX_RSA_SHA256 + hashBytes) - pr.signature = bytes(sig) - return pr.SerializeToString() - - script = Transaction.pay_script('address', address).decode('hex') -pr_string = make_payment_request(amount, script, memo) +pr_string = paymentrequest.make_payment_request(amount, script, memo, rsakey) + with open(out_file,'wb') as f: f.write(pr_string)