wip > laptop to pc

This commit is contained in:
2023-11-07 12:48:08 +01:00
parent 6e7528c2a3
commit 273b7f4552
17 changed files with 272 additions and 8 deletions

View File

View File

@@ -1 +0,0 @@
# Not sure if we're planning to log anything tho

View File

@@ -1,6 +1,10 @@
import os
from classes.User import User
from classes.Transaction import Tx
from helpers import BlockHelper as blockHelper
from helpers import TaskHelper as taskHelper
from helpers import UtilityHelper as utilityHelper
class bcolors:
HEADER = '\033[95m'
@@ -131,10 +135,24 @@ class MenuHelper:
case "Explore the Blockchain":
print("TODO")
block = blockHelper.loadBlock()
i = 0
for data in block.data:
print(f"---------------------------------- Block {str(i)} ----------------------------------")
print(f"Block hash: {data.Inputs[0][1]}")
print(f"----------------------------------------------------------------------------------")
i += 1
case "Transfer coins":
input("Enter the username of the receiver: ")
print("TODO")
new_tx = taskHelper.transaction(self)
if new_tx.is_valid():
# TODO: add to pool
print("Transaction is valid")
utilityHelper.saveFile("../data/transaction_pool.dat", new_tx)
continue
print("Transaction is invalid")
case "Cancel transaction":
print("TODO")

View File

@@ -41,6 +41,12 @@ def bytesToKeys(private_ser, public_ser):
)
return private_key, public_key
def publicBytesToKey(public_ser):
public_key = serialization.load_pem_public_key(
public_ser
)
return public_key
def sign(message, private_key):
message = bytes(str(message), 'utf-8')
signature = private_key.sign(
@@ -50,9 +56,8 @@ def sign(message, private_key):
)
return signature
def verify(message, signature, pbc_ser):
def verify(message, signature, public_key):
message = bytes(str(message), 'utf-8')
public_key = serialization.load_pem_public_key(pbc_ser)
try:
public_key.verify(
signature,

View File

@@ -0,0 +1,38 @@
from classes.Transaction import Tx
from helpers import SignatureHelper as signatureHelper
def transaction(self):
receiver = input("Enter the username of the receiver:")
if receiver == "":
return False
receiver_data = self.db.fetchUserByUsername(receiver)
if not receiver_data:
print("Username not found")
return False
receiver_public_key = signatureHelper.publicBytesToKey(receiver_data[1])
amount = input("Enter the amount excluding fees:")
if amount == "":
return False
try:
amount = int(amount)
except:
print("Wrong input, try again")
return False
fee = input("Enter fee:")
if fee == "":
return False
try:
fee = int(fee)
except:
print("Wrong input, try again")
return False
new_tx = Tx()
new_tx.createTransaction(self.user.public_key, self.user.private_key, amount, fee, receiver_public_key)
return new_tx

View File

@@ -1,6 +1,4 @@
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
import pickle
import hashlib
def computeHash(data):
@@ -8,3 +6,15 @@ def computeHash(data):
data = str(data).encode()
hash.update(data)
return hash.hexdigest()
def saveFile(fileloc, data):
savefile = open(fileloc, "wb")
pickle.dump(data, savefile)
savefile.close()
def loadFile(fileloc):
loadfile = open(fileloc ,"rb")
load = pickle.load(loadfile)
loadfile.close()
return load