-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
409 lines (300 loc) · 10.9 KB
/
main.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from block import BlockChain
from models import Property, User
from models import Transaction
import default
import random
from hmac_ import *
import json
# initializing the blockchain
blockchain = BlockChain()
def verify_user(username, password):
# ALICE
random_challenge = generate_challenge()
#BOB
bit = random.randint(0, 1)
#ALICE
response = create_response(random_challenge, bit, password)
for user in User.users:
if user.username == username:
curr_user = user
break
#BOB CHECKS
expected_response = create_response(random_challenge, bit , curr_user.password)
return verify_response(expected_response, response)
# return False
def register():
print('------------------------------------------------------------------ \n')
print('Enter a username and password:')
username = str(input("Username: "))
# check if the username already exists
for user in User.users:
if user.username == username:
print("Username already exists, choose another one \n")
register()
return
password = str(input("Password : "))
if (username != '' and password != ''):
User(username, password)
print(f'\n {username} registered successfully!')
return
else:
print("\n Username and/or password can't be empty, try again \n")
register()
def viewWealth():
print('------------------------------------------------------------------ \n')
print('Enter the username of the user whose wealth you want to see: ')
username = str(input())
user_found = False
for user in User.users:
if user.username == username:
user_found = True
curr_user = user
if not user_found:
print("The specified user does not exist \n")
return
print('Enter your password : ')
pass1 = str(input())
if(verify_user(username, pass1) == False):
print("\n You have input the incorrect pin!")
return
print(f'\n {username} has {curr_user.wealth} coins')
def verify_transaction(transaction , current_owner, curr_user, property_chosen):
random_challenge = generate_challenge()
bit = random.randint(0, 1)
secret = json.dumps(transaction.to_string())
response = create_response(random_challenge, bit, secret)
transaction_expected = Transaction(
current_owner, curr_user, property_chosen)
secret_expected = json.dumps(transaction_expected.to_string())
expected_response = create_response(random_challenge, bit , secret_expected)
return verify_response(expected_response, response)
def buy():
print('------------------------------------------------------------------ \n')
print('Enter your username: ')
username = str(input())
user_found = False
for user in User.users:
if user.username == username:
user_found = True
curr_user = user
if not user_found:
print("\n User does not exist, create a user")
return
print('Enter your password : ')
pass1 = str(input())
if(verify_user(username, pass1) == False):
print("\n You have input the incorrect pin!")
return
print('\n Enter the ID of the property you want to buy(-1 to go back): ')
propertyId = int(input())
if(propertyId == -1):
return
if(propertyId >= len(Property.properties)):
print('This Property Does not exist, see below the valid properties \n')
Property.viewProperties(Property)
buy()
return
property_chosen = Property.properties[propertyId]
if property_chosen.owner.username == username:
print("You already own this property")
return
if (curr_user.wealth < property_chosen.amount):
print("Insufficient funds")
return
if property_chosen.owner is not None:
current_owner = property_chosen.owner
current_owner.remove_property(property_chosen)
else:
current_owner = None
transaction = Transaction(
current_owner, curr_user, property_chosen)
if(verify_transaction(transaction, current_owner, curr_user, property_chosen) == False):
current_owner.add_property(property_chosen)
return
else:
print("Transaction Verified, Adding to mine pool")
property_chosen.transaction_history.append(transaction)
prev_block = blockchain.last_block()
block = blockchain.new_block(prev_block['hash'], transaction)
# blockchain.mine_block_POW(block)
curr_user.add_property(property_chosen)
print(
f"\n {curr_user.username} bought {property_chosen.name} successfully! \n {blockchain.last_block()['merkle_root']}")
def transaction_history():
print('------------------------------------------------------------------ \n')
print("Enter the ID of the property whose transaction history you want to see: ")
propertyId = int(input())
if propertyId >= len(Property.properties):
print('Invalid selection, choose again \n')
transaction_history()
property_chosen = Property.properties[propertyId]
for transaction in property_chosen.transaction_history:
print(f'''
Buyer: {transaction.buyer.username},
Seller: {transaction.seller.username}
Timestamp: {transaction.timestamp},
TxnID: {transaction.transactionId}
\n''')
def show_assets():
print('------------------------------------------------------------------ \n')
print('Enter your username: ')
username = str(input())
user_found = False
for user in User.users:
if user.username == username:
user_found = True
curr_user = user
if not user_found:
print("The specified user does not exist \n")
return
else:
print(f'\n -> Properties owned by {username} are:')
curr_user.get_assets()
def addMoney():
print('------------------------------------------------------------------ \n')
print('Enter your username: ')
username = str(input())
user_found = False
for user in User.users:
if user.username == username:
user_found = True
curr_user = user
if not user_found:
print("The specified user does not exist \n")
return
print('Enter your password : ')
pass1 = (input())
if(verify_user(username, pass1) == False):
print("\n You have input the incorrect pin!")
return
print('Enter the amount you want to add: ')
amount = int(input())
curr_user.addMoney(amount)
print(f'\n {amount} coins added to {username}')
print(f'\n {username} now has {curr_user.wealth} coins')
def register_property():
print('------------------------------------------------------------------ \n')
print('Enter your username: ')
username = str(input())
user_found = False
for user in User.users:
if user.username == username:
user_found = True
curr_user = user
if not user_found:
print("The specified user does not exist \n")
return
print('Enter your password : ')
pass1 = (input())
if(verify_user(username, pass1) == False):
print("\n You have input the incorrect pin!")
return
else:
print('Enter the name of the property:')
name = input('')
print('Enter the price of the property:')
price = int(input(''))
new_property = Property(name, price)
curr_user.register_property(new_property)
print(
f'\n {new_property.name} registered in the system by {curr_user.username}')
def viewUser():
username = input("Enter Username ")
user_found = False
for user in User.users:
if user.username == username:
user_found = True
curr_user = user
if not user_found:
print("The specified user does not exist \n")
return
print('Enter your password : ')
pass1 = (input())
if(verify_user(username, pass1) == False):
print("\n You have input the incorrect pin!")
return
property = blockchain.get_transaction_by_user(username)
for property_chosen in Property.properties:
for transaction in property_chosen.transaction_history:
if(transaction.buyer.username == username or transaction.seller.username == username):
print(f'''
Property Name : {transaction.propertyName}
Buyer: {transaction.buyer.username},
Seller: {transaction.seller.username}
Timestamp: {transaction.timestamp},
TxnID: {transaction.transactionId}
\n''')
def viewAllUser():
for user in User.users:
print(user.username)
def deregister():
username = input("Enter Username ")
user_found = False
for user in User.users:
if user.username == username:
user_found = True
curr_user = user
if not user_found:
print("The specified user does not exist \n")
return
print('Enter your password : ')
pass1 = (input())
if(verify_user(username, pass1) == False):
print("\n You have input the incorrect pin!")
return
propertyId = int(input("Enter Property ID to deregister "))
property_chosen = Property.properties[propertyId]
if(username != property_chosen.owner.username):
print("You do not own this property")
return
Property.deregister(Property ,propertyId)
print(f'{property_chosen.name} has been deregistered by {username}')
# main loop
while True:
#3. View transaction history for a property
print(""" \n ------------------------------------------------------------------ \n
Choose an action:
1. Register User
2. See all users
3. Register property
4. De-Register Property
5. View All Properties
6. Buy Property
7. View assets owned by a user
8. View Balance
9. Add Money
10. ViewUser (Transaction History)
11. Exit
""")
choice = (input())
try:
choice = int(choice)
except:
print("Invalid choice, choose again")
continue
if (choice == 1):
register()
elif (choice == 6):
Property.viewProperties(Property)
buy()
elif (choice == 4):
deregister()
#transaction_history()
elif (choice == 7):
show_assets()
elif (choice == 3):
register_property()
elif (choice == 5):
Property.viewProperties(Property)
elif (choice == 8):
viewWealth()
elif(choice == 10):
viewUser()
elif(choice == 9):
addMoney()
elif(choice == 2):
viewAllUser()
elif (choice == 11):
break
else:
print("Invalid choice, choose again")