|
29 | 29 | logger.warning(e) |
30 | 30 | logger.warning('BLE scanning/connectivity will not be available') |
31 | 31 |
|
| 32 | +# libjade in-process software emulation is optional. |
| 33 | +# It relies on the libjade.so shared library being in LD_LIBRARY_PATH. |
| 34 | +try: |
| 35 | + from .jade_sw import JadeSoftwareImpl |
| 36 | +except (ImportError, FileNotFoundError) as e: |
| 37 | + logger.debug(e) |
| 38 | + logger.debug('libjade Software Jade emulation will not be available') |
| 39 | + |
32 | 40 |
|
33 | 41 | # Default serial connection |
34 | 42 | DEFAULT_BAUD_RATE = 115200 |
|
39 | 47 | DEFAULT_BLE_SERIAL_NUMBER = None |
40 | 48 | DEFAULT_BLE_SCAN_TIMEOUT = 60 |
41 | 49 |
|
| 50 | +# Default libjade connection |
| 51 | +DEFAULT_LIBJADE_TIMEOUT = 5 |
| 52 | + |
42 | 53 |
|
43 | 54 | def _hexlify(data): |
44 | 55 | """ |
@@ -255,6 +266,31 @@ def create_ble(device_name=None, serial_number=None, |
255 | 266 | scan_timeout, loop) |
256 | 267 | return JadeAPI(impl) |
257 | 268 |
|
| 269 | + @staticmethod |
| 270 | + def create_libjade(timeout=None): |
| 271 | + """ |
| 272 | + Create a JadeAPI object using libjade (in-process software emulation). |
| 273 | + WARNING: libjade is BETA and should not be used with real funds. |
| 274 | + NOTE: raises JadeError if libjade dependencies not installed. |
| 275 | +
|
| 276 | + Parameters |
| 277 | + ---------- |
| 278 | + timeout : int, optional |
| 279 | + The read timeout when awaiting messages (Uses 5s if not given). |
| 280 | +
|
| 281 | + Returns |
| 282 | + ------- |
| 283 | + JadeAPI |
| 284 | + API object configured to use libjade. |
| 285 | + NOTE: The caller must call 'connect()' before using the instance. |
| 286 | +
|
| 287 | + Raises |
| 288 | + ------ |
| 289 | + JadeError if libjade is not available (libjade.so not installed) |
| 290 | + """ |
| 291 | + impl = JadeInterface.create_libjade(timeout) |
| 292 | + return JadeAPI(impl) |
| 293 | + |
258 | 294 | def connect(self): |
259 | 295 | """ |
260 | 296 | Try to connect the underlying transport interface (eg. serial, ble, etc.) |
@@ -1993,18 +2029,18 @@ def sign_psbt(self, network, psbt, additional_info=None): |
1993 | 2029 |
|
1994 | 2030 | class JadeInterface: |
1995 | 2031 | """ |
1996 | | - Mid-level interface to Jade |
1997 | | - Wraps either a serial or a ble connection |
| 2032 | + Mid-level interface to Jade. |
| 2033 | + Wraps either a serial or ble connection, or an in-process libjade instance. |
1998 | 2034 | Calls to send and receive bytes and cbor messages over the interface. |
1999 | 2035 |
|
2000 | 2036 | Either: |
2001 | 2037 | a) use wrapped with JadeAPI |
2002 | 2038 | (recommended) |
2003 | 2039 | or: |
2004 | | - b) use with JadeInterface.create_[serial|ble]() as jade: |
| 2040 | + b) use with JadeInterface.create_[serial|ble|libjade]() as jade: |
2005 | 2041 | ... |
2006 | 2042 | or: |
2007 | | - c) use JadeInterface.create_[serial|ble], then call connect() before |
| 2043 | + c) use JadeInterface.create_[serial|ble|libjade], then call connect() before |
2008 | 2044 | using, and disconnect() when finished |
2009 | 2045 | (caveat cranium) |
2010 | 2046 | or: |
@@ -2110,6 +2146,31 @@ def create_ble(device_name=None, serial_number=None, |
2110 | 2146 | loop=loop) |
2111 | 2147 | return JadeInterface(impl) |
2112 | 2148 |
|
| 2149 | + @staticmethod |
| 2150 | + def create_libjade(timeout=None): |
| 2151 | + """ |
| 2152 | + Create a JadeInterface object object using libjade (in-process software emulation). |
| 2153 | + WARNING: libjade is BETA and should not be used with real funds. |
| 2154 | + NOTE: raises JadeError if libjade dependencies not installed. |
| 2155 | +
|
| 2156 | + Parameters |
| 2157 | + ---------- |
| 2158 | + timeout : int, optional |
| 2159 | + The read timeout when awaiting messages (Uses 5s if not given). |
| 2160 | +
|
| 2161 | + Returns |
| 2162 | + ------- |
| 2163 | + JadeInterface |
| 2164 | + Interface object configured to use the libjade. |
| 2165 | + NOTE: The caller must call 'connect()' before using the instance. |
| 2166 | + """ |
| 2167 | + this_module = sys.modules[__name__] |
| 2168 | + if not hasattr(this_module, "JadeSoftwareImpl"): |
| 2169 | + raise JadeError(1, "libjade support not installed", None) |
| 2170 | + |
| 2171 | + impl = JadeSoftwareImpl(timeout or DEFAULT_LIBJADE_TIMEOUT) |
| 2172 | + return JadeInterface(impl) |
| 2173 | + |
2113 | 2174 | def connect(self): |
2114 | 2175 | """ |
2115 | 2176 | Try to connect the underlying transport interface (eg. serial, ble, etc.) |
|
0 commit comments