Skip to content

Commit

Permalink
Make json storage consistent so client works properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Hirsh Agarwal committed Jun 16, 2023
1 parent 27696b5 commit 99efe64
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 37 deletions.
29 changes: 19 additions & 10 deletions blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import json
from base64 import b64encode

from cryptography.hazmat.primitives.serialization import load_pem_public_key

from records import record
from records.note import Note
from records.record import RecordType
from records.userdata import UserData
from user import User


Expand Down Expand Up @@ -45,27 +45,29 @@ def get_dict(self):
return {
'index': self.index,
'timestamp': str(self.timestamp),
'data': self.data,
'block_data': self.data,
'prev_hash': self.prev_hash,
'user_id': self.user_id,
'user_id': str(self.user_id),
'hash': self.hash,
'signed_hash': self.get_b64_string(self.signed_hash)
}

def get_json(self):
return json.dumps(self.get_dict())
serializable_dict: record = self.get_dict()
serializable_dict['block_data'] = self.data.get_dict()
return json.dumps(serializable_dict)

@staticmethod
def genesis_block():
return Block(0, dt.datetime.now(), "Genesis block transaction", " ", "root")

@staticmethod
def new_block(last_block, user_id, json_block_data):
def new_block(last_block, user_id, block_data: record):
# TODO: Assert json string
index = last_block.index + 1
timestamp = dt.datetime.now()
hash_block = last_block.hash
return Block(index, timestamp, json_block_data, hash_block, user_id)
return Block(index, timestamp, block_data, hash_block, user_id)

@staticmethod
def get_b64_string(message):
Expand All @@ -77,11 +79,19 @@ def get_b64_string(message):
def from_json(json_block):
return Block(json_block['index'],
json_block['timestamp'],
json_block['data'],
json_block['block_data'],
json_block['prev_hash'],
json_block['user_id']
)

@staticmethod
def record_from_json(json_string):
json_object = json.loads(json_string)
if json_object['record_type'] == RecordType.USER_DATA.name:
return UserData.json_deserialize_userdata(json_string)
elif json_object['record_type'] == RecordType.CLINICAL_NOTE.name:
return Note.json_deserialize_note(json_string)


class Chain:
def __init__(self):
Expand Down Expand Up @@ -122,8 +132,7 @@ def get_json(self):
@staticmethod
def verify_transaction(user_origin_block, new_block):
user_data = user_origin_block.data
public_key = load_pem_public_key(user_data['public_key'].encode('utf-8'))
User.verify_message(
new_block.signed_hash,
new_block.hash_block().encode('utf-8'),
public_key)
user_data.public_key)
33 changes: 11 additions & 22 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
import json
import uuid

import requests as requests
from requests import Response

from blockchain import Block
from records.userdata import UserData
from clienthelper import ClientHelper
from user import User


class HealthChainClient:
def __init__(self, host, port):
self.host = host
self.port = port
self.helper = ClientHelper(host, port)

def add_block(self, new_block: Block):
pass

def build_user_block(self, first_name, last_name, dob, user_object: User) -> Block:
tail_block_json = requests.get("http://{}:{}/get_tail_block"
.format(self.host, self.port)).json()
tail_block = Block.from_json(tail_block_json)
init_user_block = UserData(
first_name,
last_name,
dob,
user_object.public_key
)
user_block_serialized = init_user_block.json_serialize()
return Block.new_block(tail_block, user.user_id, user_block_serialized)
request_response = requests.post("http://{}:{}/add_block".format(self.host, self.port),
data=new_block.get_json())
return request_response.json()

def get_blockchain(self) -> Response:
return requests.get("http://{}:{}/get_blockchain"
.format(self.host, self.port))
.format(self.host, self.port)).json()


if __name__ == '__main__':
client = HealthChainClient('localhost', '8080')
user = User(uuid.uuid4())
block = client.build_user_block('Hirsh', 'Agarwal', '16/08/1997', user)
block_data = json.loads(block.data)
print(block_data)
decrypted_block = UserData.decrypt_user_data_block(block_data, user.private_key)
print(decrypted_block)
block = client.helper.build_user_block('Hirsh', 'Agarwal', '16/08/1997', user)
response = client.add_block(block)
print(response)
# decrypted_block = UserData.decrypt_user_data_block(block.data, user.private_key)
# print(client.get_blockchain())
23 changes: 23 additions & 0 deletions clienthelper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests

from blockchain import Block
from records.userdata import UserData
from user import User


class ClientHelper:
def __init__(self, host, port):
self.host = host
self.port = port

def build_user_block(self, first_name, last_name, dob, user_object: User) -> Block:
tail_block_json = requests.get("http://{}:{}/get_tail_block"
.format(self.host, self.port)).json()
tail_block = Block.from_json(tail_block_json)
init_user_block = UserData(
first_name,
last_name,
dob,
user_object.public_key
)
return Block.new_block(tail_block, user_object.user_id, init_user_block)
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
print(init_user_block.json_serialize())

# Persist user
new_block = Block.new_block(first_block, user.user_id, init_user_block.json_serialize())
new_block = Block.new_block(first_block, user.user_id, init_user_block)
blockchain.add_block(new_block)

# Make first note block
Expand Down
3 changes: 2 additions & 1 deletion node.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ def add_block(self):
add_block_string = request.body.read()
add_block_json = json.loads(add_block_string)
data = add_block_json['block_data']
record_object = Block.record_from_json(json.dumps(data))
user_id = add_block_json['user_id']
last_block = self.blockchain.read_tail_block()
new_block = Block.new_block(last_block, user_id, data)
new_block = Block.new_block(last_block, user_id, record_object)
self.blockchain.add_block(new_block)
return json.dumps(new_block.get_dict())

Expand Down
6 changes: 4 additions & 2 deletions records/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ def __init__(self, clinical_note):
self.clinical_note = clinical_note

def json_serialize(self):
data = {
return json.dumps(self.get_dict())

def get_dict(self):
return {
'record_type': self.record_type.name,
'clinical_note': self.clinical_note
}
return json.dumps(data)

@staticmethod
def json_deserialize_note(json_object):
Expand Down
4 changes: 4 additions & 0 deletions records/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def __init__(self, record_type):
def json_serialize(self):
pass

@abstractmethod
def get_dict(self):
pass


class RecordType(Enum):
NOTE = 1
Expand Down
5 changes: 4 additions & 1 deletion records/userdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def __str__(self):
return json.dumps(data)

def json_serialize(self):
return json.dumps(self.get_dict())

def get_dict(self):
first_name_encrypted = self.encrypt(self.first_name)
last_name_encrypted = self.encrypt(self.last_name)
data = {
Expand All @@ -37,7 +40,7 @@ def json_serialize(self):
'dob': self.dob,
'public_key': self.public_key.public_bytes(Encoding.PEM, PublicFormat.SubjectPublicKeyInfo).decode('utf-8')
}
return json.dumps(data)
return data

def encrypt(self, data):
encrypted_bytes = self.public_key.encrypt(data.encode('utf-8'),
Expand Down

0 comments on commit 99efe64

Please sign in to comment.