aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/python_client.py
diff options
context:
space:
mode:
authoralpaylan2021-04-15 01:11:35 +0300
committeralpaylan2021-04-15 01:11:35 +0300
commit4ae6fb311f906dc5be26d60de5a0a41c72503004 (patch)
treeaae303a26fa668e899530f6563f89ea705b3d920 /scripts/python_client.py
parent7f91412ddf3895214a8b56ac520ea7a60c0d24a1 (diff)
downloadgradecoin-4ae6fb311f906dc5be26d60de5a0a41c72503004.tar.gz
gradecoin-4ae6fb311f906dc5be26d60de5a0a41c72503004.tar.bz2
gradecoin-4ae6fb311f906dc5be26d60de5a0a41c72503004.zip
python_client halfway through and updated todo
Diffstat (limited to 'scripts/python_client.py')
-rw-r--r--scripts/python_client.py111
1 files changed, 99 insertions, 12 deletions
diff --git a/scripts/python_client.py b/scripts/python_client.py
index fe96cc2..2713f47 100644
--- a/scripts/python_client.py
+++ b/scripts/python_client.py
@@ -20,6 +20,92 @@ hash = "SHA-256"
20# return priv_key.publickey() 20# return priv_key.publickey()
21 21
22 22
23import requests
24from jwt import (
25 JWT,
26 jwk_from_dict,
27 jwk_from_pem,
28)
29from jwt.utils import get_int_from_datetime
30from datetime import datetime, timedelta, timezone
31
32
33def create_hashed_transaction():
34
35 pass
36
37def create_jwt():
38 instance = JWT()
39 message = {
40 'tha': create_hashed_transaction(),
41 'iat': get_int_from_datetime(datetime.now(timezone.utc)),
42 'exp': get_int_from_datetime(
43 datetime.now(timezone.utc) + timedelta(hours=1)),
44 }
45
46 with open('rsa_private_key.pem', 'rb') as fh:
47 signing_key = jwk_from_pem(fh.read())
48
49 compact_jws = instance.encode(message, signing_key, alg='RS256')
50
51 return compact_jws
52
53
54def post_register():
55 credentials = {
56 "c": "",
57 "iv": "",
58 "key": ""
59 }
60 response = requests.post("localhost:8080/register", data=credentials)
61
62
63def post_transaction_from_bank():
64 body = {
65 "by": "{my_public_key}",
66 "source": "{bank_public_key}",
67 "target": "{my_public_key}",
68 "amount": 0,
69 "timestamp": get_int_from_datetime(datetime.now(timezone.utc)),
70 }
71 header = {'Content-Type': 'application/json', 'Authorization': f'Bearer {create_jwt()}'}
72
73 response = requests.post("localhost:8080/transaction", headers=header, data=body)
74 print(response.headers)
75 print(response.content)
76
77def post_transaction_to_user():
78 body = {
79 "by": "{my_public_key}",
80 "source": "{my_public_key}",
81 "target": "{user_public_key}",
82 "amount": 0,
83 "timestamp": get_int_from_datetime(datetime.now(timezone.utc)),
84 }
85 header = {'Content-Type': 'application/json', 'Authorization': f'Bearer {create_jwt()}'}
86
87 response = requests.post("localhost:8080/transaction", headers=header, data=body)
88
89
90def post_block():
91 credentials = {
92 "c": "",
93 "iv": "",
94 "key": ""
95 }
96 response = requests.post("localhost:8080/register", params=credentials)
97
98
99def get_transaction():
100 response = requests.get("http://localhost:8080/transaction")
101 print(response.headers)
102
103
104def get_block():
105 response = requests.get("http://localhost:8080/block")
106 print(response.headers)
107
108
23def encrypt(message, pub_key): 109def encrypt(message, pub_key):
24 cipher = PKCS1_OAEP.new(pub_key) 110 cipher = PKCS1_OAEP.new(pub_key)
25 return cipher.encrypt(message) 111 return cipher.encrypt(message)
@@ -39,18 +125,19 @@ AUd927qkxgg/nyyFqwxIbTxebxzpNX8IHMT8PgNdMxVMqnhBWxiw1nborY+pwGVL
39MwIDAQAB 125MwIDAQAB
40-----END PUBLIC KEY-----""" 126-----END PUBLIC KEY-----"""
41 } 127 }
42 with open("../secrets/gradecoin.pub", "r") as fs: 128 # with open("../secrets/gradecoin.pub", "r") as fs:
43 data = fs.read() 129 # data = fs.read()
44 pubkeyobj = RSA.importKey(data) 130 # pubkeyobj = RSA.importKey(data)
45 131 #
46 cipher = PKCS1_OAEP.new(pubkeyobj) 132 # cipher = PKCS1_OAEP.new(pubkeyobj)
47 133 #
48 ser = json.dumps(myself, separators=(',', ':')) 134 # ser = json.dumps(myself, separators=(',', ':'))
49 135 #
50 a = cipher.encrypt(ser) 136 # a = cipher.encrypt(ser)
51 137 #
52 print(f"{a}") 138 # print(f"{a}")
53 139 get_block()
140 get_transaction()
54 141
55 142
56 143