From a8e81c0bd2988ba8921d39b023b0bff115510dd0 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 26 Jun 2019 20:14:50 +0200 Subject: [PATCH] keepkey: use libusb to enumerate devices instead of hid --- electrum/plugins/keepkey/keepkey.py | 32 ++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/electrum/plugins/keepkey/keepkey.py b/electrum/plugins/keepkey/keepkey.py index 9a26d7fd0..30c49de0e 100644 --- a/electrum/plugins/keepkey/keepkey.py +++ b/electrum/plugins/keepkey/keepkey.py @@ -9,6 +9,7 @@ from electrum import constants from electrum.i18n import _ from electrum.transaction import Transaction, PartialTransaction, PartialTxInput, PartialTxOutput from electrum.keystore import Hardware_KeyStore +from electrum.plugin import Device from electrum.base_wizard import ScriptTypeNotSupported from ..hw_wallet import HW_PluginBase @@ -16,8 +17,10 @@ from ..hw_wallet.plugin import (is_any_tx_output_on_change_branch, trezor_valida get_xpubs_and_der_suffixes_from_txinout) if TYPE_CHECKING: + import usb1 from .client import KeepKeyClient + # TREZOR initialization methods TIM_NEW, TIM_RECOVER, TIM_MNEMONIC, TIM_PRIVKEY = range(0, 4) @@ -84,21 +87,40 @@ class KeepKeyPlugin(HW_PluginBase): self.types = keepkeylib.client.types self.DEVICE_IDS = (keepkeylib.transport_hid.DEVICE_IDS + keepkeylib.transport_webusb.DEVICE_IDS) - self.device_manager().register_devices(self.DEVICE_IDS) + # only "register" hid device id: + self.device_manager().register_devices(keepkeylib.transport_hid.DEVICE_IDS) + # for webusb transport, use custom enumerate function: + self.device_manager().register_enumerate_func(self.enumerate) self.libraries_available = True except ImportError: self.libraries_available = False + def enumerate(self): + from keepkeylib.transport_webusb import WebUsbTransport + results = [] + for dev in WebUsbTransport.enumerate(): + path = self._dev_to_str(dev) + results.append(Device(path=path, + interface_number=-1, + id_=path, + product_key=(dev.getVendorID(), dev.getProductID()), + usage_page=0, + transport_ui_string=f"webusb:{path}")) + return results + + @staticmethod + def _dev_to_str(dev: "usb1.USBDevice") -> str: + return ":".join(str(x) for x in ["%03i" % (dev.getBusNumber(),)] + dev.getPortNumberList()) + def hid_transport(self, pair): from keepkeylib.transport_hid import HidTransport return HidTransport(pair) def webusb_transport(self, device): from keepkeylib.transport_webusb import WebUsbTransport - for d in WebUsbTransport.enumerate(): - if device.id_.startswith(d.getSerialNumber()): - return WebUsbTransport(d) - return WebUsbTransport(device) + for dev in WebUsbTransport.enumerate(): + if device.path == self._dev_to_str(dev): + return WebUsbTransport(dev) def _try_hid(self, device): self.logger.info("Trying to connect over USB...")