2
0

gui_helpers.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. from tkinter import *
  2. from lib.chain import Chain, Block
  3. from lib.transaction import Transaction
  4. from lib.contract import Contract
  5. from lib.block import CURRENT
  6. from create_keys import get_keys
  7. import rpyc
  8. import time
  9. import json
  10. def add_contract():
  11. # add contract to the blockchain
  12. def add():
  13. contract1 = Contract()
  14. s = get_keys(sender.get())
  15. r = get_keys(recipient.get())
  16. contract1.create(title=title.get(),
  17. desc=description.get(),
  18. deadline=int(deadline.get()),
  19. price=float(price.get()),
  20. state=False,
  21. client=str(s),
  22. contractor=str(r))
  23. add_term(contract1)
  24. def add_term(contract1):
  25. def process_term():
  26. term1 = Contract.Term()
  27. term1.create(title=title2.get(),
  28. desc=description2.get(),
  29. deadline=int(deadline2.get()))
  30. contract1.add_term(term=term1)
  31. term_window.destroy()
  32. add_term(contract1)
  33. def complete():
  34. conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
  35. # Bind contract to transaction
  36. transaction1 = Transaction()
  37. transaction1.set_contract(contract=contract1)
  38. r = recipient.get()
  39. transaction1.hash(recipient=r)
  40. transaction1.timestamp = time.time()
  41. # keys need to be in plain text, otherwise the json dump fucks up
  42. transaction1.sender = str(get_keys(sender.get()))
  43. transaction1.receiver = str(get_keys(recipient.get()))
  44. transaction1.data = str(transaction1.hash_value)
  45. output = transaction1.serialize()
  46. output = json.dumps(output, sort_keys=True)
  47. print(output, type(output))
  48. conn.root.push_transaction(output)
  49. conn.close()
  50. term_window.destroy()
  51. newWindow.destroy()
  52. term_window = Toplevel(newWindow)
  53. term_window.title('Add term')
  54. Label(term_window, text='Title: ').grid(row=0, column=0)
  55. title2 = Entry(term_window, width=35, borderwidth=5)
  56. title2.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  57. Label(term_window, text='Description: ').grid(row=1, column=0)
  58. description2 = Entry(term_window, width=35, borderwidth=5)
  59. description2.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  60. Label(term_window, text='Deadline: ').grid(row=2, column=0)
  61. deadline2 = Entry(term_window, width=35, borderwidth=5)
  62. deadline2.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  63. Label(term_window, text='Recipient: ').grid(row=3, column=0)
  64. recipient1 = Entry(term_window, width=35, borderwidth=5)
  65. recipient1.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  66. Button(term_window, text='Add term', command=process_term).grid(row=4,column=0)
  67. Button(term_window, text='Done', command=complete).grid(row=4,column=1)
  68. # make new window for adding the information for the contract
  69. newWindow = Tk()
  70. newWindow.title('Add contract')
  71. # create all text boxes
  72. Label(newWindow, text='Title: ').grid(row=0, column=0)
  73. title = Entry(newWindow, width=35, borderwidth=5)
  74. title.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  75. Label(newWindow, text='Description: ').grid(row=1, column=0)
  76. description = Entry(newWindow, width=35, borderwidth=5)
  77. description.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  78. Label(newWindow, text='Deadline: ').grid(row=2, column=0)
  79. deadline = Entry(newWindow, width=35, borderwidth=5)
  80. deadline.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  81. Label(newWindow, text='Price: ').grid(row=3, column=0)
  82. price = Entry(newWindow, width=35, borderwidth=5)
  83. price.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  84. Label(newWindow, text='Sender: ').grid(row=4, column=0)
  85. sender = Entry(newWindow, width=35, borderwidth=5)
  86. sender.grid(row=4, column=1, columnspan=3, padx=10, pady=10)
  87. Label(newWindow, text='Recipient: ').grid(row=5, column=0)
  88. recipient = Entry(newWindow, width=35, borderwidth=5)
  89. recipient.grid(row=5, column=1, columnspan=3, padx=10, pady=10)
  90. # create the add contract button
  91. Button(newWindow, text = "Create contract", command=add).grid(row=6, column=1)
  92. ## old code used for checking sytax of Tkinter
  93. # def update():
  94. # def up():
  95. # chain.update_contract(int(iden.get()), term.get(), update_value.get(), name.get())
  96. # newWindow.destroy()
  97. # print('update pending approval')
  98. # newWindow = Toplevel(root)
  99. # newWindow.title('Update terms')
  100. # Label(newWindow, text='Contract id: ').grid(row=0, column=0)
  101. # iden = Entry(newWindow, width=35, borderwidth=5)
  102. # iden.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  103. # Label(newWindow, text='Term: ').grid(row=1, column=0)
  104. # term = Entry(newWindow, width=35, borderwidth=5)
  105. # term.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  106. # Label(newWindow, text='New value: ').grid(row=2, column=0)
  107. # update_value = Entry(newWindow, width=35, borderwidth=5)
  108. # update_value.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  109. # Label(newWindow, text='Name: ').grid(row=3, column=0)
  110. # name = Entry(newWindow, width=35, borderwidth=5)
  111. # name.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  112. # Button(newWindow, text='Update', command=up).grid(row=4, column=0)
  113. # def accept_update():
  114. # def accept_up():
  115. # c = comment.get()
  116. # ans = awnser.get()
  117. # if ans == 'False':
  118. # ans = False
  119. # else: ans = True
  120. # if c == '':
  121. # chain.accept_update(int(iden.get()), int(update_id.get()), ans, name.get())
  122. # else:
  123. # chain.accept_update(int(iden.get()), int(update_id.get()), ans, name.get(), c)
  124. # newWindow.destroy()
  125. # newWindow = Toplevel(root)
  126. # newWindow.title('Update terms')
  127. # Label(newWindow, text='Contract id: ').grid(row=0, column=0)
  128. # iden = Entry(newWindow, width=35, borderwidth=5)
  129. # iden.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  130. # Label(newWindow, text='Update id: ').grid(row=1, column=0)
  131. # update_id = Entry(newWindow, width=35, borderwidth=5)
  132. # update_id.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  133. # Label(newWindow, text='Awnser ').grid(row=2, column=0)
  134. # awnser = Entry(newWindow, width=35, borderwidth=5)
  135. # awnser.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  136. # Label(newWindow, text='Name: ').grid(row=3, column=0)
  137. # name = Entry(newWindow, width=35, borderwidth=5)
  138. # name.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  139. # Label(newWindow, text='Comment optional: ').grid(row=3, column=0)
  140. # comment = Entry(newWindow, width=35, borderwidth=5)
  141. # comment.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  142. # Button(newWindow, text='Update', command=accept_up).grid(row=4, column=0)
  143. # def view_updates():
  144. # status =accept_status.get()
  145. # if status == 'False':
  146. # status = False
  147. # else: status = True
  148. # iden = int(accept_id.get())
  149. # if status == None:
  150. # pprint.pprint(chain.retrieve_updates(iden))
  151. # else:
  152. # pprint.pprint(chain.retrieve_updates(iden, status))