Merge #152: Keep Trezor sessions open unless state becomes inconsistent

d00ae56 Don't send Initialize when first connecting to a Trezor (Andrew Chow)

Pull request description:

  When the client is opened, we would send an `Initialize` message. However because a client may end up being opened many times in the process of executing one command, the saved state (in particular cached passphrases) would be lost each time the client is opened due to `Initialize` being sent. In order to have it keep that state, we don't want to send `Initialize` every time. Instead we will send `GetFeatures` which does the other part of what `Initialize` does (get the features for the device). `GetFeatures` failing would indicate that the state is inconsistent (which can be caused by command abort, exceptions, etc.), so in that case, `Initialize` is sent to clear the inconsistent state.

  I tested this using the Trezor T emulator and the password prompt does not appear multiple times per command. Additionally the password prompt does not appear for every command.

  Fixes #151

ACKs for commit d00ae5:

Tree-SHA512: 76bde488bf9a486267cc0c84668db79e9f1706f10f4d12d093829bcf1a5f713a7aec9da3eae674b152071d3d5c7219c163fe01465dea9e02f1e56298d318058a
This commit is contained in:
Andrew Chow 2019-05-03 16:45:22 -04:00
commit c14896c6eb
No known key found for this signature in database
GPG Key ID: 17565732E08E5E41

View File

@ -178,7 +178,10 @@ class TrezorClient:
@tools.session
def init_device(self):
resp = self.call_raw(messages.Initialize(state=self.state))
resp = self.call_raw(messages.GetFeatures())
# If GetFeatures fails, try initializing and clearing inconsistent state on the device
if isinstance(resp, messages.Failure):
resp = self.call_raw(messages.Initialize())
if not isinstance(resp, messages.Features):
raise exceptions.TrezorException("Unexpected initial response")
else: