-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.py
71 lines (55 loc) · 1.9 KB
/
update.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
from client.klaytn import Klaytn
from client.utils import *
import urllib.request
import json, requests, subprocess
import logging
logging.basicConfig(level=logging.DEBUG)
# check update
with open('./info.json', 'r') as f:
info = json.load(f)
res = check_update(info)
if not res: # not updated
exit(0)
# updated
klay = Klaytn(info['klaytn_node'])
# get file key
file_id = res['file_id']
txhash = res['txHash']
key = klay.getInputData(txhash)
# get public URL
file_url = get_realfirmwareurl(file_id, key, info)
print(file_url)
file_name = file_url[file_url.rfind("/")+1:]
print(file_name)
# download file
urllib.request.urlretrieve(file_url, './firms/src/' + file_name)
# get file checksum
file_hash = hash_file('./firms/src/' + file_name)
# check hash in blockchain
output = subprocess.Popen(['node', 'client/send.js', 'hash', str(file_id), file_hash], stdout=subprocess.PIPE ).communicate()[0]
result = json.loads(output.strip().decode())['result']
try:
if result:
print('[*] Success: File Equal! (local-blockchain)')
# check hash(reporting)
res = requests.post(info['firmware_server'] + '/api/check/hash/' + str(file_id), json={
'hash': file_hash,
'wallet': info['device']['wallet']
})
print(res.text)
data = json.loads(res.text)
if data['equal']:
print('[*] Success: File Equal! (local-server)')
# upload to arduino
if get_ext('./firms/src/' + file_name) == '.ino':
print('[*] Success: Firmware file is .ino file!')
upload_device()
else:
print('[*] Error: Firmware file is NOT .ino file!')
else:
print('[*] Error: File is Different! (local-server)')
else:
print('[*] Error: File is Different! (local-blockchain)')
except:
print('[*] Error while executing')
print('[*] Finish')