-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathtest_sign_psbt_v1.py
318 lines (254 loc) · 13.6 KB
/
test_sign_psbt_v1.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
# Tests using the V1 version of the wallet policy language, used before version 2.1.0 of the app
# Make sure we remain compatible for some time.
from pathlib import Path
from ledger_bitcoin import WalletPolicy, MultisigWallet, AddressType, WalletType, PartialSignature
from ledger_bitcoin.psbt import PSBT
from ledger_bitcoin.wallet import AddressType
from test_utils import bip0340
from ragger.navigator import Navigator
from ragger.firmware import Firmware
from ragger_bitcoin import RaggerClient
from .instructions import *
tests_root: Path = Path(__file__).parent
def open_psbt_from_file(filename: str) -> PSBT:
raw_psbt_base64 = open(filename, "r").read()
psbt = PSBT()
psbt.deserialize(raw_psbt_base64)
return psbt
def test_sign_psbt_singlesig_pkh_1to1_v1(navigator: Navigator, firmware: Firmware, client:
RaggerClient, test_name: str):
# PSBT for a legacy 1-input 1-output spend (no change address)
psbt = open_psbt_from_file(f"{tests_root}/psbt/singlesig/pkh-1to1.psbt")
wallet = WalletPolicy(
"",
"pkh(@0)",
[
"[f5acc2fd/44'/1'/0']tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT/**"
],
version=WalletType.WALLET_POLICY_V1
)
# expected sigs:
# #0:
# "pubkey" : "02ee8608207e21028426f69e76447d7e3d5e077049f5e683c3136c2314762a4718",
# "signature" : "3045022100e55b3ca788721aae8def2eadff710e524ffe8c9dec1764fdaa89584f9726e196022012a30fbcf9e1a24df31a1010356b794ab8de438b4250684757ed5772402540f401"
result = client.sign_psbt(psbt, wallet, None, navigator,
instructions=sign_psbt_instruction_approve(
firmware),
testname=test_name)
assert result == [(
0,
PartialSignature(
pubkey=bytes.fromhex(
"02ee8608207e21028426f69e76447d7e3d5e077049f5e683c3136c2314762a4718"),
signature=bytes.fromhex(
"3045022100e55b3ca788721aae8def2eadff710e524ffe8c9dec1764fdaa89584f9726e196022012a30fbcf9e1a24df31a1010356b794ab8de438b4250684757ed5772402540f401"
)
)
)]
def test_sign_psbt_singlesig_sh_wpkh_1to2_v1(navigator: Navigator, firmware: Firmware, client:
RaggerClient, test_name: str):
# PSBT for a wrapped segwit 1-input 2-output spend (1 change address)
psbt = open_psbt_from_file(
f"{tests_root}/psbt/singlesig/sh-wpkh-1to2.psbt")
wallet = WalletPolicy(
"",
"sh(wpkh(@0))",
[
"[f5acc2fd/49'/1'/0']tpubDC871vGLAiKPcwAw22EjhKVLk5L98UGXBEcGR8gpcigLQVDDfgcYW24QBEyTHTSFEjgJgbaHU8CdRi9vmG4cPm1kPLmZhJEP17FMBdNheh3/**"
],
version=WalletType.WALLET_POLICY_V1
)
# expected sigs:
# #0:
# "pubkey" : "024ba3b77d933de9fa3f9583348c40f3caaf2effad5b6e244ece8abbfcc7244f67",
# "signature" : "30440220720722b08489c2a50d10edea8e21880086c8e8f22889a16815e306daeea4665b02203fcf453fa490b76cf4f929714065fc90a519b7b97ab18914f9451b5a4b45241201"
result = client.sign_psbt(psbt, wallet, None, navigator,
instructions=sign_psbt_instruction_approve(
firmware),
testname=test_name)
assert result == [(
0,
PartialSignature(
pubkey=bytes.fromhex(
"024ba3b77d933de9fa3f9583348c40f3caaf2effad5b6e244ece8abbfcc7244f67"),
signature=bytes.fromhex(
"30440220720722b08489c2a50d10edea8e21880086c8e8f22889a16815e306daeea4665b02203fcf453fa490b76cf4f929714065fc90a519b7b97ab18914f9451b5a4b45241201"
)
)
)]
def test_sign_psbt_singlesig_wpkh_1to2_v1(navigator: Navigator, firmware: Firmware, client:
RaggerClient, test_name: str):
# PSBT for a legacy 1-input 2-output spend (1 change address)
psbt = open_psbt_from_file(f"{tests_root}/psbt/singlesig/wpkh-1to2.psbt")
wallet = WalletPolicy(
"",
"wpkh(@0)",
[
"[f5acc2fd/84'/1'/0']tpubDCtKfsNyRhULjZ9XMS4VKKtVcPdVDi8MKUbcSD9MJDyjRu1A2ND5MiipozyyspBT9bg8upEp7a8EAgFxNxXn1d7QkdbL52Ty5jiSLcxPt1P/**"
],
version=WalletType.WALLET_POLICY_V1
)
result = client.sign_psbt(psbt, wallet, None, navigator,
instructions=sign_psbt_instruction_approve(
firmware),
testname=test_name)
# expected sigs
# #0:
# "pubkey" : "03ee2c3d98eb1f93c0a1aa8e5a4009b70eb7b44ead15f1666f136b012ad58d3068",
# "signature" : "3045022100ab44f34dd7e87c9054591297a101e8500a0641d1d591878d0d23cf8096fa79e802205d12d1062d925e27b57bdcf994ecf332ad0a8e67b8fe407bab2101255da632aa01"
assert result == [(
0,
PartialSignature(
pubkey=bytes.fromhex(
"03ee2c3d98eb1f93c0a1aa8e5a4009b70eb7b44ead15f1666f136b012ad58d3068"),
signature=bytes.fromhex(
"3045022100ab44f34dd7e87c9054591297a101e8500a0641d1d591878d0d23cf8096fa79e802205d12d1062d925e27b57bdcf994ecf332ad0a8e67b8fe407bab2101255da632aa01"
)
)
)]
def test_sign_psbt_singlesig_wpkh_2to2_v1(navigator: Navigator, firmware: Firmware, client:
RaggerClient, test_name: str):
# PSBT for a legacy 2-input 2-output spend (1 change address)
psbt = open_psbt_from_file(f"{tests_root}/psbt/singlesig/wpkh-2to2.psbt")
wallet = WalletPolicy(
"",
"wpkh(@0)",
[
"[f5acc2fd/84'/1'/0']tpubDCtKfsNyRhULjZ9XMS4VKKtVcPdVDi8MKUbcSD9MJDyjRu1A2ND5MiipozyyspBT9bg8upEp7a8EAgFxNxXn1d7QkdbL52Ty5jiSLcxPt1P/**"
],
version=WalletType.WALLET_POLICY_V1
)
result = client.sign_psbt(psbt, wallet, None, navigator,
instructions=sign_psbt_instruction_approve(
firmware),
testname=test_name)
# expected sigs
# #0:
# "pubkey" : "03455ee7cedc97b0ba435b80066fc92c963a34c600317981d135330c4ee43ac7a3",
# "signature" : "304402206b3e877655f08c6e7b1b74d6d893a82cdf799f68a5ae7cecae63a71b0339e5ce022019b94aa3fb6635956e109f3d89c996b1bfbbaf3c619134b5a302badfaf52180e01"
# #1:
# "pubkey" : "0271b5b779ad870838587797bcf6f0c7aec5abe76a709d724f48d2e26cf874f0a0",
# "signature" : "3045022100e2e98e4f8c70274f10145c89a5d86e216d0376bdf9f42f829e4315ea67d79d210220743589fd4f55e540540a976a5af58acd610fa5e188a5096dfe7d36baf3afb94001"
assert result == [(
0,
PartialSignature(
pubkey=bytes.fromhex(
"03455ee7cedc97b0ba435b80066fc92c963a34c600317981d135330c4ee43ac7a3"),
signature=bytes.fromhex(
"304402206b3e877655f08c6e7b1b74d6d893a82cdf799f68a5ae7cecae63a71b0339e5ce022019b94aa3fb6635956e109f3d89c996b1bfbbaf3c619134b5a302badfaf52180e01"
)
)
), (
1,
PartialSignature(
pubkey=bytes.fromhex(
"0271b5b779ad870838587797bcf6f0c7aec5abe76a709d724f48d2e26cf874f0a0"),
signature=bytes.fromhex(
"3045022100e2e98e4f8c70274f10145c89a5d86e216d0376bdf9f42f829e4315ea67d79d210220743589fd4f55e540540a976a5af58acd610fa5e188a5096dfe7d36baf3afb94001"
)
)
)]
def test_sign_psbt_multisig_wsh_v1(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
wallet = MultisigWallet(
name="Cold storage",
address_type=AddressType.WIT,
threshold=2,
keys_info=[
f"[76223a6e/48'/1'/0'/2']tpubDE7NQymr4AFtewpAsWtnreyq9ghkzQBXpCZjWLFVRAvnbf7vya2eMTvT2fPapNqL8SuVvLQdbUbMfWLVDCZKnsEBqp6UK93QEzL8Ck23AwF/**",
f"[f5acc2fd/48'/1'/0'/2']tpubDFAqEGNyad35aBCKUAXbQGDjdVhNueno5ZZVEn3sQbW5ci457gLR7HyTmHBg93oourBssgUxuWz1jX5uhc1qaqFo9VsybY1J5FuedLfm4dK/**",
],
version=WalletType.WALLET_POLICY_V1
)
wallet_hmac = bytes.fromhex(
"d6434852fb3caa7edbd1165084968f1691444b3cfc10cf1e431acbbc7f48451f"
)
psbt = open_psbt_from_file(f"{tests_root}/psbt/multisig/wsh-2of2.psbt")
# fees don't fit in the same page on 'flex', but they fit on 'stax'
fees_on_next_page = firmware.name == 'flex'
result = client.sign_psbt(psbt, wallet, wallet_hmac, navigator,
instructions=sign_psbt_instruction_approve(
firmware, has_spend_from_wallet=True, fees_on_next_page=fees_on_next_page),
testname=test_name)
assert result == [(
0,
PartialSignature(
pubkey=bytes.fromhex(
"036b16e8c1f979fa4cc0f05b6a300affff941459b6f20de77de55b0160ef8e4cac"),
signature=bytes.fromhex(
"304402206ab297c83ab66e573723892061d827c5ac0150e2044fed7ed34742fedbcfb26e0220319cdf4eaddff63fc308cdf53e225ea034024ef96de03fd0939b6deeea1e8bd301"
)
)
)]
def test_sign_psbt_taproot_1to2_v1(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
# PSBT for a p2tr 1-input 2-output spend (1 change address)
psbt = open_psbt_from_file(
f"{tests_root}/psbt/singlesig/tr-1to2-sighash-all.psbt")
wallet = WalletPolicy(
"",
"tr(@0)",
[
"[f5acc2fd/86'/1'/0']tpubDDKYE6BREvDsSWMazgHoyQWiJwYaDDYPbCFjYxN3HFXJP5fokeiK4hwK5tTLBNEDBwrDXn8cQ4v9b2xdW62Xr5yxoQdMu1v6c7UDXYVH27U/**"
],
version=WalletType.WALLET_POLICY_V1
)
result = client.sign_psbt(psbt, wallet, None, navigator,
instructions=sign_psbt_instruction_approve(
firmware),
testname=test_name)
assert len(result) == 1
# Unlike other transactions, Schnorr signatures are not deterministic (unless the randomness is removed)
# Therefore, for this testcase we hard-code the sighash (which was validated with Bitcoin Core 22.0 when the
# transaction was sent), and we verify the produced Schnorr signature with the reference bip340 implementation.
# sighash verified with bitcoin-core
sighash0 = bytes.fromhex(
"7A999E5AD6F53EA6448E7026061D3B4523F957999C430A5A492DFACE74AE31B6")
# get the (tweaked) pubkey from the scriptPubKey
pubkey0_psbt = psbt.inputs[0].witness_utxo.scriptPubKey[2:]
idx0, partial_sig0 = result[0]
assert idx0 == 0
assert partial_sig0.pubkey == pubkey0_psbt
# the sighash 0x01 is appended to the signature
assert len(partial_sig0.signature) == 64+1
assert partial_sig0.signature[-1] == 0x01
assert bip0340.schnorr_verify(
sighash0, pubkey0_psbt, partial_sig0.signature[:-1])
def test_sign_psbt_with_opreturn_v1(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
wallet = WalletPolicy(
"",
"wpkh(@0)",
[
"[f5acc2fd/84'/1'/0']tpubDCtKfsNyRhULjZ9XMS4VKKtVcPdVDi8MKUbcSD9MJDyjRu1A2ND5MiipozyyspBT9bg8upEp7a8EAgFxNxXn1d7QkdbL52Ty5jiSLcxPt1P/**"
],
version=WalletType.WALLET_POLICY_V1
)
psbt_b64 = "cHNidP8BAKMCAAAAAZ0gZDu3l28lrZWbtsuoIfI07zpsaXXMe6sMHHJn03LPAAAAAAD+////AgAAAAAAAAAASGpGVGhlIFRpbWVzIDAzL0phbi8yMDA5IENoYW5jZWxsb3Igb24gYnJpbmsgb2Ygc2Vjb25kIGJhaWxvdXQgZm9yIGJhbmtzLsGVmAAAAAAAFgAUK5M/aeXrJEofBL7Uno7J5OyTvJ8AAAAAAAEAcQIAAAABnpp88I3RXEU5b28rI3GGAXaWkk+w1sEqWDXFXdacKg8AAAAAAP7///8CgJaYAAAAAAAWABQTR+gqA3tduzjPjEdZ8kKx9cfgmvNabSkBAAAAFgAUCA6eZPSQK9gnq8ngOSaQ0ZdPeIVBAAAAAQEfgJaYAAAAAAAWABQTR+gqA3tduzjPjEdZ8kKx9cfgmiIGAny3XTSwBcTrn2K78sRX12OOgT51fvzsj6aGd9lQtjZiGPWswv1UAACAAQAAgAAAAIAAAAAAAAAAAAAAIgIDGZuJ2DVvV+HOOAoSBc8oYG2+qJhVsRw9/s+4oaUzVokY9azC/VQAAIABAACAAAAAgAEAAAABAAAAAA=="
psbt = PSBT()
psbt.deserialize(psbt_b64)
# to and amount fit on the same page on stax, but not on flex
to_on_next_page = firmware.name == 'flex'
hww_sigs = client.sign_psbt(psbt, wallet, None, navigator,
instructions=sign_psbt_instruction_approve(
firmware, to_on_next_page=to_on_next_page, fees_on_next_page=True),
testname=test_name)
assert len(hww_sigs) == 1
def test_sign_psbt_with_segwit_v16_v1(navigator: Navigator, firmware: Firmware, client:
RaggerClient, test_name: str):
# This psbt contains an output with future psbt version 16 (corresponding to address
# tb1sqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq4hu3px).
# The app should accept it nonetheless.
psbt_b64 = "cHNidP8BAH0CAAAAAZvg4s1Yxz9DddwBeI+qqU7hcldqGSgWPXuZZReEFYvKAAAAAAD+////AqdTiQAAAAAAFgAUK5M/aeXrJEofBL7Uno7J5OyTvJ9AQg8AAAAAACJgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAHECAAAAAYWOSCXXmA0ztidPI5A6FskW99o7nWNVeFP7rXND5B9aAAAAAAD+////AoCWmAAAAAAAFgAUE0foKgN7Xbs4z4xHWfJCsfXH4JrzWm0pAQAAABYAFF7XSHCIZoptcIrXIWce1tKqp11EaQAAAAEBH4CWmAAAAAAAFgAUE0foKgN7Xbs4z4xHWfJCsfXH4JoiBgJ8t100sAXE659iu/LEV9djjoE+dX787I+mhnfZULY2Yhj1rML9VAAAgAEAAIAAAACAAAAAAAAAAAAAIgIDGZuJ2DVvV+HOOAoSBc8oYG2+qJhVsRw9/s+4oaUzVokY9azC/VQAAIABAACAAAAAgAEAAAABAAAAAAA="
psbt = PSBT()
psbt.deserialize(psbt_b64)
wallet = WalletPolicy(
"",
"wpkh(@0)",
[
"[f5acc2fd/84'/1'/0']tpubDCtKfsNyRhULjZ9XMS4VKKtVcPdVDi8MKUbcSD9MJDyjRu1A2ND5MiipozyyspBT9bg8upEp7a8EAgFxNxXn1d7QkdbL52Ty5jiSLcxPt1P/**"
],
version=WalletType.WALLET_POLICY_V1
)
hww_sigs = client.sign_psbt(psbt, wallet, None, navigator,
instructions=sign_psbt_instruction_approve(
firmware),
testname=test_name)
assert len(hww_sigs) == 1