2
0

transaction.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. )
  71. return public_key
  72. # key_file = open(file, 'rb')
  73. # key_data = key_file.read()
  74. # key_file.close()
  75. # return key_data
  76. def hash(self, recipient=None):
  77. string_object = json.dumps(self.data, sort_keys=True)
  78. block_string = string_object.encode()
  79. # open the public key file
  80. pubkey = self.get_keys(recipient) #Sender public key - input field neccessary
  81. encrypted_data = pubkey.encrypt(
  82. block_string,
  83. padding.OAEP(
  84. mgf=padding.MGF1(algorithm=hashes.SHA256()),
  85. algorithm=hashes.SHA256(),
  86. label=None
  87. )
  88. )
  89. #these lines only neccessary for key_encryption - solve the problem of bytes
  90. # open the symmetric key file for encryoting the file
  91. #skey = open('symmetric.key','rb')
  92. #key = skey.read()
  93. # encrypt the symmetric key file with the public key
  94. #encrypted_key = rsa.encrypt(key,pubkey)
  95. self.hash_value = encrypted_data
  96. self.log(f'Hashed: {self.hash_value}')
  97. return encrypted_data
  98. def log(self, text):
  99. print(f'[ TRANS ] {text}')