summaryrefslogtreecommitdiffstats
path: root/scripts/python_client.py
blob: 2713f471b6f66f2163f9b5e6548d0c3f10cc473d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from Crypto.PublicKey import RSA
import json
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5
from Crypto import Random
from base64 import b64encode, b64decode
hash = "SHA-256"

# def newkeys(keysize):
#    random_generator = Random.new().read
#    key = RSA.generate(keysize, random_generator)
#    private, public = key, key.publickey()
#    return public, private

# def importKey(externKey):
#    return RSA.importKey(externKey)

# def getpublickey(priv_key):
#    return priv_key.publickey()


import requests
from jwt import (
    JWT,
    jwk_from_dict,
    jwk_from_pem,
)
from jwt.utils import get_int_from_datetime
from datetime import datetime, timedelta, timezone


def create_hashed_transaction():

    pass

def create_jwt():
    instance = JWT()
    message = {
        'tha': create_hashed_transaction(),
        'iat': get_int_from_datetime(datetime.now(timezone.utc)),
        'exp': get_int_from_datetime(
            datetime.now(timezone.utc) + timedelta(hours=1)),
    }

    with open('rsa_private_key.pem', 'rb') as fh:
        signing_key = jwk_from_pem(fh.read())

    compact_jws = instance.encode(message, signing_key, alg='RS256')

    return compact_jws


def post_register():
    credentials = {
        "c": "",
        "iv": "",
        "key": ""
    }
    response = requests.post("localhost:8080/register", data=credentials)


def post_transaction_from_bank():
    body = {
        "by": "{my_public_key}",
        "source": "{bank_public_key}",
        "target": "{my_public_key}",
        "amount": 0,
        "timestamp": get_int_from_datetime(datetime.now(timezone.utc)),
    }
    header = {'Content-Type': 'application/json', 'Authorization': f'Bearer {create_jwt()}'}

    response = requests.post("localhost:8080/transaction", headers=header, data=body)
    print(response.headers)
    print(response.content)

def post_transaction_to_user():
    body = {
        "by": "{my_public_key}",
        "source": "{my_public_key}",
        "target": "{user_public_key}",
        "amount": 0,
        "timestamp": get_int_from_datetime(datetime.now(timezone.utc)),
    }
    header = {'Content-Type': 'application/json', 'Authorization': f'Bearer {create_jwt()}'}

    response = requests.post("localhost:8080/transaction", headers=header, data=body)


def post_block():
    credentials = {
        "c": "",
        "iv": "",
        "key": ""
    }
    response = requests.post("localhost:8080/register", params=credentials)


def get_transaction():
    response = requests.get("http://localhost:8080/transaction")
    print(response.headers)


def get_block():
    response = requests.get("http://localhost:8080/block")
    print(response.headers)


def encrypt(message, pub_key):
    cipher = PKCS1_OAEP.new(pub_key)
    return cipher.encrypt(message)


if __name__ == "__main__":

    myself = {
        "student_id": "e2482057",
        "public_key": """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3agASpH/TplIAX0YBqmh
5q3Iq6+LcJtlUVWiI/v0T74XwYPZaJpAArHaiMUGXAWxmzfbvEo1wE9RzySYV/5k
QSpYDRekpOn0flIAQHORVbJ08s0udH6/c2AyAzqiwZbR1DRr7M90pSLvWvzHQT+c
kT6rXYcp9GlSAv3AXRw5ZYalbQf7ST/Mb4T8O1MRkAatzXg3T4x3XJ3uxHOletLL
SzsfY52kEn0uaFG6UI7UG50h8jcjqBxn+ETbn2YEZG5ecmPdYNakq2pqrdWXWMhE
AUd927qkxgg/nyyFqwxIbTxebxzpNX8IHMT8PgNdMxVMqnhBWxiw1nborY+pwGVL
MwIDAQAB
-----END PUBLIC KEY-----"""
    }
    # with open("../secrets/gradecoin.pub", "r") as fs:
    #     data = fs.read()
    #     pubkeyobj = RSA.importKey(data)
    #
    #     cipher = PKCS1_OAEP.new(pubkeyobj)
    #
    #     ser = json.dumps(myself, separators=(',', ':'))
    #
    #     a = cipher.encrypt(ser)
    #
    #     print(f"{a}")
    get_block()
    get_transaction()