import pytest from ragger.bip import calculate_public_key_and_chaincode, CurveChoice from ragger.error import ExceptionRAPDU from application_client.handshake_command_sender import Errors, HandshakeCommandSender from application_client.handshake_response_unpacker import ( compress_pubkey, unpack_get_public_key_response, ) PATHS = [ "m/44'/5353'/0'/0/0", "m/44'/5353'/0'/1/7", "m/44'/5353'/99'/0/0", "m/44'/5353'/0'", "m/44'/5354'/0'/0/0", ] @pytest.mark.parametrize("path", PATHS) def test_get_public_key_no_confirm(backend, path): client = HandshakeCommandSender(backend) pubkey, chaincode, address = unpack_get_public_key_response( client.get_public_key(path=path).data) ref_pubkey, ref_chaincode = calculate_public_key_and_chaincode( CurveChoice.Secp256k1, path=path) assert pubkey == compress_pubkey(bytes.fromhex(ref_pubkey)) assert chaincode.hex() == ref_chaincode # Handshake is bech32 (never bech32m), one HRP per network. expected_hrp = "ts" if "5354'" in path else "hs" assert address.startswith(expected_hrp + "1") def test_get_public_key_confirm_accepted(backend, scenario_navigator): client = HandshakeCommandSender(backend) path = "m/44'/5353'/0'/0/0" with client.get_public_key_with_confirmation(path=path): scenario_navigator.address_review_approve() pubkey, _, address = unpack_get_public_key_response( client.get_async_response().data) ref_pubkey, _ = calculate_public_key_and_chaincode(CurveChoice.Secp256k1, path=path) assert pubkey == compress_pubkey(bytes.fromhex(ref_pubkey)) assert address.startswith("hs1") def test_get_public_key_confirm_refused(backend, scenario_navigator): client = HandshakeCommandSender(backend) with pytest.raises(ExceptionRAPDU) as e: with client.get_public_key_with_confirmation(path="m/44'/5353'/0'/0/0"): scenario_navigator.address_review_reject() assert e.value.status == Errors.SW_DENY assert len(e.value.data) == 0