- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_cli.py
More file actions
Latest commit
131 lines (105 loc) · 3.98 KB
/
Copy pathnode_cli.py
File metadata and controls
131 lines (105 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# from uuid import uuid4
fromblockchainimportBlockchain
fromutility.verificationimportVerification
fromwalletimportWallet
classNode:
def__init__(self):
# self.wallet = str(uuid4())
self.wallet=Wallet()
self.blockchain=None
defget_transaction_details(self):
"""Inputs the transaction details (recipient & amount) from the user and returns it"""
tx_recipient=input('Enter the recipient of the transaction: ')
tx_amount=float(input('Enter the transaction amount: '))
return (tx_recipient, tx_amount)
defprint_blockchain_elements(self):
"""Print all the blocks of the blockchain"""
forblockinself.blockchain.get_chain():
print("Block: ", block)
defget_user_choice(self):
"""Inputs a choice from the user"""
wallet_loaded=self.wallet.public_key!=Noneandself.blockchain!=None
print("\n\n")
print("="*40)
ifwallet_loaded:
print("WALLET PUBLIC KEY: ", self.wallet.public_key)
print("BALANCE: {:.2f}".format(self.blockchain.get_balance()))
else:
print("[No Wallet Loaded]")
print("-"*40)
print(" SELECT AN OPTION")
print("-"*20)
ifwallet_loaded:
print("1. Add New Transaction")
print("2. Mine Blockchain")
print("3. View Blockchain")
print("4. Verify Open Transactions")
print("5. Create Wallet")
print("6. Load Wallet")
ifwallet_loaded: print("7. Save Wallet")
print("q. Quit")
returninput('Enter your choice: ')
defcommand_line_interface(self):
"""Take user input on a command line in a loop until the user quits"""
take_user_input=True
whiletake_user_input:
user_choice=self.get_user_choice()
print()
ifuser_choice=='1':
# Add transaction
recipient, amount=self.get_transaction_details()
signature=self.wallet.sign_transactions(sender=self.wallet.public_key, recipient=recipient, amount=amount)
ifself.blockchain.add_transaction(sender=self.wallet.public_key, recipient=recipient, amount=amount, signature=signature):
print("Transaction Added.")
else:
print("Transaction Failed!")
print("\nOpen Transactions: ", self.blockchain.get_open_transactions())
elifuser_choice=='2':
# Mine block
print("\nMining started...")
block=self.blockchain.mine_block()
ifblock!=None:
print("Mining done! Proof={}, Balance={:.2f}".format(block.proof, self.blockchain.get_balance()))
print("Added Block: ", block)
else:
print("Mining failed!")
elifuser_choice=='3':
# Print blockchain
self.print_blockchain_elements()
elifuser_choice=='4':
# Verify open transactions
ifVerification.verify_open_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance):
print("\nAll open transactions are valid.")
else:
print("\nSome open transactions are invalid!")
print("Open Transactions: ", self.blockchain.get_open_transactions())
elifuser_choice=='5':
# Create wallet
self.wallet.create_keys()
self.blockchain=Blockchain(self.wallet.public_key)
print("\nNew Wallet Created. Public key:", self.wallet.public_key)
elifuser_choice=='6':
# Load wallet
ifself.wallet.load_keys() ==True:
self.blockchain=Blockchain(self.wallet.public_key)
print("\nWallet Loaded from save-file. Public key:", self.wallet.public_key)
else:
print("\nFailed to load Wallet from save-file!")
elifuser_choice=='7':
# Save wallet
ifself.wallet.save_keys() ==True:
print("\nWallet saved to file.")
else:
print("\nFailed to save Wallet to file!")
elifuser_choice=='q'oruser_choice=='0':
take_user_input=False
else:
print('Invalid choice!')
ifself.blockchainandnotVerification.verify_chain(self.blockchain.get_chain(), self.blockchain.mining_difficulty):
print('Invalid Blockchain!!')
take_user_input=False
# Start UI on a command-line-interface only if this file was executed directly
if__name__=='__main__':
node=Node()
node.command_line_interface()