forked from keepkey/python-keepkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
147 lines (132 loc) · 5.75 KB
/
Copy pathconfig.py
File metadata and controls
147 lines (132 loc) · 5.75 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# This file is part of the TREZOR project.
#
# Copyright (C) 2012-2016 Marek Palatinus <[email protected]>
# Copyright (C) 2012-2016 Pavol Rusnak <[email protected]>
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
#
# The script has been modified for KeepKey Device.
from __future__ import print_function
import os
import sys
sys.path = ['../',] + sys.path
from keepkeylib.transport_pipe import PipeTransport
from keepkeylib.transport_socket import SocketTransportClient
from keepkeylib.transport_udp import UDPTransport
# Explicit transport selection via KK_TRANSPORT. Currently only "dylib" is
# implemented (UDP is the no-env-var default below). Any other non-empty
# value is rejected up-front so a typo like "dyllib" doesn't silently fall
# through to UDP with hardware autodetect disabled — which would route
# tests to whichever emulator happened to be listening on 11044.
_KNOWN_TRANSPORTS = {"dylib"}
_explicit_transport = os.getenv("KK_TRANSPORT") or None
if _explicit_transport is not None and _explicit_transport not in _KNOWN_TRANSPORTS:
raise RuntimeError(
"Unsupported KK_TRANSPORT=%r — known values: %s. Unset to use "
"default HID/WebUSB autodetect or UDP fallback." %
(_explicit_transport, sorted(_KNOWN_TRANSPORTS))
)
if os.getenv("KK_FORCE_UDP") == "1":
# Local-only escape hatch: skip HID/WebUSB autodetect so tests hit the
# UDP emulator even with a real KeepKey plugged in. NOT for CI.
hid_devices = []
webusb_devices = []
elif _explicit_transport == "dylib":
# Skip HID/WebUSB autodetect — dylib is opt-in by env var. Without
# this skip, a connected real KeepKey would win over the explicit
# request and the dylib regression suite would route to hardware.
hid_devices = []
webusb_devices = []
else:
try:
from keepkeylib.transport_hid import HidTransport
hid_devices = HidTransport.enumerate()
except Exception:
print("Error loading HID. HID devices not enumerated.")
hid_devices = []
try:
from keepkeylib.transport_webusb import WebUsbTransport
webusb_devices = WebUsbTransport.enumerate()
except Exception:
print("Error loading WebUSB. WebUSB devices not enumerated.")
webusb_devices = []
# Only count a hid device if it has more than just the U2F interface exposed
onlyU2F = len(hid_devices) > 0 and \
hid_devices[0][0] == None and hid_devices[0][1] == None and hid_devices[0][2] != None
if len(hid_devices) > 0 and not onlyU2F:
if hid_devices[0][1] != None:
print('Using KeepKey over HID')
TRANSPORT = HidTransport
TRANSPORT_ARGS = (hid_devices[0],)
TRANSPORT_KWARGS = {'debug_link': False}
DEBUG_TRANSPORT = HidTransport
DEBUG_TRANSPORT_ARGS = (hid_devices[0],)
DEBUG_TRANSPORT_KWARGS = {'debug_link': True}
else:
print('Using Raspberry Pi')
TRANSPORT = HidTransport
TRANSPORT_ARGS = (hid_devices[0],)
TRANSPORT_KWARGS = {'debug_link': False}
DEBUG_TRANSPORT = SocketTransportClient
DEBUG_TRANSPORT_ARGS = ('trezor.bo:2000',)
DEBUG_TRANSPORT_KWARGS = {}
elif len(webusb_devices) > 0:
print('Using Keepkey over webUSB')
TRANSPORT = WebUsbTransport
TRANSPORT_ARGS = (webusb_devices[0],)
TRANSPORT_KWARGS = {'debug_link': False}
DEBUG_TRANSPORT = WebUsbTransport
DEBUG_TRANSPORT_ARGS = (webusb_devices[0],)
DEBUG_TRANSPORT_KWARGS = {'debug_link': True}
elif os.getenv('KK_TRANSPORT') == 'dylib':
# In-process FFI transport against libkkemu.dylib (or libkkemu.so).
# Same firmware as UDP, different transport — exposes caller-driven
# polling bugs that the UDP daemon hides behind its own poll thread.
print('Using Emulator (dylib FFI)')
from keepkeylib.transport_dylib import DylibState, DylibTransport
_dylib_path = os.getenv('KK_DYLIB')
if not _dylib_path:
raise RuntimeError(
"KK_TRANSPORT=dylib requires KK_DYLIB=/path/to/libkkemu.dylib"
)
_dylib_state = DylibState.get_or_init(_dylib_path)
TRANSPORT = DylibTransport
TRANSPORT_ARGS = (_dylib_state, 0)
TRANSPORT_KWARGS = {}
DEBUG_TRANSPORT = DylibTransport
DEBUG_TRANSPORT_ARGS = (_dylib_state, 1)
DEBUG_TRANSPORT_KWARGS = {}
else:
print('Using Emulator')
TRANSPORT = UDPTransport
TRANSPORT_ARGS = (os.getenv('KK_TRANSPORT_MAIN', '127.0.0.1:11044'),)
TRANSPORT_KWARGS = {}
DEBUG_TRANSPORT = UDPTransport
DEBUG_TRANSPORT_ARGS = (os.getenv('KK_TRANSPORT_DEBUG', '127.0.0.1:11045'),)
DEBUG_TRANSPORT_KWARGS = {}
def enumerate_hid():
global TRANSPORT, TRANSPORT_ARGS, TRANSPORT_KWARGS, DEBUG_TRANSPORT, DEBUG_TRANSPORT_ARGS, DEBUG_TRANSPORT_KWARGS
try:
devices = HidTransport.enumerate()
except Exception:
print("Error loading HID. HID devices not enumerated.")
devices = []
if len(devices) > 0:
if devices[0][1] != None:
TRANSPORT = HidTransport
TRANSPORT_ARGS = (devices[0],)
TRANSPORT_KWARGS = {'debug_link': False}
DEBUG_TRANSPORT = HidTransport
DEBUG_TRANSPORT_ARGS = (devices[0],)
DEBUG_TRANSPORT_KWARGS = {'debug_link': True}