1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-04-26 12:09:02 +00:00

fixup! chore: handle cancel in pairing and credential flows

This commit is contained in:
M1nd3r 2025-04-15 16:39:19 +02:00
parent 582c62f149
commit 54c67997da
2 changed files with 36 additions and 2 deletions

View File

@ -39,6 +39,8 @@ if __debug__:
if TYPE_CHECKING:
from typing import Any, Callable, Concatenate, ParamSpec, Tuple
from trezorui_api import UiResult
P = ParamSpec("P")
FuncWithContext = Callable[Concatenate[PairingContext, P], Any]
@ -91,6 +93,7 @@ async def handle_pairing_request(
) -> ThpEndResponse:
if Cancel.is_type_of(message):
ctx.channel_ctx.set_channel_state(ChannelState.TP0)
raise ActionCancelled()
if not ThpPairingRequest.is_type_of(message):
@ -123,14 +126,22 @@ async def handle_pairing_request(
await _prepare_pairing(ctx)
ctx.channel_ctx.set_channel_state(ChannelState.TP3)
result: UiResult | None = None
try:
# Should raise UnexpectedMessageException
await ctx.show_pairing_method_screen()
result = await ctx.show_pairing_method_screen()
except UnexpectedMessageException as e:
raw_response = e.msg
req_type = protobuf.type_for_wire(raw_response.type)
response = message_handler.wrap_protobuf_load(raw_response.data, req_type)
if result is not None:
from trezorui_api import CONFIRMED
if result is CONFIRMED:
# TODO In UI Screen has one button with text "Cancel", but is gets confirmed
ctx.channel_ctx.set_channel_state(ChannelState.TP0)
raise ActionCancelled
if Cancel.is_type_of(response):
ctx.channel_ctx.clear()
raise SilentError("Action was cancelled by the Host")

View File

@ -150,6 +150,29 @@ def test_pairing_code_entry(client: Client) -> None:
protocol._has_valid_channel = True
def test_pairing_code_entry_cancel(client: Client) -> None:
protocol = prepare_protocol_for_pairing(client)
handle_pairing_request(client, protocol, "TestTrezor CodeEntry")
protocol._send_message(
ThpSelectMethod(selected_pairing_method=ThpPairingMethod.CodeEntry)
)
_ = protocol._read_message(ThpCodeEntryCommitment)
challenge = os.urandom(16)
protocol._send_message(ThpCodeEntryChallenge(challenge=challenge))
protocol._read_message(ThpCodeEntryCpaceTrezor)
# Code Entry code shown
# Press Cancel button
client.debug.press_yes()
failure = protocol._read_message(Failure)
assert failure.code is FailureType.ActionCancelled
def test_pairing_cancel_1(client: Client) -> None:
protocol = prepare_protocol_for_pairing(client)
@ -360,7 +383,7 @@ def test_credential_phase(client: Client) -> None:
client.debug.press_yes()
# Autoconnect issuance confirmation dialog is shown.
button_req = protocol._read_message(ButtonRequest)
assert button_req.name == "autoconnect_credential_request"
assert button_req.name == "thp_autoconnect_credential_request"
protocol._send_message(ButtonAck())
client.debug.press_yes()
# Autoconnect credential is received