transaction.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.hazmat.backends import default_backend
  9. from cryptography.hazmat.primitives import serialization
  10. from cryptography.hazmat.primitives import hashes
  11. from cryptography.hazmat.primitives.asymmetric import padding
  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.sender = None
  20. self.receiver = 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.sender = obj['sender']
  27. self.receiver = obj['receiver']
  28. def set_serialized_contract(self, serialized):
  29. if not self.locked:
  30. self.id = str(uuid.uuid1())
  31. self.data = serialized
  32. self.log('Serialized contract set')
  33. return True
  34. return False
  35. def set_contract(self, contract: Contract):
  36. if not self.locked:
  37. self.id = str(uuid.uuid1())
  38. self.data = contract.serialize()
  39. self.log(f'Contract set: {contract.id} | {contract.title}')
  40. return True
  41. return False
  42. def serialize(self, out_json=False):
  43. obj = {
  44. 'id': self.id,
  45. 'timestamp': self.timestamp,
  46. 'data': self.data,
  47. 'sender': self.sender,
  48. 'receiver': self.receiver
  49. }
  50. self.log(f'Serialized')
  51. if out_json:
  52. return json.dumps(obj, sort_keys=True)
  53. return obj
  54. def lock_hash_finish(self):
  55. if not self.locked:
  56. self.timestamp = int(time.time())
  57. self.locked = True
  58. self.hash()
  59. self.log('Locked')
  60. return True
  61. return False
  62. def get_keys(self, recipient):
  63. path = 'companies\\' + recipient + '_publickey.key'
  64. if not os.path.exists(path):
  65. make_keys(recipient)
  66. with open(path, 'rb') as f:
  67. public_key = serialization.load_pem_public_key(
  68. f.read(),
  69. backend=default_backend()
  70. return f
  71. # key_file = open(file, 'rb')
  72. # key_data = key_file.read()
  73. # key_file.close()
  74. # return key_data
  75. def hash(self, recipient=None):
  76. string_object = json.dumps(self.data, sort_keys=True)
  77. block_string = string_object.encode()
  78. # open the public key file
  79. pubkey = self.get_keys(recipient) #Sender public key - input field neccessary
  80. encrypted_data = pubkey.encrypt(
  81. block_string,
  82. padding.OAEP(
  83. mgf=padding.MGF1(algorithm=hashes.SHA256()),
  84. algorithm=hashes.SHA256(),
  85. label=None
  86. )
  87. )
  88. #these lines only neccessary for key_encryption - solve the problem of bytes
  89. # open the symmetric key file for encryoting the file
  90. #skey = open('symmetric.key','rb')
  91. #key = skey.read()
  92. # encrypt the symmetric key file with the public key
  93. #encrypted_key = rsa.encrypt(key,pubkey)
  94. self.hash_value = encrypted_data
  95. self.log(f'Hashed: {self.hash_value}')
  96. return encrypted_data
  97. def log(self, text):
  98. print(f'[ TRANS ] {text}')