1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-24 23:38:09 +00:00

client: convert generic classname-based dispatch to a static list

Only a limited number of messages should be dispatched to handlers
that can be inserted anywhere in the protocol flow. Having a fixed list
of interjecting handlers makes this clearer and prevents hard-to-find
bugs.
This commit is contained in:
matejcik 2018-11-13 16:05:49 +01:00
parent 786bccfa34
commit f3f521b028

View File

@ -114,7 +114,7 @@ class BaseClient(object):
__tracebackhide__ = True # for pytest # pylint: disable=W0612 __tracebackhide__ = True # for pytest # pylint: disable=W0612
return self.transport.read() return self.transport.read()
def callback_PinMatrixRequest(self, msg): def _callback_pin(self, msg):
pin = self.ui.get_pin(msg.type) pin = self.ui.get_pin(msg.type)
if not pin.isdigit(): if not pin.isdigit():
raise ValueError("Non-numeric PIN provided") raise ValueError("Non-numeric PIN provided")
@ -129,7 +129,7 @@ class BaseClient(object):
else: else:
return resp return resp
def callback_PassphraseRequest(self, msg): def _callback_passphrase(self, msg):
if msg.on_device: if msg.on_device:
passphrase = None passphrase = None
else: else:
@ -142,7 +142,7 @@ class BaseClient(object):
else: else:
return resp return resp
def callback_ButtonRequest(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(proto.ButtonAck())
@ -153,23 +153,19 @@ class BaseClient(object):
def call(self, msg): def call(self, msg):
resp = self.call_raw(msg) resp = self.call_raw(msg)
while True: while True:
handler_name = "callback_{}".format(resp.__class__.__name__) if isinstance(resp, proto.PinMatrixRequest):
handler = getattr(self, handler_name, None) resp = self._callback_pin(resp)
if handler is None: elif isinstance(resp, proto.PassphraseRequest):
break resp = self._callback_passphrase(resp)
resp = handler(resp) # pylint: disable=E1102 elif isinstance(resp, proto.ButtonRequest):
resp = self._callback_button(resp)
if isinstance(resp, proto.Failure): elif isinstance(resp, proto.Failure):
if resp.code == proto.FailureType.ActionCancelled: if resp.code == proto.FailureType.ActionCancelled:
raise exceptions.Cancelled raise exceptions.Cancelled
raise exceptions.TrezorFailure(resp) raise exceptions.TrezorFailure(resp)
else:
return resp return resp
def register_message(self, msg):
"""Allow application to register custom protobuf message type"""
mapping.register_message(msg)
class ProtocolMixin(object): class ProtocolMixin(object):
VENDORS = ("bitcointrezor.com", "trezor.io") VENDORS = ("bitcointrezor.com", "trezor.io")