-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
129 lines (117 loc) · 4.44 KB
/
node.py
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
from block import Block
from blockchain import Blockchain
from uuid import uuid4
from wallet import Wallet
class Node:
def __init__(self):
self.node_id = str(uuid4())
self.wallet = Wallet()
self.chain = Blockchain(self.wallet.public_key)
self.listen_for_input()
def listen_for_input(self):
"""
Method to listen to user inputs upon starting the program
"""
if not self.chain.verify_chain():
return
waiting_for_input = True
print("Please choose")
print("1: Add a new transaction value")
print("2: Mine blocks")
print("3: Output the blockchain blocks")
print("4: Find balance of partipant")
print("5: Create new wallet")
print("6: Load existing keys")
print("7: Save keys")
print("q: Quit")
while waiting_for_input:
print("*" * 40)
user_choice = self.get_user_choice()
if user_choice == "1":
transaction_details = self.get_transaction_details()
if self.wallet.public_key == None:
print("💸❌ Adding transaction failed, Please connect wallet first!")
continue
sig = self.wallet.sign_transaction(
sender=self.wallet.public_key,
recipient=transaction_details[0],
amount=transaction_details[1],
)
print("💸✍🏽 Transaction signature: {}".format(sig))
if not self.chain.add_transaction(
sender=self.wallet.public_key,
recipient=transaction_details[0],
amount=transaction_details[1],
sig=sig,
):
print("💸❌ Adding transaction failed!")
elif user_choice == "2":
if not self.chain.mine_block():
print("⛏❌ Mining failed!")
elif user_choice == "3":
self.print_blockchain_elements()
elif user_choice == "4":
participant = self.get_participant()
print(
"Balance of {} is: {:6.3f}".format(
participant, self.chain.get_balance(participant)
)
)
elif user_choice == "5":
# Create new wallet, create keys
self.wallet.create_keys()
self.chain = Blockchain(self.wallet.public_key)
print(
"💰 Wallet with public key {} created!".format(
self.wallet.public_key
)
)
elif user_choice == "6":
# Load keys from wallet.txt
self.wallet.load_keys()
print(
"💰 Wallet with public key {} loaded!".format(self.wallet.public_key)
)
self.chain = Blockchain(self.wallet.public_key)
elif user_choice == "7":
self.wallet.save_keys()
print(
"💰 Wallet with public key {} saved to wallet.txt".format(
self.wallet.public_key
)
)
elif user_choice == "q":
waiting_for_input = False
else:
print("Input was invalid, please pick a value from the list!")
if not self.chain.verify_chain():
waiting_for_input = False
print("Done!")
def get_user_choice(self):
"""
Utility function to get user input
"""
user_input = input("Your choice: ")
return user_input
def get_participant(self):
"""
Utility function to get participant's name
"""
user_input = input("Enter name of participant: ")
return user_input
def print_blockchain_elements(self):
"""
Outputting all blocks in the blockchain
"""
for block in self.chain.get_blockchain():
print(block)
def get_transaction_details(self):
"""
Gets user input for sender, recipient and amount for a transaction
"""
recipient = input("Enter recipient address: ")
amount = float(input("Enter transaction amount: "))
return (recipient, amount)
if __name__ == "__main__":
# Initialisation of node upon script execution
node = Node()