support trezorlib 0.12

This commit is contained in:
matejcik
2020-04-09 14:41:56 +02:00
parent 67ef11419a
commit d8bcca3ccb
4 changed files with 28 additions and 16 deletions

View File

@@ -11,7 +11,7 @@ setup(
scripts=['trezor_agent.py'], scripts=['trezor_agent.py'],
install_requires=[ install_requires=[
'libagent>=0.13.0', 'libagent>=0.13.0',
'trezor[hidapi]>=0.11.0' 'trezor[hidapi]>=0.12.0,<0.13'
], ],
platforms=['POSIX'], platforms=['POSIX'],
classifiers=[ classifiers=[
@@ -22,8 +22,6 @@ setup(
'Intended Audience :: System Administrators', 'Intended Audience :: System Administrators',
'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)', 'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)',
'Operating System :: POSIX', 'Operating System :: POSIX',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.6',
'Topic :: Software Development :: Libraries :: Python Modules', 'Topic :: Software Development :: Libraries :: Python Modules',

View File

@@ -26,7 +26,7 @@ class Trezor(interface.Device):
required_version = '>=1.4.0' required_version = '>=1.4.0'
ui = None # can be overridden by device's users ui = None # can be overridden by device's users
cached_state = None cached_session_id = None
def _verify_version(self, connection): def _verify_version(self, connection):
f = connection.features f = connection.features
@@ -54,11 +54,14 @@ class Trezor(interface.Device):
for _ in range(5): # Retry a few times in case of PIN failures for _ in range(5): # Retry a few times in case of PIN failures
connection = self._defs.Client(transport=transport, connection = self._defs.Client(transport=transport,
ui=self.ui, ui=self.ui,
state=self.__class__.cached_state) session_id=self.__class__.cached_session_id)
self._verify_version(connection) self._verify_version(connection)
try: try:
connection.ping(msg='', pin_protection=True) # unlock PIN # unlock PIN and passphrase
self._defs.get_address(connection,
"Testnet",
self._defs.PASSPHRASE_TEST_PATH)
return connection return connection
except (self._defs.PinException, ValueError) as e: except (self._defs.PinException, ValueError) as e:
log.error('Invalid PIN: %s, retrying...', e) log.error('Invalid PIN: %s, retrying...', e)
@@ -70,7 +73,7 @@ class Trezor(interface.Device):
def close(self): def close(self):
"""Close connection.""" """Close connection."""
self.__class__.cached_state = self.conn.state self.__class__.cached_session_id = self.conn.session_id
super().close() super().close()
def pubkey(self, identity, ecdh=False): def pubkey(self, identity, ecdh=False):

View File

@@ -8,12 +8,12 @@ import mnemonic
import semver import semver
import trezorlib import trezorlib
from trezorlib.client import TrezorClient as Client from trezorlib.client import TrezorClient as Client, PASSPHRASE_TEST_PATH
from trezorlib.exceptions import TrezorFailure, PinException from trezorlib.exceptions import TrezorFailure, PinException
from trezorlib.transport import get_transport from trezorlib.transport import get_transport
from trezorlib.messages import IdentityType from trezorlib.messages import IdentityType
from trezorlib.btc import get_public_node from trezorlib.btc import get_address, get_public_node
from trezorlib.misc import sign_identity, get_ecdh_session_key from trezorlib.misc import sign_identity, get_ecdh_session_key
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@@ -4,6 +4,11 @@ import logging
import os import os
import subprocess import subprocess
try:
from trezorlib.client import PASSPHRASE_ON_DEVICE
except Exception:
PASSPHRASE_ON_DEVICE = object()
from .. import util from .. import util
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -40,12 +45,18 @@ class UI:
binary=self.pin_entry_binary, binary=self.pin_entry_binary,
options=self.options_getter()) options=self.options_getter())
def get_passphrase(self, prompt='Passphrase:'): def get_passphrase(self, prompt='Passphrase:', available_on_device=False):
"""Ask the user for passphrase.""" """Ask the user for passphrase."""
passphrase = None passphrase = None
if self.cached_passphrase_ack: if self.cached_passphrase_ack:
passphrase = self.cached_passphrase_ack.get() passphrase = self.cached_passphrase_ack.get()
if passphrase is None: if passphrase is None:
env_passphrase = os.environ.get("TREZOR_PASSPHRASE")
if env_passphrase is not None:
passphrase = env_passphrase
elif available_on_device:
passphrase = PASSPHRASE_ON_DEVICE
else:
passphrase = interact( passphrase = interact(
title='{} passphrase'.format(self.device_name), title='{} passphrase'.format(self.device_name),
prompt=prompt, prompt=prompt,