From 4ae6fb311f906dc5be26d60de5a0a41c72503004 Mon Sep 17 00:00:00 2001
From: alpaylan
Date: Thu, 15 Apr 2021 01:11:35 +0300
Subject: python_client halfway through and updated todo

---
 scripts/python_client.py | 111 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 99 insertions(+), 12 deletions(-)

(limited to 'scripts')

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"
 #    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)
@@ -39,18 +125,19 @@ 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}")
-
+    # 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()
 
 
 
-- 
cgit v1.2.3-70-g09d2