-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sign_tx_cmd.py
More file actions
170 lines (136 loc) · 6.12 KB
/
Copy pathtest_sign_tx_cmd.py
File metadata and controls
170 lines (136 loc) · 6.12 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
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
import os
import pytest
from application_client.stellar_command_sender import Errors, StellarCommandSender
from ragger.error import ExceptionRAPDU
from stellar_sdk import Keypair
from stellar_sdk.utils import sha256
from utils import (
ROOT_SCREENSHOT_PATH,
SettingsId,
configure_device_settings,
get_testcases_names,
handle_risk_warning,
)
from dataset import MNEMONIC, SignTxTestCases
@pytest.mark.parametrize("test_name", get_testcases_names(SignTxTestCases))
def test_sign_tx(backend, scenario_navigator, navigator, device, test_name):
keypair = Keypair.from_mnemonic_phrase(MNEMONIC, index=0)
path = "m/44'/148'/0'"
transaction = getattr(SignTxTestCases, test_name)()
client = StellarCommandSender(backend)
signature_base = transaction.signature_base()
if "op_invoke_host_function" in test_name:
# For host function transactions, enable blind signing
configure_device_settings(
navigator,
device,
SettingsId.ENABLE_BLIND_SIGNING,
)
# 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_tx(path=path, transaction=signature_base):
if "op_invoke_host_function" in test_name:
# For host function transactions, handle the risk warning
handle_risk_warning(navigator, device)
# Validate the on-screen request by performing the navigation appropriate for this device
scenario_navigator.review_approve(
ROOT_SCREENSHOT_PATH,
test_name=f"test_sign_tx_{test_name}",
custom_screen_text="Sign ",
)
response = client.get_async_response().data
expected_signature = keypair.sign(sha256(transaction.signature_base()))
assert response == expected_signature
@pytest.mark.parametrize(
"test_name", get_testcases_names(SignTxTestCases, filter="cond_")
)
def test_sign_tx_with_precondition_enabled(
backend, scenario_navigator, device, navigator, test_name
):
keypair = Keypair.from_mnemonic_phrase(MNEMONIC, index=0)
path = "m/44'/148'/0'"
transaction = getattr(SignTxTestCases, test_name)()
client = StellarCommandSender(backend)
signature_base = transaction.signature_base()
configure_device_settings(navigator, device, SettingsId.ENABLE_PRECONDITION)
with client.sign_tx(path=path, transaction=signature_base):
scenario_navigator.review_approve(
ROOT_SCREENSHOT_PATH,
test_name=f"test_sign_tx_with_precondition_enabled_{test_name}",
custom_screen_text="Sign ",
)
response = client.get_async_response().data
expected_signature = keypair.sign(sha256(transaction.signature_base()))
assert response == expected_signature
def test_sign_tx_with_sequence_enabled(backend, scenario_navigator, device, navigator):
keypair = Keypair.from_mnemonic_phrase(MNEMONIC, index=0)
path = "m/44'/148'/0'"
transaction = SignTxTestCases.op_payment_asset_native()
client = StellarCommandSender(backend)
signature_base = transaction.signature_base()
configure_device_settings(navigator, device, SettingsId.ENABLE_SEQUENCE_AND_NONCE)
with client.sign_tx(path=path, transaction=signature_base):
scenario_navigator.review_approve(
ROOT_SCREENSHOT_PATH,
custom_screen_text="Sign ",
)
response = client.get_async_response().data
expected_signature = keypair.sign(sha256(transaction.signature_base()))
assert response == expected_signature
def test_sign_tx_with_tx_source_enabled(backend, scenario_navigator, device, navigator):
keypair = Keypair.from_mnemonic_phrase(MNEMONIC, index=1)
path = "m/44'/148'/1'"
transaction = SignTxTestCases.op_payment_asset_native()
client = StellarCommandSender(backend)
signature_base = transaction.signature_base()
configure_device_settings(navigator, device, SettingsId.ENABLE_TRANSACTION_SOURCE)
with client.sign_tx(path=path, transaction=signature_base):
scenario_navigator.review_approve(
ROOT_SCREENSHOT_PATH,
custom_screen_text="Sign ",
)
response = client.get_async_response().data
expected_signature = keypair.sign(sha256(transaction.signature_base()))
assert response == expected_signature
def test_sign_tx_with_nested_authorization_disabled(
backend, scenario_navigator, device, navigator
):
keypair = Keypair.from_mnemonic_phrase(MNEMONIC, index=0)
path = "m/44'/148'/0'"
transaction = SignTxTestCases.op_invoke_host_function_with_complex_sub_invocation()
client = StellarCommandSender(backend)
signature_base = transaction.signature_base()
configure_device_settings(
navigator,
device,
SettingsId.DISABLE_NESTED_AUTHORIZATION | SettingsId.ENABLE_BLIND_SIGNING,
)
with client.sign_tx(path=path, transaction=signature_base):
handle_risk_warning(navigator, device)
scenario_navigator.review_approve(
ROOT_SCREENSHOT_PATH,
custom_screen_text="Sign ",
)
response = client.get_async_response().data
expected_signature = keypair.sign(sha256(transaction.signature_base()))
assert response == expected_signature
def test_sign_tx_reject(backend, scenario_navigator):
path = "m/44'/148'/0'"
transaction = SignTxTestCases.op_payment_asset_native()
client = StellarCommandSender(backend)
with pytest.raises(ExceptionRAPDU) as e:
with client.sign_tx(path=path, transaction=transaction.signature_base()):
scenario_navigator.review_reject(ROOT_SCREENSHOT_PATH)
# Assert that we have received a refusal
assert e.value.status == Errors.SW_DENY
assert len(e.value.data) == 0
def test_sign_tx_data_too_large(backend):
path = "m/44'/148'/0'"
transaction = os.urandom(1024 * 10) # 10 KB transaction
client = StellarCommandSender(backend)
with pytest.raises(ExceptionRAPDU) as e:
with client.sign_tx(path=path, transaction=transaction):
pass
assert e.value.status == Errors.SW_REQUEST_DATA_TOO_LARGE
assert len(e.value.data) == 0