-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from keepkey/bridge-client
Bridge client
- Loading branch information
Showing
9 changed files
with
297 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#!/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() | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from waitress import serve | ||
import kkbridge | ||
serve(kkbridge.create_app(), host='127.0.0.1', port=1646) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from distutils.core import setup | ||
import py2exe | ||
|
||
setup(console=['wait-serv.py']) | ||
options = { | ||
"py2exe": { | ||
"dist_dir": "./windows/dist" | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
; Script generated by the Inno Setup Script Wizard. | ||
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! | ||
|
||
#define MyAppName "Keepkey Bridge" | ||
#define MyAppVersion "1.5" | ||
#define MyAppPublisher "Shapeshift" | ||
#define MyAppURL "shapeshift.com" | ||
#define MyAppExeName "wait-serv.exe" | ||
#define MyAppAssocName MyAppName + " File" | ||
#define MyAppAssocExt ".myp" | ||
#define MyAppAssocKey StringChange(MyAppAssocName, " ", "") + MyAppAssocExt | ||
|
||
[Setup] | ||
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications. | ||
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.) | ||
AppId={{6239ED12-BE1C-4AB6-AA1A-12300A3AE957} | ||
AppName={#MyAppName} | ||
AppVersion={#MyAppVersion} | ||
;AppVerName={#MyAppName} {#MyAppVersion} | ||
AppPublisher={#MyAppPublisher} | ||
AppPublisherURL={#MyAppURL} | ||
AppSupportURL={#MyAppURL} | ||
AppUpdatesURL={#MyAppURL} | ||
DefaultDirName={autopf}\{#MyAppName} | ||
ChangesAssociations=yes | ||
DisableProgramGroupPage=yes | ||
; Uncomment the following line to run in non administrative install mode (install for current user only.) | ||
;PrivilegesRequired=lowest | ||
OutputBaseFilename=kkbsetup | ||
Compression=lzma | ||
SolidCompression=yes | ||
WizardStyle=modern | ||
|
||
[Languages] | ||
Name: "english"; MessagesFile: "compiler:Default.isl" | ||
|
||
[Tasks] | ||
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked | ||
|
||
[Files] | ||
Source: "Z:\windows\dist\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion | ||
Source: "Z:\windows\dist\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs | ||
; NOTE: Don't use "Flags: ignoreversion" on any shared system files | ||
|
||
[Registry] | ||
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocExt}\OpenWithProgids"; ValueType: string; ValueName: "{#MyAppAssocKey}"; ValueData: ""; Flags: uninsdeletevalue | ||
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}"; ValueType: string; ValueName: ""; ValueData: "{#MyAppAssocName}"; Flags: uninsdeletekey | ||
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\{#MyAppExeName},0" | ||
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\{#MyAppExeName}"" ""%1""" | ||
Root: HKA; Subkey: "Software\Classes\Applications\{#MyAppExeName}\SupportedTypes"; ValueType: string; ValueName: ".myp"; ValueData: "" | ||
|
||
[Icons] | ||
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}" | ||
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon | ||
|
||
[Run] | ||
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent | ||
|
Binary file not shown.