2
0

transaction.py 2.9 KB

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