def certificateCreation():
    print art
    print "\nCertificate Creation Script - Block SSL\n"
    # create a key pair
    print "Creating a new key pair:"
    print "Warning: This is a pseudo-random generation.\n"
    k = crypto.PKey()
    k.generate_key(crypto.TYPE_RSA, 1024)

    # create a self-signed cert
    cert = crypto.X509()
    createCert(k, cert)

    open("certificate.crt",
         "wt").write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
    open("keys.key",
         "wt").write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k))
    print "\nCertificate created in file: certificate.crt"
    print "\nKeys saved in file: keys.key\n"

    ans2 = raw_input(
        "Do you have available satoshis in your Generation address? [Y]es [N]o, default: [Y]"
    )
    if ans2 == "Y" or ans2 == "y" or ans2 == "" or ans2 == " ":
        #Opening Generation private key from pem file
        if os.path.isfile('./Generation_Private.pem'):
            print "\nGeneration Private Key file exists."
            sk = SigningKey.from_pem(open("Generation_Private.pem").read())
            sk_string = sk.to_string()
            sk = str(sk_string)
            sk = sk.encode("hex")
        elif os.path.isfile('./Generation_Private.pem.enc'):
            print "\nGeneration Private Key encoded file exists."
            decrypt_file(key, "Generation_Private.pem.enc")
            print "\nDecrypting Generation Private Key..."
            print "Saving to Generation_Private.pem..."
            sk = SigningKey.from_pem(open("Generation_Private.pem").read())
            sk_string = sk.to_string()
            sk = str(sk_string)
            sk = sk.encode("hex")
        else:
            print "\nGeneration Private Key does not exist."
            print "\nPlease place the file in the script directory or run -i option for a new key pair.\n"
            sys.exit()
        try:
            recipient_address = open("Cert_Address.txt", "rb").read()
            blockchain_client = BlockchainInfoClient(
                "dacc6a40-1b8f-4dbb-afc7-bc9657603e83")
            send_to_address(
                recipient_address, 164887, sk,
                blockchain_client)  #make a ~10$ transactrion to cert address
            print "\nWait at least 20 minutes, and run the script with option -s to send the certificate to the blockchain."
        except Exception:
            print "\nNo balance in your Generation address.\n"
            print "Please load some bitcoins in order to submit your certificate.\n"

    else:
        print "Please load your Generation address and run the script again."

    sys.exit()
def certificateRevocation():
    print art
    print "\nCertificate Revocation Script - Block SSL\n"
    print "In which of your addresses do you still have access? default: [1]\n"
    print "\t1. All of the addresses. (Generation, Certificate, Revocation)"
    print "\t2. Only Certificate address.\n"
    print "\t3. Only Revocation address.\n"
    print "\t4. Revocation and Generation addresses.\n"
    ans = raw_input()
    blockchain_client = BlockchainInfoClient(
        "dacc6a40-1b8f-4dbb-afc7-bc9657603e83")
    if ans == "1" or ans == "" or ans == " " or ans == "2":
        address = open("Cert_Address.txt", "r").read()
        address = address.strip()
        url = "https://blockchain.info/balance?format=json&active=" + address
        r = requests.get(url)
        try:
            balance = r.json()[address]
            balance = str(balance)
            x = 1
            i = 19
            final_balance = ""
            while x == 1:
                if balance[i] == ",":
                    x += 1
                else:
                    final_balance = final_balance + balance[i]
                    i += 1
            print " Your Certificate address balance is: " + final_balance
            #Opening Generation private key from pem file
            if os.path.isfile('./Certificate_Private.pem'):
                print "\nCertificate Private Key file exists."
                sk = SigningKey.from_pem(
                    open("Certificate_Private.pem").read())
                sk_string = sk.to_string()
                sk = str(sk_string)
                sk = sk.encode("hex")
            elif os.path.isfile('./Certificate_Private.pem.enc'):
                print "\nCertificate Private Key encoded file exists."
                decrypt_file(key, "Certificate_Private.pem.enc")
                print "\nDecrypting Certificate Private Key..."
                print "Saving to Certificate_Private.pem..."
                sk = SigningKey.from_pem(
                    open("Certificate_Private.pem").read())
                sk_string = sk.to_string()
                sk = str(sk_string)
                sk = sk.encode("hex")
            else:
                print "\nCertificate Private Key does not exist."
                print "\nPlease place the .pem file in the script directory.\n"
                sys.exit()
        except ValueError, e:
            raise Exception('Invalid response from blockchain.info.')
        if ans == "1" or ans == "" or ans == " ":
            recepient_address = open("Gen_Address.txt", "rb").read()
            ans3 = raw_input("Which is your revocation reason?\n")
            size = len(ans3)
            while size > 75:
                print "String too long for OP_RETURN transaction, please repeat.\n"
                ans3 = raw_input("Which is your revocation reason?\n")
                size = len(ans3)
            data = "R1: " + ans3
        else:
            recepient_address = raw_input(
                "Give the address that you want to sent the certificate balance, for revocation purposes:\n"
            )
            data = "R2: No access to Generation address"
            #todo - check if the address is correct
        try:
            tx = make_op_return_tx(data,
                                   sk,
                                   blockchain_client,
                                   fee=1000,
                                   format='bin')
            broadcast_transaction(tx, blockchain_client)
            final_balance = final_balance - 1000
            send_to_address(recipient_address, final_balance, sk,
                            blockchain_client)
        except Exception:
            print "\nNo balance in your Certificate address.\n"
            print "If the Certificate address has 0 balance, it has been already been revoced.\n"
        if os.path.isfile('./Cert_Address.txt'):
            recepient_address = open("Cert_Address.txt", "rb").read()
        else:
            print "\nCert_Address.txt does not exist."
            recepient_address = raw_input(
                "Give the Certificate address of your certificate, for the Extreme revocation transaction:\n"
            )
        if ans == "3":
            data = "ER1: No Access to Generation and Certificate address"
        else:
            data = "ER2: No Access to Certificate address"
        #send all the balance to given address address
        print "\nYour revocation reason is: ", data
        print "\nAdding revocation reason to OP_RETURN..."
        try:
            send_to_address(recipient_address, 10000, sk, blockchain_client)
            tx = make_op_return_tx(data,
                                   sk,
                                   blockchain_client,
                                   fee=1000,
                                   format='bin')
            broadcast_transaction(tx, blockchain_client)
        except Exception:
            print "\nNo balance in your Revocation address.\n"
            print "Please load some bitcoins in order to submit your revocation reason.\n"
    sys.exit()


def createCert(k, cert):
    # create a self-signed cert
    country = raw_input("Country Name (2 letter code): ")