From 76daf9da60343866dc735ccc67b708969833a9ea Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Wed, 27 Nov 2024 12:53:04 +0100 Subject: [PATCH] test: try fix pin callback --- python/src/trezorlib/debuglink.py | 36 +++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/python/src/trezorlib/debuglink.py b/python/src/trezorlib/debuglink.py index c671595782..7920d08ea9 100644 --- a/python/src/trezorlib/debuglink.py +++ b/python/src/trezorlib/debuglink.py @@ -32,8 +32,13 @@ from pathlib import Path from mnemonic import Mnemonic from . import btc, mapping, messages, models, protobuf -from .client import MAX_PASSPHRASE_LENGTH, PASSPHRASE_ON_DEVICE, TrezorClient -from .exceptions import Cancelled, TrezorFailure +from .client import ( + MAX_PASSPHRASE_LENGTH, + MAX_PIN_LENGTH, + PASSPHRASE_ON_DEVICE, + TrezorClient, +) +from .exceptions import Cancelled, PinException, TrezorFailure from .log import DUMP_BYTES from .messages import Capability, DebugWaitType from .tools import expect, parse_path @@ -1354,6 +1359,33 @@ class TrezorClientDebugLink(TrezorClient): return _callback_button + @property + def pin_callback(self): + + def _callback_pin(session: Session, msg: messages.PinMatrixRequest) -> t.Any: + try: + pin = self.ui.get_pin(msg.type) + except Cancelled: + session.call_raw(messages.Cancel()) + raise + + if any(d not in "123456789" for d in pin) or not ( + 1 <= len(pin) <= MAX_PIN_LENGTH + ): + session.call_raw(messages.Cancel()) + raise ValueError("Invalid PIN provided") + resp = session.call_raw(messages.PinMatrixAck(pin=pin)) + if isinstance(resp, messages.Failure) and resp.code in ( + messages.FailureType.PinInvalid, + messages.FailureType.PinCancelled, + messages.FailureType.PinExpected, + ): + raise PinException(resp.code, resp.message) + else: + return resp + + return _callback_pin + @property def passphrase_callback(self): def _callback_passphrase(