def __init__(self, node_id, location): global blockchain global primary_wallets global PU_img global live_nodes global modulated_waves global node global wallet live_nodes.append(node_id) live_nodes[node_id] = canvas.create_image(10, 10, anchor=NW, image=PU_img) self.node_id = node_id self.location = location canvas.coords(live_nodes[node_id], [self.location[0], self.location[1]]) print("node has been created sir") self.N = 600 self.T = 1.0 / 800.0 self.Fm = 5 self.Ac = 5 self.Am = 2 self.Fc = 0 #used_freq[node_id]=self.Fc #avail_freq.pop(0) self.t = np.linspace(0.0, self.N * self.T, self.N) self.tf = np.linspace(0.0, 1.0 / (2.0 * self.T), self.N // 2) self.transmission_power = 20 self.t_gain = 2 self.r_gain = 2 self.carrier = np.cos(2 * pi * self.Fc * self.t) self.message = np.cos(2 * pi * self.Fm * self.t) self.km = self.Am / self.Ac self.Pc = pow(self.Ac, 2) / 2 self.Pt = self.Pc * (1 + ((self.km)**2) / 2) self.mod = self.Ac * (1 + self.km * (self.message)) * self.carrier modulated_waves[self.Fc] = self.mod self.mod_fft = fft(self.mod) primary_wallets.append(node_id) self.port = 5000 + node_id os.system( "gnome-terminal -e 'bash -c \"python node.py -p {}; exec bash\"'". format(5000 + self.node_id)) wallet = Wallet(self.port) primary_wallets[node_id] = wallet wallet.create_keys() print("Wallet created Now saving the keys") if wallet.save_keys(): global blockchain print("Blockchain created for node", self.port) blockchain = Blockchain(wallet.public_key, self.port) #print("public key for node_id "+str(self.port)+" created") else: print("creating wallet failed")
class Node: def __init__(self): # self.id = str(uuid4()) self.wallet = Wallet() self.wallet.create_keys() self.Votechain = Votechain(self.wallet.public_key) def get_vote_value(self): vote_given_to = input('Enter the party name to give vote to: ') return vote_given_to def get_user_choice(self): """Prompts the user for its choice and return it.""" user_input = input('Your choice: ') return user_input # def get_walet_balance(self): # token = self.wallet.amount # return token def listen_for_input(self): waiting_for_input=True while waiting_for_input: print('Please choose') print('1: Login') print('2: Give vote') print('q: Quit') user_choice = self.get_user_choice() if user_choice == '1': self.wallet.create_keys() print(self.wallet.public_key) elif user_choice == '2': vote_to = self.get_vote_value() signature = self.wallet.sign_vote(self.wallet.public_key, vote_to) if self.Votechain.add_vote(vote_to, self.wallet.public_key, signature): self.wallet.vote_used() print('Vote Added!') else: print('Voting Failed!') elif user_choice == '3': self.Votechain.print_open_list() elif user_choice == 'q': waiting_for_input = False
class Register: def __init__(self, node_id, address): self.wallet = Wallet(node_id) self.wallet.create_keys() self.wallet.save_keys() self.address = address self.public_key = self.wallet.public_key self.private_key = self.wallet.private_key self.id = -1 #Will be set automatically with bootstrap node's cache! self.ring = [] ''' Add this node to the ring, only the bootstrap node can add a node to the ring after checking his wallet and ip:port address bottstrap node informs all other nodes and gives the request node an id and 100 NBCs ''' def register_node_to_ring(self, node): # node is {address: , public_key} self.ring.append({ 'address': node['address'], 'public_key': node['public_key'], 'id': node['id'] }) ''' The Bootstrapper Node will POST every other node and inform it about the final ring with all the connected nodes ''' def broadcast_ring(self): for node in self.ring: if node['address'] != self.address: addr = 'http://' + node['address'] + '/connect' requests.post(addr, json=self.ring)
from utility.verification import Verification first_time = False try: with open("node.txt", mode="r") as f: content = f.readlines() NODE_ID = content[0][:-1] status = content[1] except (IOError, IndexError): print("Failed to load data from file...") sys.exit() wallet = Wallet(NODE_ID) if status == "no_wallet": wallet.create_keys() wallet.save_keys() try: with open("node.txt", mode="w") as f: f.write(NODE_ID) f.write("\n") f.write("has_wallet") except (IndexError, IOError): print("Failed to save data in node.txt...") first_time = True else: wallet.load_keys() blockchain = Blockchain(wallet.node_id, wallet.public_key) winner_chain = blockchain.get_chain()
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, port): # self.node_id = str(uuid4()) # self.node_id = 'Wez' # simple name self.port = port self.wallet = Wallet(port) self.blockchain = self.init_wallet() # self.blockchain = None def init_wallet(self): self.wallet.create_keys() return Blockchain(self.wallet.public_key, self.port) def get_transaction_value(self): """ Returns tuple of transactions details. tx_recipient and tx_amount""" tx_recipient = input("Enter the recipients name: ") tx_amount = float(input("Enter the transaction amount to be sent: ")) # Could also use a KV pair: dictionary or class return (tx_recipient, tx_amount) def get_user_choice(self): """ Returns the input of the user (input selection). """ return input("Your choice: ") def print_blockchain_elements(self): # Output blockchain list to console temp_chain = self.blockchain.chain for index in range(len(temp_chain)): print('Outputting Block: {}'.format(index)) print(temp_chain[index]) # refactor this , using the accessor is not efficient else: print('-' * 20) 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")
class Node: def __init__(self): self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): tx_recipient = input("Enter the recipient of the transaction: ") tx_amount = float(input("Please enter a transaction amount: ")) return (tx_recipient, tx_amount) def get_user_choice(self): return input("Your choice: ") def print_blockchain_elements(self): print("-" * 20) for block in self.blockchain.chain: print(block) else: print("-" * 20) 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")
class 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): tx_recipient = input('Enter the recipient\'s name: ') tx_amount = float(input('Your transaction amount please: ')) return tx_recipient, tx_amount def get_user_choice(self): user_input = input('Your choice: ') return user_input def print_blockchain_elements(self): for block in self.blockchain.chain: print('Outputting block') print(block) else: print('-' * 20) 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')
class Node: def __init__(self): # self.wallet = None self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): tx_recipient = input('\nEnter transaction recipient: ') tx_amount = float(input('\nEnter transaction amount: ')) return (tx_recipient, tx_amount) def get_user_choice(self): return input('Your choice: ') def print_blockchain_elements(self): for block in self.blockchain.chain: print(block) else: print('-' * 20) 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)
class Node: def __init__(self): #self.wallet.public_key = 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 """ 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): return input("Your choice:") def print_blockchainelements(self): #Output the blockchain list to the console for block in self.blockchain.chain: print(block) else: print('-' * 20) 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")
class Node: # ---------------------------------------------------- def __init__(self): # self.id = str(uuid4()) # self.id = 'Javier' self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) # self.blockchain = None # Functions to get the inputs that the user write def get_transaction_value(self): tx_recipient = input('Enter the recipient of the transaction: ') tx_amount = float(input('Enter the amount of the transaction: ')) # return a tuple return (tx_recipient, tx_amount) def get_user_choice(self): user_input = input('Your choice: ') return user_input # ---------------------------------------------------- # ---------------------------------------------------- # Print blocks of the blockchain def print_blockchain_blocks(self): for block in self.blockchain.get_blockchain(): print('Outputting block') print(block) else: print('-' * 20) # Input main class 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()))
class Node: def __init__(self): self.add_new = True self.open_transactions = [] self.wallet = Wallet() self.blockchain = None def get_user_choice(self): # prints option the user an enter, and returns the users choice print("1: new transaction") print("2: mine new block") print("3: end program") print("4: show balance") print("5: show blockchain") print("6: create wallet") print("7: login") user_response = input("What would you like to do? ") return user_response 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 get_transaction(self): # gets user input for a new transaction returns a tuple for the new transaction recipient = input("please provide recipient's name ") amount = float(input("please provide the amount ")) return (recipient, amount)
class Node: def __init__(self): #self.wallet.public_key = str(uuid4()) self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): """ Returns the transaction inputs of the users """ tx_recipient = input('Enter the recipient of the transaction : ') tx_amount = float(input('Input your transaction amount : ')) return tx_recipient, tx_amount def get_user_choice(self): return input("Enter your option : ") def print_blockchain_elements(self): for block in self.blockchain.get_chain(): print("Outputing blocks !") print(block) def listen_for_input(self): waiting_for_input = True while waiting_for_input: print("Select your option") print("1: Add user transaction amount") print("2: Mine Block") print("3: Output Blockchain") print("4: Check transaction validity") print("5: Create Keys") print("6: Load Keys") 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 new transaction to 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") 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("Pleasse enter the provided options") if not Verification.verify_chain(self.blockchain.get_chain()): self.print_blockchain_elements() print("Invalid Blockchain elements!") break print("Balance of {} : {:5.2f}".format( self.wallet.public_key, self.blockchain.get_balance())) else: print("User Left!") print("Done!")
class 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): tx_recipient = input('Enter the recipient of the transaction: ') tx_amount = float(input('Your transaction amount please: ')) # this is a tuple return tx_recipient, tx_amount def get_user_choice(self): user_input = input('Your choice: ') return user_input def print_blockchain_elements(self): # Output the blockchain list to the console with for loop for block in self.blockchain.chain: print('Outputting block') print(block) else: print('-' * 20) def listen_for_input(self): waiting_for_input = True # loop while while waiting_for_input: print('Please choose: ') print('1: Add a new transaction value') print('2: Mine a new block') print('3: Output blockchain') # print('4: Output participants') # print('5: Manipulate the chain') print('4: Create wallet') print('5: Load wallet') print('6: Check transaction validity') print('7: Save keys') print('q: Quit') user_choice = self.get_user_choice() if user_choice == '1': tx_data = self.get_transaction_value() # tuple unpacking since tx_data holds 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!') print(self.blockchain.get_open_transactions()) elif user_choice == '2': if not self.blockchain.mine_block(): print('Mining failed. Try creating a wallet') elif user_choice == '3': self.print_blockchain_elements() # elif user_choice == 4: # print(participants) # elif user_choice == 5: # if len(blockchain) >= 1: # blockchain[0] = { # 'previous_hash': '', # 'index': 0, # 'transactions': [{'sender': 'Ole', 'recipient': 5, 'amount': 100}] # } elif user_choice == '4': self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '5': self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif user_choice == '6': 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 == '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!') # check in any case if blockchain is valid or not if not Verification.verify_chain(self.blockchain.chain): print("Invalid blockchain") break # output formatted balance of owner with 6 digits and 2 decimal places print('Balance of {}: {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) else: print('User left!') print('Done!')
class Node: def __init__(self): #self.wallet.public_key = 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.""" 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): """ Gets user choice and returns it. """ user_input = input('Your choice: ') return user_input def print_blockchain_elements(self): """ Outputs all blocks of the blockchain. """ for block in self.blockchain.get_chain(): print('Outputting Block') print(block) else: print('_' * 20) 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 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': if Verification.verity_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.get_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('\nDone!')
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 = "Hari" 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: ")) return (tx_recipient, tx_amount) def get_user_choice(self): """Prompts the user for its choice and return it.""" user_choice = input("Your choice: ") return user_choice # This will output the blockchain blocks in loop def print_blockchain_blocks(self): """ Output all blocks of the blockchain. """ # Output the blockchain list to the console for block in self.blockchain.get_chain(): print(block) else: print("-" * 30) 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!")
class Node: def __init__(self): self.wallet = Wallet() # self.blockchain = None self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): """ Get the value entered by the user as a Float """ 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): """ Get the user choice """ return input("Your choice: ") def print_blockchain_elements(self): for block in self.blockchain.chain: print(block) else: print("-" * 20) def listem_for_input(self): waiting_for_input = True while waiting_for_input: print("\nPlease choose one option") 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") print("h: Manipulate the blockchain") print("v: Verify chain") 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 succeded") else: print("Transcation 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 valid") else: print("Not all valid") 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, please pick a valid option") if not Verification.verify_chain(self.blockchain.chain): self.print_blockchain_elements() print("Inválid blockchain") break print("Balance of {}: {:6.2f}".format( self.wallet.public_key, self.blockchain.get_balance())) else: print("User left") print("Done")
from transaction import Transaction from block import Block from wallet import Wallet import json import paho.mqtt.client as mqtt from utility.constants import SUN, MQTT import ssl wallet1 = Wallet("Nacho") wallet2 = Wallet("Mada") wallet3 = Wallet("Mihai") wallet1.create_keys() wallet2.create_keys() wallet3.create_keys() client = mqtt.Client() client.tls_set_context(ssl.create_default_context()) client.username_pw_set(MQTT.USERNAME, MQTT.PASSWORD) # client.will_set('remove_peer_node', payload=SUN.ID, qos=2) client.on_connect = on_connect #client.on_message = on_message #client.on_disconnect = on_disconnect client.connect(MQTT.SERVER, int(MQTT.SSL_PORT)) # client.loop_forever() def on_connect(client, userdata, flags, rc): client.publish("new_transaction", )
class Node : """Each device on the blockchain network. It will have its own copy of the blockchain.""" def __init__(self): #self.id=str(uuid4()) self.wallet=Wallet() self.blockchain=None self.wallet.create_keys() #Blockchain is initialised in our code only when user has a wallet self.blockchain=Blockchain(self.wallet.public_key) def get_transaction_data(self) : """Gets the user input and return it in a tuple""" tx_recipient=input("Enter the receiver : ") tx_amount=float(input("Enter the number of coins : ")) return (tx_recipient,tx_amount) def listen_for_input(self): waiting_for_input=True while waiting_for_input : print('Enter your choice :') print('1.Make a new transaction') print('2. Mine a new block ') print("3.Print all the blocks") print('4.Create wallet') print('5.Load wallet') print('6.Save keys') print("7.Quit") user_choice=input() if user_choice=='1' : #Take the user input and store it in a tuple tx_data=self.get_transaction_data() #unpack the tuple here and pass the arguments recipient,amount=tx_data signature=self.wallet.sign_transaction(self.wallet.public_key,recipient,amount) #We need a named argument since sender is the second field in the function definition if self.blockchain.add_transaction(recipient,self.wallet.public_key,signature,amount=amount) : print("Transaction added successfully") else : print("Insufficient Balance of sender") elif user_choice=='2' : #Will fail if user does not have a wallet(public key) if not self.blockchain.mine_block(): print('Mining failed ! Got not wallet ? ') print('Or, One or multiple transaction were modified !') elif user_choice=='3': print(self.blockchain.get_chain()) elif user_choice=='4': self.wallet.create_keys() #Blockchain is initialised in our code only when user has a wallet self.blockchain=Blockchain(self.wallet.public_key) elif user_choice=='5': self.wallet.load_keys() self.blockchain=Blockchain(self.wallet.public_key) elif user_choice=='6': self.wallet.save_keys() elif user_choice=='7' : waiting_for_input=False else : print("Invalid input,please input a valid value from the choices !") """Check the validity of blockchain after each user operation""" if not Verification.verify_chain(self.blockchain.get_chain()): print('Invalid blockchain!') # Break out of the loop break print('Balance of {} is {:6.2f}'.format(self.wallet.public_key,self.blockchain.get_balance()))
class Node: def __init__(self): self.wallet = Wallet() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): recipient = input('Enter the recipient of the transaction: ') amount = float(input('Your transaction amount please: ')) return recipient, amount def get_user_choice(self): user_input = input('Your choice: ') return user_input def print_blockchain_elements(self): for block in self.blockchain.chain: print('Outputting block: {}'.format(block)) else: print('-' * 20) 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
class Node: def __init__(self): self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): tx_recipient = input('Enter sender of txn:') tx_amount = float(input('Enter txn amount')) return (tx_recipient, tx_amount) def get_user_choice(self): user_input = input("User choice: ") def print_blockchain_history(self): for block in self.blockchain.get_chain: print("Outputting Block") print(block) 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
class Node: def __init__(self): # self.wallet.public_key = st self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): 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): user_input = input('Your Choice :') return user_input def print_blockchain_elements(self): for block in self.blockchain.chain: print('Outputting The Block') print(block) 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 !')
class Node: def __init__(self): #self.wallet.public_key = str(uuid4()) self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) # to fetch the user values def get_transaction_value(self): """ takes new transaction input from the user as a float""" tx_receipient = input('enter the recipient of the transaction') tx_amount = float(input('enter the amount you want to send')) return tx_receipient, tx_amount # to take what user wants to do def get_choice(self): choice = input('enter your choice') return choice #print blocks:) def print_blocks(self): for block in self.blockchain.chain: print('Outputting Block') print(block) else: print('-' * 20) 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")
class Node: # Konstruktor -------------------------------------------------------------- def __init__(self): # self.wallet = str(uuid4()) self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) # Funktionen -------------------------------------------------------------- def get_transaction_value(self): 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): user_input = input('Your choice: ') return user_input def print_blockchain_elements(self): for block in self.blockchain.chain: print('Outputting Block') print(block) else: print('-' * 20) 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!') 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!')
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!")
class Node: def __init__(self): self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): """ Returns the user input (the recipient and transaction amount) as a tuple. """ 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): user_input = input('Your choice: ') return user_input def print_options(self): 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') print('q: Quit') def print_blockchain_elements(self): #Output the blockchain list to the console #for index, block in enumerate(blockchain): for index in range(len(self.blockchain.get_chain())): print(f'Outputting block #{index}') #print(block) print(self.blockchain.get_chain()[index]) else: print('-' * 20) 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!')
class Node: def __init__(self): self.wallet = Wallet() self.blockchain = None self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_user_choice(self): user_choice = input('Your choice: ') return user_choice def get_transaction_value(self): recipient = input('Enter recipient name: ') amount = float(input('Enter transaction\'s value: ')) return recipient, amount def listen_for_input(self): while True: print('Please choose') print('1: Add new transaction') print('2: Mine block') print('3: Output the blockchain blocks ') print('4: Output the particepants blocks ') print('5: create wallet') print('6: load wallet') print('7: save wallet') print('8: get balance') print('v: verify the blockchain') print('q: Quite the APP') user_choice = self.get_user_choice() if user_choice == '1': recipient, amount = self.get_transaction_value() signtaure = self.wallet.sign_transaction( self.wallet.public_key, recipient, signtaure, amount) self.blockchain.add_transaction(recipient, self.wallet.public_key, signtaure, amount) elif user_choice == '2': self.blockchain.mine_block() elif user_choice == '3': print(self.blockchain.chain) elif user_choice == '4': print(self.blockchain.participants) 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 == '8': balance = self.blockchain.get_balance(self.wallet.public_key) print(str(balance) + ' ELIXIR') elif user_choice == 'v': if not Verification.verify_chain(self.blockchain.chain): print(self.blockchain.chain) print('invalid blockchain') break else: print('valid blockchain') elif user_choice == 'q': break else: print('Enter a valid option, please!')
class Node: def __init__(self): 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('-' * 40) def listen_for_input(self): quit_app = False while not quit_app: print('\nPlease choose:') print('1: Add a new transaction value') print('2: Mine a new block') print('3: Output Blockchain') print('4: Output Balance') print('5: Create Wallet') print('6: Load Wallet') print('7: Save Keys') print('Q: Exit application') print('\n') user_choice = self.get_user_choice() print('\n') if user_choice == '1': tx_data = self.get_transaction_value() # Unpacks the data in 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('\n') print('*' * 40) print('Transaction Added!') else: print('\n') print('*' * 40) print('Transaction Failed!') print('Insufficient Funds!') print(self.blockchain.get_open_transactions()) elif user_choice == '2': if not self.blockchain.mine_block(): print('Mining Failed! Got a wallet?') elif user_choice == '3': self.print_block_elements() elif user_choice == '4': print('*' * 40) print('{}\'s Current Balance: {:6.2f} \n'.format( self.wallet.public_key, 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 == 'Q' or user_choice == 'q': print('Quitting Application \n') quit_app = True else: print('Invalid Choice') if user_choice != 'Q' and user_choice != 'q' and user_choice != '4': print('\n') print('*' * 40) print('{} Current Balance: {:6.2f}'.format( self.wallet.public_key, self.blockchain.get_balance())) print('*' * 40) print('\n') if not Verification.verify_chain(self.blockchain.chain): self.print_block_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!')
class Node: def __init__(self): 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. """ 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): """ Returns the input from the user. """ return input('Your choice: ') def print_blockchain_elements(self): """ Print all the blockchain elements. """ print('-' * 20 + ' Printing elements ' + '-' * 20) for block in self.blockchain.get_chain(): print('Outputting Block') print(block) else: print('-' * 59) 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!')
class Node: def __init__(self): # self.wallet.public_key = str(uuid4()) self.wallet = Wallet() self.blockchain = Blockchain('') 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 get_transaction_value(self): """ Returns hte input of the user, a new transaction (recipient, amount) """ 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. """ for block in self.blockchain.chain: print('Outputting block') print(block)
class 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 float tx_recipient = input('Enter the sender of transaction: ') tx_amount = float(input('Your transaction amount: ')) return tx_recipient, tx_amount #create tuple def get_user_choice(self): """ Promps 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 to console for block in self.blockchain.chain: print('Outputting block') print(block) else: print('-' * 20) 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: Saving 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 transaction amount to 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_transaction()) 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_transaction(), 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': # This will lead to the loop to exit waiting_for_input = False else: print('Input was invalid, please pick 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!')
class Node: def __init__(self): # self.wallet.public_key = str(uuid4()) self.wallet = Wallet() self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) def get_transaction_value(self): """ Ask's the user about a transaction's data. :return: Float formatted user input. """ tx_recipient = input('Please, enter the recipient of the transaction: ') tx_amount = float(input('Please, enter the amount of the transaction: ')) return tx_recipient, tx_amount def get_user_choice(self): user_input = input('Your choice: ') return user_input def print_blockchain_elements(self): for block in self.blockchain.chain: print('Outputting block: ') print(block) else: print('-' * 20) 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') 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=tx_data[1]): print('Added transaction') else: print('Transaction rejected') elif user_choice == '2': if not self.blockchain.mine_block(): print('Mining failed!, check that you 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 == 'q': waiting_for_input = False else: print('Invalid input, please select a value from the list') if not Verification.verify_chain(self.blockchain.chain): print('Invalid blockchain') break print("Balance for {} {:6.2f}".format(self.wallet.public_key, self.blockchain.get_balance())) else: print('User left!') print('Done!')