#!/usr/bin/env python3
# create a BIP70 payment request signed with a certificate

import tlslite

from electroncash.transaction import Transaction
from electroncash import paymentrequest
from electroncash import paymentrequest_pb2 as pb2
from electroncash.address import Address

chain_file = 'mychain.pem'
cert_file = 'mycert.pem'
amount = 1000000
address = Address.from_string("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())

script = bytes.from_hex(Transaction.pay_script(address))

pr_string = paymentrequest.make_payment_request(amount, script, memo, rsakey)

with open(out_file,'wb') as f:
    f.write(pr_string)

print("Payment request was written to file '%s'"%out_file)
