summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorYigit Sever2021-04-15 03:50:34 +0300
committerYigit Sever2021-04-15 03:50:34 +0300
commitee5dcba9046cdad96af673446165af0169fe15fe (patch)
tree93b70308ad1b5483513780cb82c1d2b839dbe3b3 /scripts
parent2b866db08bd20985a570c4f0f292aaecfe1ea052 (diff)
parent75d9257d89ebf2a544c8a5db0dc1d21b7f57464d (diff)
downloadgradecoin-ee5dcba9046cdad96af673446165af0169fe15fe.tar.gz
gradecoin-ee5dcba9046cdad96af673446165af0169fe15fe.tar.bz2
gradecoin-ee5dcba9046cdad96af673446165af0169fe15fe.zip
Merge remote-tracking branch 'origin/template' into template
Diffstat (limited to 'scripts')
-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