2
0

transaction.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import hashlib
  2. import json
  3. import time
  4. import uuid
  5. from create_keys import make_keys, get_keys
  6. from lib.contract import Contract
  7. from cryptography.hazmat.backends import default_backend
  8. from cryptography.hazmat.primitives import serialization
  9. from cryptography.hazmat.primitives import hashes
  10. from cryptography.hazmat.primitives.asymmetric import padding
  11. from cryptography.fernet import Fernet
  12. class Transaction:
  13. def __init__(self):
  14. self.id = None
  15. self.data = None
  16. self.hash_value = None
  17. self.locked = False
  18. self.timestamp = 0
  19. self.key = None
  20. def from_serialized(self, jsonobj: str):
  21. obj = json.loads(jsonobj)
  22. self.id = obj['id']
  23. self.data = obj['data']
  24. self.timestamp = obj['timestamp']
  25. self.key = obj['key']
  26. def set_serialized_contract(self, serialized):
  27. if not self.locked:
  28. self.id = str(uuid.uuid1())
  29. self.data = serialized
  30. self.log('Serialized contract set')
  31. return True
  32. return False
  33. def set_contract(self, contract: dict):
  34. if not self.locked:
  35. self.id = str(uuid.uuid1())
  36. # self.data = contract.serialize()
  37. self.data = contract
  38. self.timestamp = time.time()
  39. self.key = contract['id']
  40. # self.log(f'Contract set: {contract.id} | {contract.title}')'
  41. self.log(f'Contract set: {contract["id"]}')
  42. return True
  43. return False
  44. def serialize(self, out_json=False):
  45. obj = {
  46. 'id': self.id,
  47. 'timestamp': self.timestamp,
  48. 'data': self.data,
  49. 'key': self.key,
  50. }
  51. self.log(f'Serialized')
  52. if out_json:
  53. return json.dumps(obj, sort_keys=True)
  54. return obj
  55. def lock_hash_finish(self):
  56. if not self.locked:
  57. self.timestamp = int(time.time())
  58. self.locked = True
  59. self.hash()
  60. self.log('Locked')
  61. return True
  62. return False
  63. def hash(self, contract=None):
  64. string_object = json.dumps(self.data, sort_keys=True)
  65. block_string = string_object.encode()
  66. # open the public key file
  67. symmetric_key = get_symmetric_key(contract)
  68. pubkey = get_keys(contract)
  69. chiper = Fernet(symmetric_key)
  70. encrypted_data = chiper.encrypt(block_string)
  71. encrypted_key = rsa.encrypt(symmetric_key,pubkey) # we need an extra field in the transaction
  72. # encrypted_data = pubkey.encrypt(
  73. # block_string,
  74. # padding.OAEP(
  75. # mgf=padding.MGF1(algorithm=hashes.SHA256()),
  76. # algorithm=hashes.SHA256(),
  77. # label=None
  78. # )
  79. # )
  80. self.hash_value = encrypted_data
  81. self.log(f'Hashed: {self.hash_value}')
  82. self.data = encrypted_data
  83. def log(self, text):
  84. print(f'[ TRANS ] {text}')