-
Notifications
You must be signed in to change notification settings - Fork 60
/
kkbridge.py
129 lines (94 loc) · 3.39 KB
/
kkbridge.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
#!/usr/bin/env python
from __future__ import print_function
from os import close, error
from flask import Flask, Response, request, jsonify
from flask_cors import CORS, cross_origin
import sys
sys.path = ['../',] + sys.path
from keepkeylib import client
from keepkeylib.client import KeepKeyClient
from keepkeylib.transport_webusb import WebUsbTransport
from keepkeylib import messages_pb2 as messages
import json
import binascii
PACKET_SIZE = 64
kkClient = None
def create_app():
app = Flask(__name__)
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
def initDevice():
global kkClient
if (kkClient != None):
kkClient.close()
kkClient = None
# List all connected KeepKeys on USB
devices = WebUsbTransport.enumerate()
# Check whether we found any
if len(devices) == 0:
return None
# Use first connected device
transport = WebUsbTransport(devices[0])
# Creates object for manipulating KeepKey
client = KeepKeyClient(transport)
return client
@app.route('/init')
def initKK():
global kkClient
kkClient = initDevice()
if (kkClient == None):
data = "No KeepKey found"
return Response(str(data), status=400, mimetype='application/json')
else:
data = kkClient.features
return Response(str(data), status=200, mimetype='application/json')
@app.route('/ping')
def pingKK():
global kkClient
if (kkClient == None):
kkClient = initDevice()
else:
pass
if (kkClient == None):
data = "No KeepKey found"
return Response(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"
return Response(str(data), status=404, mimetype='application/json')
return Response(str(ping), status=200, mimetype='application/json')
@app.route('/exchange/<string:kind>', methods=['GET', 'POST'])
@cross_origin()
def rest_api(kind):
global kkClient
if (kkClient == None):
kkClient = initDevice()
else:
pass
if (kkClient == None):
data = "No KeepKey found"
return Response(str(data), status=404, mimetype='application/json')
if request.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()
return Response('{}', status=404, mimetype='application/json')
return Response('{}', status=200, mimetype='application/json')
if request.method == 'GET':
data = kkClient.call_bridge_read()
body = '{"data":"' + binascii.hexlify(data).decode("utf-8") + '"}'
return Response(body, status=200, mimetype='application/json')
return Response('{}', status=404, mimetype='application/json')
return app
if __name__ == '__main__':
app = create_app()
app.run(port='1646')
#app.run()