| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import hashlib
- import json
- import time
- 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
- def set_contract(self, contract: Contract):
- if not self.locked:
- self.data = contract.serialize()
- self.log(f'Contract set: {contract.id} | {contract.title}')
- return True
- return False
- def serialize(self):
- obj = {
- 'id': self.id,
- 'timestamp': self.timestamp,
- 'data': self.data
- }
- self.log(f'Serialized')
- 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):
- string_object = json.dumps(self.data, sort_keys=True)
- block_string = string_object.encode()
- raw_hash = hashlib.sha256(block_string)
- hex_hash = raw_hash.hexdigest()
- self.hash_value = hex_hash
- self.log(f'Hashed: {self.hash_value}')
- return hex_hash
- def log(self, text):
- print(f'[ TRANS ] {text}')
|