1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-11 16:00:57 +00:00

client: proto -> messages

This commit is contained in:
matejcik 2018-11-13 16:17:04 +01:00
parent 4f9bdff564
commit 3dda5e6534

View File

@ -18,7 +18,7 @@ import logging
import sys import sys
import warnings import warnings
from . import exceptions, messages as proto, tools from . import exceptions, messages, tools
if sys.version_info.major < 3: if sys.version_info.major < 3:
raise Exception("Trezorlib does not support Python 2 anymore.") raise Exception("Trezorlib does not support Python 2 anymore.")
@ -40,8 +40,8 @@ def get_buttonrequest_value(code):
# Converts integer code to its string representation of ButtonRequestType # Converts integer code to its string representation of ButtonRequestType
return [ return [
k k
for k in dir(proto.ButtonRequestType) for k in dir(messages.ButtonRequestType)
if getattr(proto.ButtonRequestType, k) == code if getattr(messages.ButtonRequestType, k) == code
][0] ][0]
@ -73,7 +73,7 @@ class TrezorClient:
self.session_counter -= 1 self.session_counter -= 1
def cancel(self): def cancel(self):
self._raw_write(proto.Cancel()) self._raw_write(messages.Cancel())
def call_raw(self, msg): def call_raw(self, msg):
__tracebackhide__ = True # for pytest # pylint: disable=W0612 __tracebackhide__ = True # for pytest # pylint: disable=W0612
@ -93,11 +93,11 @@ class TrezorClient:
if not pin.isdigit(): if not pin.isdigit():
raise ValueError("Non-numeric PIN provided") raise ValueError("Non-numeric PIN provided")
resp = self.call_raw(proto.PinMatrixAck(pin=pin)) resp = self.call_raw(messages.PinMatrixAck(pin=pin))
if isinstance(resp, proto.Failure) and resp.code in ( if isinstance(resp, messages.Failure) and resp.code in (
proto.FailureType.PinInvalid, messages.FailureType.PinInvalid,
proto.FailureType.PinCancelled, messages.FailureType.PinCancelled,
proto.FailureType.PinExpected, messages.FailureType.PinExpected,
): ):
raise exceptions.PinException(resp.code, resp.message) raise exceptions.PinException(resp.code, resp.message)
else: else:
@ -109,17 +109,17 @@ class TrezorClient:
else: else:
passphrase = self.ui.get_passphrase() passphrase = self.ui.get_passphrase()
resp = self.call_raw(proto.PassphraseAck(passphrase=passphrase)) resp = self.call_raw(messages.PassphraseAck(passphrase=passphrase))
if isinstance(resp, proto.PassphraseStateRequest): if isinstance(resp, messages.PassphraseStateRequest):
self.state = resp.state self.state = resp.state
return self.call_raw(proto.PassphraseStateAck()) return self.call_raw(messages.PassphraseStateAck())
else: else:
return resp return resp
def _callback_button(self, msg): def _callback_button(self, msg):
__tracebackhide__ = True # for pytest # pylint: disable=W0612 __tracebackhide__ = True # for pytest # pylint: disable=W0612
# do this raw - send ButtonAck first, notify UI later # do this raw - send ButtonAck first, notify UI later
self._raw_write(proto.ButtonAck()) self._raw_write(messages.ButtonAck())
self.ui.button_request(msg.code) self.ui.button_request(msg.code)
return self._raw_read() return self._raw_read()
@ -127,14 +127,14 @@ class TrezorClient:
def call(self, msg): def call(self, msg):
resp = self.call_raw(msg) resp = self.call_raw(msg)
while True: while True:
if isinstance(resp, proto.PinMatrixRequest): if isinstance(resp, messages.PinMatrixRequest):
resp = self._callback_pin(resp) resp = self._callback_pin(resp)
elif isinstance(resp, proto.PassphraseRequest): elif isinstance(resp, messages.PassphraseRequest):
resp = self._callback_passphrase(resp) resp = self._callback_passphrase(resp)
elif isinstance(resp, proto.ButtonRequest): elif isinstance(resp, messages.ButtonRequest):
resp = self._callback_button(resp) resp = self._callback_button(resp)
elif isinstance(resp, proto.Failure): elif isinstance(resp, messages.Failure):
if resp.code == proto.FailureType.ActionCancelled: if resp.code == messages.FailureType.ActionCancelled:
raise exceptions.Cancelled raise exceptions.Cancelled
raise exceptions.TrezorFailure(resp) raise exceptions.TrezorFailure(resp)
else: else:
@ -142,8 +142,8 @@ class TrezorClient:
@tools.session @tools.session
def init_device(self): def init_device(self):
resp = self.call_raw(proto.Initialize(state=self.state)) resp = self.call_raw(messages.Initialize(state=self.state))
if not isinstance(resp, proto.Features): if not isinstance(resp, messages.Features):
raise exceptions.TrezorException("Unexpected initial response") raise exceptions.TrezorException("Unexpected initial response")
else: else:
self.features = resp self.features = resp
@ -153,7 +153,7 @@ class TrezorClient:
# If the `vendor` field doesn't exist, you probably have a mismatched # If the `vendor` field doesn't exist, you probably have a mismatched
# checkout of trezor-common. # checkout of trezor-common.
@tools.expect(proto.Success, field="message") @tools.expect(messages.Success, field="message")
def ping( def ping(
self, self,
msg, msg,
@ -161,7 +161,7 @@ class TrezorClient:
pin_protection=False, pin_protection=False,
passphrase_protection=False, passphrase_protection=False,
): ):
msg = proto.Ping( msg = messages.Ping(
message=msg, message=msg,
button_protection=button_protection, button_protection=button_protection,
pin_protection=pin_protection, pin_protection=pin_protection,
@ -172,9 +172,9 @@ class TrezorClient:
def get_device_id(self): def get_device_id(self):
return self.features.device_id return self.features.device_id
@tools.expect(proto.Success, field="message") @tools.expect(messages.Success, field="message")
def clear_session(self): def clear_session(self):
return self.call_raw(proto.ClearSession()) return self.call_raw(messages.ClearSession())
def MovedTo(where): def MovedTo(where):