new scripts: bip70 and estimate_fee

This commit is contained in:
ThomasV 2015-02-16 14:05:04 +01:00
parent 89a52a8273
commit 69ee0bd0de
2 changed files with 68 additions and 0 deletions

59
scripts/bip70 Executable file
View File

@ -0,0 +1,59 @@
# create a BIP70 payment request signed with a certificate
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
chain_file = 'mychain.pem'
cert_file = 'mycert.pem'
amount = 1000000
address = "18U5kpCAU4s8weFF8Ps5n8HAfpdUjDVF64"
memo = "blah"
out_file = "payreq"
with open(chain_file, 'r') as f:
chain = tlslite.X509CertChain()
chain.parsePemList(f.read())
certificates = pb2.X509Certificates()
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)
with open(out_file,'wb') as f:
f.write(pr_string)
print "Payment request was written to file '%s'"%out_file

9
scripts/estimate_fee Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env python
import util, json
peers = util.get_peers()
results = util.send_request(peers, {'method':'blockchain.estimatefee','params':[1]})
print json.dumps(results, indent=4)