vls-core 1.0.0-rc.1

A library for implementing a Lightning signer, which externalizes and secures cryptographic operations.
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
use crate::prelude::*;
use crate::tx::script::ANCHOR_OUTPUT_VALUE_SATOSHI;
use anyhow::anyhow;
use bitcoin::consensus::Encodable;
use bitcoin::io::sink;
use bitcoin::secp256k1::ecdsa::Signature;
use bitcoin::secp256k1::{All, PublicKey, Secp256k1};
use bitcoin::sighash::EcdsaSighashType;
use bitcoin::transaction::Version;
use bitcoin::{Amount, CompressedPublicKey, ScriptBuf, Sequence, TxIn, Witness, WitnessProgram};
use bitcoin::{Transaction, TxOut, VarInt};
use lightning::ln::chan_utils::{
    get_commitment_transaction_number_obscure_factor, get_revokeable_redeemscript,
    get_to_countersigner_keyed_anchor_redeemscript, make_funding_redeemscript,
    ChannelTransactionParameters, TxCreationKeys,
};
use lightning::sign::{
    DelayedPaymentOutputDescriptor, SpendableOutputDescriptor, StaticPaymentOutputDescriptor,
};

/// The maximum value of an input or output in milli satoshi
pub const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000;

/// The minimum value of the dust limit in satoshis - for p2wsh outputs
/// (such as anchors)
pub const MIN_DUST_LIMIT_SATOSHIS: u64 = 330;
/// The minimum value of the dust limit in satoshis - for segwit in general
/// This is also the minimum negotiated dust limit
pub const MIN_CHAN_DUST_LIMIT_SATOSHIS: u64 = 354;

/// The expected weight of a commitment transaction
pub(crate) fn expected_commitment_tx_weight(opt_anchors: bool, num_untrimmed_htlc: usize) -> usize {
    const COMMITMENT_TX_BASE_WEIGHT: usize = 724;
    const COMMITMENT_TX_BASE_ANCHOR_WEIGHT: usize = 1124;
    const COMMITMENT_TX_WEIGHT_PER_HTLC: usize = 172;
    let base_weight =
        if opt_anchors { COMMITMENT_TX_BASE_ANCHOR_WEIGHT } else { COMMITMENT_TX_BASE_WEIGHT };
    base_weight + num_untrimmed_htlc * COMMITMENT_TX_WEIGHT_PER_HTLC
}

/// The weight of a mutual close transaction.
pub(crate) fn mutual_close_tx_weight(unsigned_tx: &Transaction) -> usize {
    // NOTE related to issue 165 - we use 72 here because we might as well assume low-S
    // for the signature, and some node implementations use that.
    // However, nodes may use 73 to be consistent with BOLT-3.
    // That's OK because we will be more lenient on the fee.
    const EXPECTED_MUTUAL_CLOSE_WITNESS_WEIGHT: usize = //
        2 + 1 + 4 + // witness-marker-and-flag witness-element-count 4-element-lengths
        72 + 72 + // <signature_for_pubkey1> <signature_for_pubkey2>
        1 + 1 + 33 + 1 + 33 + 1 + 1; // 2 <pubkey1> <pubkey2> 2 OP_CHECKMULTISIG
    unsigned_tx.weight().to_wu() as usize + EXPECTED_MUTUAL_CLOSE_WITNESS_WEIGHT
}

/// Estimated weight of a sweep transaction (justice, delayed, counterparty HTLC).
pub(crate) fn estimated_sweep_tx_weight(unsigned_tx: &Transaction) -> usize {
    // Per-type witness weight breakdown (per BOLT 3):
    //   to_local / justice sweeps:  ~157-158 WU (overestimate)
    //   Received HTLC timeout:      ~220 WU     (matches)
    //   Offered HTLC preimage:      ~245-248 WU (underestimate by ~25-28 WU, ~4% of total tx weight)
    // 220 is used as a middle-ground estimate across all sweep variants.
    // Per-type witness weights are deferred to issue #522.
    const EXPECTED_SWEEP_WITNESS_WEIGHT: usize = 220;
    unsigned_tx.weight().to_wu() as usize + EXPECTED_SWEEP_WITNESS_WEIGHT
}

/// Possibly adds a change output to the given transaction, always doing so if there are excess
/// funds available beyond the requested feerate.
/// Assumes at least one input will have a witness (ie spends a segwit output).
/// Returns an Err(()) if the requested feerate cannot be met.
pub fn maybe_add_change_output(
    tx: &mut Transaction,
    input_value: u64,
    witness_max_weight: u64,
    feerate_sat_per_1000_weight: u32,
    change_destination_script: ScriptBuf,
) -> Result<(), ()> {
    if input_value > MAX_VALUE_MSAT / 1000 {
        //bail!("Input value is greater than max satoshis");
        return Err(());
    }

    let mut output_value = Amount::ZERO;
    for output in tx.output.iter() {
        output_value = output_value.checked_add(output.value).unwrap();
        if output_value.to_sat() >= input_value {
            // bail!("Ouput value equals or exceeds input value");
            return Err(());
        }
    }

    let dust_value = change_destination_script.minimal_non_dust();
    let mut change_output = TxOut { script_pubkey: change_destination_script, value: Amount::ZERO };
    let change_len = change_output.consensus_encode(&mut sink()).map_err(|_| ())?;
    let mut weight_with_change: i64 =
        tx.weight().to_wu() as i64 + 2 + witness_max_weight as i64 + change_len as i64 * 4;
    // Include any extra bytes required to push an extra output.
    weight_with_change += (VarInt(tx.output.len() as u64 + 1).size()
        - VarInt(tx.output.len() as u64).size()) as i64
        * 4;
    // When calculating weight, add two for the flag bytes
    let difference = input_value.checked_sub(output_value.to_sat()).ok_or(())?;
    let change_value: i64 =
        difference as i64 - weight_with_change * feerate_sat_per_1000_weight as i64 / 1000;
    if change_value >= dust_value.to_sat() as i64 {
        change_output.value = Amount::from_sat(change_value as u64);
        tx.output.push(change_output);
    } else if difference as i64
        - (tx.weight().to_wu() as i64 + 2 + witness_max_weight as i64)
            * feerate_sat_per_1000_weight as i64
            / 1000
        < 0
    {
        // bail!("Requested fee rate cannot be met");
        return Err(());
    }

    Ok(())
}

/// Estimate the feerate for an HTLC transaction
pub(crate) fn estimate_feerate_per_kw(total_fee: u64, weight: u64) -> u32 {
    // we want the highest feerate that can give rise to this total fee
    (((total_fee * 1000) + 999) / weight) as u32
}

pub(crate) fn add_holder_sig(
    tx: &mut Transaction,
    holder_sig: Signature,
    counterparty_sig: Signature,
    holder_funding_key: &PublicKey,
    counterparty_funding_key: &PublicKey,
) {
    let funding_redeemscript =
        make_funding_redeemscript(&holder_funding_key, &counterparty_funding_key);

    tx.input[0].witness.push(Vec::new());
    let mut ser_holder_sig = holder_sig.serialize_der().to_vec();
    ser_holder_sig.push(EcdsaSighashType::All as u8);
    let mut ser_cp_sig = counterparty_sig.serialize_der().to_vec();
    ser_cp_sig.push(EcdsaSighashType::All as u8);

    let holder_sig_first =
        holder_funding_key.serialize()[..] < counterparty_funding_key.serialize()[..];

    if holder_sig_first {
        tx.input[0].witness.push(ser_holder_sig);
        tx.input[0].witness.push(ser_cp_sig);
    } else {
        tx.input[0].witness.push(ser_cp_sig);
        tx.input[0].witness.push(ser_holder_sig);
    }

    tx.input[0].witness.push(funding_redeemscript.as_bytes().to_vec());
}

pub(crate) fn is_tx_non_malleable(tx: &Transaction, segwit_flags: &[bool]) -> bool {
    assert_eq!(tx.input.len(), segwit_flags.len(), "tx and segwit_flags must have same length");
    segwit_flags.iter().all(|flag| *flag)
}

/// Decode a commitment transaction and return the outputs that we need to watch.
/// Our main output index and any HTLC output indexes are returned.
///
/// `cp_per_commitment_point` is filled in if known, otherwise None.  It might
/// not be known if the signer is old, before we started collecting counterparty secrets.
/// If it is None, then we won't be able to tell the difference between a counterparty
/// to-self output and an HTLC output.
///
/// The counterparty parameters must be populated.
pub fn decode_commitment_tx(
    tx: &Transaction,
    holder_per_commitment_point: &PublicKey,
    cp_per_commitment_point: &Option<PublicKey>,
    params: &ChannelTransactionParameters,
    secp_ctx: &Secp256k1<All>,
) -> (Option<u32>, Vec<u32>) {
    let cp_params = params.counterparty_parameters.as_ref().unwrap();

    let opt_anchors = params.channel_type_features.supports_anchors_nonzero_fee_htlc_tx()
        || params.channel_type_features.supports_anchors_zero_fee_htlc_tx();
    let holder_pubkeys = &params.holder_pubkeys;
    let cp_pubkeys = &cp_params.pubkeys;

    let holder_non_delayed_script = if opt_anchors {
        get_to_countersigner_keyed_anchor_redeemscript(&holder_pubkeys.payment_point).to_p2wsh()
    } else {
        let bitcoin_key =
            CompressedPublicKey::from_slice(&holder_pubkeys.payment_point.serialize()).unwrap();
        let prog = WitnessProgram::p2wpkh(&bitcoin_key);
        ScriptBuf::new_witness_program(&prog)
    };

    // compute the transaction keys we would have used if this is a holder commitment
    let holder_tx_keys = TxCreationKeys::derive_new(
        secp_ctx,
        &holder_per_commitment_point,
        &holder_pubkeys.delayed_payment_basepoint,
        &holder_pubkeys.htlc_basepoint,
        &cp_pubkeys.revocation_basepoint,
        &cp_pubkeys.htlc_basepoint,
    );

    let holder_delayed_redeem_script = get_revokeable_redeemscript(
        &holder_tx_keys.revocation_key,
        cp_params.selected_contest_delay,
        &holder_tx_keys.broadcaster_delayed_payment_key,
    );

    let holder_delayed_script = holder_delayed_redeem_script.to_p2wsh();

    let cp_delayed_script = if let Some(cp_per_commitment_point) = cp_per_commitment_point {
        // compute the transaction keys we would have used if this is a holder commitment
        let cp_tx_keys = TxCreationKeys::derive_new(
            secp_ctx,
            &cp_per_commitment_point,
            &cp_pubkeys.delayed_payment_basepoint,
            &cp_pubkeys.htlc_basepoint,
            &holder_pubkeys.revocation_basepoint,
            &holder_pubkeys.htlc_basepoint,
        );

        let cp_delayed_redeem_script = get_revokeable_redeemscript(
            &cp_tx_keys.revocation_key,
            params.holder_selected_contest_delay,
            &cp_tx_keys.broadcaster_delayed_payment_key,
        );

        Some(cp_delayed_redeem_script.to_p2wsh())
    } else {
        None
    };

    let mut htlcs = Vec::new();
    let mut main_output_index = None;

    // find the output that pays to us, if any
    for (idx, output) in tx.output.iter().enumerate() {
        // we don't track anchors
        if output.value == ANCHOR_OUTPUT_VALUE_SATOSHI {
            continue;
        }

        if Some(&output.script_pubkey) == cp_delayed_script.as_ref() {
            continue;
        }

        // look for our main output, either when broadcast by us or by our counterparty
        if output.script_pubkey == holder_non_delayed_script
            || output.script_pubkey == holder_delayed_script
        {
            main_output_index = Some(idx as u32);
        } else if output.script_pubkey.is_p2wsh() {
            htlcs.push(idx as u32);
        }
    }

    (main_output_index, htlcs)
}

/// Decode a commitment transaction and return the commitment number if it is a commitment tx
pub fn decode_commitment_number(
    tx: &Transaction,
    params: &ChannelTransactionParameters,
) -> Option<u64> {
    let holder_pubkeys = &params.holder_pubkeys;
    let cp_params = params.counterparty_parameters.as_ref().unwrap();
    let cp_pubkeys = &cp_params.pubkeys;

    let obscure_factor = get_commitment_transaction_number_obscure_factor(
        &holder_pubkeys.payment_point,
        &cp_pubkeys.payment_point,
        params.is_outbound_from_holder,
    );

    // if the tx has more than one input, it's not a standard closing tx,
    // so we bail
    if tx.input.len() != 1 {
        return None;
    }

    // check if the input sequence and locktime are set to standard commitment tx values
    if (tx.input[0].sequence.0 >> 8 * 3) as u8 != 0x80
        || (tx.lock_time.to_consensus_u32() >> 8 * 3) as u8 != 0x20
    {
        return None;
    }

    // forward counting
    let commitment_number = (((tx.input[0].sequence.0 as u64 & 0xffffff) << 3 * 8)
        | (tx.lock_time.to_consensus_u32() as u64 & 0xffffff))
        ^ obscure_factor;
    Some(commitment_number)
}

/// Create a spending transaction, helper function used in [`KeysManagerClient::spend_spendable_outputs`].
///
/// [`KeysManagerClient::spend_spendable_outputsspend_spendable_outputs`] vls_protocol_client::KeysManagerClient::spend_spendable_outputsspend_spendable_outputs
pub fn create_spending_transaction(
    descriptors: &[&SpendableOutputDescriptor],
    outputs: Vec<TxOut>,
    change_destination_script: ScriptBuf,
    feerate_sats_per_1000_weight: u32,
) -> anyhow::Result<Transaction> {
    let mut input = Vec::new();
    let mut input_value = Amount::ZERO;
    let mut witness_weight = 0;
    let mut output_set = UnorderedSet::with_capacity(descriptors.len());
    for outp in descriptors {
        match outp {
            SpendableOutputDescriptor::StaticPaymentOutput(descriptor) => {
                input.push(TxIn {
                    previous_output: descriptor.outpoint.into_bitcoin_outpoint(),
                    script_sig: ScriptBuf::new(),
                    sequence: Sequence(1),
                    witness: Witness::new(),
                });
                witness_weight += StaticPaymentOutputDescriptor::max_witness_length(descriptor);
                input_value = input_value.checked_add(descriptor.output.value).unwrap();
                if !output_set.insert(descriptor.outpoint) {
                    return Err(anyhow!("duplicate"));
                }
            }
            SpendableOutputDescriptor::DelayedPaymentOutput(descriptor) => {
                input.push(TxIn {
                    previous_output: descriptor.outpoint.into_bitcoin_outpoint(),
                    script_sig: ScriptBuf::new(),
                    sequence: Sequence(descriptor.to_self_delay as u32),
                    witness: Witness::new(),
                });
                witness_weight += DelayedPaymentOutputDescriptor::MAX_WITNESS_LENGTH;
                input_value = input_value.checked_add(descriptor.output.value).unwrap();
                if !output_set.insert(descriptor.outpoint) {
                    return Err(anyhow!("duplicate"));
                }
            }

            SpendableOutputDescriptor::StaticOutput {
                ref outpoint,
                ref output,
                channel_keys_id: _,
            } => {
                input.push(TxIn {
                    previous_output: outpoint.into_bitcoin_outpoint(),
                    script_sig: ScriptBuf::new(),
                    sequence: Sequence::ZERO,
                    witness: Witness::default(),
                });
                witness_weight += 1 + 73 + 34;
                input_value = input_value.checked_add(output.value).unwrap();
                if !output_set.insert(*outpoint) {
                    return Err(anyhow!("duplicate"));
                }
            }
        }

        if input_value.to_sat() > MAX_VALUE_MSAT / 1000 {
            return Err(anyhow!("overflow"));
        }
    }

    let mut spend_tx = Transaction {
        version: Version::TWO,
        lock_time: bitcoin::absolute::LockTime::ZERO,
        input,
        output: outputs,
    };
    maybe_add_change_output(
        &mut spend_tx,
        input_value.to_sat(),
        witness_weight,
        feerate_sats_per_1000_weight,
        change_destination_script,
    )
    .map_err(|()| anyhow!("could not add or change"))?;
    Ok(spend_tx)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::channel::ChannelBase;
    use crate::util::test_utils::{
        init_node_and_channel, make_test_channel_setup, TEST_NODE_CONFIG, TEST_SEED,
    };
    use bitcoin::consensus::deserialize;
    use bitcoin::hashes::hex::FromHex;
    use bitcoin::secp256k1::SecretKey;
    use lightning::ln::chan_utils::{htlc_success_tx_weight, htlc_timeout_tx_weight};
    use lightning::types::features::ChannelTypeFeatures;

    use bitcoin::blockdata::constants::genesis_block;
    use bitcoin::blockdata::transaction::TxIn;
    use bitcoin::hashes::Hash;
    use bitcoin::secp256k1::Secp256k1;
    use bitcoin::Network;
    use bitcoin::{Amount, ScriptBuf, Transaction, TxOut, Witness};
    use lightning::chain::transaction::OutPoint as LightningOutPoint;
    use lightning::sign::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor};

    #[test]
    fn test_parse_closing_tx_holder() {
        let secp_ctx = Secp256k1::new();
        let commitment_number = 0;
        let (node, channel_id) =
            init_node_and_channel(TEST_NODE_CONFIG, TEST_SEED[0], make_test_channel_setup());
        let params = node
            .with_channel(&channel_id, |channel| Ok(channel.make_channel_parameters()))
            .unwrap();

        let (holder_commitment, per_commitment_point) = node
            .with_channel(&channel_id, |channel| {
                let per_commitment_point = channel.get_per_commitment_point(commitment_number)?;
                Ok((
                    channel.make_holder_commitment_tx(
                        commitment_number,
                        &per_commitment_point,
                        123,
                        1000,
                        100,
                        Vec::new(),
                    ),
                    per_commitment_point,
                ))
            })
            .unwrap();
        let holder_tx = holder_commitment.trust().built_transaction().transaction.clone();
        let parsed_commitment_number = decode_commitment_number(&holder_tx, &params).unwrap();
        assert_eq!(parsed_commitment_number, commitment_number);
        let (parsed_main, htlcs) =
            decode_commitment_tx(&holder_tx, &per_commitment_point, &None, &params, &secp_ctx);
        let our_main =
            holder_tx.output.iter().position(|txout| txout.script_pubkey.is_p2wsh()).unwrap();
        assert_eq!(parsed_main, Some(our_main as u32));
        assert!(htlcs.is_empty());
    }

    #[test]
    fn test_parse_closing_tx_counterparty() {
        let secp_ctx = Secp256k1::new();
        let commitment_number = 0;
        let (node, channel_id) =
            init_node_and_channel(TEST_NODE_CONFIG, TEST_SEED[0], make_test_channel_setup());
        let params = node
            .with_channel(&channel_id, |channel| Ok(channel.make_channel_parameters()))
            .unwrap();

        let cp_per_commitment_secret = SecretKey::from_slice(&[2; 32]).unwrap();
        let cp_per_commitment_point =
            PublicKey::from_secret_key(&secp_ctx, &cp_per_commitment_secret);
        let (cp_commitment, holder_per_commitment_point) = node
            .with_channel(&channel_id, |channel| {
                // this is not used in the test because we are parsing a counterparty commitment,
                // but we need to set it to something different than the counterparty one
                let holder_per_commitment_point =
                    channel.get_per_commitment_point(commitment_number)?;
                Ok((
                    channel.make_counterparty_commitment_tx(
                        &cp_per_commitment_point,
                        commitment_number,
                        123,
                        1000,
                        100,
                        Vec::new(),
                    ),
                    holder_per_commitment_point,
                ))
            })
            .unwrap();
        let cp_tx = cp_commitment.trust().built_transaction().transaction.clone();
        let parsed_commit_number = decode_commitment_number(&cp_tx, &params).unwrap();
        assert_eq!(parsed_commit_number, commitment_number);
        let (parsed_main, htlcs) = decode_commitment_tx(
            &cp_tx,
            &holder_per_commitment_point,
            &Some(cp_per_commitment_point),
            &params,
            &secp_ctx,
        );
        let our_main =
            cp_tx.output.iter().position(|txout| txout.script_pubkey.is_p2wpkh()).unwrap();
        assert_eq!(parsed_main, Some(our_main as u32));
        println!("htlcs: {:?}", htlcs);
        assert!(htlcs.is_empty());
    }

    #[test]
    fn test_estimate_feerate() {
        let non_anchor_features = ChannelTypeFeatures::empty();
        let mut anchor_features = ChannelTypeFeatures::empty();
        anchor_features.set_anchors_zero_fee_htlc_tx_optional();
        let weights = vec![
            htlc_timeout_tx_weight(&non_anchor_features),
            htlc_timeout_tx_weight(&anchor_features),
            htlc_success_tx_weight(&non_anchor_features),
            htlc_success_tx_weight(&anchor_features),
        ];

        // make sure the feerate is not lower than 253 at the low end,
        // so as not to fail policy check
        let feerate = 253;
        for weight in &weights {
            let total_fee = (feerate as u64 * *weight) / 1000;
            let estimated_feerate = super::estimate_feerate_per_kw(total_fee, *weight);
            assert!(estimated_feerate >= 253);
        }

        // make sure that the total tx fee stays the same after estimating the rate and recomputing the fee
        // so as to recreate an identical transaction
        for feerate in (300..5000).step_by(10) {
            for weight in &weights {
                let total_fee = (feerate as u64 * *weight) / 1000;
                let estimated_feerate = super::estimate_feerate_per_kw(total_fee, *weight);
                let recovered_total_fee = (estimated_feerate as u64 * *weight) / 1000;
                assert_eq!(total_fee, recovered_total_fee);
            }
        }
    }

    #[test]
    fn test_issue_165() {
        let tx: Transaction = deserialize(&Vec::from_hex("0200000001b78e0523c17f8ac709eec54654cc849529c05584bfda6e04c92a3b670476f2a20000000000ffffffff017d4417000000000016001476168b09afc66bd3956efb25cd8b83650bda0c5f00000000").unwrap()).unwrap();
        let tx_weight = tx.weight();
        let spk = tx.output[0].script_pubkey.len();
        let weight = super::mutual_close_tx_weight(&tx);
        let fee = 1524999 - tx.output[0].value.to_sat();
        let estimated_feerate = super::estimate_feerate_per_kw(fee, weight as u64);
        let expected_tx_weight = (4 +                                           // version
            1 +                                           // input count
            36 +                                          // prevout
            1 +                                           // script length (0)
            4 +                                           // sequence
            1 +                                           // output count
            4                                             // lock time
        )*4 +                                         // * 4 for non-witness parts
            ((8+1) +                            // output values and script length
                spk as u64) * 4; // scriptpubkey and witness multiplier
        assert_eq!(expected_tx_weight, tx_weight.to_wu());
        // CLN was actually missing the pubkey length byte, so the feerate is genuinely too low
        assert_eq!(estimated_feerate, 252);
    }

    fn create_mock_transaction(outputs: Vec<TxOut>) -> Transaction {
        Transaction {
            version: Version::TWO,
            lock_time: bitcoin::absolute::LockTime::ZERO,
            input: vec![TxIn {
                previous_output: bitcoin::OutPoint::new(
                    genesis_block(Network::Bitcoin).txdata[0].compute_txid(),
                    0,
                ),
                script_sig: ScriptBuf::new(),
                sequence: Sequence::ZERO,
                witness: Witness::new(),
            }],
            output: outputs,
        }
    }

    fn create_mock_script() -> ScriptBuf {
        let hash = bitcoin::WPubkeyHash::from_slice(&[
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        ])
        .unwrap();
        ScriptBuf::new_p2wpkh(&hash)
    }

    #[test]
    fn test_maybe_add_change_output_success() {
        let mut tx = create_mock_transaction(vec![TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: create_mock_script(),
        }]);
        let input_value = 150_000;
        let witness_max_weight = 100;
        let feerate_sat_per_1000_weight = 1000;
        let change_destination_script = create_mock_script();

        let result = maybe_add_change_output(
            &mut tx,
            input_value,
            witness_max_weight,
            feerate_sat_per_1000_weight,
            change_destination_script.clone(),
        );

        assert!(result.is_ok());
        assert_eq!(tx.output.len(), 2);
        assert_eq!(
            tx.output[1].script_pubkey.to_string(),
            "OP_0 OP_PUSHBYTES_20 0102030405060708090a0b0c0d0e0f1011121314"
        );
        assert_eq!(tx.output[1].value.to_sat(), 49446);
    }

    #[test]
    fn test_maybe_add_change_output_input_too_large() {
        let mut tx = create_mock_transaction(vec![TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: create_mock_script(),
        }]);
        let input_value = MAX_VALUE_MSAT / 1000 + 1;
        let witness_max_weight = 100;
        let feerate_sat_per_1000_weight = 1000;
        let change_destination_script = create_mock_script();

        let result = maybe_add_change_output(
            &mut tx,
            input_value,
            witness_max_weight,
            feerate_sat_per_1000_weight,
            change_destination_script,
        );

        assert!(result.is_err());
    }

    #[test]
    fn test_maybe_add_change_output_output_exceeds_input() {
        let mut tx = create_mock_transaction(vec![TxOut {
            value: Amount::from_sat(200_000),
            script_pubkey: create_mock_script(),
        }]);
        let input_value = 100_000;
        let witness_max_weight = 100;
        let feerate_sat_per_1000_weight = 1000;
        let change_destination_script = create_mock_script();

        let result = maybe_add_change_output(
            &mut tx,
            input_value,
            witness_max_weight,
            feerate_sat_per_1000_weight,
            change_destination_script,
        );

        assert!(result.is_err());
    }

    #[test]
    fn test_maybe_add_change_output_insufficient_for_fee() {
        let mut tx = create_mock_transaction(vec![TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: create_mock_script(),
        }]);
        let input_value = 100_100;
        let witness_max_weight = 100;
        let feerate_sat_per_1000_weight = 1000;
        let change_destination_script = create_mock_script();

        let result = maybe_add_change_output(
            &mut tx,
            input_value,
            witness_max_weight,
            feerate_sat_per_1000_weight,
            change_destination_script,
        );

        assert!(result.is_err());
    }

    #[test]
    fn test_create_spending_transaction_static_payment() {
        let _secp = Secp256k1::new();
        let bitcoin_outpoint =
            bitcoin::OutPoint::new(genesis_block(Network::Bitcoin).txdata[0].compute_txid(), 0);
        let outpoint =
            LightningOutPoint { txid: bitcoin_outpoint.txid, index: bitcoin_outpoint.vout as u16 };
        let descriptor =
            SpendableOutputDescriptor::StaticPaymentOutput(StaticPaymentOutputDescriptor {
                outpoint,
                output: TxOut {
                    value: Amount::from_sat(100_000),
                    script_pubkey: create_mock_script(),
                },
                channel_keys_id: [0u8; 32],
                channel_value_satoshis: 100_000,
                channel_transaction_parameters: None,
            });
        let outputs =
            vec![TxOut { value: Amount::from_sat(50_000), script_pubkey: create_mock_script() }];
        let change_destination_script = create_mock_script();
        let feerate_sats_per_1000_weight = 1000;

        let result = create_spending_transaction(
            &[&descriptor],
            outputs.clone(),
            change_destination_script.clone(),
            feerate_sats_per_1000_weight,
        );

        assert!(result.is_ok());
        let tx = result.unwrap();
        assert_eq!(tx.input.len(), 1);
        assert_eq!(
            tx.input[0].previous_output.to_string(),
            "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0"
        );
        assert_eq!(tx.input[0].sequence, Sequence(1));
        assert_eq!(tx.output.len(), 2);
        assert_eq!(tx.output[0], outputs[0]);
        assert_eq!(
            tx.output[1].script_pubkey.to_string(),
            "OP_0 OP_PUSHBYTES_20 0102030405060708090a0b0c0d0e0f1011121314"
        );
    }

    #[test]
    fn test_create_spending_transaction_duplicate_outpoint() {
        let _secp = Secp256k1::new();
        let bitcoin_outpoint =
            bitcoin::OutPoint::new(genesis_block(Network::Bitcoin).txdata[0].compute_txid(), 0);
        let outpoint =
            LightningOutPoint { txid: bitcoin_outpoint.txid, index: bitcoin_outpoint.vout as u16 };
        let descriptor =
            SpendableOutputDescriptor::StaticPaymentOutput(StaticPaymentOutputDescriptor {
                outpoint,
                output: TxOut {
                    value: Amount::from_sat(100_000),
                    script_pubkey: create_mock_script(),
                },
                channel_keys_id: [0u8; 32],
                channel_value_satoshis: 100_000,
                channel_transaction_parameters: None,
            });
        let outputs =
            vec![TxOut { value: Amount::from_sat(50_000), script_pubkey: create_mock_script() }];
        let change_destination_script = create_mock_script();
        let feerate_sats_per_1000_weight = 1000;

        let result = create_spending_transaction(
            &[&descriptor, &descriptor],
            outputs,
            change_destination_script,
            feerate_sats_per_1000_weight,
        );

        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "duplicate");
    }

    #[test]
    fn test_create_spending_transaction_value_overflow() {
        let _secp = Secp256k1::new();
        let bitcoin_outpoint =
            bitcoin::OutPoint::new(genesis_block(Network::Bitcoin).txdata[0].compute_txid(), 0);
        let outpoint =
            LightningOutPoint { txid: bitcoin_outpoint.txid, index: bitcoin_outpoint.vout as u16 };
        let descriptor =
            SpendableOutputDescriptor::StaticPaymentOutput(StaticPaymentOutputDescriptor {
                outpoint,
                output: TxOut {
                    value: Amount::from_sat(MAX_VALUE_MSAT / 1000 + 1),
                    script_pubkey: create_mock_script(),
                },
                channel_keys_id: [0u8; 32],
                channel_value_satoshis: MAX_VALUE_MSAT / 1000 + 1,
                channel_transaction_parameters: None,
            });
        let outputs =
            vec![TxOut { value: Amount::from_sat(50_000), script_pubkey: create_mock_script() }];
        let change_destination_script = create_mock_script();
        let feerate_sats_per_1000_weight = 1000;

        let result = create_spending_transaction(
            &[&descriptor],
            outputs,
            change_destination_script,
            feerate_sats_per_1000_weight,
        );

        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "overflow");
    }
}