def listen_for_input(self): # ---------------------------------------------------- # variable to finish the while loop waiting_for_input = True # while loop while waiting_for_input: print('Please choose: ') print('1: Add a new transaction value') print('2: Output the blockchain blocks') print('3: Mine a new block') print('4: Check transaction validity') print('5: Create new wallet') print('6: Load Wallet') print('7: Save wallet keys') print('q: Finish the transactions') 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': self.print_blockchain_blocks() elif user_choice == '3': if not self.blockchain.mine_block(): print('Mining failed!') 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!!') if not Verification.verify_chain(self.blockchain.get_blockchain()): self.print_blockchain_blocks() print('The blockchain was modified, Invalid!') break print('Balance of {}:{:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance()))
def resolve(self): winner_chain = self.chain replace = False for node in self.__peer_nodes: url = 'http://{}/chain'.format(node) try: response = requests.get(url) node_chain = response.json() node_chain = [ Block(block['index'], block['previous_hash'], [ Transaction(tx['sender'], tx['recipient'], tx['signature'], tx['amount']) for tx in block['transactions'] ], block['proof'], block['timestamp']) for block in node_chain ] node_chain_length = len(node_chain) local_chain_length = len(winner_chain) if node_chain_length > local_chain_length and Verification.verify_chain( node_chain): winner_chain = node_chain replace = True except requests.exceptions.ConnectionError: continue self.resolve_conflicts = False self.chain = winner_chain if replace: self.__open_transactions = [] self.save_data() return replace
def listen_for_input(self): waiting_for_input = True while waiting_for_input: print() print('Please choose'.upper()) print('-' * 25) print('1. Add transaction') print('2. Mine block') print('3. Output blockchain') print('4. Check transaction validity') print('5. Create wallet') print('6. Load wallet') print('7. Save wallet') print('0. Exit') print('-' * 25) 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): 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() elif user_choice == '4': Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance) 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 == '0': waiting_for_input = False else: print('Invalid choice') if not Verification.verify_chain(self.blockchain.chain): print('Invalid chain') break print('The balance of {} is {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) else: print('Goodbye')
def resolve(self): winner_chain = self.chain replace = False # check which peer_nodes have which blockchain (peer_nodes storage in the blockchain.txt) for node in self.__peer_nodes: url = 'http://{}/chain'.format(node) try: response = requests.get(url) # extract json like a dictionary node_chain = response.json() # transform the chain into a list of block objects and the transactions inside into a list of transaction objects node_chain = [ Block(block['index'], block['previous_hash'], [ Transaction(tx['sender'], tx['recipient'], tx['signature'], tx['amount']) for tx in block['transactions'] ], block['proof'], block['timestamp']) for block in node_chain ] # compare local blockchain with the blockchain of each peer_node storaged node_chain_length = len(node_chain) local_chain_length = len(winner_chain) if node_chain_length > local_chain_length and Verification.verify_chain( node_chain): winner_chain = node_chain replace = True # except can occurs when a peer_node is not connected except requests.exceptions.ConnectionError: continue self.resolve_conflicts = False self.chain = winner_chain if replace: self.__open_transactions = [] self.save_data() return replace
def listen_to_input(self): waiting_for_input = True while waiting_for_input: print('Please chose') print('1: Add new transaction value') print('2: Mine a new block') print('3: Output the blockchain blocks') print('4: Check transaction validity') print('5: Create new 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_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': open_transactions = self.blockchain.get_open_transactions() if Verification.verify_transactions(open_transactions, self.blockchain.get_balance): print('All transaction is 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(30 * '-') print('Balance of {}:{:6.2f} coins'.format(self.wallet.public_key, self.blockchain.get_balance())) else: print('User left!')
def prompt_for_input(self): while True: print("Please choose") print("1: Add a new transaction value") print("2: Mine a block") print("3: Print blocks") print("4: Check validity of all transactions") print("5: Create wallet, make sure to save too") 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("Added transaction") else: print("Transaction failed") elif user_choice == "2": if not self.blockchain.mine_block(): print("No wallet found, mining failed") 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": break else: print("Input was invalid, please pick a value from the list!") if not Verification.verify_chain(self.blockchain.chain): print("Invalid blockchain!") break print("Balance of {}: {:*^10.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: print('Please choose an option: ') 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 a new wallet') print('6: Load a wallet') print('7: Save a wallet (keys)') print('9: 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, amount, self.wallet.public_key, signature): print('Transaction has been added successfully!') else: print('The transaction has failed!') print(self.blockchain.get_open_transactions()) elif user_choice == '2': if not self.blockchain.mine_block(): print('The mining failed. Check if you already have a 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 == '9': waiting_for_input = False else: print('Input was invalid, please, choose a value from the list!') # After each action, we verify if the chain was not manipulated. if not Verification.verify_chain(self.blockchain.get_chain()): print('Invalid blockchain!') waiting_for_input = False print('Balance of {}: {:6.2f}'.format(self.wallet.public_key, self.blockchain.get_balance())) print('Done!')
def listen_for_input(self): while self.add_new: user_answer = self.get_user_choice() if user_answer == "1": if self.wallet.public_key == None: print( "transaction cancelled, please log in or create account" ) continue new_transaction = self.get_transaction() recipient, amount = new_transaction signature = self.wallet.sign_transaction( self.wallet.public_key, recipient, amount) if self.blockchain.add_value(signature, self.wallet.public_key, recipient, amount): print("success!!") else: print("transaction cancelled") elif user_answer == "2": if not self.blockchain.mine_block(): print("no wallet found") elif user_answer == "3": self.add_new = False elif user_answer == "4": if self.wallet.public_key == None: print("please login or create a new account") continue print("balance for {} : {:6.2f}".format( self.wallet.public_key, self.blockchain.get_balance())) elif user_answer == "5": self.blockchain.print_blockchain() elif user_answer == "6": self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_answer == "7": self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) else: print("answer invalid") if not Verification.verify_chain(self.blockchain.chain): print("hack detected") 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 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_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('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 !')
def listen_for_input(self): while True: print('==================================') print('Please choose') print('0: Create wallet') print('1: Load wallet') print('2: Save wallet') print('3: Add a new transaction') print('4: Mine new block') print('5: Show blockchain') print('6: Show balance') print('7: Show open transactions') print('8: Check transaction validity') print('q: Quit') print('==================================') user_choice = self.get_user_choice() if user_choice == '0': # Create wallet self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '1': # Load wallet self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '2': # Save wallet self.wallet.save_keys() elif user_choice == '3': # Add new open transaction tx_data = self.get_transaction_value() # returns a 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=amount): print('Added transaction!') else: print('Transaction failed!') elif user_choice == '4': # Mine new block if not self.blockchain.mine_block(): print('Mining failed. Wallet wasnot found') elif user_choice == '5': # Show blockchain self.print_blockchain_elements() elif user_choice == '6': # Show balance (amount_sent, amount_recieved, balance) = self.blockchain.get_balance() print(f'{self.wallet.public_key[:5]} sent: {amount_sent:.2f}') print( f'{self.wallet.public_key[:5]} recieved: {amount_recieved:.2f}' ) print( f'Balance of {self.wallet.public_key[:5]}: {balance:.2f}') elif user_choice == '7': # Show open transactions print(self.blockchain.get_open_transactions()) elif user_choice == '8': if Verification.check_transaction_validity( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All transactions are valid') else: print('There are invalid transactions!') elif user_choice == 'q': break else: print('Input was invalid, please pick a value from the list!') if not Verification.verify_chain(self.blockchain.chain): print('Invalid blockchain') break 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_quit = True while waiting_for_quit: print('Please choose') print('1 - Add a new transaction value') print('2 - Mine block') print('3 - Output the blockchain blocks') print('4 - Check validaty of transactions') 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('Transaction added') else: print('Failed to add, balance does not match') print('Transactions: {}'.format(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.output_blocks() elif user_choice == '4': if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All transactions are valid') else: print('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': print('Done') waiting_for_quit = False else: print('Input invalid, please choose other.') if not Verification.verify_chain(self.blockchain.chain): self.output_blocks() print('Invalid blockchain') break print('\n') print('Balance of {}: {:6.2f} '.format(self.wallet.public_key, self.blockchain.get_balance())) else: print('User left')