|
|
@@ -1,8 +1,8 @@
|
|
|
import json
|
|
|
import os
|
|
|
+import pprint
|
|
|
import time
|
|
|
from tkinter import *
|
|
|
-import pprint
|
|
|
|
|
|
import rpyc
|
|
|
from create_keys import get_plain_key, get_private_key, make_keys
|
|
|
@@ -19,8 +19,11 @@ def new_contract():
|
|
|
name = str(title.get())
|
|
|
|
|
|
if not os.path.isdir('contract_keys\\' + name):
|
|
|
+
|
|
|
+ # get data form gui
|
|
|
make_keys(name)
|
|
|
pubkey = get_plain_key(name)
|
|
|
+
|
|
|
# set discriptors of contract
|
|
|
contract = {'id':pubkey,
|
|
|
'type':'init',
|
|
|
@@ -38,15 +41,22 @@ def new_contract():
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ # create transaction and set contract to contents
|
|
|
t = Transaction()
|
|
|
t.set_contract(contract)
|
|
|
+
|
|
|
+ # encrypt data
|
|
|
t.hash(contract=name)
|
|
|
+
|
|
|
+ # push transaction to blockchain
|
|
|
temp = t.serialize(out_json=True)
|
|
|
conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
|
|
|
conn.root.push_transaction(temp)
|
|
|
|
|
|
conn.close()
|
|
|
newWindow.destroy()
|
|
|
+
|
|
|
+ # if contract name already exists, print message and try again
|
|
|
else:
|
|
|
print('Contract name already exists, choose another')
|
|
|
newWindow.destroy()
|
|
|
@@ -88,15 +98,24 @@ def new_contract():
|
|
|
recipient.insert(0, 'adam')
|
|
|
|
|
|
# create the add contract button
|
|
|
- Button(newWindow, text = "Create contract", command=add).grid(row=6, column=1)
|
|
|
+ Button(newWindow, text = "Create contract", command=add).grid(row=6, column=1, padx=10, pady=5)
|
|
|
+
|
|
|
|
|
|
def find_transaction():
|
|
|
+
|
|
|
+ # get current state of contract
|
|
|
def find():
|
|
|
conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
|
|
|
+
|
|
|
+ # find all transactions of a contract
|
|
|
iden = get_plain_key(str(key.get()))
|
|
|
transactions = conn.root.find_transactions(iden)
|
|
|
+
|
|
|
+ # check if contract exists
|
|
|
if len(transactions) == 0:
|
|
|
print('No contract found')
|
|
|
+
|
|
|
+ # build current state of contract and print to console
|
|
|
else:
|
|
|
transactions = decrypt_transactions(transactions, str(key.get()))
|
|
|
print('Current contract state')
|
|
|
@@ -105,26 +124,37 @@ def find_transaction():
|
|
|
conn.close()
|
|
|
newWindow.destroy()
|
|
|
|
|
|
+ # get current state and all transactions
|
|
|
def findall():
|
|
|
conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
|
|
|
+
|
|
|
+ # find all transactions of a contract
|
|
|
iden = get_plain_key(str(key.get()))
|
|
|
transactions = conn.root.find_transactions(iden)
|
|
|
+
|
|
|
+ # check if contract exists
|
|
|
if len(transactions) == 0:
|
|
|
print('No contract found')
|
|
|
+
|
|
|
+ # build current state of contract and print to console
|
|
|
else:
|
|
|
transactions = decrypt_transactions(transactions, str(key.get()))
|
|
|
print('All updates')
|
|
|
+
|
|
|
+ # show all updates of contract
|
|
|
for item in transactions:
|
|
|
if item['type'] != 'init':
|
|
|
pprint.pprint(item)
|
|
|
print('\n')
|
|
|
|
|
|
+ # show contract
|
|
|
print('Current contract state')
|
|
|
pprint.pprint(current_contract_state(transactions, iden=iden))
|
|
|
|
|
|
conn.close()
|
|
|
newWindow.destroy()
|
|
|
|
|
|
+ # gui setup
|
|
|
newWindow = Tk()
|
|
|
newWindow.title('Find contract')
|
|
|
|
|
|
@@ -133,14 +163,15 @@ def find_transaction():
|
|
|
key.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
|
|
|
key.insert(0, 'test')
|
|
|
|
|
|
- Button(newWindow, text='find contracts', command=find).grid(row=2, column=0)
|
|
|
- Button(newWindow, text='find contracts and updates', command=findall).grid(row=2, column=1)
|
|
|
+ Button(newWindow, text='find contracts', command=find).grid(row=2, column=0, padx=10, pady=5)
|
|
|
+ Button(newWindow, text='find contracts and updates', command=findall).grid(row=2, column=1, padx=10, pady=5)
|
|
|
|
|
|
|
|
|
def add_term():
|
|
|
def add():
|
|
|
conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
|
|
|
|
|
|
+ # retrieve data from gui
|
|
|
iden = get_plain_key(str(contract.get()))
|
|
|
attr = str(change.get())
|
|
|
value = str(values.get())
|
|
|
@@ -149,6 +180,7 @@ def add_term():
|
|
|
transactions = decrypt_transactions(transactions, str(contract.get()))
|
|
|
state = current_contract_state(transactions, iden)
|
|
|
|
|
|
+ # check type of transaction
|
|
|
if value == '':
|
|
|
t = 'remove'
|
|
|
elif attr in state['data'] or attr in state['terms']:
|
|
|
@@ -170,6 +202,7 @@ def add_term():
|
|
|
if comment:
|
|
|
update['comment'] = comment
|
|
|
|
|
|
+ # create transacion, hash it and push to blockchain
|
|
|
t = Transaction()
|
|
|
t.set_contract(update)
|
|
|
t.hash(contract=str(contract.get()))
|
|
|
@@ -202,16 +235,20 @@ def add_term():
|
|
|
comments.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
|
|
|
comments.insert(0, 'dit is een comment')
|
|
|
|
|
|
- Button(newWindow, text='Add term', command=add).grid(row=4, column=1)
|
|
|
+ Button(newWindow, text='Add term', command=add).grid(row=4, column=1, padx=10, pady=5)
|
|
|
|
|
|
def accept_updates():
|
|
|
def show():
|
|
|
conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
|
|
|
+
|
|
|
+ # find all transactions based on given contract
|
|
|
name = str(contract.get())
|
|
|
transactions = conn.root.find_transactions(get_plain_key(name))
|
|
|
transactions = decrypt_transactions(transactions, name)
|
|
|
updates = retrieve_updates(transactions, name, accepted=False)
|
|
|
conn.close()
|
|
|
+
|
|
|
+ # show updates to accept
|
|
|
if len(updates) == 0:
|
|
|
print('No updates to accept')
|
|
|
else:
|
|
|
@@ -221,6 +258,8 @@ def accept_updates():
|
|
|
|
|
|
def accept():
|
|
|
conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
|
|
|
+
|
|
|
+ # get data from gui
|
|
|
name = str(contract.get())
|
|
|
update_id = int(change.get())
|
|
|
answer = accept_status.get()
|
|
|
@@ -230,6 +269,7 @@ def accept_updates():
|
|
|
|
|
|
comment_text = str(comment.get())
|
|
|
|
|
|
+ # set transaction data
|
|
|
update = {
|
|
|
'id':get_plain_key(name),
|
|
|
'update id':update_id,
|
|
|
@@ -240,6 +280,7 @@ def accept_updates():
|
|
|
if comment_text:
|
|
|
update['comment'] = comment_text
|
|
|
|
|
|
+ # create transacion, hash it and push to blockchain
|
|
|
t = Transaction()
|
|
|
t.set_contract(update)
|
|
|
t.hash(contract=str(contract.get()))
|
|
|
@@ -388,6 +429,7 @@ def retrieve_updates(all_updates, iden, accepted=None):
|
|
|
|
|
|
updates = {}
|
|
|
|
|
|
+ # for all updates, only find modify and remove updates
|
|
|
for item in all_updates:
|
|
|
if item['type'] == 'init':
|
|
|
continue
|
|
|
@@ -402,8 +444,9 @@ def retrieve_updates(all_updates, iden, accepted=None):
|
|
|
if accepted == None:
|
|
|
return updates
|
|
|
|
|
|
+ # only return updates based on accepted argument
|
|
|
return [updates[x] for x in list(updates.keys()) if updates[x]['accepted'] == accepted]
|
|
|
|
|
|
-
|
|
|
+# creates list of all decrypted transactions
|
|
|
def decrypt_transactions(transactions, name):
|
|
|
return [json.loads(decrypt_data(name, x)) for x in transactions]
|