-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlockChain.py
282 lines (221 loc) · 11.9 KB
/
BlockChain.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
import time
import hashlib as hasher
import datetime as date
import random
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Signature import pkcs1_15
from Block import Block
from Transaction import Transaction
class BlockChain:
def __init__(self):
self.blockchain = []
self.utxo_array = []
self.manufacturers_list = []
self.other_users_list = []
self.global_index = 0
self.pow_proof = int(0)
# The function would verify all the blocks in the given blockchain
def verify_blockchain(self):
previous_block = self.blockchain[0]
count = 1
for block in self.blockchain[1:]:
print('\nFor the block #' + str(count) + ': ')
for transaction in block.supply_data:
print('The item ID is ' + str(transaction.item_id) +
' and the associated timestamp is ' + str(transaction.timestamp))
if(str(previous_block.hash) == str(block.previous_hash)):
print('The hash values have been verified.')
sha = hasher.sha256()
sha.update(str(int(block.proof_of_work)).encode('utf-8'))
hash_value = sha.hexdigest()
print('The PoW number is ' + str(block.proof_of_work) +
' and the associated hash is ' + hash_value)
print('------------------------------------------------------------------------------------------------------------------------')
print('\n\n')
# Function for generating manufacturer keys
def generate_manufacturer_keys(self, number):
for item in range(0, int(number)):
self.manufacturers_list.append(
RSA.generate(1024, Random.new().read))
# print(self.manufacturers_list)
print('\nThe manufacturer keys have been generated.')
# Function for generating stakeholder keys
def generate_other_keys(self, number):
for item in range(0, int(number)):
self.other_users_list.append(RSA.generate(1024, Random.new().read))
# print(self.other_users_list)
print('\nThe stakeholder keys have been generated.')
# Function for tracking an item
def track_item(self, item_code):
not_found_flag = True
for block in self.blockchain[1:]:
for transaction in block.supply_data:
if(item_code == transaction.item_id):
if(not_found_flag):
print('\nThe item (' + item_code +
') has been found and the tracking details are: ')
not_found_flag = False
manufacturer_suppplier = False
manufacturer_receiver = False
supplier_count = 0
supplier_not_found_flag = True
for item in self.manufacturers_list:
supplier_count = supplier_count + 1
if str(transaction.supplier_puk.exportKey("PEM").decode('utf-8')) == str(item.publickey().exportKey("PEM").decode('utf-8')):
supplier_not_found_flag = False
manufacturer_suppplier = True
break
if(supplier_not_found_flag):
supplier_count = 0
for item in self.other_users_list:
supplier_count = supplier_count + 1
if str(transaction.supplier_puk.exportKey("PEM").decode('utf-8')) == str(item.publickey().exportKey("PEM").decode('utf-8')):
supplier_not_found_flag = False
break
receiver_count = 0
receiver_not_found_flag = True
for item in self.manufacturers_list:
receiver_count = receiver_count + 1
if str(transaction.receiver_puk) == str(item.publickey().exportKey("PEM").decode('utf-8')):
receiver_not_found_flag = False
manufacturer_receiver = True
break
if(receiver_not_found_flag):
receiver_count = 0
for item in self.other_users_list:
receiver_count = receiver_count + 1
if str(transaction.receiver_puk) == str(item.publickey().exportKey("PEM").decode('utf-8')):
receiver_not_found_flag = False
break
final_result = ""
if(manufacturer_suppplier):
final_result = final_result + "Manufacturer #" + \
str(supplier_count) + " transferred the asset to "
else:
final_result = final_result + "Stakeholder #" + \
str(supplier_count) + " transferred the asset to "
if(manufacturer_receiver):
final_result = final_result + "Manufacturer #" + \
str(receiver_count) + " at " + \
str(transaction.timestamp)
else:
final_result = final_result + "Stakeholder #" + \
str(receiver_count) + " at " + \
str(transaction.timestamp)
print(final_result)
if(not_found_flag):
print('\nThe item code was not found in the blockchain.')
# This function is used for viewing all the blocks and the transactions in the blockchain
def view_blockchain(self):
print('\n\nThe list of blocks are: \n')
for block in self.blockchain:
print('\n------------------------------------------------------------------------------------------------------------------------')
print(block.index)
print(block.timestamp)
print(block.supply_data)
print(block.proof_of_work)
print(block.hash)
print(block.previous_hash)
print('------------------------------------------------------------------------------------------------------------------------')
print('\n\n')
# This function is used to view all the Unspend Transaction Outputs
def view_UTXO(self):
print('\n\nThe list of UTXO are: \n')
for transaction in self.utxo_array:
print('\n------------------------------------------------------------------------------------------------------------------------')
print(transaction.supplier_puk.exportKey("PEM").decode('utf-8'))
print(transaction.receiver_puk)
print(transaction.item_id)
print(transaction.timestamp)
print(transaction.signature)
print('------------------------------------------------------------------------------------------------------------------------')
print('\n\n')
# This function is used to generate a transaction
def make_transaction(self, supplier_key, receiver_key, item_id):
# Selection functions for the keys and the item ID
selection = input('\nSelect type of key (M/O) for supplier: ')
if selection == 'M':
index = int(input('There are a total of ' +
str(len(self.manufacturers_list)) + ' users. Enter your selection: ')) - 1
supplier_key = self.manufacturers_list[index]
elif selection == 'O':
index = int(input('There are a total of ' +
str(len(self.other_users_list)) + ' users. Enter your selection: ')) - 1
supplier_key = self.other_users_list[index]
selection = input('\nSelect type of key (M/O) for receiver: ')
if selection == 'M':
index = int(input('There are a total of ' +
str(len(self.manufacturers_list)) + ' users. Enter your selection: ')) - 1
receiver_key = self.manufacturers_list[index]
elif selection == 'O':
index = int(input('There are a total of ' +
str(len(self.other_users_list)) + ' users. Enter your selection: ')) - 1
receiver_key = self.other_users_list[index]
receiver_puk = receiver_key.publickey().exportKey("PEM").decode('utf-8')
item_id = input('Enter the ID of the tracked item: ')
# Acquiring the details for the transactions
supplier_puk = supplier_key.publickey()
timestamp = date.datetime.now()
# Generating the message text and the signature
message = str(supplier_puk.exportKey("PEM").decode('utf-8')) + \
str(receiver_puk) + item_id + str(timestamp)
hash_message = SHA256.new(message.encode('utf-8'))
supplier_prk = RSA.import_key(supplier_key.exportKey("DER"))
signature = pkcs1_15.new(supplier_prk).sign(hash_message)
# Creating a new transaction
new_transaction = Transaction(
supplier_puk, receiver_puk, item_id, timestamp, signature)
self.utxo_array.append(new_transaction)
# The function for mining the block in the supply blockchain
def mine_block(self):
max_range = len(self.utxo_array)
transaction_amount = random.randint(0, max_range)
transaction_array = []
print('\nThe number of selected transactions for the block is: ' +
str(transaction_amount))
if(transaction_amount):
for index in range(0, transaction_amount):
# All verifications for the transactions
if(self.utxo_array[0].verify_transaction()):
print('\nThe sign verification for transaction #' +
str(index + 1) + ' was true!')
if(self.utxo_array[0].check_item_code(self)):
print(
'The item code has been found. Checking the previous owner details.')
if(self.utxo_array[0].check_previous_owner(self)):
print('Verification of previous owner has been done!')
transaction_array.append(self.utxo_array[0])
else:
print('Verification of previous owner has failed!')
else:
print(
'The item code was not found on blockchain. Checking for manufacturer credentials.')
if(self.utxo_array[0].check_manufacturer_credentials(self)):
print(
'The new item has been added under the manufacturer.')
transaction_array.append(self.utxo_array[0])
else:
print(
'The transaction key is not authorised as a manufacturer!')
else:
print('The sign verification for transaction #' +
str(index + 1) + ' was false!')
self.utxo_array.pop(0)
if(len(transaction_array) != 0):
new_block = Block(self.global_index, date.datetime.now(
), transaction_array, self.blockchain[self.global_index - 1].hash)
self.global_index = self.global_index + 1
self.blockchain.append(new_block)
else:
# Prevent addition of blocks with no transactions
print(
'No transactions have been selected and therefore no block has been added!')
def add_block(self, block):
self.blockchain.append(block)
# This function is used to create genesis block
def create_genesis_block(self):
self.global_index = self.global_index + 1
print('\n\nThe genesis block is being created.')
return Block(0, date.datetime.now(), "GENESIS BLOCK", "0")