diff options
author | alpaylan | 2021-04-16 01:03:21 +0300 |
---|---|---|
committer | alpaylan | 2021-04-16 01:03:21 +0300 |
commit | b4212a90caa899785402c06d57216e75de0f1c88 (patch) | |
tree | 8adbe6eb6b451eee20d181f26ab771e0c5a920ee /scripts | |
parent | 82864341afc78b23b358cd775c70ffbfa0d0303f (diff) | |
parent | 72f8ae422eeb03ed87c7819af5d5e25758267b03 (diff) | |
download | gradecoin-b4212a90caa899785402c06d57216e75de0f1c88.tar.gz gradecoin-b4212a90caa899785402c06d57216e75de0f1c88.tar.bz2 gradecoin-b4212a90caa899785402c06d57216e75de0f1c88.zip |
Merge remote-tracking branch 'origin/main'
# Conflicts:
# src/schema.rs
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/python_client.py | 111 |
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 | ||
23 | import requests | ||
24 | from jwt import ( | ||
25 | JWT, | ||
26 | jwk_from_dict, | ||
27 | jwk_from_pem, | ||
28 | ) | ||
29 | from jwt.utils import get_int_from_datetime | ||
30 | from datetime import datetime, timedelta, timezone | ||
31 | |||
32 | |||
33 | def create_hashed_transaction(): | ||
34 | |||
35 | pass | ||
36 | |||
37 | def 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 | |||
54 | def post_register(): | ||
55 | credentials = { | ||
56 | "c": "", | ||
57 | "iv": "", | ||
58 | "key": "" | ||
59 | } | ||
60 | response = requests.post("localhost:8080/register", data=credentials) | ||
61 | |||
62 | |||
63 | def 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 | |||
77 | def 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 | |||
90 | def post_block(): | ||
91 | credentials = { | ||
92 | "c": "", | ||
93 | "iv": "", | ||
94 | "key": "" | ||
95 | } | ||
96 | response = requests.post("localhost:8080/register", params=credentials) | ||
97 | |||
98 | |||
99 | def get_transaction(): | ||
100 | response = requests.get("http://localhost:8080/transaction") | ||
101 | print(response.headers) | ||
102 | |||
103 | |||
104 | def get_block(): | ||
105 | response = requests.get("http://localhost:8080/block") | ||
106 | print(response.headers) | ||
107 | |||
108 | |||
23 | def encrypt(message, pub_key): | 109 | def 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 | |||
39 | MwIDAQAB | 125 | MwIDAQAB |
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 | ||