forked from LedgerHQ/app-bitcoin-new
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_get_extended_pubkey.py
244 lines (191 loc) · 9.48 KB
/
test_get_extended_pubkey.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
import threading
import pytest
from bitcoin_client.ledger_bitcoin import Client
from bitcoin_client.ledger_bitcoin.exception import DenyError, NotSupportedError
from speculos.client import SpeculosClient
def test_get_extended_pubkey_standard_display(client: Client, comm: SpeculosClient, is_speculos:
bool, model: str):
testcases = {
"m/44'/1'/0'": "tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT",
"m/44'/1'/10'": "tpubDCwYjpDhUdPGp21gSpVay2QPJVh6WNySWMXPhbcu1DsxH31dF7mY18oibbu5RxCLBc1Szerjscuc3D5HyvfYqfRvc9mesewnFqGmPjney4d",
"m/44'/1'/2'/1/42": "tpubDGF9YgHKv6qh777rcqVhpmDrbNzgophJM9ec7nHiSfrbss7fVBXoqhmZfohmJSvhNakDHAspPHjVVNL657tLbmTXvSeGev2vj5kzjMaeupT",
"m/48'/1'/4'/1'/0/7": "tpubDK8WPFx4WJo1R9mEL7Wq325wBiXvkAe8ipgb9Q1QBDTDUD2YeCfutWtzY88NPokZqJyRPKHLGwTNLT7jBG59aC6VH8q47LDGQitPB6tX2d7",
"m/49'/1'/1'/1/3": "tpubDGnetmJDCL18TyaaoyRAYbkSE9wbHktSdTS4mfsR6inC8c2r6TjdBt3wkqEQhHYPtXpa46xpxDaCXU2PRNUGVvDzAHPG6hHRavYbwAGfnFr",
"m/84'/1'/2'/0/10": "tpubDG9YpSUwScWJBBSrhnAT47NcT4NZGLcY18cpkaiWHnkUCi19EtCh8Heeox268NaFF6o56nVeSXuTyK6jpzTvV1h68Kr3edA8AZp27MiLUNt",
"m/86'/1'/4'/1/12": "tpubDHTZ815MvTaRmo6Qg1rnU6TEU4ZkWyA56jA1UgpmMcBGomnSsyo34EZLoctzZY9MTJ6j7bhccceUeXZZLxZj5vgkVMYfcZ7DNPsyRdFpS3f",
}
if not is_speculos:
pytest.skip("Requires speculos")
def ux_thread():
event = comm.wait_for_text_event("Confirm public key")
# press right until the last screen (will press the "right" button more times than needed)
while "Reject" != event["text"]:
comm.press_and_release("right")
event = comm.get_next_event()
# go back to the Accept screen, then accept
comm.press_and_release("left")
comm.press_and_release("both")
def ux_thread_stax():
event = comm.get_next_event()
while "Approve" not in event["text"]:
if "Tap to continue" in event["text"]:
comm.finger_touch(55, 550)
event = comm.get_next_event()
comm.finger_touch(55, 550)
for path, pubkey in testcases.items():
if model == "stax":
x = threading.Thread(target=ux_thread_stax)
else:
x = threading.Thread(target=ux_thread)
x.start()
assert pubkey == client.get_extended_pubkey(
path=path,
display=True
)
x.join()
def test_get_extended_pubkey_standard_nodisplay(client: Client):
testcases = {
"m/44'/1'/0'": "tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT",
"m/44'/1'/10'": "tpubDCwYjpDhUdPGp21gSpVay2QPJVh6WNySWMXPhbcu1DsxH31dF7mY18oibbu5RxCLBc1Szerjscuc3D5HyvfYqfRvc9mesewnFqGmPjney4d",
"m/44'/1'/2'/1/42": "tpubDGF9YgHKv6qh777rcqVhpmDrbNzgophJM9ec7nHiSfrbss7fVBXoqhmZfohmJSvhNakDHAspPHjVVNL657tLbmTXvSeGev2vj5kzjMaeupT",
"m/48'/1'/4'/1'/0/7": "tpubDK8WPFx4WJo1R9mEL7Wq325wBiXvkAe8ipgb9Q1QBDTDUD2YeCfutWtzY88NPokZqJyRPKHLGwTNLT7jBG59aC6VH8q47LDGQitPB6tX2d7",
"m/49'/1'/1'/1/3": "tpubDGnetmJDCL18TyaaoyRAYbkSE9wbHktSdTS4mfsR6inC8c2r6TjdBt3wkqEQhHYPtXpa46xpxDaCXU2PRNUGVvDzAHPG6hHRavYbwAGfnFr",
"m/84'/1'/2'/0/10": "tpubDG9YpSUwScWJBBSrhnAT47NcT4NZGLcY18cpkaiWHnkUCi19EtCh8Heeox268NaFF6o56nVeSXuTyK6jpzTvV1h68Kr3edA8AZp27MiLUNt",
"m/86'/1'/4'/1/12": "tpubDHTZ815MvTaRmo6Qg1rnU6TEU4ZkWyA56jA1UgpmMcBGomnSsyo34EZLoctzZY9MTJ6j7bhccceUeXZZLxZj5vgkVMYfcZ7DNPsyRdFpS3f",
# support up to 8 steps
"m/86'/1'/4'/1/2/3/4/5": "tpubDNcjumrTe1BBYEc1FmMaJZQw47mbvb4LfX4YwqC6GQ18PfMfuH3BEYREfdHm2gWXkSJ3JiXHF11iKnbJxzxp5qkgo8BBy2L48FRvrLhpTuh",
# the following two paths test compatibility with Unchained Capital's multisig setup
"m/45'/1'/0'": "tpubDCy2BKyxJFzACNgThkunvdnkHNotREK9LQDw8L9J1gx26SyzfoeJynJgWekzkramggmahVAgeHPxfpnvFtJ7hcYADrsVUnsPSei2tY9fBLL",
"m/45'/1'/0'/1": "tpubDFGDxRGdGFKekUtPuta4p9Kw2a2PSeyyhSTa7KNENJfBuJ78EEsL1LxwAA8ddSxZFWBT9gYRuLDoa2rwdix56WRsq77vAJ2iqeyPw6UBeyt",
}
for path, pubkey in testcases.items():
assert pubkey == client.get_extended_pubkey(
path=path,
display=False
)
def test_get_extended_pubkey_nonstandard_nodisplay(client: Client):
# as these paths are not standard, the app should reject immediately if display=False
testcases = [
"m", # unusual to export the root key
"m/44'",
"m/44'/1'",
"m/44'/10'/0'", # wrong coin_type
"m/44'/1'/0", # first step should be hardened
"m/44'/1/0'", # second step should be hardened
"m/44/1'/0'", # third step should be hardened
"m/48'/1'/0'/0'", # script_type is 1' or 2' for BIP-0048
"m/48'/1'/0'/3'", # script_type is 1' or 2' for BIP-0048
"m/999'/1'/0'", # no standard with this purpose
]
for path in testcases:
with pytest.raises(NotSupportedError):
client.get_extended_pubkey(
path=path,
display=False
)
def test_get_extended_pubkey_non_standard(client: Client, comm: SpeculosClient, is_speculos: bool,
model: str):
# Test the successful UX flow for a non-standard path (here, root path)
# (Slow test, not feasible to repeat it for many paths)
if not is_speculos:
pytest.skip("Requires speculos")
def ux_thread():
event = comm.wait_for_text_event("path is unusual")
# press right until the last screen (will press the "right" button more times than needed)
while "Reject" != event["text"]:
comm.press_and_release("right")
event = comm.get_next_event()
# go back to the Accept screen, then accept
comm.press_and_release("left")
comm.press_and_release("both")
def ux_thread_stax():
event = comm.get_next_event()
while "Approve" not in event["text"]:
if "Tap to continue" in event["text"] or "Warning" in event["text"]:
comm.finger_touch(55, 550)
event = comm.get_next_event()
comm.finger_touch(55, 550)
if model == "stax":
x = threading.Thread(target=ux_thread_stax)
else:
x = threading.Thread(target=ux_thread)
x.start()
pub_key = client.get_extended_pubkey(
path="m", # root pubkey
display=True
)
x.join()
assert pub_key == "tpubD6NzVbkrYhZ4YgUx2ZLNt2rLYAMTdYysCRzKoLu2BeSHKvzqPaBDvf17GeBPnExUVPkuBpx4kniP964e2MxyzzazcXLptxLXModSVCVEV1T"
def test_get_extended_pubkey_non_standard_reject_early(client: Client, comm: SpeculosClient,
is_speculos: bool, model: str):
# Test rejecting after the "Reject if you're not sure" warning
# (Slow test, not feasible to repeat it for many paths)
if not is_speculos:
pytest.skip("Requires speculos")
def ux_thread():
comm.wait_for_text_event("path is unusual")
comm.press_and_release("right")
comm.wait_for_text_event("Confirm public key")
comm.press_and_release("right")
comm.wait_for_text_event("111'/222'/333'")
comm.press_and_release("right")
comm.wait_for_text_event("not sure") # second line of "Reject if you're not sure"
comm.press_and_release("both")
def ux_thread_stax():
comm.wait_for_text_event("Tap to continue")
comm.finger_touch(55, 550)
comm.wait_for_text_event("Tap to continue")
comm.finger_touch(55, 550)
comm.wait_for_text_event("Tap to continue")
comm.finger_touch(55, 550)
comm.wait_for_text_event("Tap to continue")
comm.finger_touch(55, 550)
comm.wait_for_text_event("Approve")
comm.finger_touch(55, 650)
if model == "stax":
x = threading.Thread(target=ux_thread_stax)
else:
x = threading.Thread(target=ux_thread)
x.start()
with pytest.raises(DenyError):
client.get_extended_pubkey(
path="m/111'/222'/333'",
display=True
)
x.join()
def test_get_extended_pubkey_non_standard_reject(client: Client, comm: SpeculosClient, is_speculos:
bool, model: str):
# Test rejecting at the end
# (Slow test, not feasible to repeat it for many paths)
if not is_speculos:
pytest.skip("Requires speculos")
def ux_thread():
event = comm.wait_for_text_event("path is unusual")
# press right until the last screen (will press the "right" button more times than needed)
while "Reject" != event["text"]:
comm.press_and_release("right")
event = comm.get_next_event()
# finally, reject
comm.press_and_release("both")
def ux_thread_stax():
comm.wait_for_text_event("Tap to continue")
comm.finger_touch(55, 550)
comm.wait_for_text_event("Tap to continue")
comm.finger_touch(55, 550)
comm.wait_for_text_event("Tap to continue")
comm.finger_touch(55, 550)
comm.wait_for_text_event("Tap to continue")
comm.finger_touch(55, 550)
comm.wait_for_text_event("Approve")
comm.finger_touch(55, 650)
if model == "stax":
x = threading.Thread(target=ux_thread_stax)
else:
x = threading.Thread(target=ux_thread)
x.start()
with pytest.raises(DenyError):
client.get_extended_pubkey(
path="m/111'/222'/333'",
display=True
)
x.join()