mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-30 17:01:34 +00:00
hw plugins: Allow custom enumerate functions
trezor: Adding support for all supported transports (HID, WebUSB, UDP, Bridge)
This commit is contained in:
parent
d56dba8039
commit
460e88ee53
3 changed files with 24 additions and 29 deletions
|
@ -312,6 +312,8 @@ class DeviceMgr(ThreadJob, PrintError):
|
||||||
# What we recognise. Each entry is a (vendor_id, product_id)
|
# What we recognise. Each entry is a (vendor_id, product_id)
|
||||||
# pair.
|
# pair.
|
||||||
self.recognised_hardware = set()
|
self.recognised_hardware = set()
|
||||||
|
# Custom enumerate functions for devices we don't know about.
|
||||||
|
self.enumerate_func = set()
|
||||||
# For synchronization
|
# For synchronization
|
||||||
self.lock = threading.RLock()
|
self.lock = threading.RLock()
|
||||||
self.hid_lock = threading.RLock()
|
self.hid_lock = threading.RLock()
|
||||||
|
@ -334,6 +336,9 @@ class DeviceMgr(ThreadJob, PrintError):
|
||||||
for pair in device_pairs:
|
for pair in device_pairs:
|
||||||
self.recognised_hardware.add(pair)
|
self.recognised_hardware.add(pair)
|
||||||
|
|
||||||
|
def register_enumerate_func(self, func):
|
||||||
|
self.enumerate_func.add(func)
|
||||||
|
|
||||||
def create_client(self, device, handler, plugin):
|
def create_client(self, device, handler, plugin):
|
||||||
# Get from cache first
|
# Get from cache first
|
||||||
client = self.client_lookup(device.id_)
|
client = self.client_lookup(device.id_)
|
||||||
|
@ -509,6 +514,7 @@ class DeviceMgr(ThreadJob, PrintError):
|
||||||
self.print_error("scanning devices...")
|
self.print_error("scanning devices...")
|
||||||
with self.hid_lock:
|
with self.hid_lock:
|
||||||
hid_list = hid.enumerate(0, 0)
|
hid_list = hid.enumerate(0, 0)
|
||||||
|
|
||||||
# First see what's connected that we know about
|
# First see what's connected that we know about
|
||||||
devices = []
|
devices = []
|
||||||
for d in hid_list:
|
for d in hid_list:
|
||||||
|
@ -524,6 +530,10 @@ class DeviceMgr(ThreadJob, PrintError):
|
||||||
devices.append(Device(d['path'], interface_number,
|
devices.append(Device(d['path'], interface_number,
|
||||||
id_, product_key, usage_page))
|
id_, product_key, usage_page))
|
||||||
|
|
||||||
|
# Let plugin handlers enumerate devices we don't know about
|
||||||
|
for f in self.enumerate_func:
|
||||||
|
devices.extend(f())
|
||||||
|
|
||||||
# Now find out what was disconnected
|
# Now find out what was disconnected
|
||||||
pairs = [(dev.path, dev.id_) for dev in devices]
|
pairs = [(dev.path, dev.id_) for dev in devices]
|
||||||
disconnected_ids = []
|
disconnected_ids = []
|
||||||
|
|
|
@ -84,38 +84,22 @@ class TrezorCompatiblePlugin(HW_PluginBase):
|
||||||
def __init__(self, parent, config, name):
|
def __init__(self, parent, config, name):
|
||||||
HW_PluginBase.__init__(self, parent, config, name)
|
HW_PluginBase.__init__(self, parent, config, name)
|
||||||
self.main_thread = threading.current_thread()
|
self.main_thread = threading.current_thread()
|
||||||
# FIXME: move to base class when Ledger is fixed
|
|
||||||
if self.libraries_available:
|
if self.libraries_available:
|
||||||
self.device_manager().register_devices(self.DEVICE_IDS)
|
self.device_manager().register_enumerate_func(self.enumerate)
|
||||||
|
|
||||||
def _try_hid(self, device):
|
def create_client(self, device, handler):
|
||||||
self.print_error("Trying to connect over USB...")
|
|
||||||
try:
|
try:
|
||||||
return self.hid_transport(device)
|
self.print_error("Trying to connect to TREZOR...")
|
||||||
|
transport = self.transport(device)
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
# see fdb810ba622dc7dbe1259cbafb5b28e19d2ab114
|
|
||||||
# raise
|
|
||||||
self.print_error("cannot connect at", device.path, str(e))
|
self.print_error("cannot connect at", device.path, str(e))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _try_bridge(self, device):
|
|
||||||
self.print_error("Trying to connect over Trezor Bridge...")
|
|
||||||
try:
|
|
||||||
return self.bridge_transport({'path': hexlify(device.path)})
|
|
||||||
except BaseException as e:
|
|
||||||
self.print_error("cannot connect to bridge", str(e))
|
|
||||||
return None
|
|
||||||
|
|
||||||
def create_client(self, device, handler):
|
|
||||||
# disable bridge because it seems to never returns if keepkey is plugged
|
|
||||||
#transport = self._try_bridge(device) or self._try_hid(device)
|
|
||||||
transport = self._try_hid(device)
|
|
||||||
if not transport:
|
if not transport:
|
||||||
self.print_error("cannot connect to device")
|
self.print_error("cannot connect at", device.path)
|
||||||
return
|
return
|
||||||
|
|
||||||
self.print_error("connected to device at", device.path)
|
self.print_error("connected to device at", device.path)
|
||||||
|
|
||||||
client = self.client_class(transport, handler, self)
|
client = self.client_class(transport, handler, self)
|
||||||
|
|
||||||
# Try a ping for device sanity
|
# Try a ping for device sanity
|
||||||
|
|
|
@ -16,21 +16,22 @@ class TrezorPlugin(TrezorCompatiblePlugin):
|
||||||
from . import client
|
from . import client
|
||||||
import trezorlib
|
import trezorlib
|
||||||
import trezorlib.ckd_public
|
import trezorlib.ckd_public
|
||||||
import trezorlib.transport_hid
|
|
||||||
import trezorlib.messages
|
import trezorlib.messages
|
||||||
|
import trezorlib.device
|
||||||
self.client_class = client.TrezorClient
|
self.client_class = client.TrezorClient
|
||||||
self.ckd_public = trezorlib.ckd_public
|
self.ckd_public = trezorlib.ckd_public
|
||||||
self.types = trezorlib.messages
|
self.types = trezorlib.messages
|
||||||
self.DEVICE_IDS = (trezorlib.transport_hid.DEV_TREZOR1, trezorlib.transport_hid.DEV_TREZOR2)
|
self.DEVICE_IDS = ('TREZOR',)
|
||||||
self.libraries_available = True
|
self.libraries_available = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
self.libraries_available = False
|
self.libraries_available = False
|
||||||
TrezorCompatiblePlugin.__init__(self, *args)
|
TrezorCompatiblePlugin.__init__(self, *args)
|
||||||
|
|
||||||
def hid_transport(self, device):
|
def enumerate(self):
|
||||||
from trezorlib.transport_hid import HidTransport
|
from trezorlib.device import TrezorDevice
|
||||||
return HidTransport.find_by_path(device.path)
|
from electrum.plugins import Device
|
||||||
|
return [Device(str(d), -1, str(d), 'TREZOR', 0) for d in TrezorDevice.enumerate()]
|
||||||
|
|
||||||
def bridge_transport(self, d):
|
def transport(self, device):
|
||||||
from trezorlib.transport_bridge import BridgeTransport
|
from trezorlib.device import TrezorDevice
|
||||||
return BridgeTransport(d)
|
return TrezorDevice.find_by_path(device.path)
|
||||||
|
|
Loading…
Add table
Reference in a new issue