2
0

gui_helpers.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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=s,
  22. contractor=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 = get_keys(sender.get())
  43. transaction1.receiver = get_keys(recipient.get())
  44. transaction1.data = transaction1.hash_value
  45. output = transaction1.serialize()
  46. print(output, type(output))
  47. conn.root.push_transaction(output)
  48. conn.close()
  49. term_window.destroy()
  50. newWindow.destroy()
  51. term_window = Toplevel(newWindow)
  52. term_window.title('Add term')
  53. Label(term_window, text='Title: ').grid(row=0, column=0)
  54. title2 = Entry(term_window, width=35, borderwidth=5)
  55. title2.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  56. Label(term_window, text='Description: ').grid(row=1, column=0)
  57. description2 = Entry(term_window, width=35, borderwidth=5)
  58. description2.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  59. Label(term_window, text='Deadline: ').grid(row=2, column=0)
  60. deadline2 = Entry(term_window, width=35, borderwidth=5)
  61. deadline2.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  62. Label(term_window, text='Recipient: ').grid(row=3, column=0)
  63. recipient1 = Entry(term_window, width=35, borderwidth=5)
  64. recipient1.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  65. Button(term_window, text='Add term', command=process_term).grid(row=4,column=0)
  66. Button(term_window, text='Done', command=complete).grid(row=4,column=1)
  67. # make new window for adding the information for the contract
  68. newWindow = Tk()
  69. newWindow.title('Add contract')
  70. # create all text boxes
  71. Label(newWindow, text='Title: ').grid(row=0, column=0)
  72. title = Entry(newWindow, width=35, borderwidth=5)
  73. title.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  74. Label(newWindow, text='Description: ').grid(row=1, column=0)
  75. description = Entry(newWindow, width=35, borderwidth=5)
  76. description.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  77. Label(newWindow, text='Deadline: ').grid(row=2, column=0)
  78. deadline = Entry(newWindow, width=35, borderwidth=5)
  79. deadline.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  80. Label(newWindow, text='Price: ').grid(row=3, column=0)
  81. price = Entry(newWindow, width=35, borderwidth=5)
  82. price.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  83. Label(newWindow, text='Sender: ').grid(row=4, column=0)
  84. sender = Entry(newWindow, width=35, borderwidth=5)
  85. sender.grid(row=4, column=1, columnspan=3, padx=10, pady=10)
  86. Label(newWindow, text='Recipient: ').grid(row=5, column=0)
  87. recipient = Entry(newWindow, width=35, borderwidth=5)
  88. recipient.grid(row=5, column=1, columnspan=3, padx=10, pady=10)
  89. # create the add contract button
  90. Button(newWindow, text = "Create contract", command=add).grid(row=6, column=1)
  91. ## old code used for checking sytax of Tkinter
  92. # def update():
  93. # def up():
  94. # chain.update_contract(int(iden.get()), term.get(), update_value.get(), name.get())
  95. # newWindow.destroy()
  96. # print('update pending approval')
  97. # newWindow = Toplevel(root)
  98. # newWindow.title('Update terms')
  99. # Label(newWindow, text='Contract id: ').grid(row=0, column=0)
  100. # iden = Entry(newWindow, width=35, borderwidth=5)
  101. # iden.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  102. # Label(newWindow, text='Term: ').grid(row=1, column=0)
  103. # term = Entry(newWindow, width=35, borderwidth=5)
  104. # term.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  105. # Label(newWindow, text='New value: ').grid(row=2, column=0)
  106. # update_value = Entry(newWindow, width=35, borderwidth=5)
  107. # update_value.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  108. # Label(newWindow, text='Name: ').grid(row=3, column=0)
  109. # name = Entry(newWindow, width=35, borderwidth=5)
  110. # name.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  111. # Button(newWindow, text='Update', command=up).grid(row=4, column=0)
  112. # def accept_update():
  113. # def accept_up():
  114. # c = comment.get()
  115. # ans = awnser.get()
  116. # if ans == 'False':
  117. # ans = False
  118. # else: ans = True
  119. # if c == '':
  120. # chain.accept_update(int(iden.get()), int(update_id.get()), ans, name.get())
  121. # else:
  122. # chain.accept_update(int(iden.get()), int(update_id.get()), ans, name.get(), c)
  123. # newWindow.destroy()
  124. # newWindow = Toplevel(root)
  125. # newWindow.title('Update terms')
  126. # Label(newWindow, text='Contract id: ').grid(row=0, column=0)
  127. # iden = Entry(newWindow, width=35, borderwidth=5)
  128. # iden.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  129. # Label(newWindow, text='Update id: ').grid(row=1, column=0)
  130. # update_id = Entry(newWindow, width=35, borderwidth=5)
  131. # update_id.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  132. # Label(newWindow, text='Awnser ').grid(row=2, column=0)
  133. # awnser = Entry(newWindow, width=35, borderwidth=5)
  134. # awnser.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  135. # Label(newWindow, text='Name: ').grid(row=3, column=0)
  136. # name = Entry(newWindow, width=35, borderwidth=5)
  137. # name.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  138. # Label(newWindow, text='Comment optional: ').grid(row=3, column=0)
  139. # comment = Entry(newWindow, width=35, borderwidth=5)
  140. # comment.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  141. # Button(newWindow, text='Update', command=accept_up).grid(row=4, column=0)
  142. # def view_updates():
  143. # status =accept_status.get()
  144. # if status == 'False':
  145. # status = False
  146. # else: status = True
  147. # iden = int(accept_id.get())
  148. # if status == None:
  149. # pprint.pprint(chain.retrieve_updates(iden))
  150. # else:
  151. # pprint.pprint(chain.retrieve_updates(iden, status))