create block function added

This commit is contained in:
Spekulaas
2023-11-07 17:33:50 +01:00
parent 273b7f4552
commit c0155f94fe
10 changed files with 131 additions and 65 deletions

View File

@@ -1,5 +1,3 @@
import os
from classes.User import User
from classes.Transaction import Tx
from helpers import BlockHelper as blockHelper
@@ -76,7 +74,7 @@ class MenuHelper:
case "Login":
self.user = User(self.db)
logged_in = self.user.login()
self.clearScreen()
utilityHelper.clearScreen()
if logged_in:
print(f'Welcome {self.user.username}')
return
@@ -146,7 +144,7 @@ class MenuHelper:
case "Transfer coins":
new_tx = taskHelper.transaction(self)
if new_tx.is_valid():
if new_tx != False and new_tx.is_valid():
# TODO: add to pool
print("Transaction is valid")
utilityHelper.saveFile("../data/transaction_pool.dat", new_tx)
@@ -161,9 +159,15 @@ class MenuHelper:
print("TODO")
case "Check the pool":
print("TODO")
transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
x = 0
for transaction in transactions:
print(f"---------------------------------------{x}-------------------------------------------")
print(f"{transaction}")
x += 1
case "Mine a block":
taskHelper.createBlock(self)
print("TODO")
case _:
@@ -203,12 +207,9 @@ class MenuHelper:
try:
choice = int(input())
except:
self.clearScreen()
utilityHelper.clearScreen()
print("Wrong input, try again")
return None
self.clearScreen()
utilityHelper.clearScreen()
return choice
def clearScreen(self):
os.system('cls' if os.name == 'nt' else 'clear')
return choice

View File

@@ -47,9 +47,17 @@ def publicBytesToKey(public_ser):
)
return public_key
def privateBytesToKey(private_ser):
private_key = serialization.load_pem_private_key(
private_ser,
password=None
)
return private_key
def sign(message, private_key):
message = bytes(str(message), 'utf-8')
signature = private_key.sign(
key = privateBytesToKey(private_key)
signature = key.sign(
message,
padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH),
hashes.SHA256()

View File

@@ -1,5 +1,6 @@
from classes.Transaction import Tx
from helpers import SignatureHelper as signatureHelper
from helpers import UtilityHelper as utilityHelper
def transaction(self):
receiver = input("Enter the username of the receiver:")
@@ -11,8 +12,6 @@ def transaction(self):
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
@@ -34,5 +33,48 @@ def transaction(self):
return False
new_tx = Tx()
new_tx.createTransaction(self.user.public_key, self.user.private_key, amount, fee, receiver_public_key)
return new_tx
new_tx.createTransaction(self.user.public_ser, self.user.private_ser, amount, fee, receiver_data[1])
return new_tx
def createBlock(self):
transactions = Tx()
transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
if len(transactions) < 5:
print("You need atleast 5 transactions to mine a block")
return False
print("Select items out the pool to mine a block, last 2 values added to list.")
if input("Press enter to continue... (type b to return)") == "b":
return False
utilityHelper.clearScreen()
x = 0
fees = {}
for transaction in transactions:
fees[x] = transaction.inputs[0][1] - transaction.outputs[0][1]
x+=1
fees_list = list(fees.keys())
selected_transactions = fees_list[-2:]
available_transactions = fees_list[:-2]
# print fees with values and keys if key is in available_transactions
for key, value in fees.items():
if key in available_transactions:
print(f"{key} -- fee: {value}")
# get user input
print("Enter the numbers of the transactions you want to mine, seperated by a comma. (ex: 1,2,3)")
user_input = input(">>: ")
if user_input == "":
return False
# seperate user input by comma, check if all the values are in available_transactions
user_input = user_input.split(",")
for i in user_input:
if int(i) not in available_transactions:
print("Wrong input, try again")
return False
fees_list.append(int(i))

View File

@@ -1,5 +1,6 @@
import pickle
import hashlib
import os
def computeHash(data):
hash = hashlib.sha256()
@@ -7,14 +8,22 @@ def computeHash(data):
hash.update(data)
return hash.hexdigest()
def clearScreen():
os.system('cls' if os.name == 'nt' else 'clear')
def saveFile(fileloc, data):
savefile = open(fileloc, "wb")
savefile = open(fileloc, "ab")
pickle.dump(data, savefile)
savefile.close()
def loadFile(fileloc):
loadfile = open(fileloc ,"rb")
load = pickle.load(loadfile)
loadfile.close()
return load
objs = []
while True:
try:
objs.append(pickle.load(loadfile))
except EOFError:
break
return objs