- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode.py
More file actions
Latest commit
315 lines (282 loc) · 9.26 KB
/
Copy pathnode.py
File metadata and controls
315 lines (282 loc) · 9.26 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
fromflaskimportFlask, jsonify, request, send_from_directory
fromwalletimportWallet
fromflask_corsimportCORS
fromblockchainimportBlockchain
importast
app=Flask(__name__)
CORS(app)
@app.route('/', methods= ['GET'])
defget_node_ui():
returnsend_from_directory('ui', 'node.html')
@app.route('/network', methods= ['GET'])
defget_network_ui():
returnsend_from_directory('ui', 'network.html')
@app.route('/wallets', methods= ['GET'])
defwallets():
returnsend_from_directory('ui', 'wallets.html')
@app.route('/wallet', methods=['POST'])
defcreate_keys():
wallet.create_keys()
ifwallet.save_keys():
globalblockchain
blockchain=Blockchain(wallet.public_key, port)
response= {
'public_key': wallet.public_key,
'private_key': wallet.private_key,
'funds': blockchain.get_balance()
}
returnjsonify(response), 201
else:
response= {
'message': 'Saving keys failed'
}
returnjsonify(response), 500
@app.route('/wallet', methods=['GET'])
defload_keys():
ifwallet.load_keys():
globalblockchain
blockchain=Blockchain(wallet.public_key, port)
response= {
'public_key': wallet.public_key,
'private_key': wallet.private_key,
'funds': blockchain.get_balance()
}
returnjsonify(response), 201
else:
response= {
'message': "No wallet to load"
}
returnjsonify(response), 500
@app.route('/show-wallets', methods=["GET"])
defshow_wallets():
wallets=wallet.show_keys()
print("WALLETS", wallets)
public_keys=""
forlineinwallets:
print("line",line)
print('\n')
keys=ast.literal_eval(line)
public_keys=keys['public']
print("key",public_keys)
print('\n')
response= {
'wallets': wallets,
'message': 'Done'
}
returnjsonify(response), 200
@app.route('/balance', methods= ['Get'])
defget_balance():
balance=blockchain.get_balance()
ifbalance!=None:
response= {
'message': 'Balance fetched',
'funds': balance
}
returnjsonify(response), 201
else:
response= {
'message': 'Adding a block failed',
'wallet_set_up': wallet.public_key!=None
}
returnjsonify(response), 500
@app.route('/broadcast-transaction', methods=['POST'])
defbroadcast_transaction():
values=request.get_json()
ifnotvalues:
response= {
'message': 'No data found'
}
returnjsonify(response), 400
required= ['sender', 'recipient', 'amount', 'signature']
ifnotall(keyinvaluesforkeyinrequired):
response= {
'message': 'Some data is missing.'
}
returnjsonify(response), 400
success=blockchain.add_transaction(values['recipient'], values['sender'], values['signature'], values['amount'], is_receiving=True)
ifsuccess:
response= {
'message': 'Successfully added transaction.',
'transaction': {
'sender': values['sender'],
'recipient': values['recipient'],
'amount': values['amount'],
'signature': values['signature']
}
}
returnjsonify(response), 201
else:
response= {
'message': 'Failed in creating a transaction'
}
returnjsonify(response), 500
@app.route('/broadcast-block', methods=['POST'])
defbroadcast_block():
values=request.get_json()
ifnotvalues:
response= {
'message': 'No data found.'
}
returnjsonify(response), 400
if'block'notinvalues:
response= {
'message': "Some data is missing."
}
returnjsonify(response), 400
block=values['block']
ifblock['index'] ==blockchain.chain[-1].index+1:
ifblockchain.add_block(block):
response= {'message': 'Block Added'}
returnjsonify(response), 201
else:
response= {'message': 'Block seems invalid.'}
returnjsonify(response), 409
elifblock['index'] >blockchain.chain[-1].index:
response= {
'message': 'Blockchain seems to differ from local blockchain.'
}
blockchain.resolve_conflicts=True
returnjsonify(response), 200
else:
response= {
'message': 'Blockchain seems to be shorter, block not added.'
}
returnjsonify(response), 409
@app.route('/transaction', methods= ['POST'])
defadd_transaction():
ifwallet.public_key==None:
response= {
'message': 'No wallet set up.'
}
returnjsonify(response), 400
values=request.get_json()
ifnotvalues:
response= {
'message': 'No data found'
}
returnjsonify(response), 400
required_fields= ['recipient', 'amount']
ifnotall(fieldinvaluesforfieldinrequired_fields):
response= {
'message': 'Required data is missing'
}
returnjsonify(response), 400
signature=wallet.sign_transaction(wallet.public_key, values['recipient'], values['amount'])
success=blockchain.add_transaction(values['recipient'], wallet.public_key, signature, values['amount'])
ifsuccess:
response= {
'message': 'Successfully added transaction.',
'transaction': {
'sender': wallet.public_key,
'recipient': values['recipient'],
'amount': values['amount'],
'signature': signature
},
'funds': blockchain.get_balance()
}
returnjsonify(response)
else:
response= {
'message': 'Failed in creating a transaction'
}
returnjsonify(response), 500
@app.route('/mine', methods= ['POST'])
defmine():
ifblockchain.resolve_conflicts==True:
response= {'message': 'Resolve conflicts first, block not added!'}
returnjsonify(response), 409
block=blockchain.mine_block()
ifblock!=None:
dict_block=block.__dict__.copy()
dict_block['transactions'] = [tx.__dict__fortxindict_block['transactions']]
response= {
'message': 'Block added successfully',
'block': dict_block,
'funds': blockchain.get_balance()
}
returnjsonify(response), 201
else:
response= {
'message': 'Adding a block failed',
'wallet_set_up': wallet.public_key!=None
}
returnjsonify(response), 500
@app.route('/resolve-conflicts', methods=['POST'])
defresolve_conflicts():
replaced=blockchain.resolve()
ifreplaced:
response= {'message': 'Chain was replaced!'}
else:
response= {'message': 'Local chain kept!'}
returnjsonify(response), 200
@app.route('/transactions', methods= ['GET'])
defget_open_transactions():
trasactions=blockchain.get_open_transactions()
dict_transactions= [tx.__dict__fortxintrasactions]
response= {
'message': 'Feteched open transactions',
'transactions': dict_transactions
}
returnjsonify(response), 200
@app.route('/chain', methods=['GET'])
defget_chain():
chain_snapshot=blockchain.chain
dict_chain= [block.__dict__.copy() forblockinchain_snapshot]
fordict_blockindict_chain:
dict_block['transactions'] = [tx.__dict__fortxindict_block['transactions']]
returnjsonify(dict_chain), 200
@app.route('/node', methods= ['POST'])
defadd_node():
values=request.get_json()
ifnotvalues:
response= {
'message': 'No data attached.'
}
returnjsonify(response), 400
if'node'notinvalues:
response= {
'message': 'No node data found.'
}
returnjsonify(response), 400
node=values.get('node')
blockchain.add_peer_node(node)
response= {
'message': 'Node added successfully',
'all_nodes': blockchain.get_peer_nodes()
}
returnjsonify(response), 201
@app.route('/node/<node_url>', methods= ['DELETE'])
defremove_node(node_url):
ifnode_url==''ornode_url==None:
response= {
'message': "No data attached"
}
returnjsonify(response), 400
ifnode_urlnotinblockchain.get_peer_nodes():
response= {
'message': 'Node not present'
}
returnjsonify(response), 400
blockchain.remove_peer_node(node_url)
response= {
'message': 'Node removed',
'all_nodes': blockchain.get_peer_nodes()
}
returnjsonify(response), 201
@app.route('/nodes', methods= ["GET"])
defget_nodes():
nodes=blockchain.get_peer_nodes()
response= {
'message': 'All peer nodes',
'all_nodes': nodes
}
returnjsonify(response), 201
if__name__=='__main__':
fromargparseimportArgumentParser
parser=ArgumentParser()
parser.add_argument('-p', '--port', type=int, default=5000)
args=parser.parse_args()
port=args.port
wallet=Wallet(port)
blockchain=Blockchain(wallet.public_key, port)
app.run(host='0.0.0.0', port=port)