gui_helpers.py 7.4 KB

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