def listen_for_input(self): waiting_for_input = True # A while loop for the user input interface # It's a loop that exits once waiting_for_input becomes False or when break is called while waiting_for_input: print("Please choose:") print("1: Add a new transaction value") print("2: Mine a new block") print("3: Output the blockchain blocks") print("4: Check transaction validity") print("5: Create wallet") print("6: Load wallet") print("7: Save wallet keys") print("q: Quit") user_choice = self.get_user_choice() if "1" == user_choice: tx_data = self.get_transaction_value() recipient, amount = tx_data # Add the transaction amount to the blockchain signature = self.wallet.sign_transaction(self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print("Added transaction!") else: print("Transaction failed!") print(self.blockchain.get_open_transactions()) elif "2" == user_choice: if not self.blockchain.mine_block(): print("Mining failed. Got no wallet?") elif "3" == user_choice: self.print_blockchain_elements() elif "4" == user_choice: if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print("All transactions are valid") else: print("There are invalid transactions") elif "5" == user_choice: self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif "6" == user_choice: self.wallet.load_key() self.blockchain = Blockchain(self.wallet.public_key) elif "7" == user_choice: self.wallet.save_keys() elif "q" == user_choice: # This will lead to the loop to exist because it's running condition becomes False waiting_for_input = False else: print("Input was invalid, please pick a value from the list!") if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print("Invalid blockchain!") # Break out of the loop break print("Balance of '{}': {:6.2f}".format(self.wallet.public_key, self.blockchain.get_balance())) else: print("User left!") print("Done!")
def listen_for_input(self): """Starts the node and waits for user input.""" waiting_for_input = True # A while loop for the user input interface # It's a loop that exits once waiting_for_input becomes False or when break is called while waiting_for_input: print('Please choose') print('1: Add a new transaction value') print('2: Mine a new block') print('3: Output the blockchain blocks') print('4: Check transaction validity') print('5: Create wallet') print('6: Load wallet') print('7: Save keys') print('q: Quit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data # Add the transaction amount to the blockchain signature = self.wallet.sign_transaction(self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print('Added transaction!') else: print('Transaction failed!') print(self.blockchain.get_open_transactions()) elif user_choice == '2': if not self.blockchain.mine_block(): print('Mining failed. Got no wallet?') elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All transactions are valid') else: print('There are invalid transactions') elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': # This will lead to the loop to exist because it's running condition becomes False waiting_for_input = False else: print('Input was invalid, please pick a value from the list!') if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print('Invalid blockchain!') # Break out of the loop break print('Balance of {}: {:6.2f}'.format(self.wallet.public_key, self.blockchain.get_balance())) else: print('User left!') print('Done!')
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print('Please choose') print('1: Add a new transaction value') print('2: Mine a new block') print('3: Output the blockchain blocks') print('4: Check transaction validity') print('5: Create Wallet') print('6: Load Wallet') print('7: Save Keys') print('q: Quit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print('Added transaction!') else: print('Transaction failed!') print(self.blockchain.get_last_blockchain_value) elif user_choice == '2': if not self.blockchain.mine_block(): print('I left my wallet in El Segundo') elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions( self.blockchain.get_open_transactions, self.blockchain.get_balance): print('All transactions are valid') else: print('There are invalid transactions') elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print('Input was invalid, please pick a value from the list!') if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print('Invalid blockchain!') break print('Balance of {}: {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) else: print('User left!') print('Done!')
def UI(self): while self.waiting_for_input: print('Enter Choice') print('1:Add transaction') print('2:Mine bLock') print('3: Create Wallet') print('4: Check Transaction valididty: ') print('5:Print bocks') print('6: Load Wallet') print('7: Save Keys') print("q:Quit") user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() receipent, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, receipent, amount) if self.blockchain.add_transaction( receipent=receipent, sender=self.wallet.public_key, signature=signature, amount=amount): print("Successful") else: print("Failed") # print(open_transaction) elif user_choice == '2': if not self.blockchain.mine_block(): print("Mining Failed") elif user_choice == '3': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '4': if Verification.verify_transactions( self.blockchain.get_open_transaction(), self.blockchain.get_balance): print("Valid Transaction") else: print("Invalid transaction") elif user_choice == '5': self.print_blockchain() elif user_choice == '6': self.wallet.load_keys() print('Loaded') elif user_choice == '7': if self.wallet.private_key is not None and self.wallet.public_key is not None: self.wallet.save_keys() print('Saved') elif user_choice == 'q': self.waiting_for_input = False else: print("Invalid Choice") if not Verification.verify_chain(self.blockchain.get_chain()): print("Invalid Blockchain") break print('Balance of {} : {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) else: print("User Left")
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print("Please choose:") print("1: Add a new transaction value") print("2: Min a new block") print("3: Output the blockchain blocks") print("4: Check transaction validity") print("5: Create Wallet") print("6: Load Wallet") print("7: Save Keys") print("q: To quit") user_choice = self.get_user_choice() if (user_choice == "1"): tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print("Added transaction!") else: print("Transaction failed") print(self.blockchain.get_open_transactions()) elif (user_choice == "2"): if not self.blockchain.mine_block(): print("Mining failed. Got no Wallet?") elif (user_choice == "3"): self.print_blockchainelements() elif (user_choice == "4"): if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print("All transactions are valid") else: print("A transaction is invalid") elif (user_choice == "5"): self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif (user_choice == "6"): self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif (user_choice == "7"): self.wallet.save_keys() elif (user_choice == "q"): waiting_for_input = False else: print("Input was invalid, please pick a value from the list") if Verification.verify_chain(self.blockchain.chain) == False: print("invalid blockchain") waiting_for_input = False print('Balance of {}: {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) else: print("User left!") print("Done")
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print("Please Choose") print("1: Add a new trasaction value") print("2: Mine a new block ") print("3: Output the blockchain blocks") print("4: Check transaction validity") print("5: Create wallet") print("6: Load wallet") print("7: save wallet keys ") print("q: Quit") user_choice = self.get_user_choice() if user_choice == "1": tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transactions(self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, amount, signature): print('Added Transaction') else: print('Transaction Failed') print(self.blockchain.get_open_transactions()) elif user_choice == "2": if not self.blockchain.mine_block(): print('Mining failed. Got no wallet') elif user_choice == "3": self.print_blockchain_element() elif user_choice == "4": if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print("All transactions are valid") else: print("There are invalid transactions") elif user_choice == "5": self.wallet.create_keys() self.blockchain = BlockChain(self.wallet.public_key) elif user_choice == "6": self.wallet.load_keys() self.blockchain = BlockChain(self.wallet.public_key) elif user_choice == "7": if self.wallet.public_key != None and self.wallet.private_key != None: self.wallet.save_keys() elif user_choice == "q": waiting_for_input = False else: print("Invalid input. Please select a value from the list") if not Verification.verify_chain(self.blockchain.get_chain()): print("Invalid blockchain") break print( f'Balance of {self.wallet.public_key}: {self.blockchain.get_balance():6.2f}') else: print("User left") print('Done')
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print('\n Please choose') print('1: Add a new transaction value') print('2: Mine a new block') print('3: Output the blockchain blocks') print('4: Check transactions validity') print('5: Create wallet') print('6: Load wallet') print('7: Save keys') print('q: Quit') user_choice = self.get_user_choice() if user_choice == '1': recipient, amount = self.get_transaction_value() signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print("Added transaction!") else: print("Transaction failed \n") print(self.blockchain.get_open_transactions()) elif user_choice == '2': if not self.blockchain.mine_block(): print("Mining has failed. Got no wallet?") elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print("All transactions are valid") else: print("There are invalid transactions") elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print('Input was invalid, please pick a value from the list!') if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print('Invalid blockchain!') break print( f"Balance of {self.wallet.public_key}: {self.blockchain.get_balance()}" ) print(self.blockchain.get_balance()) else: print('User left!')
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print('Please choose:') print('1: Add a new transaction value') print('2: Mine the current block') print('3: Ouput the blockchain blocks') print('4: Verify transactions validitya') print('5: Create wallet') print('6: Load wallet') print('7: Save keys') print('q: Quit') user_choice = self.get_user_choice() if user_choice == '1': recipient, amount = self.get_transaction_value() signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(self.wallet.public_key, recipient, signature, amount): print('Transction succeeded') else: print('Transaction failed') print(self.blockchain.get_open_transactions()) elif user_choice == '2': if not self.blockchain.mine_block(): print('Mining failed. Got no wallet?') elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('Transactions are valid') else: print('There is an invalid transaction') elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print('Input was invalid!!') if self.blockchain != None and (not Verification.verify_chain( self.blockchain.chain)): print('Invalid blockchain') break print('Done')
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print('---Choose your action---') print('1: Add new transactin value.') print('2: Mine a new block.') print('3: Output the blockchain.') print('4: Check transaction validity.') print('5: Create wallet.') print('6: Load wallet.') print('7: Save wallet.') print('q: Quit.') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print('New transaction was added!') else: print('Transaction failed! Error.') elif user_choice == '2': if self.blockchain.mine_block(): print('Successfully mined.') else: print('Mining faled. Got no wallet.') elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All transactions is valid.') else: print('Transactions validation is faled!') elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print('Invalid input. Try again.') if not Verification.verify_chain(self.blockchain.chain): print('ERROR! Chain is not valid!') break print('Balance of {}: {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) print('Goodbye.')
def listen_for_input(self): while True: choice = self.get_menu_input() if choice == 1: tx_recipient, tx_amount = self.get_transaction_input() signature = self.wallet.sign_transactions( self.wallet.public_key, tx_recipient, tx_amount) if self.blockchain.add_transaction( recipient=tx_recipient, sender=self.wallet.public_key, signature=signature, amount=tx_amount): print('Added Transaction') else: print('Transaction Failed!') print(self.blockchain.get_open_transactions()) elif choice == 2: if not self.blockchain.mine_block(): print('Mining Failed...Got wallet ?') elif choice == 3: self.display_blockchain() elif choice == 4: if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All transactions are valid!') else: print('Invalid Tx present!') elif choice == 5: self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif choice == 6: self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif choice == 7: self.wallet.save_keys() elif choice == 8: break else: print('Invalid Input!') if not Verification.verify_chain_integrity( self.blockchain.get_chain()): print('Block chain has been compromised .... x x x x ') break print( f'Balance of {self.wallet.public_key} = {self.blockchain.get_balance():.2f}' ) print('Done :) ') return 0
def listen_for_input(self): while True: print('Please choose') print('1: Add a new transaction') print('2: Print blockchain') print('3: Quit') print('4: Verify all transaction') print('5: Mine a new block') print('6: Create wallet') print('7: Load wallet') print('8: Save wallet') user_choice = self.get_user_choice() if user_choice == 1: tx_data = self.get_transaction_value() tx_recipient, tx_amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, tx_recipient, tx_amount) if self.blockchain.add_transaction(tx_recipient, self.wallet.public_key, signature, amount=tx_amount): print('Added transaction!') else: print('Transaction failed!') print(self.blockchain.get_open_transactions()) elif user_choice == 2: self.print_blockchain_elements() elif user_choice == 4: if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All transactions are valid') else: print('There are invalid transactions') elif user_choice == 3: break elif user_choice == 5: if not self.blockchain.mine_block(): print("Mining failed") elif user_choice == 6: self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == 7: self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == 8: self.wallet.save_keys() else: print('Invalid choice') if not Verification.verify_chain(self.blockchain.chain): print('Invalid blockchain!') break print('Balance of {}: {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) print("Done!")
def listen_for_input(self): waiting_for_input = True while waiting_for_input: self.print_options() user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data # Add the transaction amount to the blockchain signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print('Added transaction') else: print('Transaction failed!') print(self.blockchain.get_open_transactions()) elif user_choice == '2': if not self.blockchain.mine_block(): print('Mining failed. Got no wallet with valid keys?') elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All transactions are valid') else: print('There are invalid transactions') elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print('Option not recognised!') if not Verification.verify_chain(self.blockchain.get_chain()): print("***Invalid blockchain!***") break print( f'{self.wallet.public_key}\'s Balance is {self.blockchain.get_balance(self.wallet.public_key):6.2f}' ) else: #Loop is done - didn't break print('User Left!') print('Done!')
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print(""" Please choose: 1) Add a new transaction value 2) Mine a new block 3) Output the blockchain blocks 4) Check transaction validity 5) Create wallet 6) Load keys 7) Save keys q: Quit """) user_choice = self.get_user_choice() if user_choice == '1': recipient, amount = self.get_transaction_value() signature = self.wallet.sign_transaction(sender=self.wallet.public_key, recipient=recipient, amount=amount) if self.blockchain.add_transaction(sender=self.wallet.public_key, recipient=recipient, amount=amount, signature=signature): print('Added transaction') else: print('Transaction failed') # print(open_transactions) elif user_choice == '2': if not self.blockchain.mine_block(): print("Mining failed. Got no wallet?") elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('all transaction are valid') else: print('there are invalid transactions') elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print('invalid input, choose a value from the list') print("Balance of {}: {:6.2f}".format(self.wallet.public_key, self.blockchain.get_balance())) if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print('invalid blockchain!') break
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print("Please choose: ") print("1: Add a new transaction value") print("2: Mine a new block") print("3: Output the blockchain blocks") print("4: Check transactions validity") print("5: Create wallet") print("6: Load wallet") print("7: Save keys") print("q: Quit") user_choice = self.get_user_choice() if user_choice == "1": recipient, amount = self.get_transaction_value() signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient=recipient, sender=self.wallet.public_key, signature=signature, amount=amount): print("Added transaction") else: print("Transaction failed") elif user_choice == "2": if not self.blockchain.mine_block(self.wallet.public_key): print("Mining failed. Got not wallet?") elif user_choice == "3": self.print_blockchain_elements() elif user_choice == "4": if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print("All transactions are valid") else: print("There are invalid transactions") elif user_choice == "5": self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == "6": self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == "7": self.wallet.save_keys_to_file() elif user_choice == "q": waiting_for_input = False else: print("Input was invalid, please pick a value from the list!") if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print("invalid blockchain") break print( f"Balance of {self.wallet.public_key}: {self.blockchain.get_balance():6.2f}") else: print("User left") print("Done")
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print('Please choose') print('1: Add a new transaction value') print('2: Mine block') print('3: Output the blockchain blocks') print('4: Check transaction validity') print('q: quit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() # Unpacking the tuple # Pulls out first el and stores in recipient then pulls second el and stores in amount recipient, amount = tx_data # Since sender = owner, we need to put amount = amount to skip the default for sender if self.blockchain.add_transaction(recipient, self.id, amount=amount): print('Added transaction!') else: print('Transaction failed!') print(self.blockchain.get_open_transactions()) elif user_choice == '2': self.blockchain.mine_block() elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': # Calling the class directly with static methods if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All transactions are valid') else: print('There are invalid transactions') elif user_choice == 'q': waiting_for_input = False else: print('Invalid input. Please pick from the menu items') if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print('Invalid blockchain!') break else: print('Verfication Occurred') # Using format method to output nice string print('Balance of {}:{:6.2f}'.format( self.id, self.blockchain.get_balance())) else: print('User left!') print('Done!')
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print('Please choose :') print('1: Add a new transaction value') print('2: Mine a new block') print('3: Output the blockchain blocks') print('4: Check transaction validity') print('q: Quit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data if self.blockchain.add_transaction(recipient, self.id, amount=amount): print('Added transaction') else: print('Transaction failed!') print(self.blockchain.get_open_transactions()) elif user_choice == '2': self.blockchain.mine_block() elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions( self.blockchain.open_transactions, self.blockchain.get_balance): print('----- All transactions are valid -----') else: print('----- There are invalid transactions -----') elif user_choice == 'q': waiting_for_input = False else: print('Invalid input') if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print('Invalid blockchain!') break print('Balance of {} : {:6.2f}'.format( self.id, self.blockchain.get_balance())) print('Done !')
def listen_for_input(self): wating_for_input = True while wating_for_input: print("please choose: ") print("1: Add new transaction value") print("2: Mine a new block") print("3: Print the blockchain") print("4: check transaction va lidity") print("5: create wallet") print("6: load wallet") print("7: save keys") print("q: Quit !") user_choice = self.get_user_choice() if user_choice == "1": tx_data = self.get_transaction_value() recipient, amount = tx_data signature= self.wallet.sign_transaction(self.wallet.public_key,recipient,amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key,signature , amount=amount): print("Transaction added") else: print("Adding transation failed") print(self.blockchain.get_open_transactions()) elif user_choice == "q": wating_for_input = False elif user_choice == "2": if not self.blockchain.mine_block(): print("Mining field, Wallet is inavailable!") elif user_choice == "3": self.print_blockchain_elements() elif user_choice == "4": if Verification.verify_transactions(self.blockchain.open_transactions, self.blockchain.get_balence): print('All Transactions are valid') else: print("There are invalid transaction") elif user_choice =="5": # saving the keys in files self.wallet.create_keys() # initializing the blockchian self.blockchain = Blockchain(self.wallet.public_key) elif user_choice =="6": self.wallet.load_keys() elif user_choice =="7": self.wallet.save_to_file() if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print("invalid blockchain") break print(" the balance of {} is : {:6.2f}".format( self.wallet.public_key, self.blockchain.get_balence())) else: print("User left !") print('Programme terminer !')
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print('Please choose') print('1: Add new transaction value') print('2: Mine new block') print('3: Output the blockchain blocks') print('4: Check transaction validity') print('5: Create wallet') print('6: Load wallet') print('7: Save keys') print('q: Quit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction(self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient=recipient, sender=self.wallet.public_key, signature=signature, amount=amount): print('Added transaction!') else: print('Transaction failed') print(self.blockchain.get_open_transactions()) elif user_choice == '2': if not self.blockchain.mine_block(): print('Mining failed. Got no wallet?') elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All txs are valid') else: print('There are invalid txs') elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print('Input was invalid') print('Balance of {}: {:6.2f}'.format(self.wallet.public_key, self.blockchain.get_balance())) if not Verification.verify_chain(self.blockchain.get_chain()): print('Invalid blockchain') break
def listen_to_user_input(self): waiting_for_user_input = True while waiting_for_user_input: print("Make a choice") print("1: Add new transaction value") print("2: Mine a new block") print("3: Outputting the blockchain blocks") print("4: Check transactions validity") print("5: Create wallet") print("6: Load wallet") print("7: Save keys") print("Q ----> Quit") user_choice = self.get_user_choice() if user_choice == "1": recipient, amount = self.get_transaction_value() signature = self.wallet.sign_transaction(self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print("Transaction added successfuly!") else: print("Transaction failed!") print(self.blockchain.get_open_transactions()) elif user_choice == "2": if not self.blockchain.mine_block(): print("Mining failed. Got no wallet?") elif user_choice == "3": self.print_blockchain_elements() elif user_choice == "4": if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print("All transactions are valid") else: print("There are invalid transanction") elif user_choice == "5": self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key, "") elif user_choice == "6": self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key, "") elif user_choice == "7": self.wallet.save_keys() elif user_choice.lower() == 'q': break else: print("Invalid input, Please enter an option from those displayed on your screen!") if not Verification.verify_blockchain(self.blockchain.chain): self.print_blockchain_elements() print("Invalid blockchain!") break print("Balance of {}: {:6.2f}".format(self.wallet.public_key, self.blockchain.get_balance())) print("Done!")
def mine_block(self): """ Create a new block and adds open transactions to it. """ if self.public_key == None: return None last_block = self.__chain[-1] hashed_block = hash_block_256(last_block) node_id, proof = self.proof_of_stake() copied_transaction = self.__transactions[:] if not Verification.verify_transactions(copied_transaction, self.get_balance): return None block = Block(len(self.__chain), node_id, proof, copied_transaction, hashed_block) return block
def listen_for_input(self): while True: user_input = self.get_user_choice() if user_input == '1': tx_data = self.get_transaction_value() #unpacking tuple recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount): print('Transaction Added') else: print('Transaction Failed') #print(open_transactions) elif user_input == '2': if not self.blockchain.mine_block(): print('No Wallet. Mining Failed') elif user_input == '3': self.display_blockchain() elif user_input == '4': if Verification.verify_chain(self.blockchain.chain): print('\nThe Blockchain is Valid') else: print('\nThe Blockchain has been compromised') break elif user_input == '5': if Verification.verify_transactions( self.blockchain.get_open_transaction, self.blockchain.get_balances): print('Valid Transactions') else: print('Invalid Transactions') elif user_input == '6': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_input == '7': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_input == '8': self.wallet.save_keys() else: break print('New Balance for {} : {:10.2f}'.format( self.wallet.public_key, self.blockchain.get_balances())) if not Verification.verify_chain(self.blockchain.chain): print('\nThe Blockchain has been compromised') break
def listen_for_inputs(self): waiting_for_input = True while waiting_for_input: print('1: transaction') print('2: mine block') print('3: history') print('4: validity') print('5: create wallet') print('6: load wallet') print('7: save wallet') print('q: exit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print("added transaction") else: print('transaction failed') elif user_choice == '2': if not self.blockchain.mine_block(): print("Mining failed") elif user_choice == '3': self.print_blockchain_history() elif user_choice == '4': if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('all true') else: print("invalid exist") elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print("input invalid") if not Verification.verify_chain(self.blockchain.get_chain()): self.print_blockchain_history() print("invalid blockchain") break
def listen_for_input(self): wait_for_input = True while wait_for_input: print("Wybierz akcje ") print('1 Nowa wartość transakcji \n2 Mine nowy blok \n3 Wyświetl bloki blockchaina \n4 Sprawdź ważność transakcji \n5 Stwórz portfel \n6 Wczytaj portfel \n7 Zapisz klucze\nq Aby wyjść') user_choice = self.get_user_choice() if user_choice == '1': recipient, amount = self.get_transaction_val() signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print('Dodano transakcje') else: print('Niepowodznie w dodawaniu transakcji') # print(open_transactions) elif user_choice == '2': if not self.blockchain.mine_block(): print('Niepowodzenie miningu, sprawdź czy posiadasz portfel') elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print("Transakcje są poprawne") else: print("Transakcje błędne") elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == 'q': wait_for_input = False else: print('Niepoprawna cyfra ') if not Verification.verify_chain(self.blockchain.chain): print("Blockchain został zmanipulowany!") break print("Bilans konta {} : {:6.2f}".format( self.wallet.public_key, self.blockchain.get_balance())) else: print("Użytkownik wyszedł")
def listening_for_input(self): waiting_for_input = True while waiting_for_input: choice = self.get_user_choice() if choice == '1': if self.wallet.public_key==None: print('Transaction failed!') continue new_recipient, new_amount = self.get_transaction_value() signature=self.wallet.sign_transaction(self.wallet.public_key,new_recipient,new_amount) if self.blockchain.add_transaction(new_recipient, self.wallet.public_key, signature, new_amount)!=None: print('Added transaction!') else: print('Transaction failed!') elif choice == '2': if not self.blockchain.mine_block(): print('Mining failed. Got no wallet?') elif choice == '3': self.print_blockchain_elements() elif choice == '4': if Verification.verify_transactions(self.blockchain.get_balance,self.blockchain.get_open_transactions()): print('All transactions are valid') else: print('There are invalid transactions') print(self.blockchain.get_open_transactions()) elif choice == '5': self.wallet.create_keys() self.blockchain=Blockchain(self.wallet.public_key) elif choice == '6': self.wallet.load_keys() self.blockchain=Blockchain(self.wallet.public_key) elif choice == 'q': waiting_for_input = False else: print('Input was invalid, please pick a value from the list!') if not Verification.verify_blockchain(self.blockchain.chain): print('Invalid blockchain!') break print(f"Balance of {self.wallet.public_key}: {self.blockchain.get_balance():6.2f}") else: print('User left!') print("Done!")
def listen_for_input(self): waiting_for_input = True while waiting_for_input: user_choice = self.print_menu() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data # tuple unpacking if self.blockchain.add_transaction(recipient, self.id, amount=amount): print('Added transaction!') else: print('Transaction failed') print(self.blockchain.get_open_transactions()) elif user_choice == '2': self.blockchain.mine_block() elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': print('=== Transactions are all valid: ' + str( Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance))) elif user_choice == 'q': waiting_for_input = False else: print('Input was invalid, please pick from the list') if not Verification.verify_chain(self.blockchain.get_chain()): print('Invalid blockchain') waiting_for_input = False print(f'Balance for {self.id}: {self.blockchain.get_balance()}') else: print('User left!') print('Done!')
def listen_for_input(self): quit_input = False while quit_input is False: print("Select option... ") print(f'1. Add new transaction: ') print(f'2. Mine new block') print(f'3. Output blockchain: ') print(f'4. Check transaction validity: ') print(f'5. Create Wallet') print(f'6. Load Wallet') print(f'7. Save Keys') print(f'q. Quit') user_choice = self.get_user_choice() if user_choice == "1": print("*** Adding Transaction Start ***") tx_data = self.get_transaction_value() tx_recipient, tx_amount = tx_data signature = self.wallet.sign_transaction(self.wallet.public_key, tx_recipient, tx_amount) if self.blockchain.add_transaction(tx_recipient, self.wallet.public_key, signature, amount=tx_amount): print("Success: add_transaction") else: print("Failed: add_transaction") print(self.blockchain.get_open_transactions()) print("*** Adding Transaction End ***") elif user_choice == "2": print("*** Mining Block Start ***") if not self.blockchain.mine_block(self.wallet.public_key): print("Mining failed, You have no wallet!") print("*** Mining Block End ***") elif user_choice == "3": print("*** Outputing Blockchain Start ***") self.print_blockchain_elements() print("*** Outputing Blockchain End ***") elif user_choice == "4": print("*** Verifying Blockchain Start ***") if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print("All transactions verfied") else: print("Invalid transactions found") print("*** Verifying Blockchain End ***") elif user_choice == "5": print("*** Creating your wallet ***") # Store in file for dev purposes self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key, self.port) print("*** Create wallet completed ***") elif user_choice == "6": self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key, self.port) elif user_choice == "7": print("*** Saving `keys ***") self.wallet.save_keys() print("*** Saving Keys Complete ***") elif user_choice == "q": quit_input = True else: print("Input invalid. Please choose a valid choice") if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print("Invalid Blockchain") break print(f'Balance for {self.wallet.public_key}: {self.blockchain.get_balance():6.2f}') else: print("User left") print("Done")
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print("Please choose") print("1. Add a new transaction value") print("2. Mine a new block") print("3. Output the blockchain blocks") print("4. Check transaction validity") print("5. Create Wallet") print("6. Load Wallet") print("7. Save Wallet Keys") print("q. Exit") user_choice = self.get_user_choice() if user_choice == "1": tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction( recipient=recipient, sender=self.wallet.public_key, signature=signature, amount=amount): print("Added transaction!!") else: print("Transaction failed!!") print(self.blockchain.get_open_transactions()) elif user_choice == "2": if not self.blockchain.mine_block(): print("Mining failed. Got no wallet?") elif user_choice == "3": self.print_blockchain_elements() elif user_choice == "4": if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print("All transactions are valid") else: print("There are invalid transactions") elif user_choice == "5": self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == "6": self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == "7": self.wallet.save_keys() elif user_choice == "q": waiting_for_input = False else: print("Invalid Choice, please try again") if not Verification.verify_chain(self.blockchain.chain): print("Invalid blockchain") break print("-" * 30) print('Balance of {}: {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) print("-" * 30) else: print("goodbye")
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print('Please choose') print('1: Add a new transaction value') print('2: Mine a new block') print('3: Output the blockchain blocks') print('4: Check transaction validity') print('5: Create wallet') print('6: Load wallet') print('7: Save keys') print('q: Quit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print('Added transaction') else: print('Transaction failed') print(self.blockchain.open_transactions) elif user_choice == '2': if not self.blockchain.mine_block(): print('Mining failed.') elif user_choice == '3': self.print_blockchain_elements() elif user_choice == '4': verifier = Verification() if verifier.verify_transactions( self.blockchain.open_transactions, self.blockchain.get_balance): print('All transactions are valid') else: print('There are invalid transactions') elif user_choice == '5': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': waiting_for_input = False else: print('Invalid choice') # Verification.verify_chain() works here despite not instantiating that class because verify_chain is a @staticmethod if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print('Invalid blockchain') break print('Balance of {}: {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) else: print('User left!') print('Done')
def listen_to_input(self): while True: print('Choose:') print('1: Add value to the blockchain') print('2 : Mine Block') print('3: print the blocks') print('4: Create wallet') print('5: load Wallet') print('6: check transaction validity on open transaction') print('7: save wallet') print('q: Quit') choice = self.get_choice() if choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print('transaction added!!') else: print('transaction failed!') print(self.blockchain.get_open_transaction) elif choice == '2': if not self.blockchain.mine_block(): print('Mining failed. Got no wallet') # print(blockchain) elif choice == '3': self.print_blocks() elif choice == '4': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif choice == '5': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif choice == '6': if Verification.verify_transactions( self.blockchain.get_open_transaction(), self.blockchain.get_balance): print('transactions are valid') else: print('transactions are not valid') elif choice == '7': self.wallet.save_keys() elif choice == 'q': break else: print('input was invalid, check from the list') if not Verification.block_verify(self.blockchain.chain): print('invalid chain') break print('Balance of {} is : {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) else: print('user left') print("done")
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print('1: Add new transaction value') print('2: Mine new block') print('3: Output blockchain blocks') print('4: Check transaction validity') print('5: Create wallet') print('6: Load wallet') print('7: Save keys') print('q: Quit') user_choice = self.get_user_choice().lower() if user_choice == '1': tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print('-' * 20) print('\n*** Success. Transaction added. ***\n') print('-' * 20) else: print('-' * 20) print('\n*** Insuficient funds. Transaction failed. ***\n') print('-' * 20) print(self.blockchain.get_open_transactions()) elif user_choice == '2': if not self.blockchain.mine_block(): print('-' * 20) print('Mining failed! Do you have a wallet?') print('-' * 20) elif user_choice == '3': print('>' * 20) self.print_blockchain_elements() print('>' * 20) elif user_choice == '4': if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('-' * 20) print('All transactions are valid.') print('-' * 20) elif user_choice == '5': # self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '7': self.wallet.save_keys() elif user_choice == 'q': print('-' * 20) print('Goodbye') print('-' * 20) waiting_for_input = False else: print('-' * 20) print(' \n*** Invalid input. Try again. ***\n') print('-' * 20) if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print('^' * 20) print('\n*** Error. Invalid blockchain. ***\n') print('^' * 20) break print('<' * 20) print('Balance of {}: {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) print('<' * 20)
def listen_for_input(self): """Starts the node and waits for user input.""" waiting_for_input = True # A while loop for the user input interface # It's a loop that exits once waiting_for_input becomes False or when break is called while waiting_for_input: print("Please enter your choice") print("1: Add a new transaction value") print("2: Mine Block") print("3: Output the blockchain blocks") print("4: Check transaction validity") print("5: Create Wallet") print("6: Load Wallet") print("7: Save keys") print("q: Quit") user_choice = self.get_user_choice() if user_choice == "1": tx_data = self.get_transaction_value() recipient, amount = tx_data signature = self.wallet.sign_transaction(self.wallet.public_key, recipient, amount) # Add the transaction amount to the blockchain if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount): print('Added transaction!') else: print('Transaction failed!') print(self.blockchain.get_open_transactions()) elif user_choice == "2": if not self.blockchain.mine_block(): print("Mining Failed. Got no Wallet?") elif user_choice == "3": self.print_blockchain_blocks() elif user_choice == "4": # verifier = Verification() if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balances): print("All transactions are valid!") else: print("There are some transactions failed!") elif user_choice == "5": self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == "6": self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == "7": self.wallet.save_keys() elif user_choice == "q": # This will lead to the loop to exist because it's running condition becomes False waiting_for_input = False else: print("Input is invalid, please pick a value from a list.") # verifier = Verification() if not Verification.verify_chain(self.blockchain.get_chain()): self.print_blockchain_blocks() print("Invalid Blockchain!") # Break out of the loop break print("Balance of {}: {:6.2f}".format(self.wallet.public_key, self.blockchain.get_balances())) else: print("User Left!") print ("Done!")