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')
Example #2
0
    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()))
Example #3
0
    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!')
Example #4
0
 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!")
Example #5
0
    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!')
Example #6
0
    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):
        """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!')
Example #8
0
    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')