2
0
Эх сурвалжийг харах

gui/encytption/blockchain intergration almost complete for adding contracts

justtheboss97 4 жил өмнө
parent
commit
f185440baf
5 өөрчлөгдсөн 38 нэмэгдсэн , 53 устгасан
  1. 12 9
      create_keys.py
  2. 0 5
      gui.py
  3. 20 10
      gui/gui_helpers.py
  4. 4 1
      lib/contract.py
  5. 2 28
      lib/transaction.py

+ 12 - 9
create_keys.py

@@ -1,4 +1,5 @@
 import rsa
+import os
 #from cryptography.fernet import Fernet
 # create the symmetric key only for the JSON file - we are going to only encrypt the keys 
 #key = Fernet.generate_key()
@@ -40,12 +41,14 @@ def make_keys(company):
     with open('companies\\' + company + '_privatekey.key','wb') as f:
         f.write(pem)    
     
-    #write the public key to a file
-    # pukey = open('companies\\' + company + '_publickey.key','wb')
-    # pukey.write(pubkey.save_pkcs1('PEM'))
-    # pukey.close()
-
-    # # write the private key to a file
-    # prkey = open('companies\\' + company + '_privatekey.key','wb')
-    # prkey.write(privkey.save_pkcs1('PEM'))
-    # prkey.close()
+def get_keys(company):
+    path = 'companies\\' + company + '_publickey.key'
+    if not os.path.exists(path):
+        make_keys(company)
+
+    with open(path, 'rb') as f:
+        public_key = serialization.load_pem_public_key(
+            f.read(),
+            backend=default_backend()
+            )
+        return public_key

+ 0 - 5
gui.py

@@ -9,9 +9,6 @@ from lib.contract import Contract
 import rpyc
 from rpyc.utils.server import ThreadPoolServer
 
-# chain = Chain()
-# chain.populate()
-# conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
 
 root = Tk()
 root.title('dsp blockchain')
@@ -43,5 +40,3 @@ Button(root, text="New contract", command=add_contract).grid(row=1, column=0)
 
 root.mainloop()
 
-# conn.close()
-print('test')

+ 20 - 10
gui/gui_helpers.py

@@ -3,17 +3,24 @@ from lib.chain import Chain, Block
 from lib.transaction import Transaction
 from lib.contract import Contract
 from lib.block import CURRENT
-
+from create_keys import get_keys
+import rpyc
 
 def add_contract():
 
     # add contract to the blockchain
     def add():
         contract1 = Contract()
+        s = get_keys(sender.get())
+        r = get_keys(recipient.get())
+
         contract1.create(title=title.get(),
                          desc=description.get(),
                          deadline=int(deadline.get()),
-                         price=float(price.get()))
+                         price=float(price.get()),
+                         state = False,
+                         client=s,
+                         contractor=r)
 
 
         add_term(contract1)
@@ -32,6 +39,8 @@ def add_contract():
             add_term(contract1)
 
         def complete():
+            conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
+
             # Bind contract to transaction
             transaction1 = Transaction()
             transaction1.set_contract(contract=contract1)
@@ -45,11 +54,8 @@ def add_contract():
             # send to push_transaction(hashed:str)
             # hashed has the format: {’sender’:..,’receiver’:…, ‘data’:encrypted contract} (see github readme)
             # deamon does the rest
-            
-            print(transaction1.hash_value)
-
-
-
+            conn.root.push_transaction({'sender':get_keys(sender.get()), 'reciever':get_keys(recipient.get()), 'data':transaction1.hash_value})           
+            conn.close()
             term_window.destroy()
             newWindow.destroy()
 
@@ -98,12 +104,16 @@ def add_contract():
     price =  Entry(newWindow, width=35, borderwidth=5)
     price.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
 
-    Label(newWindow, text='Recipient: ').grid(row=4, column=0)
+    Label(newWindow, text='Sender: ').grid(row=4, column=0)
+    sender =  Entry(newWindow, width=35, borderwidth=5)
+    sender.grid(row=4, column=1, columnspan=3, padx=10, pady=10)
+
+    Label(newWindow, text='Recipient: ').grid(row=5, column=0)
     recipient =  Entry(newWindow, width=35, borderwidth=5)
-    recipient.grid(row=4, column=1, columnspan=3, padx=10, pady=10)
+    recipient.grid(row=5, column=1, columnspan=3, padx=10, pady=10)
 
     # create the add contract button
-    Button(newWindow, text = "Create contract", command=add).grid(row=5, column=1)
+    Button(newWindow, text = "Create contract", command=add).grid(row=6, column=1)
 
 
 

+ 4 - 1
lib/contract.py

@@ -72,13 +72,16 @@ class Contract(object):
             return True
         return False
 
-    def create(self, title: str, desc: str, deadline: int, price: float, **kwargs):
+    def create(self, title: str, desc: str, deadline: int, price: float, state:bool, client, contractor, **kwargs):
         if self.id is None:
             self.id = str(uuid.uuid1())
             self.title = title
             self.description = desc
             self.deadline = deadline
             self.price = price
+            self.state = state
+            self.client = client
+            self.contractor = contractor
             self.log(f'Contract created: {self.id}')
             return True
         return False

+ 2 - 28
lib/transaction.py

@@ -2,9 +2,8 @@ import hashlib
 import json
 import time
 import uuid
-import os
 
-from create_keys import make_keys
+from create_keys import make_keys, get_keys
 from lib.contract import Contract
 from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.primitives import serialization
@@ -67,29 +66,13 @@ class Transaction:
             return True
         return False
 
-    def get_keys(self, recipient):
-        path = 'companies\\' + recipient + '_publickey.key'
-        if not os.path.exists(path):
-            make_keys(recipient)
-
-        with open(path, 'rb') as f:
-            public_key = serialization.load_pem_public_key(
-                f.read(),
-                backend=default_backend()
-                )
-            return public_key
-    
-        # key_file = open(file, 'rb')
-        # key_data = key_file.read()
-        # key_file.close()
-        # return key_data
 
     def hash(self, recipient=None):
         string_object = json.dumps(self.data, sort_keys=True)
         block_string = string_object.encode()
 
         # open the public key file
-        pubkey = self.get_keys(recipient) #Sender public key - input field neccessary
+        pubkey = get_keys(recipient) #Sender public key - input field neccessary
 
         encrypted_data = pubkey.encrypt(
             block_string,
@@ -100,15 +83,6 @@ class Transaction:
             )
         )
 
-        #these lines only neccessary for key_encryption - solve the problem of bytes
-        # open the symmetric key file for encryoting the file
-        #skey = open('symmetric.key','rb')
-        #key = skey.read()
-
-
-        # encrypt the symmetric key file with the public key
-        #encrypted_key = rsa.encrypt(key,pubkey)
-
         self.hash_value = encrypted_data
         self.log(f'Hashed: {self.hash_value}')