当前位置: 首页>>代码示例>>Python>>正文


Python Wallet.load_keys方法代码示例

本文整理汇总了Python中wallet.Wallet.load_keys方法的典型用法代码示例。如果您正苦于以下问题:Python Wallet.load_keys方法的具体用法?Python Wallet.load_keys怎么用?Python Wallet.load_keys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在wallet.Wallet的用法示例。


在下文中一共展示了Wallet.load_keys方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from wallet import Wallet [as 别名]
# 或者: from wallet.Wallet import load_keys [as 别名]
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!")
开发者ID:HariKumarValluru,项目名称:Blockchain,代码行数:98,代码来源:OLD_node.py


注:本文中的wallet.Wallet.load_keys方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。