From 69c54eb42534ac3239270cd24ddaaf802c4606ad Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sun, 21 May 2017 17:29:57 +0300 Subject: [PATCH] device: allow Qt-based PIN entry for Trezor/Keepkey --- libagent/device/keepkey_defs.py | 2 +- libagent/device/trezor.py | 24 ++++++++++++++++++++++++ libagent/device/trezor_defs.py | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/libagent/device/keepkey_defs.py b/libagent/device/keepkey_defs.py index 68802af..48ab192 100644 --- a/libagent/device/keepkey_defs.py +++ b/libagent/device/keepkey_defs.py @@ -4,6 +4,6 @@ from keepkeylib.client import CallException as Error from keepkeylib.client import KeepKeyClient as Client -from keepkeylib.messages_pb2 import PassphraseAck +from keepkeylib.messages_pb2 import PassphraseAck, PinMatrixAck from keepkeylib.transport_hid import HidTransport as Transport from keepkeylib.types_pb2 import IdentityType diff --git a/libagent/device/trezor.py b/libagent/device/trezor.py index 3729e88..d142424 100644 --- a/libagent/device/trezor.py +++ b/libagent/device/trezor.py @@ -33,11 +33,35 @@ class Trezor(interface.Device): 'non-empty' if self.passphrase else 'empty', self) return self._defs.PassphraseAck(passphrase=self.passphrase) + def create_pin_handler(conn): + try: + from PyQt5.QtWidgets import QApplication, QInputDialog, QLineEdit + except ImportError: + return conn.callback_PinMatrixRequest # CLI-based PIN handler + + def qt_handler(_): + label = ('Use the numeric keypad to describe number positions.\n' + 'The layout is:\n' + ' 7 8 9\n' + ' 4 5 6\n' + ' 1 2 3\n' + 'Please enter PIN:') + app = QApplication([]) + qd = QInputDialog() + qd.setTextEchoMode(QLineEdit.Password) + qd.setLabelText(label) + qd.show() + app.exec_() + return self._defs.PinMatrixAck(pin=qd.textValue()) + + return qt_handler + for d in self._defs.Transport.enumerate(): log.debug('endpoint: %s', d) transport = self._defs.Transport(d) connection = self._defs.Client(transport) connection.callback_PassphraseRequest = passphrase_handler + connection.callback_PinMatrixRequest = create_pin_handler(connection) f = connection.features log.debug('connected to %s %s', self, f.device_id) log.debug('label : %s', f.label) diff --git a/libagent/device/trezor_defs.py b/libagent/device/trezor_defs.py index 6bf4e0e..1baeabd 100644 --- a/libagent/device/trezor_defs.py +++ b/libagent/device/trezor_defs.py @@ -4,7 +4,7 @@ from trezorlib.client import CallException as Error from trezorlib.client import TrezorClient as Client -from trezorlib.messages_pb2 import PassphraseAck +from trezorlib.messages_pb2 import PassphraseAck, PinMatrixAck from trezorlib.transport_bridge import BridgeTransport from trezorlib.transport_hid import HidTransport from trezorlib.types_pb2 import IdentityType