Skip to content

Commit

Permalink
Support for Trezor client library v0.12
Browse files Browse the repository at this point in the history
(cherry picked from commit 3fb277d)
  • Loading branch information
Bertrand256 committed Feb 15, 2021
1 parent b7d97cd commit a1d6379
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 110 deletions.
119 changes: 95 additions & 24 deletions src/hw_intf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sqlite3
import threading
from functools import partial
from io import BytesIO
from typing import Optional, Tuple, List, ByteString, Callable, Dict
import sys

Expand All @@ -32,6 +33,9 @@
hd_tree_db_map: Dict[str, int] = {} # Dict[str <hd tree ident>, int <db id>]


log = logging.getLogger('dmt.hw_intf')


def control_trezor_keepkey_libs(connecting_to_hw):
"""
Check if trying to switch between Trezor and Keepkey on Linux. It's not allowed because Trezor/Keepkey's client
Expand Down Expand Up @@ -306,6 +310,17 @@ def get_hw_firmware_version(hw_session: HwSessionInfo):
return hw_session.hw_client.getFirmwareVersion().get('version')


def firmware_update(hw_client, raw_data: bytes):
hw_type = get_hw_type(hw_client)
if hw_type == HWType.trezor:
import hw_intf_trezor as trezor
trezor.firmware_update(hw_client, raw_data)
elif HWType.keepkey:
hw_client.firmware_update(fp=BytesIO(raw_data))
elif hw_type == HWType.ledger_nano_s:
raise Exception('Ledger Nano S is not supported.')


@control_hw_call
def sign_tx(hw_session: HwSessionInfo, utxos_to_spend: List[UtxoType],
tx_outputs: List[TxOutputType], tx_fee):
Expand Down Expand Up @@ -387,43 +402,99 @@ def sign(ctrl, display_label):
return sig


@control_hw_call
def change_pin(hw_session: HwSessionInfo, remove=False):
if hw_session.app_config.hw_type == HWType.trezor:
def change_pin(hw_client, remove=False):
hw_type = get_hw_type(hw_client)
if hw_type == HWType.trezor:
import hw_intf_trezor as trezor

return trezor.change_pin(hw_session, remove)
return trezor.change_pin(hw_client, remove)

elif hw_session.app_config.hw_type == HWType.keepkey:
elif hw_type == HWType.keepkey:
import hw_intf_keepkey as keepkey

return keepkey.change_pin(hw_session, remove)
return keepkey.change_pin(hw_client, remove)

elif hw_session.app_config.hw_type == HWType.ledger_nano_s:
elif hw_type == HWType.ledger_nano_s:

raise Exception('Ledger Nano S not supported.')
raise Exception('Ledger Nano S is not supported.')

else:
logging.error('Invalid HW type: ' + str(hw_session.app_config.hw_type))
logging.error('Invalid HW type: ' + str(hw_type))


@control_hw_call
def enable_passphrase(hw_session: HwSessionInfo, passphrase_enabled):
if hw_session.app_config.hw_type == HWType.trezor:
import hw_intf_trezor
def action_on_device_message(message=DEFAULT_HW_BUSY_MESSAGE, title=DEFAULT_HW_BUSY_TITLE):
def decorator_f(func):
def wrapped_f(*args, **kwargs):
hw_client = None
hw_client_names = ('MyTrezorClient', 'KeepkeyClient')

hw_intf_trezor.enable_passphrase(hw_session, passphrase_enabled)
# look for hw client:
for arg in args:
name = type(arg).__name__
if name in hw_client_names:
hw_client = arg
break

elif hw_session.app_config.hw_type == HWType.keepkey:
if not hw_client:
for arg_name in kwargs:
name = type(kwargs[arg_name]).__name__
if name in hw_client_names:
hw_client = kwargs[arg_name]
break

hw_session.hw_client.apply_settings(use_passphrase=passphrase_enabled)
def thread_dialog(ctrl):
if ctrl:
ctrl.dlg_config_fun(dlg_title=title, show_progress_bar=False)
ctrl.display_msg_fun(message)

elif hw_session.app_config.hw_type == HWType.ledger_nano_s:
return func(*args, **kwargs)

raise Exception('Ledger Nano S not supported.')
return WndUtils.run_thread_dialog(thread_dialog, (), True, show_window_delay_ms=1000,
force_close_dlg_callback=partial(cancel_hw_thread_dialog, hw_client))
return wrapped_f
return decorator_f


@action_on_device_message()
def enable_passphrase(hw_client, passphrase_enabled):
hw_type = get_hw_type(hw_client)
if hw_type == HWType.trezor:
import hw_intf_trezor
hw_intf_trezor.enable_passphrase(hw_client, passphrase_enabled)
elif hw_type == HWType.keepkey:
hw_client.apply_settings(use_passphrase=passphrase_enabled)
elif hw_type == HWType.ledger_nano_s:
raise Exception('Ledger Nano S is not supported.')
else:
logging.error('Invalid HW type: ' + str(hw_type))


@action_on_device_message()
def set_passphrase_always_on_device(hw_client, enabled):
hw_type = get_hw_type(hw_client)
if hw_type == HWType.trezor:
import hw_intf_trezor
hw_intf_trezor.set_passphrase_always_on_device(hw_client, enabled)
elif hw_type == HWType.keepkey:
raise Exception('Keepkey not supported.')
elif hw_type == HWType.ledger_nano_s:
raise Exception('Ledger Nano S is not supported.')
else:
logging.error('Invalid HW type: ' + str(hw_type))


@action_on_device_message()
def set_wipe_code(hw_client, enabled):
hw_type = get_hw_type(hw_client)
if hw_type == HWType.trezor:
import hw_intf_trezor
hw_intf_trezor.set_wipe_code(hw_client, enabled)
elif hw_type == HWType.keepkey:
raise Exception('Keepkey not supported.')
elif hw_type == HWType.ledger_nano_s:
raise Exception('Ledger Nano S is not supported.')
else:
logging.error('Invalid HW type: ' + str(hw_session.app_config.hw_type))
logging.error('Invalid HW type: ' + str(hw_type))


@control_hw_call
Expand Down Expand Up @@ -662,7 +733,7 @@ def load(ctrl, hw_device_id: str, mnemonic: str, pin: str, passphrase_enbled: bo
True)


def recovery_device(hw_type: HWType, hw_device_id: str, word_count: int, passphrase_enabled: bool, pin_enabled: bool,
def recover_device(hw_type: HWType, hw_device_id: str, word_count: int, passphrase_enabled: bool, pin_enabled: bool,
hw_label: str, parent_window = None) -> Tuple[Optional[str], bool]:
"""
:param hw_type: app_config.HWType
Expand Down Expand Up @@ -690,13 +761,13 @@ def load(ctrl, hw_type: HWType, hw_device_id: str, word_count: int, passphrase_e
if hw_device_id:
if hw_type == HWType.trezor:

from hw_intf_trezor import recovery_device
return recovery_device(hw_device_id, word_count, passphrase_enabled, pin_enabled, hw_label)
from hw_intf_trezor import recover_device
return recover_device(hw_device_id, word_count, passphrase_enabled, pin_enabled, hw_label)

elif hw_type == HWType.keepkey:

from hw_intf_keepkey import recovery_device
return recovery_device(hw_device_id, word_count, passphrase_enabled, pin_enabled, hw_label)
from hw_intf_keepkey import recover_device
return recover_device(hw_device_id, word_count, passphrase_enabled, pin_enabled, hw_label)

else:
raise Exception('Not supported by Ledger Nano S.')
Expand Down
Loading

0 comments on commit a1d6379

Please sign in to comment.