import hashlib import json import time import uuid from create_keys import get_keys, get_symmetric_key, make_keys from gui.encyption import encrypt_data from lib.contract import Contract class Transaction: def __init__(self): self.id = None self.data = None self.hash_value = None self.locked = False self.timestamp = 0 self.key = None self.encrypted_key = None def from_serialized(self, jsonobj: str): print('FROM SERIALIZED: \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n') obj = json.loads(jsonobj) self.id = obj['id'] self.data = obj['data'] self.timestamp = obj['timestamp'] self.key = obj['key'] self.encrypted_key = obj['encrypted_key'] def set_serialized_contract(self, serialized): if not self.locked: self.id = str(uuid.uuid1()) self.data = serialized self.log('Serialized contract set') return True return False def set_contract(self, contract: dict): if not self.locked: self.id = str(uuid.uuid1()) # self.data = contract.serialize() self.data = contract self.timestamp = time.time() self.key = contract['id'] # self.log(f'Contract set: {contract.id} | {contract.title}')' self.log(f'Contract set: {contract["id"]}') return True return False def serialize(self, out_json=False): obj = { 'id': self.id, 'timestamp': self.timestamp, 'data': self.data, 'key': self.key, 'encrypted_key': self.encrypted_key } self.log(f'Serialized') if out_json: return json.dumps(obj, sort_keys=True) return obj def lock_hash_finish(self): if not self.locked: self.timestamp = int(time.time()) self.locked = True self.hash() self.log('Locked') return True return False def hash(self, contract=None): string_object = json.dumps(self.data, sort_keys=True) block_string = string_object.encode() self.data, self.encrypted_key = encrypt_data(contract, block_string) self.log(f'Hashed: {self.hash_value}') def log(self, text): print(f'[ TRANS ] {text}')