Skip to content

Commit

Permalink
Pass 'state' during TREZOR initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
romanz committed Mar 27, 2018
1 parent 5efb752 commit 7443fc6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions libagent/device/keepkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def _defs(self):

required_version = '>=1.0.4'

def _override_state_handler(self, _):
"""No support for `state` handling on Keepkey."""

def pubkey(self, identity, ecdh=False):
"""Return public key."""
_verify_support(identity, ecdh)
Expand Down
15 changes: 13 additions & 2 deletions libagent/device/trezor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def new_handler(_):
conn.callback_PinMatrixRequest = new_handler

cached_passphrase_ack = None
cached_state = None

def _override_passphrase_handler(self, conn):
if self.ui is None:
Expand Down Expand Up @@ -77,6 +78,14 @@ def new_handler(msg):

conn.callback_PassphraseRequest = new_handler

def _override_state_handler(self, conn):
def callback_PassphraseStateRequest(msg):
log.debug('caching state from %r', msg)
self.__class__.cached_state = msg.state
return self._defs.PassphraseStateAck()

conn.callback_PassphraseStateRequest = callback_PassphraseStateRequest

def _verify_version(self, connection):
f = connection.features
log.debug('connected to %s %s', self, f.device_id)
Expand All @@ -100,10 +109,12 @@ def connect(self):
raise interface.NotFoundError('{} not connected'.format(self))

log.debug('transports: %s', transports)
for _ in range(5):
connection = self._defs.Client(transports[0])
for _ in range(5): # Retry a few times in case of PIN failures
connection = self._defs.Client(transport=transports[0],
state=self.__class__.cached_state)
self._override_pin_handler(connection)
self._override_passphrase_handler(connection)
self._override_state_handler(connection)
self._verify_version(connection)

try:
Expand Down
2 changes: 1 addition & 1 deletion libagent/device/trezor_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from trezorlib.client import CallException, PinException
from trezorlib.client import TrezorClient as Client
from trezorlib.messages import IdentityType, PassphraseAck, PinMatrixAck
from trezorlib.messages import IdentityType, PassphraseAck, PinMatrixAck, PassphraseStateAck
from trezorlib.device import TrezorDevice


Expand Down

0 comments on commit 7443fc6

Please sign in to comment.