forked from keepkey/python-keepkey
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkkbridge.py
More file actions
Latest commit
129 lines (94 loc) · 3.39 KB
/
Copy pathkkbridge.py
File metadata and controls
129 lines (94 loc) · 3.39 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
#!/usr/bin/env python
from __future__ importprint_function
fromosimportclose, error
fromflaskimportFlask, Response, request, jsonify
fromflask_corsimportCORS, cross_origin
importsys
sys.path= ['../',] +sys.path
fromkeepkeylibimportclient
fromkeepkeylib.clientimportKeepKeyClient
fromkeepkeylib.transport_webusbimportWebUsbTransport
fromkeepkeylibimportmessages_pb2asmessages
importjson
importbinascii
PACKET_SIZE=64
kkClient=None
defcreate_app():
app=Flask(__name__)
CORS(app)
app.config['CORS_HEADERS'] ='Content-Type'
definitDevice():
globalkkClient
if (kkClient!=None):
kkClient.close()
kkClient=None
# List all connected KeepKeys on USB
devices=WebUsbTransport.enumerate()
# Check whether we found any
iflen(devices) ==0:
returnNone
# Use first connected device
transport=WebUsbTransport(devices[0])
# Creates object for manipulating KeepKey
client=KeepKeyClient(transport)
returnclient
@app.route('/init')
definitKK():
globalkkClient
kkClient=initDevice()
if (kkClient==None):
data="No KeepKey found"
returnResponse(str(data), status=400, mimetype='application/json')
else:
data=kkClient.features
returnResponse(str(data), status=200, mimetype='application/json')
@app.route('/ping')
defpingKK():
globalkkClient
if (kkClient==None):
kkClient=initDevice()
else:
pass
if (kkClient==None):
data="No KeepKey found"
returnResponse(str(data), status=404, mimetype='application/json')
try:
ping=kkClient.call(messages.Ping(message='Duck, a bridge!', button_protection=True))
except:
kkClient.close()
kkClient=None
data="No KeepKey found"
returnResponse(str(data), status=404, mimetype='application/json')
returnResponse(str(ping), status=200, mimetype='application/json')
@app.route('/exchange/<string:kind>', methods=['GET', 'POST'])
@cross_origin()
defrest_api(kind):
globalkkClient
if (kkClient==None):
kkClient=initDevice()
else:
pass
if (kkClient==None):
data="No KeepKey found"
returnResponse(str(data), status=404, mimetype='application/json')
ifrequest.method=='POST':
content=request.get_json(silent=True)
msg=bytearray.fromhex(content["data"])
try:
kkClient.call_bridge(msg)
except:
kkClient.close()
kkClient=None
kkClient=initDevice()
returnResponse('{}', status=404, mimetype='application/json')
returnResponse('{}', status=200, mimetype='application/json')
ifrequest.method=='GET':
data=kkClient.call_bridge_read()
body='{"data":"'+binascii.hexlify(data).decode("utf-8") +'"}'
returnResponse(body, status=200, mimetype='application/json')
returnResponse('{}', status=404, mimetype='application/json')
returnapp
if__name__=='__main__':
app=create_app()
app.run(port='1646')
#app.run()