forked from LedgerHQ/app-aptos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pubkey_cmd.py
More file actions
106 lines (94 loc) · 5.17 KB
/
Copy pathtest_pubkey_cmd.py
File metadata and controls
106 lines (94 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pytest
from application_client.aptos_command_sender import AptosCommandSender, Errors
from application_client.aptos_response_unpacker import unpack_get_public_key_response
from ragger.bip import calculate_public_key_and_chaincode, CurveChoice
from ragger.error import ExceptionRAPDU
from ragger.navigator import NavInsID, NavIns
from utils import ROOT_SCREENSHOT_PATH
# In this test we check that the GET_PUBLIC_KEY works in non-confirmation mode
def test_get_public_key_no_confirm(backend):
for path in ["m/44'/637'/1'/0'/0'", "m/44'/637'/0'/0'/1'", "m/44'/637'/255'/255'/255'", "m/44'/637'/2147483647'/0'/0'/0'/0'/0'/0'/0'"]:
client = AptosCommandSender(backend)
response = client.get_public_key(path=path).data
_, public_key, _, chain_code = unpack_get_public_key_response(response)
ref_public_key, ref_chain_code = calculate_public_key_and_chaincode(CurveChoice.Ed25519Slip, path=path)
ref_public_key_bytes = bytearray.fromhex(ref_public_key)
ref_public_key_bytes[0] = 0x04 # Set the first byte to 0x04 to indicate that it is uncompressed
assert public_key.hex() == ref_public_key_bytes.hex()
assert chain_code.hex() == ref_chain_code
# In this test we check that the GET_PUBLIC_KEY works in confirmation mode
def test_get_public_key_confirm_accepted(firmware, backend, navigator, test_name):
client = AptosCommandSender(backend)
path = "m/44'/637'/1'/0'/0'"
with client.get_public_key_with_confirmation(path=path):
if firmware.device.startswith("nano"):
navigator.navigate_until_text_and_compare(NavInsID.RIGHT_CLICK,
[NavInsID.BOTH_CLICK],
"Approve",
ROOT_SCREENSHOT_PATH,
test_name)
else:
if firmware.device == "flex":
qr_coordinates = (80, 430)
elif firmware.device == "stax":
qr_coordinates = (70, 530)
elif firmware.device == "apex_p":
qr_coordinates = (43, 299)
instructions = [
NavInsID.USE_CASE_VIEW_DETAILS_NEXT,
NavIns(NavInsID.TOUCH, qr_coordinates),
NavInsID.USE_CASE_ADDRESS_CONFIRMATION_EXIT_QR,
NavInsID.USE_CASE_ADDRESS_CONFIRMATION_CONFIRM,
NavInsID.USE_CASE_STATUS_DISMISS
]
navigator.navigate_and_compare(ROOT_SCREENSHOT_PATH,
test_name,
instructions)
response = client.get_async_response().data
_, public_key, _, chain_code = unpack_get_public_key_response(response)
ref_public_key, ref_chain_code = calculate_public_key_and_chaincode(CurveChoice.Ed25519Slip, path=path)
ref_public_key_bytes = bytearray.fromhex(ref_public_key)
ref_public_key_bytes[0] = 0x04 # Set the first byte to 0x04 to indicate that it is uncompressed
print(f"MUTABLE DATA: {ref_public_key_bytes.hex()}")
print(f"Public key: {public_key.hex()}")
print(f"Ref public key: {ref_public_key}")
print(f"Chain code: {chain_code.hex()}")
print(f"Ref chain code: {ref_chain_code}")
assert public_key.hex() == ref_public_key_bytes.hex()
assert chain_code.hex() == ref_chain_code
# In this test we check that the GET_PUBLIC_KEY in confirmation mode replies an error if the user refuses
def test_get_public_key_confirm_refused(firmware, backend, navigator, test_name):
client = AptosCommandSender(backend)
path = "m/44'/637'/1'/0'/0'"
if firmware.device.startswith("nano"):
with pytest.raises(ExceptionRAPDU) as e:
with client.get_public_key_with_confirmation(path=path):
navigator.navigate_until_text_and_compare(NavInsID.RIGHT_CLICK,
[NavInsID.BOTH_CLICK],
"Reject",
ROOT_SCREENSHOT_PATH,
test_name)
# Assert that we have received a refusal
assert e.value.status == Errors.SW_DENY
assert len(e.value.data) == 0
else:
instructions_set = [
[
NavInsID.USE_CASE_REVIEW_REJECT,
NavInsID.USE_CASE_STATUS_DISMISS
],
[
NavInsID.USE_CASE_VIEW_DETAILS_NEXT,
NavInsID.USE_CASE_ADDRESS_CONFIRMATION_CANCEL,
NavInsID.USE_CASE_STATUS_DISMISS
]
]
for i, instructions in enumerate(instructions_set):
with pytest.raises(ExceptionRAPDU) as e:
with client.get_public_key_with_confirmation(path=path):
navigator.navigate_and_compare(ROOT_SCREENSHOT_PATH,
test_name + f"/part{i}",
instructions)
# Assert that we have received a refusal
assert e.value.status == Errors.SW_DENY
assert len(e.value.data) == 0