transaction.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import hashlib
  2. import json
  3. import time
  4. import uuid
  5. import os
  6. from create_keys import make_keys
  7. from lib.contract import Contract
  8. from cryptography.fernet import Fernet
  9. import rsa
  10. class Transaction:
  11. def __init__(self):
  12. self.id = None
  13. self.data = None
  14. self.hash_value = None
  15. self.locked = False
  16. self.timestamp = 0
  17. self.sender = None
  18. self.receiver = None
  19. def from_serialized(self, jsonobj: str):
  20. obj = json.loads(jsonobj)
  21. self.id = obj['id']
  22. self.data = obj['data']
  23. self.timestamp = obj['timestamp']
  24. self.sender = obj['sender']
  25. self.receiver = obj['receiver']
  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: Contract):
  34. if not self.locked:
  35. self.id = str(uuid.uuid1())
  36. self.data = contract.serialize()
  37. self.log(f'Contract set: {contract.id} | {contract.title}')
  38. return True
  39. return False
  40. def serialize(self, out_json=False):
  41. obj = {
  42. 'id': self.id,
  43. 'timestamp': self.timestamp,
  44. 'data': self.data,
  45. 'sender': self.sender,
  46. 'receiver': self.receiver
  47. }
  48. self.log(f'Serialized')
  49. if out_json:
  50. return json.dumps(obj, sort_keys=True)
  51. return obj
  52. def lock_hash_finish(self):
  53. if not self.locked:
  54. self.timestamp = int(time.time())
  55. self.locked = True
  56. self.hash()
  57. self.log('Locked')
  58. return True
  59. return False
  60. def get_keys(self, recipient):
  61. path = 'companies\\' + recipient + '_publickey.key'
  62. if not os.path.exists(path):
  63. make_keys(recipient)
  64. with open(path, 'rb') as f:
  65. return rsa.PublicKey.load_pkcs1(f.read())
  66. # key_file = open(file, 'rb')
  67. # key_data = key_file.read()
  68. # key_file.close()
  69. # return key_data
  70. def hash(self, recipient=None):
  71. string_object = json.dumps(self.data, sort_keys=True)
  72. block_string = string_object.encode()
  73. # open the public key file
  74. pubkey = self.get_keys(recipient) #Sender public key - input field neccessary
  75. print(pubkey)
  76. # create the cipher
  77. cipher = Fernet(pubkey)
  78. # encrypt the data
  79. encrypted_data = cipher.encrypt(block_string)
  80. #these lines only neccessary for key_encryption - solve the problem of bytes
  81. # open the symmetric key file for encryoting the file
  82. #skey = open('symmetric.key','rb')
  83. #key = skey.read()
  84. # encrypt the symmetric key file with the public key
  85. #encrypted_key = rsa.encrypt(key,pubkey)
  86. self.hash_value = encrypted_data
  87. self.log(f'Hashed: {self.hash_value}')
  88. return encrypted_data
  89. def log(self, text):
  90. print(f'[ TRANS ] {text}')