def load_keys(): if wallet.load_keys(): global blockchain blockchain = Blockchain(wallet.public_key, port) response = { 'public_key': wallet.public_key, 'private_key': wallet.private_key, 'funds': blockchain.get_balance() } return jsonify(response), 201 else: response = { 'message': 'Loading the keys failed.' } return jsonify(response), 500
from block import Block from blockhain import Blockchain blockchain = Blockchain() # mine for 10 blocks for n in range(10): blockchain.mine(Block(f"Block {n + 1}")) # prints mining output while blockchain.head is not None: print(blockchain.head) blockchain.head = blockchain.head.next
if node_url == '' or node_url == None: response = { 'message': 'No node found.' } return jsonify(response), 400 blockchain.remove_peer_node(node_url) response = { 'message': 'Node removed', 'all_nodes': blockchain.get_peer_nodes() } return jsonify(response), 200 @app.route('/nodes', methods=['GET']) def get_nodes(): nodes = blockchain.get_peer_nodes() response = { 'all_nodes': nodes } return jsonify(response), 200 if __name__ == '__main__': from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument('-p', '--port', type=int, default=5000) args = parser.parse_args() port = args.port wallet = Wallet(port) blockchain = Blockchain(wallet.public_key, port) app.run(host='0.0.0.0', port=port)
class Node: """The node which runs the local blockchain instance. Attributes: :id: The id of the node. :blockchain: The blockchain which is run by this node. """ def __init__(self): # self.id = str(uuid4()) self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): """ Returns the input of the user (a new transaction amount) as a float. """ # Get the user input, transform it from a string to a float and store it in user_input tx_recipient = input('Enter the recipient of the transaction: ') tx_amount = float(input('Your transaction amount please: ')) return tx_recipient, tx_amount def get_user_choice(self): """Prompts the user for its choice and return it.""" user_input = input('Your choice: ') return user_input def print_blockchain_elements(self): """ Output all blocks of the blockchain. """ # Output the blockchain list to the console for block in self.blockchain.chain: print('Outputting Block') print(block) else: 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 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): """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 __init__(self): # self.id = str(uuid4()) self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key)