-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiot_batch_verify_vechain.py
57 lines (48 loc) · 1.85 KB
/
iot_batch_verify_vechain.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
import os
import hashlib
import json
import pprint as pp
import iot_env as ie
import vechain as VC
import time
# List all files in a directory using os.listdir
data_path = ie.data_dir
vidListNo = 1
if __name__ == "__main__":
try:
kv_file = open("iot_order_vid", 'r')
order_vid_content = kv_file.read()
if order_vid_content:
order_vid = json.loads(order_vid_content)
pp.pprint(order_vid)
else:
order_vid = {}
except Exception as e:
print(e)
order_vid = {}
start_time = time.time()
vc = VC.VeChain()
for orderNo in os.listdir(data_path):
if os.path.isfile(os.path.join(data_path, orderNo)):
if not orderNo in order_vid:
print("Order data hash has not been uploaded to vechain ,verify later ...... ".format(orderNo))
else:
# generate file data hash
f = open(data_path + orderNo)
content = f.read()
m = hashlib.sha256()
m.update(content.encode('utf-8'))
data_hash = '0x' + m.hexdigest()
print('hash result is {}'.format(data_hash))
f.close()
# upload data hash pair with vid for request No . with uid
print('vid is {}'.format(order_vid[orderNo]))
vid_lists = [order_vid[orderNo]]
data_hash_vechain = vc.query_latest_hash(vid_lists)
pp.pprint(data_hash_vechain)
if data_hash == data_hash_vechain:
print('Order No . {} verified ,trust it ...'.format(orderNo))
else:
print('Order No . {} not verified , not a trust data '.format(orderNo))
process_time = time.time() - start_time
print("Total block chain verify time {} seconds".format(int(process_time)))