colorized warning, success and error messages
This commit is contained in:
parent
9b34bf0b3f
commit
e0edbfefaf
@ -19,14 +19,7 @@ class User:
|
|||||||
user = self.db.loginUser(input_username, hashed_password)
|
user = self.db.loginUser(input_username, hashed_password)
|
||||||
|
|
||||||
# check if user exists
|
# check if user exists
|
||||||
# if user:
|
|
||||||
# private_key, public_key = Signature.bytesToKeys(user[0], user[1])
|
|
||||||
# self.private_key = private_key
|
|
||||||
# self.public_key = public_key
|
|
||||||
# self.username = user[2]
|
|
||||||
# return True
|
|
||||||
if user:
|
if user:
|
||||||
private_key, public_key = Signature.bytesToKeys(user[0], user[1])
|
|
||||||
self.private_ser = user[0]
|
self.private_ser = user[0]
|
||||||
self.public_ser = user[1]
|
self.public_ser = user[1]
|
||||||
self.username = user[2]
|
self.username = user[2]
|
||||||
@ -41,7 +34,7 @@ class User:
|
|||||||
|
|
||||||
# check if username is already taken
|
# check if username is already taken
|
||||||
if self.db.fetchUserByUsername(input_username):
|
if self.db.fetchUserByUsername(input_username):
|
||||||
print("Username already taken")
|
print(f"{UtilityHelper.errorMessage('Username already taken')}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# create sig for user
|
# create sig for user
|
||||||
@ -63,63 +56,63 @@ class User:
|
|||||||
|
|
||||||
def updatePassword(self):
|
def updatePassword(self):
|
||||||
# Get new password
|
# Get new password
|
||||||
new_password = getpass('Enter your new password: ')
|
new_password = getpass("Enter your new password: ")
|
||||||
check_new_password = getpass('Enter your new password again: ')
|
check_new_password = getpass("Enter your new password again: ")
|
||||||
while new_password != check_new_password:
|
while new_password != check_new_password:
|
||||||
print('Passwords do not match')
|
print(f"{UtilityHelper.warningMessage('Passwords do not match')}")
|
||||||
new_password = getpass('Enter your new password: ')
|
new_password = getpass("Enter your new password: ")
|
||||||
check_new_password = getpass('Enter your new password again: ')
|
check_new_password = getpass("Enter your new password again: ")
|
||||||
|
|
||||||
old_password = getpass('Enter your old password: ')
|
old_password = getpass("Enter your old password: ")
|
||||||
hashed_new_password = UtilityHelper.computeHash(new_password)
|
hashed_new_password = UtilityHelper.computeHash(new_password)
|
||||||
hashed_old_password = UtilityHelper.computeHash(old_password)
|
hashed_old_password = UtilityHelper.computeHash(old_password)
|
||||||
|
|
||||||
if self.db.changePassword(self.private_ser, hashed_old_password, hashed_new_password) == True:
|
if self.db.changePassword(self.private_ser, hashed_old_password, hashed_new_password) == True:
|
||||||
print('Password updated')
|
print(f"{UtilityHelper.successMessage('Password updated')}")
|
||||||
else:
|
else:
|
||||||
print('Something went wrong while trying to update password..')
|
print(f"{UtilityHelper.errorMessage('Something went wrong while trying to update password..')}")
|
||||||
|
|
||||||
def updateAccount(self):
|
def updateAccount(self):
|
||||||
# Get new username
|
# Get new username
|
||||||
new_username = input('Enter your new username: ')
|
new_username = input("Enter your new username: ")
|
||||||
|
|
||||||
# check if username is already taken
|
# check if username is already taken
|
||||||
if self.db.fetchUserByUsername(new_username):
|
if self.db.fetchUserByUsername(new_username):
|
||||||
print("Username already taken")
|
print(f"{UtilityHelper.errorMessage('Username already taken')}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if self.db.changeUsername(self.private_ser, new_username) == True:
|
if self.db.changeUsername(self.private_ser, new_username) == True:
|
||||||
self.username = new_username
|
self.username = new_username
|
||||||
print('Username updated')
|
print(f"{UtilityHelper.successMessage('Username updated')}")
|
||||||
else:
|
else:
|
||||||
print('Something went wrong while trying to update username..')
|
print(f"{UtilityHelper.errorMessage('Something went wrong while trying to update username..')}")
|
||||||
|
|
||||||
|
|
||||||
def deleteAccount(self):
|
def deleteAccount(self):
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
print('Are you sure you want to delete this account? (y/n)\n(this will burn all coins available in this account) (y/n)')
|
print(f"Are you sure you want to delete this account?\n{UtilityHelper.warningMessage('(this will burn all coins available in this account)')} (y/n)")
|
||||||
confirm = input('>>: ')
|
confirm = input(">>: ")
|
||||||
if confirm == 'y' or confirm == 'Y':
|
if confirm == "y" or confirm == "Y":
|
||||||
print('Deleting account..')
|
print(f"{UtilityHelper.warningMessage('Deleting account..')}")
|
||||||
break
|
break
|
||||||
|
|
||||||
elif confirm == 'n' or confirm == 'N':
|
elif confirm == "n" or confirm == "N":
|
||||||
return False
|
return False
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('Invalid input')
|
print(f"{UtilityHelper.warningMessage('Invalid input')}")
|
||||||
|
|
||||||
if self.db.deleteUser(self.private_ser) == True:
|
if self.db.deleteUser(self.private_ser) == True:
|
||||||
print('Account deleted')
|
print(f"{UtilityHelper.successMessage('Account deleted')}")
|
||||||
self.logout()
|
self.logout()
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
print('Something went wrong while trying to delete account..')
|
print(f"{UtilityHelper.errorMessage('Something went wrong while trying to delete account..')}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def printAccountInfo(self):
|
def printAccountInfo(self):
|
||||||
print('Username: ' + self.username)
|
print("Username: " + self.username)
|
||||||
print(self.public_ser)
|
print(self.public_ser)
|
||||||
print(self.private_ser)
|
print(self.private_ser)
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ class MenuHelper:
|
|||||||
# check if choice is in menu
|
# check if choice is in menu
|
||||||
match self.start_menu[choice]:
|
match self.start_menu[choice]:
|
||||||
case "Exit":
|
case "Exit":
|
||||||
print('Exiting system')
|
print(f"{utilityHelper.blinkMessage('Exiting system')}")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
case "Explore the Blockchain":
|
case "Explore the Blockchain":
|
||||||
@ -69,7 +69,7 @@ class MenuHelper:
|
|||||||
if logged_in:
|
if logged_in:
|
||||||
print(f'Welcome {self.user.username}')
|
print(f'Welcome {self.user.username}')
|
||||||
return
|
return
|
||||||
print("Login failed")
|
print(f"{utilityHelper.errorMessage('Login failed')}")
|
||||||
self.user = None
|
self.user = None
|
||||||
|
|
||||||
case "Sign up":
|
case "Sign up":
|
||||||
@ -77,19 +77,19 @@ class MenuHelper:
|
|||||||
registered = self.user.register()
|
registered = self.user.register()
|
||||||
utilityHelper.clearScreen()
|
utilityHelper.clearScreen()
|
||||||
if registered:
|
if registered:
|
||||||
print("Registration successful")
|
print(f"{utilityHelper.successMessage('Registration successful')}")
|
||||||
new_tx = Tx()
|
new_tx = Tx()
|
||||||
new_tx.createRewardTransaction(self.user.public_ser, self.user.private_ser, "SIGNUP")
|
new_tx.createRewardTransaction(self.user.public_ser, self.user.private_ser, "SIGNUP")
|
||||||
if new_tx != False and new_tx.is_valid():
|
if new_tx != False and new_tx.is_valid():
|
||||||
print("Received sign up bonus")
|
print(f"{utilityHelper.blinkMessage('Received sign up bonus')}")
|
||||||
utilityHelper.saveFile("../data/transaction_pool.dat", new_tx)
|
utilityHelper.saveFile("../data/transaction_pool.dat", new_tx)
|
||||||
return
|
return
|
||||||
print("Transaction is invalid")
|
print(f"{utilityHelper.errorMessage('Transaction is invalid')}")
|
||||||
print("Registration failed")
|
print(f"{utilityHelper.errorMessage('Registration failed')}")
|
||||||
self.user = None
|
self.user = None
|
||||||
|
|
||||||
case _:
|
case _:
|
||||||
print("Wrong input, try again")
|
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
|
||||||
|
|
||||||
def runUserMainMenu(self):
|
def runUserMainMenu(self):
|
||||||
utilityHelper.loginStartup(self)
|
utilityHelper.loginStartup(self)
|
||||||
@ -102,7 +102,7 @@ class MenuHelper:
|
|||||||
# check if choice is in menu
|
# check if choice is in menu
|
||||||
match self.user_main_menu[choice]:
|
match self.user_main_menu[choice]:
|
||||||
case "Exit":
|
case "Exit":
|
||||||
print('Exiting system')
|
print(f"{utilityHelper.blinkMessage('Exiting system')}")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
case "Log out":
|
case "Log out":
|
||||||
@ -116,7 +116,7 @@ class MenuHelper:
|
|||||||
self.runUserBlockchainMenu()
|
self.runUserBlockchainMenu()
|
||||||
|
|
||||||
case _:
|
case _:
|
||||||
print("Wrong input, try again")
|
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
|
||||||
|
|
||||||
def runUserBlockchainMenu(self):
|
def runUserBlockchainMenu(self):
|
||||||
while(self.user):
|
while(self.user):
|
||||||
@ -142,10 +142,10 @@ class MenuHelper:
|
|||||||
case "Transfer coins":
|
case "Transfer coins":
|
||||||
new_tx = transactionHelper.transaction(self)
|
new_tx = transactionHelper.transaction(self)
|
||||||
if new_tx != False and new_tx.is_valid():
|
if new_tx != False and new_tx.is_valid():
|
||||||
print("Transaction is valid")
|
print(f"{utilityHelper.successMessage('Transaction is valid')}")
|
||||||
utilityHelper.saveFile("../data/transaction_pool.dat", new_tx)
|
utilityHelper.saveFile("../data/transaction_pool.dat", new_tx)
|
||||||
continue
|
continue
|
||||||
print("Transaction is invalid")
|
print(f"{utilityHelper.errorMessage('Transaction is invalid')}")
|
||||||
|
|
||||||
|
|
||||||
case "Check balance":
|
case "Check balance":
|
||||||
@ -156,7 +156,7 @@ class MenuHelper:
|
|||||||
case "Check the pool":
|
case "Check the pool":
|
||||||
transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
|
transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
|
||||||
if len(transactions) == 0:
|
if len(transactions) == 0:
|
||||||
print("Transaction pool is empty")
|
print(f"{utilityHelper.warningMessage('Transaction pool is empty')}")
|
||||||
continue
|
continue
|
||||||
x = 0
|
x = 0
|
||||||
for transaction in transactions:
|
for transaction in transactions:
|
||||||
@ -169,7 +169,7 @@ class MenuHelper:
|
|||||||
blockHelper.createBlock(self)
|
blockHelper.createBlock(self)
|
||||||
|
|
||||||
case _:
|
case _:
|
||||||
print("Wrong input, try again")
|
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
|
||||||
|
|
||||||
def runUserSettingsMenu(self):
|
def runUserSettingsMenu(self):
|
||||||
while(self.user):
|
while(self.user):
|
||||||
@ -197,7 +197,7 @@ class MenuHelper:
|
|||||||
self.user = None
|
self.user = None
|
||||||
|
|
||||||
case _:
|
case _:
|
||||||
print("Wrong input, try again")
|
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
|
||||||
|
|
||||||
def getMenuInput(self, menu):
|
def getMenuInput(self, menu):
|
||||||
self.printMenu(menu)
|
self.printMenu(menu)
|
||||||
@ -206,12 +206,12 @@ class MenuHelper:
|
|||||||
choice = int(input(">>: "))
|
choice = int(input(">>: "))
|
||||||
except:
|
except:
|
||||||
utilityHelper.clearScreen()
|
utilityHelper.clearScreen()
|
||||||
print("Wrong input, try again")
|
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if choice >= len(menu):
|
if choice >= len(menu):
|
||||||
utilityHelper.clearScreen()
|
utilityHelper.clearScreen()
|
||||||
print("Wrong input, try again")
|
print(f"{utilityHelper.warningMessage('Wrong input, try again')}")
|
||||||
return None
|
return None
|
||||||
utilityHelper.clearScreen()
|
utilityHelper.clearScreen()
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ class bcolors:
|
|||||||
ENDC = '\033[0m'
|
ENDC = '\033[0m'
|
||||||
BOLD = '\033[1m'
|
BOLD = '\033[1m'
|
||||||
UNDERLINE = '\033[4m'
|
UNDERLINE = '\033[4m'
|
||||||
|
BLINK = '\033[5m'
|
||||||
|
|
||||||
def computeHash(data):
|
def computeHash(data):
|
||||||
hash = hashlib.sha256()
|
hash = hashlib.sha256()
|
||||||
@ -57,27 +58,29 @@ def loginStartup(self):
|
|||||||
blockHelper.validateMinedBlock(self)
|
blockHelper.validateMinedBlock(self)
|
||||||
# TODO - check if there are any notifcations
|
# TODO - check if there are any notifcations
|
||||||
|
|
||||||
def printError(text):
|
def errorMessage(text):
|
||||||
print(f"{bcolors.FAIL}{text}{bcolors.ENDC}")
|
return bcolors.FAIL + text + bcolors.ENDC
|
||||||
|
|
||||||
def printSuccess(text):
|
def successMessage(text):
|
||||||
print(f"{bcolors.OKGREEN}{text}{bcolors.ENDC}")
|
return bcolors.OKGREEN + text + bcolors.ENDC
|
||||||
|
|
||||||
def printWarning(text):
|
def warningMessage(text):
|
||||||
print(f"{bcolors.WARNING}{text}{bcolors.ENDC}")
|
return bcolors.WARNING + text + bcolors.ENDC
|
||||||
|
|
||||||
def printInfo(text):
|
def infoMessage(text):
|
||||||
print(f"{bcolors.OKBLUE}{text}{bcolors.ENDC}")
|
return bcolors.OKBLUE + text + bcolors.ENDC
|
||||||
|
|
||||||
def printBold(text):
|
def boldMessage(text):
|
||||||
print(f"{bcolors.BOLD}{text}{bcolors.ENDC}")
|
return bcolors.BOLD + text + bcolors.ENDC
|
||||||
|
|
||||||
def printUnderline(text):
|
def underlineMessage(text):
|
||||||
print(f"{bcolors.UNDERLINE}{text}{bcolors.ENDC}")
|
return bcolors.UNDERLINE + text + bcolors.ENDC
|
||||||
|
|
||||||
def printHeader(text):
|
|
||||||
print(f"{bcolors.HEADER}{text}{bcolors.ENDC}")
|
|
||||||
|
|
||||||
|
def headerMessage(text):
|
||||||
|
return bcolors.HEADER + text + bcolors.ENDC
|
||||||
|
|
||||||
|
def blinkMessage(text):
|
||||||
|
return bcolors.BLINK + text + bcolors.ENDC
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user