| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import hashlib
- import json
- import time
- import uuid
- from create_keys import make_keys, get_keys
- from lib.contract import Contract
- from cryptography.hazmat.backends import default_backend
- from cryptography.hazmat.primitives import serialization
- from cryptography.hazmat.primitives import hashes
- from cryptography.hazmat.primitives.asymmetric import padding
- class Transaction:
- def __init__(self):
- self.id = None
- self.data = None
- self.hash_value = None
- self.locked = False
- self.timestamp = 0
- self.key = None
- def from_serialized(self, jsonobj: str):
- obj = json.loads(jsonobj)
- self.id = obj['id']
- self.data = obj['data']
- self.timestamp = obj['timestamp']
- self.key = obj['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,
- }
- 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()
- # open the public key file
- pubkey = get_keys(contract) #Sender public key - input field neccessary
- encrypted_data = pubkey.encrypt(
- block_string,
- padding.OAEP(
- mgf=padding.MGF1(algorithm=hashes.SHA256()),
- algorithm=hashes.SHA256(),
- label=None
- )
- )
- self.hash_value = encrypted_data
- self.log(f'Hashed: {self.hash_value}')
- self.data = encrypted_data
- def log(self, text):
- print(f'[ TRANS ] {text}')
|