data.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import json
  2. import os
  3. import time
  4. from tkinter import *
  5. import rpyc
  6. from create_keys import get_plain_key, get_private_key, make_keys
  7. from lib.block import CURRENT
  8. from lib.chain import Block, Chain
  9. from lib.transaction import Transaction
  10. from gui.encyption import decrypt_data
  11. # from gui.encyption import decrypt
  12. def new_contract():
  13. def add():
  14. name = str(title.get())
  15. if not os.path.isdir('contract_keys\\' + name):
  16. make_keys(name)
  17. pubkey = get_plain_key(name)
  18. # set discriptors of contract
  19. contract = {'id':pubkey,
  20. 'type':'init',
  21. 'date of initiation':time.time(),
  22. 'data': {'client':str(sender.get()),
  23. 'contractor':str(recipient.get()),
  24. 'discription':str(description.get()),
  25. 'name':name},
  26. 'terms' : {'deadline': int(deadline.get()),
  27. 'accepted': False,
  28. 'progress': '0%',
  29. 'price':int(price.get()),
  30. 'Sign time': 'not yet signed',
  31. 'comments':{}
  32. }
  33. }
  34. t = Transaction()
  35. t.set_contract(contract)
  36. t.hash(contract=name)
  37. temp = t.serialize(out_json=True)
  38. conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
  39. conn.root.push_transaction(temp)
  40. conn.close()
  41. newWindow.destroy()
  42. else:
  43. print('Contract name already exists, choose another')
  44. newWindow.destroy()
  45. new_contract()
  46. newWindow = Tk()
  47. newWindow.title('Add contract')
  48. # create all text boxes
  49. Label(newWindow, text='Title: ').grid(row=0, column=0)
  50. title = Entry(newWindow, width=35, borderwidth=5)
  51. title.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  52. title.insert(0, 'test')
  53. Label(newWindow, text='Description: ').grid(row=1, column=0)
  54. description = Entry(newWindow, width=35, borderwidth=5)
  55. description.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  56. description.insert(0, 'test description')
  57. Label(newWindow, text='Deadline: ').grid(row=2, column=0)
  58. deadline = Entry(newWindow, width=35, borderwidth=5)
  59. deadline.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  60. deadline.insert(0, '1')
  61. Label(newWindow, text='Price: ').grid(row=3, column=0)
  62. price = Entry(newWindow, width=35, borderwidth=5)
  63. price.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  64. price.insert(0, '69420')
  65. Label(newWindow, text='Sender: ').grid(row=4, column=0)
  66. sender = Entry(newWindow, width=35, borderwidth=5)
  67. sender.grid(row=4, column=1, columnspan=3, padx=10, pady=10)
  68. sender.insert(0, 'justin')
  69. Label(newWindow, text='Recipient: ').grid(row=5, column=0)
  70. recipient = Entry(newWindow, width=35, borderwidth=5)
  71. recipient.grid(row=5, column=1, columnspan=3, padx=10, pady=10)
  72. recipient.insert(0, 'adam')
  73. # create the add contract button
  74. Button(newWindow, text = "Create contract", command=add).grid(row=6, column=1)
  75. def find_transaction():
  76. def find():
  77. conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
  78. iden = get_plain_key(str(key.get()))
  79. transactions = conn.root.find_transactions(iden)
  80. transactions = decrypt_transactions(transactions, str(key.get()))
  81. print(current_contract_state(transactions, iden=iden))
  82. conn.close()
  83. newWindow.destroy()
  84. newWindow = Tk()
  85. newWindow.title('Add contract')
  86. Label(newWindow, text='Contract key: ').grid(row=0, column=0)
  87. key = Entry(newWindow, width=35, borderwidth=5)
  88. key.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  89. key.insert(0, 'test')
  90. Button(newWindow, text='find contracts', command=find).grid(row=1, column=1)
  91. def add_term():
  92. def add():
  93. conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
  94. iden = get_plain_key(str(contract.get()))
  95. attr = str(change.get())
  96. value = str(values.get())
  97. comment = str(comments.get())
  98. transactions = conn.root.find_transactions(iden)
  99. transactions = decrypt_transactions(transactions, str(contract.get()))
  100. state = current_contract_state(transactions, iden)
  101. if attr in state['data'] or attr in state['terms']:
  102. t = 'modify'
  103. else: t = 'update'
  104. # add new or updated info to block
  105. update = {'id':iden,
  106. 'update id':len(get_updates(transactions, iden=iden)),
  107. 'updated':attr,
  108. 'type':t,
  109. 'last update':time.time(),
  110. 'accepted': True,
  111. 'change':{attr:value}}
  112. # check if comment is passed and add it if necassary
  113. if comment:
  114. update['comment'] = comment
  115. t = Transaction()
  116. t.set_contract(update)
  117. t.hash(contract=str(contract.get()))
  118. conn.root.push_transaction(t.serialize(out_json=True))
  119. #### TEST CODE PLEASE IGONRE ####
  120. update = {
  121. 'id':iden,
  122. 'update id':0,
  123. 'accept':True,
  124. 'type':'accept',
  125. 'last update':time.time()
  126. }
  127. if comment:
  128. update['comment'] = comment
  129. t = Transaction()
  130. t.set_contract(update)
  131. t.hash(contract=str(contract.get()))
  132. conn.root.push_transaction(t.serialize(out_json=True))
  133. #### END TEST CODE PLEASE DONT IGONRE ####
  134. conn.close()
  135. newWindow.destroy()
  136. newWindow = Tk()
  137. newWindow.title('Add contract')
  138. # create all text boxes
  139. Label(newWindow, text='Contract: ').grid(row=0, column=0)
  140. contract = Entry(newWindow, width=35, borderwidth=5)
  141. contract.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  142. contract.insert(0, '1')
  143. Label(newWindow, text='What to change or add: ').grid(row=1, column=0)
  144. change = Entry(newWindow, width=35, borderwidth=5)
  145. change.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  146. change.insert(0, 'progress')
  147. Label(newWindow, text='New value: ').grid(row=2, column=0)
  148. values = Entry(newWindow, width=35, borderwidth=5)
  149. values.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  150. values.insert(0, '50%')
  151. Label(newWindow, text='comment (optional): ').grid(row=2, column=0)
  152. comments = Entry(newWindow, width=35, borderwidth=5)
  153. comments.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  154. comments.insert(0, 'dit is een comment')
  155. Button(newWindow, text='Add term', command=add).grid(row=4, column=1)
  156. def current_contract_state(transactions, iden):
  157. # set up contract state
  158. state = {'id':iden,
  159. 'data':{},
  160. 'terms':{}
  161. }
  162. # save not yet approved updates
  163. updates = {}
  164. # loop over all transaction in block
  165. for item in transactions:
  166. # save data about our project
  167. if item['id'] == iden:
  168. # set state to inital contract state
  169. if item['type'] == 'init':
  170. state = item
  171. continue
  172. # if update is not accept update, save it to updates dict
  173. if item['type'] != 'accept':
  174. # comments are always accepted
  175. if item['updated'] != 'comments':
  176. updates[item['update id']] = item
  177. continue
  178. # if accepted is true, retrieve update form dict and add to contract state
  179. elif item['accept'] == True:
  180. item = updates[item['update id']]
  181. # set attr to whatever has been updated
  182. attr = item['updated']
  183. # check if the update was removal
  184. if item['type'] == 'remove':
  185. # delete item in the right part of the dict
  186. try:
  187. if attr in state['data']:
  188. del state['data'][attr]
  189. else:
  190. del state['terms'][attr]
  191. except:
  192. pass
  193. if item['type'] == 'update':
  194. state['terms'][attr] = item['change'][attr]
  195. # check if updated attr is data or terms
  196. elif attr in state['data']:
  197. state['data'][attr] = item['change'][attr]
  198. elif attr in state['terms']:
  199. # special case for comments
  200. if attr == 'comments' and item['type'] not in ['remove', 'init']:
  201. k = list(item['change'][attr].keys())[0]
  202. state['terms'][attr][k] = item['change'][attr][k]
  203. # case for normal updates
  204. else:
  205. state['terms'][attr] = item['change'][attr]
  206. # special cases for last update and date of initiation
  207. elif 'last update' in item:
  208. state['last update'] = item['last update']
  209. return state
  210. def get_updates(transactions, iden=None, attr=None, accepted=None):
  211. updates = []
  212. # get all blocks and loop over transactions
  213. for item in transactions:
  214. # if attr is '' get updates for all attributes
  215. if attr == None:
  216. # if iden is None, get updates for all contracts
  217. # updates for all contracts and attrs
  218. if iden == None:
  219. if item['type'] != 'init':
  220. updates.append(item)
  221. # updates for specific contracts from all attr
  222. else:
  223. if item['type'] != 'init' and item['id'] == iden:
  224. updates.append(item)
  225. else:
  226. # updates for specific attrs from all contract
  227. if iden == None:
  228. if item['type'] != 'init' and item['updated'] == attr:
  229. updates.append(item)
  230. # updates for specific contract and specific attr
  231. else:
  232. if item['type'] != 'init' and item['updated'] == attr and item['id'] == iden:
  233. updates.append(item)
  234. return updates
  235. def decrypt_transactions(transactions, name):
  236. decrypted_data = [json.loads(decrypt_data(name, x)) for x in transactions]
  237. print(type(decrypted_data[0]))
  238. # old code
  239. # for encrypted in transactions:
  240. # decrypting_message = private_key.decrypt(
  241. # encrypted,
  242. # padding.OAEP(
  243. # mgf=padding.MGF1(algorithm=hashes.SHA256()),
  244. # algorithm=hashes.SHA256(),
  245. # label=None
  246. # )
  247. # )
  248. # decrypted.append(decrypting_message)
  249. return decrypted_data