Debian Codegraph @

tests/test_lnutil.py

raw symbols SCIP
1 import os
2 import json
3 from typing import Dict, List
4
5 from electrum import bitcoin
6 from electrum.json_db import StoredDict
7 from electrum.lnutil import (
8 RevocationStore, get_per_commitment_secret_from_seed, make_offered_htlc, make_received_htlc, make_commitment,
9 make_htlc_tx_witness, make_htlc_tx_output, make_htlc_tx_inputs, secret_to_pubkey, derive_blinded_pubkey,
10 derive_privkey, derive_pubkey, make_htlc_tx, extract_ctn_from_tx, get_compressed_pubkey_from_bech32,
11 ScriptHtlc, calc_fees_for_commitment_tx, UpdateAddHtlc, LnFeatures, ln_compare_features,
12 IncompatibleLightningFeatures, ChannelType, offered_htlc_trim_threshold_sat, received_htlc_trim_threshold_sat,
13 ImportedChannelBackupStorage, list_enabled_ln_feature_bits, PaymentFeeBudget, LnFeatureContexts
14 )
15 from electrum.util import bfh, MyEncoder
16 from electrum.transaction import Transaction, PartialTransaction, Sighash
17 from electrum.lnworker import LNWallet
18 from electrum.wallet import Standard_Wallet
19 from electrum.simple_config import SimpleConfig
20
21 from . import ElectrumTestCase, as_testnet
22 from . import restore_wallet_from_text__for_unittest
23 from .test_bitcoin import disable_ecdsa_r_value_grinding
24
25
26 # test vectors for a single channel
27 # https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#appendix-c-commitment-and-htlc-transaction-test-vectors
28 funding_tx_id = '8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be'
29 funding_output_index = 0
30 funding_amount_satoshi = 10000000
31 commitment_number = 42
32 local_delay = 144
33 local_dust_limit_satoshi = 546
34
35 local_payment_basepoint = bytes.fromhex('034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa')
36 remote_payment_basepoint = bytes.fromhex('032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991')
37 # obs = get_obscured_ctn(42, local_payment_basepoint, remote_payment_basepoint)
38 local_funding_privkey = bytes.fromhex('30ff4956bbdd3222d44cc5e8a1261dab1e07957bdac5ae88fe3261ef321f374901')
39 local_funding_pubkey = bytes.fromhex('023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb')
40 remote_funding_pubkey = bytes.fromhex('030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c1')
41 local_privkey = bytes.fromhex('bb13b121cdc357cd2e608b0aea294afca36e2b34cf958e2e6451a2f27469449101')
42 localpubkey = bytes.fromhex('030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e7')
43 remotepubkey = bytes.fromhex('0394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b')
44 local_delayedpubkey = bytes.fromhex('03fd5960528dc152014952efdb702a88f71e3c1653b2314431701ec77e57fde83c')
45 local_revocation_pubkey = bytes.fromhex('0212a140cd0c6539d07cd08dfe09984dec3251ea808b892efeac3ede9402bf2b19')
46 # funding wscript = 5221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae
47
48
49 # anchor test vectors are from https://github.com/lightningnetwork/lightning-rfc/commit/1739746afa3863ca783df9be4b7b0338afb63b49
50 anchor_test_vector_path = os.path.join(os.path.dirname(__file__), "anchor-vectors.json")
51 with open(anchor_test_vector_path) as f:
52 ANCHOR_TEST_VECTORS = json.load(f)
53
54 # in a commitment transaction with all the below htlcs, the order is different,
55 # indices 1 and 2 are swapped
56 TEST_HTLCS = [
57 {
58 'incoming': True,
59 'amount': 1000000,
60 'expiry': 500,
61 'preimage': "0000000000000000000000000000000000000000000000000000000000000000",
62 },
63 {
64 'incoming': True,
65 'amount': 2000000,
66 'expiry': 501,
67 'preimage': "0101010101010101010101010101010101010101010101010101010101010101",
68 },
69 {
70 'incoming': False,
71 'amount': 2000000,
72 'expiry': 502,
73 'preimage': "0202020202020202020202020202020202020202020202020202020202020202",
74 },
75 {
76 'incoming': False,
77 'amount': 3000000,
78 'expiry': 503,
79 'preimage': "0303030303030303030303030303030303030303030303030303030303030303",
80 },
81 {
82 'incoming': True,
83 'amount': 4000000,
84 'expiry': 504,
85 'preimage': "0404040404040404040404040404040404040404040404040404040404040404",
86 }
87 ]
88
89
90 class TestLNUtil(ElectrumTestCase):
91 def test_shachain_store(self):
92 tests = [
93 {
94 "name": "insert_secret correct sequence",
95 "inserts": [
96 {
97 "index": 281474976710655,
98 "secret": "7cc854b54e3e0dcdb010d7a3fee464a9687b" +\
99 "e6e8db3be6854c475621e007a5dc",
100 "successful": True
101 },
102 {
103 "index": 281474976710654,
104 "secret": "c7518c8ae4660ed02894df8976fa1a3659c1" +\
105 "a8b4b5bec0c4b872abeba4cb8964",
106 "successful": True
107 },
108 {
109 "index": 281474976710653,
110 "secret": "2273e227a5b7449b6e70f1fb4652864038b1" +\
111 "cbf9cd7c043a7d6456b7fc275ad8",
112 "successful": True
113 },
114 {
115 "index": 281474976710652,
116 "secret": "27cddaa5624534cb6cb9d7da077cf2b22ab2" +\
117 "1e9b506fd4998a51d54502e99116",
118 "successful": True
119 },
120 {
121 "index": 281474976710651,
122 "secret": "c65716add7aa98ba7acb236352d665cab173" +\
123 "45fe45b55fb879ff80e6bd0c41dd",
124 "successful": True
125 },
126 {
127 "index": 281474976710650,
128 "secret": "969660042a28f32d9be17344e09374b37996" +\
129 "2d03db1574df5a8a5a47e19ce3f2",
130 "successful": True
131 },
132 {
133 "index": 281474976710649,
134 "secret": "a5a64476122ca0925fb344bdc1854c1c0a59" +\
135 "fc614298e50a33e331980a220f32",
136 "successful": True
137 },
138 {
139 "index": 281474976710648,
140 "secret": "05cde6323d949933f7f7b78776bcc1ea6d9b" +\
141 "31447732e3802e1f7ac44b650e17",
142 "successful": True
143 }
144 ]
145 },
146 {
147 "name": "insert_secret #1 incorrect",
148 "inserts": [
149 {
150 "index": 281474976710655,
151 "secret": "02a40c85b6f28da08dfdbe0926c53fab2d" +\
152 "e6d28c10301f8f7c4073d5e42e3148",
153 "successful": True
154 },
155 {
156 "index": 281474976710654,
157 "secret": "c7518c8ae4660ed02894df8976fa1a3659" +\
158 "c1a8b4b5bec0c4b872abeba4cb8964",
159 "successful": False
160 }
161 ]
162 },
163 {
164 "name": "insert_secret #2 incorrect (#1 derived from incorrect)",
165 "inserts": [
166 {
167 "index": 281474976710655,
168 "secret": "02a40c85b6f28da08dfdbe0926c53fab2de6" +\
169 "d28c10301f8f7c4073d5e42e3148",
170 "successful": True
171 },
172 {
173 "index": 281474976710654,
174 "secret": "dddc3a8d14fddf2b68fa8c7fbad274827493" +\
175 "7479dd0f8930d5ebb4ab6bd866a3",
176 "successful": True
177 },
178 {
179 "index": 281474976710653,
180 "secret": "2273e227a5b7449b6e70f1fb4652864038b1" +\
181 "cbf9cd7c043a7d6456b7fc275ad8",
182 "successful": True
183 },
184 {
185 "index": 281474976710652,
186 "secret": "27cddaa5624534cb6cb9d7da077cf2b22a" +\
187 "b21e9b506fd4998a51d54502e99116",
188 "successful": False
189 }
190 ]
191 },
192 {
193 "name": "insert_secret #3 incorrect",
194 "inserts": [
195 {
196 "index": 281474976710655,
197 "secret": "7cc854b54e3e0dcdb010d7a3fee464a9687b" +\
198 "e6e8db3be6854c475621e007a5dc",
199 "successful": True
200 },
201 {
202 "index": 281474976710654,
203 "secret": "c7518c8ae4660ed02894df8976fa1a3659c1" +\
204 "a8b4b5bec0c4b872abeba4cb8964",
205 "successful": True
206 },
207 {
208 "index": 281474976710653,
209 "secret": "c51a18b13e8527e579ec56365482c62f180b" +\
210 "7d5760b46e9477dae59e87ed423a",
211 "successful": True
212 },
213 {
214 "index": 281474976710652,
215 "secret": "27cddaa5624534cb6cb9d7da077cf2b22ab2" +\
216 "1e9b506fd4998a51d54502e99116",
217 "successful": False
218 }
219 ]
220 },
221 {
222 "name": "insert_secret #4 incorrect (1,2,3 derived from incorrect)",
223 "inserts": [
224 {
225 "index": 281474976710655,
226 "secret": "02a40c85b6f28da08dfdbe0926c53fab2de6" +\
227 "d28c10301f8f7c4073d5e42e3148",
228 "successful": True
229 },
230 {
231 "index": 281474976710654,
232 "secret": "dddc3a8d14fddf2b68fa8c7fbad274827493" +\
233 "7479dd0f8930d5ebb4ab6bd866a3",
234 "successful": True
235 },
236 {
237 "index": 281474976710653,
238 "secret": "c51a18b13e8527e579ec56365482c62f18" +\
239 "0b7d5760b46e9477dae59e87ed423a",
240 "successful": True
241 },
242 {
243 "index": 281474976710652,
244 "secret": "ba65d7b0ef55a3ba300d4e87af29868f39" +\
245 "4f8f138d78a7011669c79b37b936f4",
246 "successful": True
247 },
248 {
249 "index": 281474976710651,
250 "secret": "c65716add7aa98ba7acb236352d665cab1" +\
251 "7345fe45b55fb879ff80e6bd0c41dd",
252 "successful": True
253 },
254 {
255 "index": 281474976710650,
256 "secret": "969660042a28f32d9be17344e09374b379" +\
257 "962d03db1574df5a8a5a47e19ce3f2",
258 "successful": True
259 },
260 {
261 "index": 281474976710649,
262 "secret": "a5a64476122ca0925fb344bdc1854c1c0a" +\
263 "59fc614298e50a33e331980a220f32",
264 "successful": True
265 },
266 {
267 "index": 281474976710649,
268 "secret": "05cde6323d949933f7f7b78776bcc1ea6d9b" +\
269 "31447732e3802e1f7ac44b650e17",
270 "successful": False
271 }
272 ]
273 },
274 {
275 "name": "insert_secret #5 incorrect",
276 "inserts": [
277 {
278 "index": 281474976710655,
279 "secret": "7cc854b54e3e0dcdb010d7a3fee464a9687b" +\
280 "e6e8db3be6854c475621e007a5dc",
281 "successful": True
282 },
283 {
284 "index": 281474976710654,
285 "secret": "c7518c8ae4660ed02894df8976fa1a3659c1a" +\
286 "8b4b5bec0c4b872abeba4cb8964",
287 "successful": True
288 },
289 {
290 "index": 281474976710653,
291 "secret": "2273e227a5b7449b6e70f1fb4652864038b1" +\
292 "cbf9cd7c043a7d6456b7fc275ad8",
293 "successful": True
294 },
295 {
296 "index": 281474976710652,
297 "secret": "27cddaa5624534cb6cb9d7da077cf2b22ab21" +\
298 "e9b506fd4998a51d54502e99116",
299 "successful": True
300 },
301 {
302 "index": 281474976710651,
303 "secret": "631373ad5f9ef654bb3dade742d09504c567" +\
304 "edd24320d2fcd68e3cc47e2ff6a6",
305 "successful": True
306 },
307 {
308 "index": 281474976710650,
309 "secret": "969660042a28f32d9be17344e09374b37996" +\
310 "2d03db1574df5a8a5a47e19ce3f2",
311 "successful": False
312 }
313 ]
314 },
315 {
316 "name": "insert_secret #6 incorrect (5 derived from incorrect)",
317 "inserts": [
318 {
319 "index": 281474976710655,
320 "secret": "7cc854b54e3e0dcdb010d7a3fee464a9687b" +\
321 "e6e8db3be6854c475621e007a5dc",
322 "successful": True
323 },
324 {
325 "index": 281474976710654,
326 "secret": "c7518c8ae4660ed02894df8976fa1a3659c1a" +\
327 "8b4b5bec0c4b872abeba4cb8964",
328 "successful": True
329 },
330 {
331 "index": 281474976710653,
332 "secret": "2273e227a5b7449b6e70f1fb4652864038b1" +\
333 "cbf9cd7c043a7d6456b7fc275ad8",
334 "successful": True
335 },
336 {
337 "index": 281474976710652,
338 "secret": "27cddaa5624534cb6cb9d7da077cf2b22ab21" +\
339 "e9b506fd4998a51d54502e99116",
340 "successful": True
341 },
342 {
343 "index": 281474976710651,
344 "secret": "631373ad5f9ef654bb3dade742d09504c567" +\
345 "edd24320d2fcd68e3cc47e2ff6a6",
346 "successful": True
347 },
348 {
349 "index": 281474976710650,
350 "secret": "b7e76a83668bde38b373970155c868a65330" +\
351 "4308f9896692f904a23731224bb1",
352 "successful": True
353 },
354 {
355 "index": 281474976710649,
356 "secret": "a5a64476122ca0925fb344bdc1854c1c0a59f" +\
357 "c614298e50a33e331980a220f32",
358 "successful": True
359 },
360 {
361 "index": 281474976710648,
362 "secret": "05cde6323d949933f7f7b78776bcc1ea6d9b" +\
363 "31447732e3802e1f7ac44b650e17",
364 "successful": False
365 }
366 ]
367 },
368 {
369 "name": "insert_secret #7 incorrect",
370 "inserts": [
371 {
372 "index": 281474976710655,
373 "secret": "7cc854b54e3e0dcdb010d7a3fee464a9687b" +\
374 "e6e8db3be6854c475621e007a5dc",
375 "successful": True
376 },
377 {
378 "index": 281474976710654,
379 "secret": "c7518c8ae4660ed02894df8976fa1a3659c1a" +\
380 "8b4b5bec0c4b872abeba4cb8964",
381 "successful": True
382 },
383 {
384 "index": 281474976710653,
385 "secret": "2273e227a5b7449b6e70f1fb4652864038b1" +\
386 "cbf9cd7c043a7d6456b7fc275ad8",
387 "successful": True
388 },
389 {
390 "index": 281474976710652,
391 "secret": "27cddaa5624534cb6cb9d7da077cf2b22ab21" +\
392 "e9b506fd4998a51d54502e99116",
393 "successful": True
394 },
395 {
396 "index": 281474976710651,
397 "secret": "c65716add7aa98ba7acb236352d665cab173" +\
398 "45fe45b55fb879ff80e6bd0c41dd",
399 "successful": True
400 },
401 {
402 "index": 281474976710650,
403 "secret": "969660042a28f32d9be17344e09374b37996" +\
404 "2d03db1574df5a8a5a47e19ce3f2",
405 "successful": True
406 },
407 {
408 "index": 281474976710649,
409 "secret": "e7971de736e01da8ed58b94c2fc216cb1d" +\
410 "ca9e326f3a96e7194fe8ea8af6c0a3",
411 "successful": True
412 },
413 {
414 "index": 281474976710648,
415 "secret": "05cde6323d949933f7f7b78776bcc1ea6d" +\
416 "9b31447732e3802e1f7ac44b650e17",
417 "successful": False
418 }
419 ]
420 },
421 {
422 "name": "insert_secret #8 incorrect",
423 "inserts": [
424 {
425 "index": 281474976710655,
426 "secret": "7cc854b54e3e0dcdb010d7a3fee464a9687b" +\
427 "e6e8db3be6854c475621e007a5dc",
428 "successful": True
429 },
430 {
431 "index": 281474976710654,
432 "secret": "c7518c8ae4660ed02894df8976fa1a3659c1a" +\
433 "8b4b5bec0c4b872abeba4cb8964",
434 "successful": True
435 },
436 {
437 "index": 281474976710653,
438 "secret": "2273e227a5b7449b6e70f1fb4652864038b1" +\
439 "cbf9cd7c043a7d6456b7fc275ad8",
440 "successful": True
441 },
442 {
443 "index": 281474976710652,
444 "secret": "27cddaa5624534cb6cb9d7da077cf2b22ab21" +\
445 "e9b506fd4998a51d54502e99116",
446 "successful": True
447 },
448 {
449 "index": 281474976710651,
450 "secret": "c65716add7aa98ba7acb236352d665cab173" +\
451 "45fe45b55fb879ff80e6bd0c41dd",
452 "successful": True
453 },
454 {
455 "index": 281474976710650,
456 "secret": "969660042a28f32d9be17344e09374b37996" +\
457 "2d03db1574df5a8a5a47e19ce3f2",
458 "successful": True
459 },
460 {
461 "index": 281474976710649,
462 "secret": "a5a64476122ca0925fb344bdc1854c1c0a" +\
463 "59fc614298e50a33e331980a220f32",
464 "successful": True
465 },
466 {
467 "index": 281474976710648,
468 "secret": "a7efbc61aac46d34f77778bac22c8a20c6" +\
469 "a46ca460addc49009bda875ec88fa4",
470 "successful": False
471 }
472 ]
473 }
474 ]
475
476 for test in tests:
477 receiver = RevocationStore(StoredDict({}, None))
478 for insert in test["inserts"]:
479 secret = bytes.fromhex(insert["secret"])
480
481 try:
482 receiver.add_next_entry(secret)
483 except Exception as e:
484 if insert["successful"]:
485 raise Exception("Failed ({}): error was received but it shouldn't: {}".format(test["name"], e))
486 else:
487 if not insert["successful"]:
488 raise Exception("Failed ({}): error wasn't received".format(test["name"]))
489
490 for insert in test["inserts"]:
491 secret = bytes.fromhex(insert["secret"])
492 index = insert["index"]
493 if insert["successful"]:
494 self.assertEqual(secret, receiver.retrieve_secret(index))
495
496 print("Passed ({})".format(test["name"]))
497
498 def test_shachain_produce_consume(self):
499 seed = bitcoin.sha256(b"shachaintest")
500 consumer = RevocationStore(StoredDict({}, None))
501 for i in range(10000):
502 secret = get_per_commitment_secret_from_seed(seed, RevocationStore.START_INDEX - i)
503 try:
504 consumer.add_next_entry(secret)
505 except Exception as e:
506 raise Exception("iteration " + str(i) + ": " + str(e))
507 if i % 1000 == 0:
508 c1 = consumer
509 s1 = json.dumps(c1.storage, cls=MyEncoder)
510 c2 = RevocationStore(StoredDict(json.loads(s1), None))
511 s2 = json.dumps(c2.storage, cls=MyEncoder)
512 self.assertEqual(s1, s2)
513
514 def test_commitment_tx_with_all_five_HTLCs_untrimmed_minimum_feerate(self):
515 to_local_msat = 6988000000
516 to_remote_msat = 3000000000
517 local_feerate_per_kw = 0
518 # base commitment transaction fee = 0
519 # actual commitment transaction fee = 0
520
521 per_commitment_secret = 0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100
522 per_commitment_point = secret_to_pubkey(per_commitment_secret)
523
524 remote_htlcpubkey = remotepubkey
525 local_htlcpubkey = localpubkey
526
527 htlc_cltv_timeout = {}
528 htlc_payment_preimage = {}
529 htlc = {}
530 htlc_pubkeys = {
531 "revocation_pubkey": local_revocation_pubkey,
532 "remote_htlcpubkey": remote_htlcpubkey,
533 "local_htlcpubkey": local_htlcpubkey,
534 }
535
536 htlc_cltv_timeout[2] = 502
537 htlc_payment_preimage[2] = b"\x02" * 32
538 htlc[2] = make_offered_htlc(**htlc_pubkeys, payment_hash=bitcoin.sha256(htlc_payment_preimage[2]), has_anchors=False)
539
540 htlc_cltv_timeout[3] = 503
541 htlc_payment_preimage[3] = b"\x03" * 32
542 htlc[3] = make_offered_htlc(**htlc_pubkeys, payment_hash=bitcoin.sha256(htlc_payment_preimage[3]), has_anchors=False)
543
544 htlc_cltv_timeout[0] = 500
545 htlc_payment_preimage[0] = b"\x00" * 32
546 htlc[0] = make_received_htlc(**htlc_pubkeys, payment_hash=bitcoin.sha256(htlc_payment_preimage[0]), cltv_abs=htlc_cltv_timeout[0], has_anchors=False)
547
548 htlc_cltv_timeout[1] = 501
549 htlc_payment_preimage[1] = b"\x01" * 32
550 htlc[1] = make_received_htlc(**htlc_pubkeys, payment_hash=bitcoin.sha256(htlc_payment_preimage[1]), cltv_abs=htlc_cltv_timeout[1], has_anchors=False)
551
552 htlc_cltv_timeout[4] = 504
553 htlc_payment_preimage[4] = b"\x04" * 32
554 htlc[4] = make_received_htlc(**htlc_pubkeys, payment_hash=bitcoin.sha256(htlc_payment_preimage[4]), cltv_abs=htlc_cltv_timeout[4], has_anchors=False)
555
556 remote_signature = bfh("304402204fd4928835db1ccdfc40f5c78ce9bd65249b16348df81f0c44328dcdefc97d630220194d3869c38bc732dd87d13d2958015e2fc16829e74cd4377f84d215c0b70606")
557 output_commit_tx = "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8007e80300000000000022002052bfef0479d7b293c27e0f1eb294bea154c63a3294ef092c19af51409bce0e2ad007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110e0a06a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e04004730440220275b0c325a5e9355650dc30c0eccfbc7efb23987c24b556b9dfdd40effca18d202206caceb2c067836c51f296740c7ae807ffcbfbf1dd3a0d56b6de9a5b247985f060147304402204fd4928835db1ccdfc40f5c78ce9bd65249b16348df81f0c44328dcdefc97d630220194d3869c38bc732dd87d13d2958015e2fc16829e74cd4377f84d215c0b7060601475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220"
558
559 htlc_obj = {}
560 for num, msat in [(0, 1000 * 1000),
561 (2, 2000 * 1000),
562 (1, 2000 * 1000),
563 (3, 3000 * 1000),
564 (4, 4000 * 1000)]:
565 htlc_obj[num] = UpdateAddHtlc(amount_msat=msat, payment_hash=bitcoin.sha256(htlc_payment_preimage[num]), cltv_abs=0, htlc_id=None, timestamp=0)
566 htlcs = [ScriptHtlc(htlc[x], htlc_obj[x]) for x in range(5)]
567
568 our_commit_tx = make_commitment(
569 ctn=commitment_number,
570 local_funding_pubkey=local_funding_pubkey,
571 remote_funding_pubkey=remote_funding_pubkey,
572 remote_payment_pubkey=remotepubkey,
573 funder_payment_basepoint=local_payment_basepoint,
574 fundee_payment_basepoint=remote_payment_basepoint,
575 revocation_pubkey=local_revocation_pubkey,
576 delayed_pubkey=local_delayedpubkey,
577 to_self_delay=local_delay,
578 funding_txid=funding_tx_id,
579 funding_pos=funding_output_index,
580 funding_sat=funding_amount_satoshi,
581 local_amount=to_local_msat,
582 remote_amount=to_remote_msat,
583 dust_limit_sat=local_dust_limit_satoshi,
584 fees_per_participant=calc_fees_for_commitment_tx(num_htlcs=len(htlcs), feerate=local_feerate_per_kw, is_local_initiator=True, has_anchors=False),
585 htlcs=htlcs,
586 has_anchors=False
587 )
588 self.sign_and_insert_remote_sig(our_commit_tx, remote_funding_pubkey, remote_signature, local_funding_pubkey, local_funding_privkey)
589 self.assertEqual(str(our_commit_tx), output_commit_tx)
590
591 signature_for_output_remote_htlc = {}
592 signature_for_output_remote_htlc[0] = "304402206a6e59f18764a5bf8d4fa45eebc591566689441229c918b480fb2af8cc6a4aeb02205248f273be447684b33e3c8d1d85a8e0ca9fa0bae9ae33f0527ada9c162919a6"
593 signature_for_output_remote_htlc[2] = "3045022100d5275b3619953cb0c3b5aa577f04bc512380e60fa551762ce3d7a1bb7401cff9022037237ab0dac3fe100cde094e82e2bed9ba0ed1bb40154b48e56aa70f259e608b"
594 signature_for_output_remote_htlc[1] = "304402201b63ec807771baf4fdff523c644080de17f1da478989308ad13a58b51db91d360220568939d38c9ce295adba15665fa68f51d967e8ed14a007b751540a80b325f202"
595 signature_for_output_remote_htlc[3] = "3045022100daee1808f9861b6c3ecd14f7b707eca02dd6bdfc714ba2f33bc8cdba507bb182022026654bf8863af77d74f51f4e0b62d461a019561bb12acb120d3f7195d148a554"
596 signature_for_output_remote_htlc[4] = "304402207e0410e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625153f8596692a801d"
597
598 output_htlc_tx = {}
599 SUCCESS = True
600 TIMEOUT = False
601 output_htlc_tx[0] = (SUCCESS, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219700000000000000000001e8030000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402206a6e59f18764a5bf8d4fa45eebc591566689441229c918b480fb2af8cc6a4aeb02205248f273be447684b33e3c8d1d85a8e0ca9fa0bae9ae33f0527ada9c162919a60147304402207cb324fa0de88f452ffa9389678127ebcf4cabe1dd848b8e076c1a1962bf34720220116ed922b12311bd602d67e60d2529917f21c5b82f25ff6506c0f87886b4dfd5012000000000000000000000000000000000000000000000000000000000000000008a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a914b8bcb07f6344b42ab04250c86a6e8b75d3fdbbc688527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f401b175ac686800000000")
602
603 output_htlc_tx[2] = (TIMEOUT, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219701000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d5275b3619953cb0c3b5aa577f04bc512380e60fa551762ce3d7a1bb7401cff9022037237ab0dac3fe100cde094e82e2bed9ba0ed1bb40154b48e56aa70f259e608b0147304402205735e9f335dfd123f730ac5bf184fd7d5b672e4d84c51a3f0478cc229bb44936022018b1cec3e3b29e5cc335d7e326bc29d75a7e063216427d081cb83ebdbd828b4d01008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000")
604
605 output_htlc_tx[1] = (SUCCESS, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219702000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402201b63ec807771baf4fdff523c644080de17f1da478989308ad13a58b51db91d360220568939d38c9ce295adba15665fa68f51d967e8ed14a007b751540a80b325f202014730440220481a48f83c358ae0f220e37f88e56b3d434cefaded82065b8e7a9fd78fee7a26022022674ab37a4c39e6efba302f760ca05931d8add8d65231c5bf34a6c2a76b15bf012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000")
606
607 output_htlc_tx[3] = (TIMEOUT, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219703000000000000000001b80b0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100daee1808f9861b6c3ecd14f7b707eca02dd6bdfc714ba2f33bc8cdba507bb182022026654bf8863af77d74f51f4e0b62d461a019561bb12acb120d3f7195d148a554014730440220643aacb19bbb72bd2b635bc3f7375481f5981bace78cdd8319b2988ffcc6704202203d27784ec8ad51ed3bd517a05525a5139bb0b755dd719e0054332d186ac0872701008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000")
608
609 output_htlc_tx[4] = (SUCCESS, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219704000000000000000001a00f0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402207e0410e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625153f8596692a801d014730440220549e80b4496803cbc4a1d09d46df50109f546d43fbbf86cd90b174b1484acd5402205f12a4f995cb9bded597eabfee195a285986aa6d93ae5bb72507ebc6a4e2349e012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000")
610
611 htlc_output_index = {0: 0, 1: 2, 2: 1, 3: 3, 4: 4}
612
613 for i in range(5):
614 self.assertEqual(output_htlc_tx[i][1], self.htlc_tx(
615 htlc[i],
616 htlc_output_index[i],
617 htlcs[i].htlc.amount_msat,
618 htlc_payment_preimage[i],
619 signature_for_output_remote_htlc[i],
620 output_htlc_tx[i][0],
621 htlc_cltv_timeout[i] if not output_htlc_tx[i][0] else 0,
622 local_feerate_per_kw,
623 our_commit_tx,
624 False,
625 ))
626
627 def htlc_tx(self, htlc: bytes, htlc_output_index: int, amount_msat: int,
628 htlc_payment_preimage: bytes, remote_htlc_sig: str,
629 success: bool, cltv_abs: int,
630 local_feerate_per_kw: int, our_commit_tx: PartialTransaction,
631 has_anchors: bool) -> str:
632 _script, our_htlc_tx_output = make_htlc_tx_output(
633 amount_msat=amount_msat,
634 local_feerate=local_feerate_per_kw,
635 revocationpubkey=local_revocation_pubkey,
636 local_delayedpubkey=local_delayedpubkey,
637 success=success,
638 to_self_delay=local_delay,
639 has_anchors=has_anchors
640 )
641 our_htlc_tx_inputs = make_htlc_tx_inputs(
642 htlc_output_txid=our_commit_tx.txid(),
643 htlc_output_index=htlc_output_index,
644 amount_msat=amount_msat,
645 witness_script=htlc)
646 our_htlc_tx = make_htlc_tx(
647 cltv_abs=cltv_abs,
648 inputs=our_htlc_tx_inputs,
649 output=our_htlc_tx_output)
650
651 remote_sighash = Sighash.ALL
652 if has_anchors:
653 remote_sighash = Sighash.ANYONECANPAY | Sighash.SINGLE
654 our_htlc_tx.inputs()[0].nsequence = 1
655
656 our_htlc_tx.inputs()[0].sighash = Sighash.ALL
657 local_sig = our_htlc_tx.sign_txin(0, local_privkey[:-1])
658
659 our_htlc_tx_witness = make_htlc_tx_witness(
660 remotehtlcsig=bfh(remote_htlc_sig) + remote_sighash.to_bytes(1, 'big'),
661 localhtlcsig=local_sig,
662 payment_preimage=htlc_payment_preimage if success else b'', # will put 00 on witness if timeout
663 witness_script=htlc)
664 our_htlc_tx._inputs[0].witness = our_htlc_tx_witness
665 return str(our_htlc_tx)
666
667 def test_commitment_tx_with_one_output(self):
668 to_local_msat= 6988000000
669 to_remote_msat= 3000000000
670 local_feerate_per_kw= 9651181
671 remote_signature = bfh("3044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e")
672 output_commit_tx= "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8001c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431100400473044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b101473044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220"
673
674 our_commit_tx = make_commitment(
675 ctn=commitment_number,
676 local_funding_pubkey=local_funding_pubkey,
677 remote_funding_pubkey=remote_funding_pubkey,
678 remote_payment_pubkey=remotepubkey,
679 funder_payment_basepoint=local_payment_basepoint,
680 fundee_payment_basepoint=remote_payment_basepoint,
681 revocation_pubkey=local_revocation_pubkey,
682 delayed_pubkey=local_delayedpubkey,
683 to_self_delay=local_delay,
684 funding_txid=funding_tx_id,
685 funding_pos=funding_output_index,
686 funding_sat=funding_amount_satoshi,
687 local_amount=to_local_msat,
688 remote_amount=to_remote_msat,
689 dust_limit_sat=local_dust_limit_satoshi,
690 fees_per_participant=calc_fees_for_commitment_tx(num_htlcs=0, feerate=local_feerate_per_kw, is_local_initiator=True, has_anchors=False),
691 htlcs=[],
692 has_anchors=False
693 )
694 self.sign_and_insert_remote_sig(our_commit_tx, remote_funding_pubkey, remote_signature, local_funding_pubkey, local_funding_privkey)
695
696 self.assertEqual(str(our_commit_tx), output_commit_tx)
697
698 def test_commitment_tx_with_fee_greater_than_funder_amount(self):
699 to_local_msat= 6988000000
700 to_remote_msat= 3000000000
701 local_feerate_per_kw= 9651936
702 remote_signature = bfh("3044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e")
703 output_commit_tx= "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8001c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de8431100400473044022031a82b51bd014915fe68928d1abf4b9885353fb896cac10c3fdd88d7f9c7f2e00220716bda819641d2c63e65d3549b6120112e1aeaf1742eed94a471488e79e206b101473044022064901950be922e62cbe3f2ab93de2b99f37cff9fc473e73e394b27f88ef0731d02206d1dfa227527b4df44a07599289e207d6fd9cca60c0365682dcd3deaf739567e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220"
704
705 our_commit_tx = make_commitment(
706 ctn=commitment_number,
707 local_funding_pubkey=local_funding_pubkey,
708 remote_funding_pubkey=remote_funding_pubkey,
709 remote_payment_pubkey=remotepubkey,
710 funder_payment_basepoint=local_payment_basepoint,
711 fundee_payment_basepoint=remote_payment_basepoint,
712 revocation_pubkey=local_revocation_pubkey,
713 delayed_pubkey=local_delayedpubkey,
714 to_self_delay=local_delay,
715 funding_txid=funding_tx_id,
716 funding_pos=funding_output_index,
717 funding_sat=funding_amount_satoshi,
718 local_amount=to_local_msat,
719 remote_amount=to_remote_msat,
720 dust_limit_sat=local_dust_limit_satoshi,
721 fees_per_participant=calc_fees_for_commitment_tx(num_htlcs=0, feerate=local_feerate_per_kw, is_local_initiator=True, has_anchors=False),
722 htlcs=[],
723 has_anchors=False
724 )
725 self.sign_and_insert_remote_sig(our_commit_tx, remote_funding_pubkey, remote_signature, local_funding_pubkey, local_funding_privkey)
726
727 self.assertEqual(str(our_commit_tx), output_commit_tx)
728
729 def test_extract_commitment_number_from_tx(self):
730 raw_tx = "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8007e80300000000000022002052bfef0479d7b293c27e0f1eb294bea154c63a3294ef092c19af51409bce0e2ad007000000000000220020403d394747cae42e98ff01734ad5c08f82ba123d3d9a620abda88989651e2ab5d007000000000000220020748eba944fedc8827f6b06bc44678f93c0f9e6078b35c6331ed31e75f8ce0c2db80b000000000000220020c20b5d1f8584fd90443e7b7b720136174fa4b9333c261d04dbbd012635c0f419a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110e0a06a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e04004730440220275b0c325a5e9355650dc30c0eccfbc7efb23987c24b556b9dfdd40effca18d202206caceb2c067836c51f296740c7ae807ffcbfbf1dd3a0d56b6de9a5b247985f060147304402204fd4928835db1ccdfc40f5c78ce9bd65249b16348df81f0c44328dcdefc97d630220194d3869c38bc732dd87d13d2958015e2fc16829e74cd4377f84d215c0b7060601475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220"
731 tx = Transaction(raw_tx)
732 self.assertEqual(commitment_number, extract_ctn_from_tx(tx, 0, local_payment_basepoint, remote_payment_basepoint))
733
734 def test_per_commitment_secret_from_seed(self):
735 self.assertEqual(0x02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148.to_bytes(byteorder="big", length=32),
736 get_per_commitment_secret_from_seed(0x0000000000000000000000000000000000000000000000000000000000000000.to_bytes(byteorder="big", length=32), 281474976710655))
737 self.assertEqual(0x7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc.to_bytes(byteorder="big", length=32),
738 get_per_commitment_secret_from_seed(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.to_bytes(byteorder="big", length=32), 281474976710655))
739 self.assertEqual(0x56f4008fb007ca9acf0e15b054d5c9fd12ee06cea347914ddbaed70d1c13a528.to_bytes(byteorder="big", length=32),
740 get_per_commitment_secret_from_seed(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.to_bytes(byteorder="big", length=32), 0xaaaaaaaaaaa))
741 self.assertEqual(0x9015daaeb06dba4ccc05b91b2f73bd54405f2be9f217fbacd3c5ac2e62327d31.to_bytes(byteorder="big", length=32),
742 get_per_commitment_secret_from_seed(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.to_bytes(byteorder="big", length=32), 0x555555555555))
743 self.assertEqual(0x915c75942a26bb3a433a8ce2cb0427c29ec6c1775cfc78328b57f6ba7bfeaa9c.to_bytes(byteorder="big", length=32),
744 get_per_commitment_secret_from_seed(0x0101010101010101010101010101010101010101010101010101010101010101.to_bytes(byteorder="big", length=32), 1))
745
746 def test_key_derivation(self):
747 # BOLT3, Appendix E
748 base_secret = 0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
749 per_commitment_secret = 0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100
750 revocation_basepoint_secret = 0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
751 base_point = secret_to_pubkey(base_secret)
752 self.assertEqual(base_point, bfh('036d6caac248af96f6afa7f904f550253a0f3ef3f5aa2fe6838a95b216691468e2'))
753 per_commitment_point = secret_to_pubkey(per_commitment_secret)
754 self.assertEqual(per_commitment_point, bfh('025f7117a78150fe2ef97db7cfc83bd57b2e2c0d0dd25eaf467a4a1c2a45ce1486'))
755 localpubkey = derive_pubkey(base_point, per_commitment_point)
756 self.assertEqual(localpubkey, bfh('0235f2dbfaa89b57ec7b055afe29849ef7ddfeb1cefdb9ebdc43f5494984db29e5'))
757 localprivkey = derive_privkey(base_secret, per_commitment_point)
758 self.assertEqual(localprivkey, 0xcbced912d3b21bf196a766651e436aff192362621ce317704ea2f75d87e7be0f)
759 revocation_basepoint = secret_to_pubkey(revocation_basepoint_secret)
760 self.assertEqual(revocation_basepoint, bfh('036d6caac248af96f6afa7f904f550253a0f3ef3f5aa2fe6838a95b216691468e2'))
761 revocationpubkey = derive_blinded_pubkey(revocation_basepoint, per_commitment_point)
762 self.assertEqual(revocationpubkey, bfh('02916e326636d19c33f13e8c0c3a03dd157f332f3e99c317c141dd865eb01f8ff0'))
763
764 def test_simple_commitment_tx_with_no_HTLCs(self):
765 to_local_msat = 7000000000
766 to_remote_msat = 3000000000
767 local_feerate_per_kw = 15000
768 # base commitment transaction fee = 10860
769 # actual commitment transaction fee = 10860
770 # to_local amount 6989140 wscript 63210212a140cd0c6539d07cd08dfe09984dec3251ea808b892efeac3ede9402bf2b1967029000b2752103fd5960528dc152014952efdb702a88f71e3c1653b2314431701ec77e57fde83c68ac
771 # to_remote amount 3000000 P2WPKH(0394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b)
772 remote_signature = bfh("3045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c0")
773 # local_signature = 3044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c3836939
774 our_commit_tx = make_commitment(
775 ctn=commitment_number,
776 local_funding_pubkey=local_funding_pubkey,
777 remote_funding_pubkey=remote_funding_pubkey,
778 remote_payment_pubkey=remotepubkey,
779 funder_payment_basepoint=local_payment_basepoint,
780 fundee_payment_basepoint=remote_payment_basepoint,
781 revocation_pubkey=local_revocation_pubkey,
782 delayed_pubkey=local_delayedpubkey,
783 to_self_delay=local_delay,
784 funding_txid=funding_tx_id,
785 funding_pos=funding_output_index,
786 funding_sat=funding_amount_satoshi,
787 local_amount=to_local_msat,
788 remote_amount=to_remote_msat,
789 dust_limit_sat=local_dust_limit_satoshi,
790 fees_per_participant=calc_fees_for_commitment_tx(num_htlcs=0, feerate=local_feerate_per_kw, is_local_initiator=True, has_anchors=False),
791 htlcs=[],
792 has_anchors=False
793 )
794 self.sign_and_insert_remote_sig(our_commit_tx, remote_funding_pubkey, remote_signature, local_funding_pubkey, local_funding_privkey)
795 ref_commit_tx_str = '02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8002c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311054a56a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400473044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c383693901483045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c001475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220'
796 self.assertEqual(str(our_commit_tx), ref_commit_tx_str)
797
798 @disable_ecdsa_r_value_grinding
799 def test_commitment_tx_anchors_test_vectors(self):
800 # this test is only valid for the original anchor output test vectors (not anchors-zero-fee-htlcs),
801 # therefore we patch the effective htlc tx weight to result in a finite weight
802 from electrum import lnutil
803 effective_htlc_tx_weight_original = lnutil.effective_htlc_tx_weight
804
805 def effective_htlc_tx_weight_patched(success: bool, has_anchors: bool):
806 return lnutil.HTLC_SUCCESS_WEIGHT_ANCHORS if success else lnutil.HTLC_TIMEOUT_WEIGHT_ANCHORS
807 lnutil.effective_htlc_tx_weight = effective_htlc_tx_weight_patched
808 try:
809 self._test_commitment_tx_anchors_test_vectors()
810 finally:
811 lnutil.effective_htlc_tx_weight = effective_htlc_tx_weight_original
812
813 def _test_commitment_tx_anchors_test_vectors(self):
814 for test_vector in ANCHOR_TEST_VECTORS:
815 with self.subTest(test_vector['Name']):
816 to_local_msat = test_vector['LocalBalance']
817 to_remote_msat = test_vector['RemoteBalance']
818 local_feerate_per_kw = test_vector['FeePerKw']
819 ref_commit_tx_str = test_vector['ExpectedCommitmentTxHex']
820 remote_signature = bfh(test_vector['RemoteSigHex'])
821 use_test_htlcs = test_vector['UseTestHtlcs']
822 htlc_descs = test_vector['HtlcDescs'] # type: List[Dict[str, str]]
823
824 remote_htlcpubkey = remotepubkey
825 local_htlcpubkey = localpubkey
826
827 # test of the commitment transaction, build htlc outputs first
828 test_htlcs = {}
829 if use_test_htlcs:
830 # only consider htlcs whose sweep transaction creates outputs above dust limit
831 threshold_sat_received = received_htlc_trim_threshold_sat(dust_limit_sat=local_dust_limit_satoshi, feerate=local_feerate_per_kw, has_anchors=True)
832 threshold_sat_offered = offered_htlc_trim_threshold_sat(dust_limit_sat=local_dust_limit_satoshi, feerate=local_feerate_per_kw, has_anchors=True)
833 for test_index, test_htlc in enumerate(TEST_HTLCS):
834 if test_htlc['incoming']:
835 htlc_script = make_received_htlc(
836 revocation_pubkey=local_revocation_pubkey,
837 remote_htlcpubkey=remote_htlcpubkey,
838 local_htlcpubkey=local_htlcpubkey,
839 payment_hash=bitcoin.sha256(bfh(test_htlc['preimage'])),
840 cltv_abs=test_htlc['expiry'],
841 has_anchors=True)
842 else:
843 htlc_script = make_offered_htlc(
844 revocation_pubkey=local_revocation_pubkey,
845 remote_htlcpubkey=remote_htlcpubkey,
846 local_htlcpubkey=local_htlcpubkey,
847 payment_hash=bitcoin.sha256(bfh(test_htlc['preimage'])),
848 has_anchors=True)
849 update_add_htlc = UpdateAddHtlc(
850 amount_msat=test_htlc['amount'],
851 payment_hash=bitcoin.sha256(bfh(test_htlc['preimage'])),
852 cltv_abs=test_htlc['expiry'],
853 htlc_id=None,
854 timestamp=0)
855 # only add htlcs whose spending transaction creates above-dust outputs
856 # TODO: should we include this check in make_commitment?
857 if test_htlc['amount'] // 1000 >= (threshold_sat_received if test_htlc['incoming'] else threshold_sat_offered):
858 test_htlcs[test_index] = ScriptHtlc(htlc_script, update_add_htlc)
859
860 our_commit_tx = make_commitment(
861 ctn=commitment_number,
862 local_funding_pubkey=local_funding_pubkey,
863 remote_funding_pubkey=remote_funding_pubkey,
864 remote_payment_pubkey=remote_payment_basepoint, # no key rotation for anchors
865 funder_payment_basepoint=local_payment_basepoint,
866 fundee_payment_basepoint=remote_payment_basepoint,
867 revocation_pubkey=local_revocation_pubkey,
868 delayed_pubkey=local_delayedpubkey,
869 to_self_delay=local_delay,
870 funding_txid=funding_tx_id,
871 funding_pos=funding_output_index,
872 funding_sat=funding_amount_satoshi,
873 local_amount=to_local_msat,
874 remote_amount=to_remote_msat,
875 dust_limit_sat=local_dust_limit_satoshi,
876 fees_per_participant=calc_fees_for_commitment_tx(num_htlcs=len(test_htlcs), feerate=local_feerate_per_kw, is_local_initiator=True, has_anchors=True),
877 htlcs=list(test_htlcs.values()),
878 has_anchors=True
879 )
880 self.sign_and_insert_remote_sig(our_commit_tx, remote_funding_pubkey, remote_signature, local_funding_pubkey, local_funding_privkey)
881 self.assertEqual(str(our_commit_tx), ref_commit_tx_str) # only works without r value grinding
882
883 # test the transactions spending the htlc outputs
884 # we need to keep track of the htlc order in order to compare to test vectors
885 sorted_htlcs = {h[0]: h[1] for h in sorted(test_htlcs.items(), key=lambda x: (x[1].htlc.amount_msat, -x[1].htlc.cltv_abs))}
886 if use_test_htlcs:
887 for output_index, (test_index, htlc) in enumerate(sorted_htlcs.items()):
888 test_htlc = TEST_HTLCS[test_index]
889 our_htlc = self.htlc_tx(
890 htlc=htlc.redeem_script,
891 htlc_output_index=output_index + 2, # first two are anchors
892 amount_msat=htlc.htlc.amount_msat,
893 htlc_payment_preimage=bfh(test_htlc['preimage']),
894 remote_htlc_sig=htlc_descs[output_index]['RemoteSigHex'],
895 success=test_htlc['incoming'],
896 cltv_abs=test_htlc['expiry'] if not test_htlc['incoming'] else 0, # expiry is for timeout transaction
897 local_feerate_per_kw=local_feerate_per_kw,
898 our_commit_tx=our_commit_tx,
899 has_anchors=True
900 )
901 ref_htlc = htlc_descs[output_index]['ResolutionTxHex']
902 self.assertEqual(our_htlc, ref_htlc) # only works without r value grinding
903
904 def sign_and_insert_remote_sig(self, tx: PartialTransaction, remote_pubkey: bytes, remote_signature: bytes, pubkey: bytes, privkey: bytes):
905 assert type(remote_pubkey) is bytes
906 assert len(remote_pubkey) == 33
907 assert type(remote_signature) is bytes
908 assert type(pubkey) is bytes
909 assert type(privkey) is bytes
910 assert len(pubkey) == 33
911 assert len(privkey) == 33
912 tx.sign({pubkey: privkey[:-1]})
913 sighash = Sighash.to_sigbytes(Sighash.ALL)
914 tx.add_signature_to_txin(txin_idx=0, signing_pubkey=remote_pubkey, sig=remote_signature + sighash)
915
916 def test_get_compressed_pubkey_from_bech32(self):
917 self.assertEqual(b'\x03\x84\xef\x87\xd9d\xa2\xaaa7=\xff\xb8\xfe=t8[}>;\n\x13\xa8e\x8eo:\xf5Mi\xb5H',
918 get_compressed_pubkey_from_bech32('ln1qwzwlp7evj325cfh8hlm3l3awsu9klf78v9p82r93ehn4a2ddx65s66awg5'))
919
920 def test_ln_features_validate_transitive_dependencies(self):
921 features = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
922 self.assertTrue(features.validate_transitive_dependencies(context=LnFeatureContexts.INIT))
923 features = LnFeatures.PAYMENT_SECRET_OPT
924 self.assertFalse(features.validate_transitive_dependencies(context=LnFeatureContexts.BOLT11_INVOICE))
925 features = LnFeatures.PAYMENT_SECRET_REQ
926 self.assertFalse(features.validate_transitive_dependencies(context=LnFeatureContexts.BOLT11_INVOICE))
927 features = LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_REQ
928 self.assertTrue(features.validate_transitive_dependencies(context=LnFeatureContexts.BOLT11_INVOICE))
929 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ
930 self.assertFalse(features.validate_transitive_dependencies(context=LnFeatureContexts.BOLT11_INVOICE))
931 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_OPT
932 self.assertTrue(features.validate_transitive_dependencies(context=LnFeatureContexts.BOLT11_INVOICE))
933 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_REQ
934 self.assertTrue(features.validate_transitive_dependencies(context=LnFeatureContexts.BOLT11_INVOICE))
935 features = LnFeatures.BASIC_MPP_OPT
936 self.assertTrue(features.validate_transitive_dependencies(context=LnFeatureContexts.BOLT12_INVOICE))
937
938 def test_ln_features_for_init_message(self):
939 features = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
940 self.assertEqual(features, features.for_init_message())
941 features = LnFeatures.PAYMENT_SECRET_OPT
942 self.assertEqual(features, features.for_init_message())
943 features = LnFeatures.PAYMENT_SECRET_REQ
944 self.assertEqual(features, features.for_init_message())
945 features = LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_REQ
946 self.assertEqual(features, features.for_init_message())
947 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ
948 self.assertEqual(features, features.for_init_message())
949 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_OPT
950 self.assertEqual(features, features.for_init_message())
951 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_REQ
952 self.assertEqual(features, features.for_init_message())
953
954 def test_ln_features_for_bolt11_invoice(self):
955 features = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
956 self.assertEqual(LnFeatures(0), features.for_bolt11_invoice())
957 features = LnFeatures.PAYMENT_SECRET_OPT
958 self.assertEqual(features, features.for_bolt11_invoice())
959 features = LnFeatures.PAYMENT_SECRET_REQ
960 self.assertEqual(features, features.for_bolt11_invoice())
961 features = LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_REQ
962 self.assertEqual(features, features.for_bolt11_invoice())
963 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
964 self.assertEqual(LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ,
965 features.for_bolt11_invoice())
966 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_OPT | LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
967 self.assertEqual(LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_OPT,
968 features.for_bolt11_invoice())
969 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_REQ
970 self.assertEqual(features, features.for_bolt11_invoice())
971
972 def test_ln_features_for_bolt12_invoice(self):
973 features = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.VAR_ONION_OPT | LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
974 self.assertEqual(LnFeatures.BASIC_MPP_OPT, features.for_bolt12_invoice())
975
976 def test_ln_compare_features(self):
977 f1 = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ | LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT
978 f2 = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
979 self.assertEqual(LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ | LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT,
980 ln_compare_features(f1, f2))
981 self.assertEqual(LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ | LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT,
982 ln_compare_features(f2, f1))
983 # note that the args are not commutative; if we (first arg) REQ a feature, OPT will get auto-set
984 f1 = LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT
985 f2 = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
986 self.assertEqual(LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT,
987 ln_compare_features(f1, f2))
988 self.assertEqual(LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ | LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT,
989 ln_compare_features(f2, f1))
990
991 f1 = LnFeatures(0)
992 f2 = LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT
993 self.assertEqual(LnFeatures(0), ln_compare_features(f1, f2))
994 self.assertEqual(LnFeatures(0), ln_compare_features(f2, f1))
995
996 f1 = LnFeatures(0)
997 f2 = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
998 with self.assertRaises(IncompatibleLightningFeatures):
999 ln_compare_features(f1, f2)
1000 with self.assertRaises(IncompatibleLightningFeatures):
1001 ln_compare_features(f2, f1)
1002
1003 f1 = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT | LnFeatures.VAR_ONION_OPT
1004 f2 = LnFeatures.PAYMENT_SECRET_OPT | LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ | LnFeatures.VAR_ONION_OPT
1005 self.assertEqual(LnFeatures.PAYMENT_SECRET_OPT |
1006 LnFeatures.PAYMENT_SECRET_REQ |
1007 LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT |
1008 LnFeatures.VAR_ONION_OPT,
1009 ln_compare_features(f1, f2))
1010 self.assertEqual(LnFeatures.PAYMENT_SECRET_OPT |
1011 LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT |
1012 LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ |
1013 LnFeatures.VAR_ONION_OPT,
1014 ln_compare_features(f2, f1))
1015
1016 def test_list_enabled_ln_feature_bits(self):
1017 self.assertEqual((0, 2, 6), list_enabled_ln_feature_bits(77))
1018 self.assertEqual((), list_enabled_ln_feature_bits(0))
1019
1020 def test_ln_features_supports(self):
1021 f_null = LnFeatures(0)
1022 f_opt = LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT
1023 f_req = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
1024 f_optreq = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ | LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT
1025 self.assertFalse(f_null.supports(LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT))
1026 self.assertFalse(f_null.supports(LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ))
1027 self.assertTrue(f_opt.supports(LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT))
1028 self.assertTrue(f_opt.supports(LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ))
1029 self.assertTrue(f_req.supports(LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT))
1030 self.assertTrue(f_req.supports(LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ))
1031 self.assertTrue(f_optreq.supports(LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT))
1032 self.assertTrue(f_optreq.supports(LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ))
1033 with self.assertRaises(ValueError):
1034 f_opt.supports(f_optreq)
1035 with self.assertRaises(ValueError):
1036 f_optreq.supports(f_optreq)
1037 f1 = LnFeatures.BASIC_MPP_OPT | LnFeatures.PAYMENT_SECRET_REQ | LnFeatures.OPTION_DATA_LOSS_PROTECT_OPT | LnFeatures.VAR_ONION_OPT
1038 self.assertTrue(f1.supports(LnFeatures.PAYMENT_SECRET_OPT))
1039 self.assertTrue(f1.supports(LnFeatures.BASIC_MPP_REQ))
1040 self.assertFalse(f1.supports(LnFeatures.OPTION_STATIC_REMOTEKEY_OPT))
1041 self.assertFalse(f1.supports(LnFeatures.OPTION_TRAMPOLINE_ROUTING_REQ_ELECTRUM))
1042
1043 def test_lnworker_decode_channel_update_msg(self):
1044 msg_without_prefix = bytes.fromhex("439b71c8ddeff63004e4ff1f9764a57dcf20232b79d9d669aef0e31c42be8e44208f7d868d0133acb334047f30e9399dece226ccd98e5df5330adf7f356290516fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000008762700054a00005ef2cf9c0101009000000000000003e80000000000000001000000002367b880")
1045 # good messages
1046 self.assertNotEqual(
1047 None,
1048 LNWallet._decode_channel_update_msg(msg_without_prefix))
1049 self.assertNotEqual(
1050 None,
1051 LNWallet._decode_channel_update_msg(bytes.fromhex("0102") + msg_without_prefix))
1052 # bad messages
1053 self.assertEqual(
1054 None,
1055 LNWallet._decode_channel_update_msg(bytes.fromhex("0102030405")))
1056 self.assertEqual(
1057 None,
1058 LNWallet._decode_channel_update_msg(bytes.fromhex("ffff") + msg_without_prefix))
1059 self.assertEqual(
1060 None,
1061 LNWallet._decode_channel_update_msg(bytes.fromhex("0101") + msg_without_prefix))
1062
1063 def test_channel_type__complies_with_features(self):
1064 """test compliance and-non compliance with LN features"""
1065 # non-cflag-related peer_features should be ignored (e.g. BASIC_MPP_REQ):
1066 pfeatures = LnFeatures(LnFeatures.BASIC_MPP_REQ | LnFeatures.OPTION_STATIC_REMOTEKEY_OPT)
1067 ctype = ChannelType.OPTION_STATIC_REMOTEKEY
1068 self.assertTrue(ctype.complies_with_features(pfeatures))
1069
1070 # SRK missing from pfeatures:
1071 pfeatures = LnFeatures(LnFeatures.BASIC_MPP_REQ | LnFeatures.OPTION_TRAMPOLINE_ROUTING_OPT_ELECTRUM)
1072 ctype = ChannelType.OPTION_STATIC_REMOTEKEY
1073 self.assertFalse(ctype.complies_with_features(pfeatures))
1074
1075 # ANCHORS missing from pfeatures:
1076 pfeatures = LnFeatures(LnFeatures.BASIC_MPP_REQ | LnFeatures.OPTION_STATIC_REMOTEKEY_OPT)
1077 ctype = ChannelType.OPTION_STATIC_REMOTEKEY | ChannelType.OPTION_ANCHORS
1078 self.assertFalse(ctype.complies_with_features(pfeatures))
1079
1080 # SRK channel_type still allowed, even though ANCHORS has been negotiated:
1081 pfeatures = LnFeatures(LnFeatures.BASIC_MPP_REQ | LnFeatures.OPTION_STATIC_REMOTEKEY_OPT | LnFeatures.OPTION_ANCHORS_OPT)
1082 ctype = ChannelType.OPTION_STATIC_REMOTEKEY
1083 self.assertTrue(ctype.complies_with_features(pfeatures))
1084 # same, *even if* ANCHORS is set to REQ. OPT or REQ no longer matters after negotiation finishes:
1085 pfeatures = LnFeatures(LnFeatures.BASIC_MPP_REQ | LnFeatures.OPTION_STATIC_REMOTEKEY_OPT | LnFeatures.OPTION_ANCHORS_REQ)
1086 ctype = ChannelType.OPTION_STATIC_REMOTEKEY
1087 self.assertTrue(ctype.complies_with_features(pfeatures))
1088
1089 # ANCHORS ctype allowed if pfeatures are correctly negotiated:
1090 pfeatures = LnFeatures(LnFeatures.BASIC_MPP_REQ | LnFeatures.OPTION_STATIC_REMOTEKEY_OPT | LnFeatures.OPTION_ANCHORS_OPT)
1091 ctype = ChannelType.OPTION_STATIC_REMOTEKEY | ChannelType.OPTION_ANCHORS
1092 self.assertTrue(ctype.complies_with_features(pfeatures))
1093 # again, OPT or REQ does not matter:
1094 pfeatures = LnFeatures(LnFeatures.BASIC_MPP_REQ | LnFeatures.OPTION_STATIC_REMOTEKEY_OPT | LnFeatures.OPTION_ANCHORS_REQ)
1095 ctype = ChannelType.OPTION_STATIC_REMOTEKEY | ChannelType.OPTION_ANCHORS
1096 self.assertTrue(ctype.complies_with_features(pfeatures))
1097
1098 def test_to_tlv_bytes(self):
1099 features = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ
1100 self.assertEqual(features.to_tlv_bytes(), bfh('01'))
1101 features = LnFeatures.OPTION_ROUTE_BLINDING_OPT
1102 self.assertEqual(features.to_tlv_bytes(), bfh('02000000'))
1103 features = LnFeatures.OPTION_DATA_LOSS_PROTECT_REQ |\
1104 LnFeatures.OPTION_ROUTE_BLINDING_OPT |\
1105 LnFeatures.BASIC_MPP_OPT
1106 self.assertEqual(features.to_tlv_bytes(), bfh('02020001'))
1107 self.assertEqual(LnFeatures(0).to_tlv_bytes(), b'')
1108
1109 @as_testnet
1110 async def test_decode_imported_channel_backup_v0(self):
1111 encrypted_cb = "channel_backup:Adn87xcGIs9H2kfp4VpsOaNKWCHX08wBoqq37l1cLYKGlJamTeoaLEwpJA81l1BXF3GP/mRxqkY+whZG9l51G8izIY/kmMSvnh0DOiZEdwaaT/1/MwEHfsEomruFqs+iW24SFJPHbMM7f80bDtIxcLfZkKmgcKBAOlcqtq+dL3U3yH74S8BDDe2L4snaxxpCjF0JjDMBx1UR/28D+QlIi+lbvv1JMaCGXf+AF1+3jLQf8+lVI+rvFdyArws6Ocsvjf+ANQeSGUwW6Nb2xICQcMRgr1DO7bO4pgGu408eYRr2v3ayJBVtnKwSwd49gF5SDSjTDAO4CCM0uj9H5RxyzH7fqotkd9J80MBr84RiBXAeXKz+Ap8608/FVqgQ9BOcn6LhuAQdE5zXpmbQyw5jUGkPvHuseR+rzthzncy01odUceqTNg=="
1112 config = SimpleConfig({'electrum_path': self.electrum_path})
1113 d = restore_wallet_from_text__for_unittest("9dk", path=None, config=config)
1114 wallet1 = d['wallet'] # type: Standard_Wallet
1115 decoded_cb = ImportedChannelBackupStorage.from_encrypted_str(encrypted_cb, password=wallet1.get_fingerprint())
1116 self.assertEqual(
1117 ImportedChannelBackupStorage(
1118 funding_txid='97767fdefef3152319363b772914d71e5eb70e793b835c13dce20037d3ac13fe',
1119 funding_index=1,
1120 funding_address='tb1qfsxllwl2edccpar9jas9wsxd4vhcewlxqwmn0w27kurkme3jvkdqn4msdp',
1121 is_initiator=True,
1122 node_id=bfh('02bf82e22f99dcd7ac1de4aad5152ce48f0694c46ec582567f379e0adbf81e2d0f'),
1123 privkey=bfh('7e634853dc47f0bc2f2e0d1054b302fcb414371ddbd889f29ba8aa4e8b62c772'),
1124 host='lightning.electrum.org',
1125 port=9739,
1126 channel_seed=bfh('ce9bad44ff8521d9f57fd202ad7cdedceb934f0056f42d0f3aa7a576b505332a'),
1127 local_delay=1008,
1128 remote_delay=720,
1129 remote_payment_pubkey=bfh('02a1bbc818e2e88847016a93c223eb4adef7bb8becb3709c75c556b6beb3afe7bd'),
1130 remote_revocation_pubkey=bfh('022f28b7d8d1f05768ada3df1b0966083b8058e1e7197c57393e302ec118d7f0ae'),
1131 local_payment_pubkey=None,
1132 multisig_funding_privkey=None,
1133 ),
1134 decoded_cb,
1135 )
1136
1137 @as_testnet
1138 async def test_decode_imported_channel_backup_v1(self):
1139 encrypted_cb = "channel_backup:AVYIedu0qSLfY2M2bBxF6dA4RAxcmobp+3h9mxALWWsv5X7hhNg0XYOKNd11FE6BJOZgZnIZ4CCAlHtLNj0/9S5GbNhbNZiQXxeHMwC1lHvtjawkwSejIJyOI52DkDFHBAGZRd4fJjaPJRHnUizWfySVR4zjd08lTinpoIeL7C7tXBW1N6YqceqV7RpeoywlBXJtFfCCuw0hnUKgq3SMlBKapkNAIgGrg15aIHNcYeENxCxr5FD1s7DIwFSECqsBVnu/Ogx2oii8BfuxqJq8vuGq4Ib/BVaSVtdb2E1wklAor/CG0p9Fg9mFWND98JD+64nz9n/knPFFyHxTXErn+ct3ZcStsLYynWKUIocgu38PtzCJ7r5ivqOw4O49fbbzdjcgMUGklPYxjuinETneCo+dCPa1uepOGTqeOYmnjVYtYZYXOlWV1F5OtNoM7MwwJjAbz84="
1140 config = SimpleConfig({'electrum_path': self.electrum_path})
1141 d = restore_wallet_from_text__for_unittest("9dk", path=None, config=config)
1142 wallet1 = d['wallet'] # type: Standard_Wallet
1143 decoded_cb = ImportedChannelBackupStorage.from_encrypted_str(encrypted_cb, password=wallet1.get_fingerprint())
1144 self.assertEqual(
1145 ImportedChannelBackupStorage(
1146 funding_txid='97767fdefef3152319363b772914d71e5eb70e793b835c13dce20037d3ac13fe',
1147 funding_index=1,
1148 funding_address='tb1qfsxllwl2edccpar9jas9wsxd4vhcewlxqwmn0w27kurkme3jvkdqn4msdp',
1149 is_initiator=True,
1150 node_id=bfh('02bf82e22f99dcd7ac1de4aad5152ce48f0694c46ec582567f379e0adbf81e2d0f'),
1151 privkey=bfh('7e634853dc47f0bc2f2e0d1054b302fcb414371ddbd889f29ba8aa4e8b62c772'),
1152 host='195.201.207.61',
1153 port=9739,
1154 channel_seed=bfh('ce9bad44ff8521d9f57fd202ad7cdedceb934f0056f42d0f3aa7a576b505332a'),
1155 local_delay=1008,
1156 remote_delay=720,
1157 remote_payment_pubkey=bfh('02a1bbc818e2e88847016a93c223eb4adef7bb8becb3709c75c556b6beb3afe7bd'),
1158 remote_revocation_pubkey=bfh('022f28b7d8d1f05768ada3df1b0966083b8058e1e7197c57393e302ec118d7f0ae'),
1159 local_payment_pubkey=bfh('0308d686712782a44b0cef220485ad83dae77853a5bf8501a92bb79056c9dcb25a'),
1160 multisig_funding_privkey=None,
1161 ),
1162 decoded_cb,
1163 )
1164
1165 async def test_payment_fee_budget(self):
1166 config = SimpleConfig()
1167 # test value above cutoff
1168 invoice_amount_msat = 1_000_000 * 1000
1169 budget = PaymentFeeBudget.from_invoice_amount(
1170 invoice_amount_msat=invoice_amount_msat,
1171 config=config,
1172 )
1173 reversed_fee_msat = PaymentFeeBudget.reverse_from_total_amount(
1174 total_amount_msat=invoice_amount_msat + budget.fee_msat,
1175 config=config,
1176 )
1177 self.assertGreater(budget.fee_msat, config.LIGHTNING_PAYMENT_FEE_CUTOFF_MSAT)
1178 self.assertEqual(reversed_fee_msat, budget.fee_msat)
1179
1180 # test value below cutoff
1181 invoice_amount_msat = 1000
1182 budget = PaymentFeeBudget.from_invoice_amount(
1183 invoice_amount_msat=invoice_amount_msat,
1184 config=config,
1185 )
1186 reversed_fee_msat = PaymentFeeBudget.reverse_from_total_amount(
1187 total_amount_msat=invoice_amount_msat + budget.fee_msat,
1188 config=config,
1189 )
1190 self.assertEqual(budget.fee_msat, config.LIGHTNING_PAYMENT_FEE_CUTOFF_MSAT)
1191 self.assertEqual(reversed_fee_msat, budget.fee_msat)
1192
1193