forked from Concordium/concordium-ledger-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sign_cmd.py
342 lines (302 loc) · 13.9 KB
/
test_sign_cmd.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import pytest
from application_client.boilerplate_transaction import Transaction
from application_client.boilerplate_command_sender import (
BoilerplateCommandSender,
Errors,
InsType,
)
from application_client.boilerplate_response_unpacker import (
unpack_get_public_key_response,
unpack_sign_tx_response,
)
from ragger.error import ExceptionRAPDU
from ragger.navigator import NavInsID
from utils import navigate_until_text_and_compare, instructions_builder, split_message
MAX_SCHEDULE_PAIRS_IN_ONE_APDU: int = (250 // 16) * 16
MAX_APDU_LEN: int = 255
# In this test we send to the device a transaction to sign and validate it on screen
# The transaction is short and will be sent in one chunk
# We will ensure that the displayed information is correct by using screenshots comparison
@pytest.mark.active_test_scope
def test_sign_tx_simple_transfer_legacy_path(
backend, firmware, navigator, default_screenshot_path, test_name
):
# Use the app interface instead of raw interface
client = BoilerplateCommandSender(backend)
# The path used for this entire test
path: str = "m/1105/0/0/0/0/2/0/0"
# Create the transaction that will be sent to the device for signing
transaction = "20a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7000000000000000a0000000000000064000000290000000063de5da70320a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7ffffffffffffffff"
transaction = bytes.fromhex(transaction)
# Send the sign device instruction.
# As it requires on-screen validation, the function is asynchronous.
# It will yield the result when the navigation is done
with client.sign_simple_transfer(path=path, transaction=transaction):
# Validate the on-screen request by performing the navigation appropriate for this device
navigate_until_text_and_compare(
firmware, navigator, "Sign", default_screenshot_path, test_name
)
# The device as yielded the result, parse it and ensure that the signature is correct
response = client.get_async_response().data
response_hex = response.hex()
print("response", response_hex)
assert (
response_hex
== "d1617ee706805c0bc6a43260ece93a7ceba37aaefa303251cf19bdcbbe88c0a3d3878dcb965cdb88ff380fdb1aa4b321671f365d7258e878d18fa1b398a1a10f"
)
# assert check_signature_validity(public_key, der_sig, transaction)
# In this test we send to the device a transaction to sign and validate it on screen
# The transaction is short and will be sent in one chunk
# We will ensure that the displayed information is correct by using screenshots comparison
@pytest.mark.active_test_scope
def test_sign_tx_simple_transfer_new_path(
backend, firmware, navigator, default_screenshot_path, test_name
):
# Use the app interface instead of raw interface
client = BoilerplateCommandSender(backend)
# The path used for this entire test
path: str = "m/44/919/0/0/0/0"
# Create the transaction that will be sent to the device for signing
transaction = "20a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7000000000000000a0000000000000064000000290000000063de5da70320a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7ffffffffffffffff"
transaction = bytes.fromhex(transaction)
# Send the sign device instruction.
# As it requires on-screen validation, the function is asynchronous.
# It will yield the result when the navigation is done
with client.sign_simple_transfer(path=path, transaction=transaction):
# Validate the on-screen request by performing the navigation appropriate for this device
navigate_until_text_and_compare(
firmware, navigator, "Sign", default_screenshot_path, test_name
)
# The device as yielded the result, parse it and ensure that the signature is correct
response = client.get_async_response().data
response_hex = response.hex()
print("response", response_hex)
assert (
response_hex
== "e5f112237d58f908c44385827e71048869db7e8f513e2ceb5da6a6370e2088f4371f93d6e08f9f6c1dd92c74fe565727b8f81600541e817d35cfeec4cc3bc408"
)
# In this test we send to the device a transaction to sign and validate it on screen
# The transaction is short and will be sent in one chunk
# We will ensure that the displayed information is correct by using screenshots comparison
@pytest.mark.active_test_scope
def test_sign_tx_simple_transfer_with_memo_legacy_path(
backend, firmware, navigator, default_screenshot_path, test_name
):
# Use the app interface instead of raw interface
client = BoilerplateCommandSender(backend)
# The path used for this entire test
path: str = "m/1105/0/0/0/0/2/0/0"
# Create the transaction that will be sent to the device for signing
header_and_to_address = "20a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7000000000000000a0000000000000064000000290000000063de5da71620a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7"
header_and_to_address = bytes.fromhex(header_and_to_address)
memo = "6474657374"
memo = bytes.fromhex(memo)
amount = "ffffffffffffffff"
amount = bytes.fromhex(amount)
# Send the sign device instruction.
# As it requires on-screen validation, the function is asynchronous.
# It will yield the result when the navigation is done
with client.sign_simple_transfer_with_memo(
path=path,
header_and_to_address=header_and_to_address,
memo=memo,
amount=amount,
):
# Validate the on-screen request by performing the navigation appropriate for this device
navigate_until_text_and_compare(
firmware, navigator, "Sign", default_screenshot_path, test_name
)
# The device as yielded the result, parse it and ensure that the signature is correct
response = client.get_async_response().data
response_hex = response.hex()
print("response", response_hex)
# TODO: verify the signature
assert (
response_hex
== "a588094eef4ed6053df2ab4b851bc5ec09b311c204d2fa94a9c7d7c8feebf74de26d2d2a547f18c4e959b24388394305ebd3dca99653de1cb1aa689bb6674207"
)
# assert check_signature_validity(public_key, der_sig, transaction)
@pytest.mark.active_test_scope
def test_sign_tx_transfer_with_schedule_legacy_path(
backend, firmware, navigator, default_screenshot_path, test_name
):
# Initialize the command sender client
client = BoilerplateCommandSender(backend)
# Define the path for the transaction
path = "m/1105/0/0/0/0/2/0/0"
# Create the transaction that will be sent to the device for signing
header_and_to_address = "20a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7000000000000000a0000000000000064000000290000000063de5da71320a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7"
header_and_to_address = bytes.fromhex(header_and_to_address)
# Define the schedule pairs
pairs = [
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
]
joined_pairs = bytes.fromhex("".join(pairs))
# Ensure pairs are a multiple of 16 bytes
if len(joined_pairs) % 16 != 0:
raise ValueError("Pairs must be a multiple of 16 bytes")
# Split the pairs into chunks for APDU transmission
pairs_chunk = split_message(joined_pairs, MAX_SCHEDULE_PAIRS_IN_ONE_APDU)
# Send the first part of the transaction signing request
with client.sign_tx_with_schedule_part_1(
path=path,
header_and_to_address=header_and_to_address,
num_pairs=len(pairs),
):
# Navigate and compare screenshots for validation
if firmware.is_nano:
navigator.navigate_and_compare(
default_screenshot_path,
test_name,
instructions_builder(5, backend),
10,
True,
False,
)
else:
navigate_until_text_and_compare(
firmware,
navigator,
"Continue",
default_screenshot_path,
test_name,
True,
False,
NavInsID.USE_CASE_CHOICE_CONFIRM,
)
# Process each chunk of pairs
screenshots_so_far = 6 if firmware.is_nano else 3
for chunk in pairs_chunk:
nbgl_confirm_instruction = NavInsID.USE_CASE_CHOICE_CONFIRM
number_of_pairs_in_chunk = len(chunk) // 16
# Create the instructions to validate each pair
instructions = []
for _ in range(number_of_pairs_in_chunk):
if _ == number_of_pairs_in_chunk - 1:
nbgl_confirm_instruction = NavInsID.USE_CASE_REVIEW_CONFIRM
instructions.extend(
instructions_builder(2, backend, nbgl_confirm_instruction)
)
# Send the second part of the transaction signing request
with client.sign_tx_with_schedule_part_2(data=chunk):
# Navigate and compare screenshots for validation
navigator.navigate_and_compare(
default_screenshot_path,
test_name,
instructions,
10,
True,
True,
screenshots_so_far,
)
screenshots_so_far += number_of_pairs_in_chunk * 3
# The device as yielded the result, parse it and ensure that the signature is correct
response = client.get_async_response().data
response_hex = response.hex()
print("km------------response", response_hex)
# TODO: verify the signature
assert (
response_hex
== "e22fa38f78a79db71e84376c4eec2382166cdc412994207e7631b0ba3828f069b17b6f30351a64c50e5efacec3fe25161e9f7131e0235cd740739b24e0b06308"
)
@pytest.mark.active_test_scope
def test_sign_tx_transfer_with_schedule_and_memo_legacy_path(
backend, firmware, navigator, default_screenshot_path, test_name
):
# Initialize the command sender client
client = BoilerplateCommandSender(backend)
# Define the path for the transaction
path = "m/1105/0/0/0/0/2/0/0"
# Create the transaction that will be sent to the device for signing
header_and_to_address = "20a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7000000000000000a0000000000000064000000290000000063de5da71820a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7"
header_and_to_address = bytes.fromhex(header_and_to_address)
# Define the memo
memo = "6474657374" # "test" in hex
memo = bytes.fromhex(memo)
memo_chunks = split_message(memo, MAX_APDU_LEN)
# Define the schedule pairs
pairs = [
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
]
joined_pairs = bytes.fromhex("".join(pairs))
# Ensure pairs are a multiple of 16 bytes
if len(joined_pairs) % 16 != 0:
raise ValueError("Pairs must be a multiple of 16 bytes")
# Split the pairs into chunks for APDU transmission
pairs_chunk = split_message(joined_pairs, MAX_SCHEDULE_PAIRS_IN_ONE_APDU)
# Send the first part of the transaction signing request
response = client.sign_tx_with_schedule_and_memo_part_1(
path=path,
header_and_to_address=header_and_to_address,
num_pairs=len(pairs),
memo_length=len(memo),
)
print("km------------response", response)
assert response.status == 0x9000
# Send the part with the memo
for chunk in memo_chunks:
with client.sign_tx_with_schedule_and_memo_part_2(memo_chunk=chunk):
# Navigate and compare screenshots for validation
if firmware.is_nano:
navigator.navigate_and_compare(
default_screenshot_path,
test_name,
instructions_builder(6, backend),
10,
True,
False,
)
else:
navigate_until_text_and_compare(
firmware,
navigator,
"Continue",
default_screenshot_path,
test_name,
True,
False,
NavInsID.USE_CASE_CHOICE_CONFIRM,
)
# Process each chunk of pairs
screenshots_so_far = 7 if firmware.is_nano else 3
for chunk in pairs_chunk:
nbgl_confirm_instruction = NavInsID.USE_CASE_CHOICE_CONFIRM
number_of_pairs_in_chunk = len(chunk) // 16
# Create the instructions to validate each pair
instructions = []
for _ in range(number_of_pairs_in_chunk):
if _ == number_of_pairs_in_chunk - 1:
nbgl_confirm_instruction = NavInsID.USE_CASE_REVIEW_CONFIRM
instructions.extend(
instructions_builder(2, backend, nbgl_confirm_instruction)
)
# Send the second part of the transaction signing request
with client.sign_tx_with_schedule_part_2(data=chunk):
# Navigate and compare screenshots for validation
navigator.navigate_and_compare(
default_screenshot_path,
test_name,
instructions,
10,
True,
True,
screenshots_so_far,
)
screenshots_so_far += number_of_pairs_in_chunk * 3
# The device as yielded the result, parse it and ensure that the signature is correct
response = client.get_async_response().data
response_hex = response.hex()
print("km------------response", response_hex)
# TODO: verify the signature
assert (
response_hex
== "9056db36dfa7b0ba722660b2becb227ed490dcaff9e332a7fba4c6d534ff0ff3368b21da8e7ebb62891be561261abd7c0435dfb46e596b1116c9996269d2a70b"
)