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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
use crate::prelude::*;
use core::cmp;
use core::fmt;

use crate::signer::vls_channel_signer::VlsChannelSigner;
use bitcoin::blockdata::opcodes::all::{
    OP_CHECKMULTISIG, OP_CHECKSIG, OP_CHECKSIGVERIFY, OP_CLTV, OP_CSV, OP_DROP, OP_DUP, OP_ELSE,
    OP_ENDIF, OP_EQUAL, OP_EQUALVERIFY, OP_HASH160, OP_IF, OP_IFDUP, OP_NOTIF, OP_PUSHNUM_1,
    OP_PUSHNUM_16, OP_PUSHNUM_2, OP_SIZE, OP_SWAP,
};
use bitcoin::secp256k1::{PublicKey, Secp256k1};
use bitcoin::{Address, Amount, Network, ScriptBuf};
use bitcoin::{Script, TxOut};
use lightning::types::payment::PaymentHash;

use serde_derive::{Deserialize, Serialize};
use serde_with::{serde_as, Bytes, IfIsHumanReadable};

use crate::channel::ChannelSetup;
use crate::policy::error::{
    mismatch_error, script_format_error, transaction_format_error, ValidationError,
};
use crate::tx::script::{expect_data, expect_number, expect_op, expect_script_end};
use crate::util::debug_utils::{DebugBytes, DebugPayload};
use crate::util::AddedItemsIter;
use vls_common::HexEncode;

const MAX_DELAY: i64 = 2016;
/// Value for anchor outputs
pub(crate) const ANCHOR_SAT: Amount = Amount::from_sat(330);

/// Phase 1 HTLC info
#[derive(Clone)]
pub struct HTLCInfo {
    /// HTLC value
    pub value_sat: u64,
    /// RIPEMD160 of 32 bytes hash
    pub payment_hash_hash: [u8; 20],
    /// This is zero (unknown) for offered HTLCs in phase 1
    pub cltv_expiry: u32,
}

// Implement manually so we can have hex encoded payment_hash_hash.
impl fmt::Debug for HTLCInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HTLCInfo")
            .field("value_sat", &self.value_sat)
            .field("payment_hash_hash", &DebugBytes(&self.payment_hash_hash))
            .field("cltv_expiry", &self.cltv_expiry)
            .finish()
    }
}

/// A helper for serializing PaymentHash
#[serde_as]
#[derive(Serialize, Deserialize)]
#[serde(remote = "PaymentHash")]
pub struct PaymentHashDef(#[serde_as(as = "IfIsHumanReadable<_, Bytes>")] pub [u8; 32]);

/// HTLC information (Phase 2).
///
/// Represents a single HTLC with full payment details including CLTV expiry.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HTLCInfo2 {
    /// HTLC value in satoshis
    pub value_sat: u64,
    /// Payment hash for this HTLC
    #[serde(with = "PaymentHashDef")]
    pub payment_hash: PaymentHash,
    /// CLTV expiry block height, it is zero for offered HTLCs in phase 1
    pub cltv_expiry: u32,
}

// Implement manually because PaymentHash doesn't support
impl Ord for HTLCInfo2 {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.value_sat
            .cmp(&other.value_sat)
            .then_with(|| self.payment_hash.0.cmp(&other.payment_hash.0))
            .then_with(|| self.cltv_expiry.cmp(&other.cltv_expiry))
    }
}

// Implement manually because PaymentHash doesn't support
impl PartialOrd for HTLCInfo2 {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

// Implement manually so we can have hex encoded payment_hash.
impl fmt::Debug for HTLCInfo2 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HTLCInfo2")
            .field("value_sat", &self.value_sat)
            .field("payment_hash", &DebugBytes(&self.payment_hash.0))
            .field("cltv_expiry", &self.cltv_expiry)
            .finish()
    }
}

/// This trait answers whether a preimage is known
pub trait PreimageMap {
    /// Whether a preimage is known for the payment hash
    fn has_preimage(&self, hash: &PaymentHash) -> bool;
}

/// Commitment transaction information (Phase 2).
///
/// Contains the state of a commitment transaction including output values,
/// HTLCs, and feerate. HTLC vectors are kept sorted for canonical comparison.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommitmentInfo2 {
    /// Whether the counterparty is the broadcaster of this commitment
    pub is_counterparty_broadcaster: bool,
    /// Value in satoshis going to the countersigner
    pub to_countersigner_value_sat: u64,
    /// Value in satoshis going to the broadcaster
    pub to_broadcaster_value_sat: u64,
    /// HTLCs offered by the broadcaster
    pub offered_htlcs: Vec<HTLCInfo2>,
    /// HTLCs received by the broadcaster
    pub received_htlcs: Vec<HTLCInfo2>,
    /// Fee rate in satoshis per 1000 weight units
    pub feerate_per_kw: u32,
}

impl CommitmentInfo2 {
    /// Construct a normalized CommitmentInfo2
    /// The caller is responsible for checking that the values are valid (e.g. do not overflow/
    /// underflow).
    pub fn new(
        is_counterparty_broadcaster: bool,
        to_countersigner_value_sat: u64,
        to_broadcaster_value_sat: u64,
        mut offered_htlcs: Vec<HTLCInfo2>,
        mut received_htlcs: Vec<HTLCInfo2>,
        feerate_per_kw: u32,
    ) -> CommitmentInfo2 {
        offered_htlcs.sort();
        received_htlcs.sort();
        CommitmentInfo2 {
            is_counterparty_broadcaster,
            to_countersigner_value_sat,
            to_broadcaster_value_sat,
            offered_htlcs,
            received_htlcs,
            feerate_per_kw,
        }
    }

    /// Returns true if there are no pending HTLCS
    pub fn htlcs_is_empty(&self) -> bool {
        self.offered_htlcs.is_empty() && self.received_htlcs.is_empty()
    }

    /// Returns offered HTLCs added and removed in new commitment tx
    pub fn delta_offered_htlcs<'a>(
        &'a self,
        new: &'a CommitmentInfo2,
    ) -> (AddedItemsIter<'a, HTLCInfo2>, AddedItemsIter<'a, HTLCInfo2>) {
        (
            AddedItemsIter::new(&self.offered_htlcs, &new.offered_htlcs),
            AddedItemsIter::new(&new.offered_htlcs, &self.offered_htlcs),
        )
    }

    /// Returns offered HTLCs added and removed in new commitment tx
    pub fn delta_received_htlcs<'a>(
        &'a self,
        new: &'a CommitmentInfo2,
    ) -> (AddedItemsIter<'a, HTLCInfo2>, AddedItemsIter<'a, HTLCInfo2>) {
        (
            AddedItemsIter::new(&self.received_htlcs, &new.received_htlcs),
            AddedItemsIter::new(&new.received_htlcs, &self.received_htlcs),
        )
    }

    /// Value in satoshis to holder and counterparty, respectively.
    /// Does not include HTLCs.
    pub fn value_to_parties(&self) -> (u64, u64) {
        if self.is_counterparty_broadcaster {
            (self.to_countersigner_value_sat, self.to_broadcaster_value_sat)
        } else {
            (self.to_broadcaster_value_sat, self.to_countersigner_value_sat)
        }
    }

    /// The total output value of this transaction.
    /// This is smaller than the total channel value, due to on-chain fees.
    pub fn total_value(&self) -> u64 {
        self.to_broadcaster_value_sat
            + self.to_countersigner_value_sat
            + self.offered_htlcs.iter().map(|h| h.value_sat).sum::<u64>()
            + self.received_htlcs.iter().map(|h| h.value_sat).sum::<u64>()
    }

    /// Compute claimable balance in sat, defined as the sum of:
    /// - the output to us
    /// - HTLCs offered to us for which the preimage is known
    /// - HTLCs we offer for which the preimage is unknown
    pub fn claimable_balance<T: PreimageMap>(
        &self,
        preimage_map: &T,
        is_outbound: bool,
        channel_value: u64,
    ) -> u64 {
        let mut balance = self.value_to_parties().0;
        if is_outbound {
            let total_value = self.total_value();
            let fee = channel_value
                .checked_sub(total_value)
                .expect("channel_value should be >= total_value");
            balance = balance
                .checked_add(fee)
                .expect("claimable balance should not overflow after adding fee");
        }
        let (offered, received) = if self.is_counterparty_broadcaster {
            (&self.received_htlcs, &self.offered_htlcs)
        } else {
            (&self.offered_htlcs, &self.received_htlcs)
        };
        for o in offered {
            if !preimage_map.has_preimage(&o.payment_hash) {
                balance = balance.checked_add(o.value_sat).expect("overflow");
            }
        }
        for r in received {
            if preimage_map.has_preimage(&r.payment_hash) {
                balance = balance.checked_add(r.value_sat).expect("overflow");
            }
        }
        balance
    }

    /// Return the total received and offered htlc balances
    pub fn htlc_balance(&self) -> (u64, u64, u32, u32) {
        let mut sum_received: u64 = 0;
        let mut sum_offered: u64 = 0;
        let (offered, received) = if self.is_counterparty_broadcaster {
            (&self.received_htlcs, &self.offered_htlcs)
        } else {
            (&self.offered_htlcs, &self.received_htlcs)
        };
        for o in offered {
            sum_offered = sum_offered.checked_add(o.value_sat).expect("overflow");
        }
        for r in received {
            sum_received = sum_received.checked_add(r.value_sat).expect("overflow");
        }
        (sum_received, sum_offered, received.len() as u32, offered.len() as u32)
    }
}

#[allow(dead_code)]
#[allow(missing_docs)]
pub struct CommitmentInfo {
    pub is_counterparty_broadcaster: bool,
    pub to_countersigner_address: Option<Address>,
    pub to_countersigner_pubkey: Option<PublicKey>,
    pub to_countersigner_value_sat: u64,
    pub to_countersigner_anchor_count: u16,
    /// Broadcaster revocation pubkey
    pub revocation_pubkey: Option<PublicKey>,
    pub to_broadcaster_delayed_pubkey: Option<PublicKey>,
    pub to_broadcaster_value_sat: u64,
    pub to_self_delay: u16,
    pub to_broadcaster_anchor_count: u16,
    pub offered_htlcs: Vec<HTLCInfo>,
    pub received_htlcs: Vec<HTLCInfo>,
}

// Define manually because AddressData's fmt::Debug is lame.
impl fmt::Debug for CommitmentInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CommitmentInfo")
            .field("is_counterparty_broadcaster", &self.is_counterparty_broadcaster)
            // Wrap the to_countersigner_address AddressData w/ a nicer printing one.
            .field(
                "to_countersigner_address",
                &self.to_countersigner_address.as_ref().map(|p| DebugPayload(p)),
            )
            .field("to_countersigner_pubkey", &self.to_countersigner_pubkey)
            .field("to_countersigner_value_sat", &self.to_countersigner_value_sat)
            .field("to_countersigner_anchor_count", &self.to_countersigner_anchor_count)
            .field("revocation_pubkey", &self.revocation_pubkey)
            .field("to_broadcaster_delayed_pubkey", &self.to_broadcaster_delayed_pubkey)
            .field("to_broadcaster_value_sat", &self.to_broadcaster_value_sat)
            .field("to_self_delay", &self.to_self_delay)
            .field("to_broadcaster_anchor_count", &self.to_broadcaster_anchor_count)
            .field("offered_htlcs", &self.offered_htlcs)
            .field("received_htlcs", &self.received_htlcs)
            .finish()
    }
}

pub(crate) fn parse_received_htlc_script(
    script: &Script,
    is_anchors: bool,
) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>, i64), ValidationError> {
    let iter = &mut script.instructions();
    expect_op(iter, OP_DUP)?;
    expect_op(iter, OP_HASH160)?;
    let revocation_hash = expect_data(iter)?;
    expect_op(iter, OP_EQUAL)?;
    expect_op(iter, OP_IF)?;
    expect_op(iter, OP_CHECKSIG)?;
    expect_op(iter, OP_ELSE)?;
    let remote_htlc_pubkey = expect_data(iter)?;
    expect_op(iter, OP_SWAP)?;
    expect_op(iter, OP_SIZE)?;
    let thirty_two = expect_number(iter)?;
    if thirty_two != 32 {
        return Err(mismatch_error(format!("expected 32, saw {}", thirty_two)));
    }
    expect_op(iter, OP_EQUAL)?;
    expect_op(iter, OP_IF)?;
    expect_op(iter, OP_HASH160)?;
    let payment_hash_vec = expect_data(iter)?;
    expect_op(iter, OP_EQUALVERIFY)?;
    expect_op(iter, OP_PUSHNUM_2)?;
    expect_op(iter, OP_SWAP)?;
    let local_htlc_pubkey = expect_data(iter)?;
    expect_op(iter, OP_PUSHNUM_2)?;
    expect_op(iter, OP_CHECKMULTISIG)?;
    expect_op(iter, OP_ELSE)?;
    expect_op(iter, OP_DROP)?;
    let cltv_expiry = expect_number(iter)?;
    expect_op(iter, OP_CLTV)?;
    expect_op(iter, OP_DROP)?;
    expect_op(iter, OP_CHECKSIG)?;
    expect_op(iter, OP_ENDIF)?;
    if is_anchors {
        expect_op(iter, OP_PUSHNUM_1)?;
        expect_op(iter, OP_CSV)?;
        expect_op(iter, OP_DROP)?;
    }
    expect_op(iter, OP_ENDIF)?;
    expect_script_end(iter)?;
    Ok((revocation_hash, remote_htlc_pubkey, payment_hash_vec, local_htlc_pubkey, cltv_expiry))
}

pub(crate) fn parse_offered_htlc_script(
    script: &Script,
    is_anchors: bool,
) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>), ValidationError> {
    let iter = &mut script.instructions();
    expect_op(iter, OP_DUP)?;
    expect_op(iter, OP_HASH160)?;
    let revocation_hash = expect_data(iter)?;
    expect_op(iter, OP_EQUAL)?;
    expect_op(iter, OP_IF)?;
    expect_op(iter, OP_CHECKSIG)?;
    expect_op(iter, OP_ELSE)?;
    let remote_htlc_pubkey = expect_data(iter)?;
    expect_op(iter, OP_SWAP)?;
    expect_op(iter, OP_SIZE)?;
    let thirty_two = expect_number(iter)?;
    if thirty_two != 32 {
        return Err(mismatch_error(format!("expected 32, saw {}", thirty_two)));
    }
    expect_op(iter, OP_EQUAL)?;
    expect_op(iter, OP_NOTIF)?;
    expect_op(iter, OP_DROP)?;
    expect_op(iter, OP_PUSHNUM_2)?;
    expect_op(iter, OP_SWAP)?;
    let local_htlc_pubkey = expect_data(iter)?;
    expect_op(iter, OP_PUSHNUM_2)?;
    expect_op(iter, OP_CHECKMULTISIG)?;
    expect_op(iter, OP_ELSE)?;
    expect_op(iter, OP_HASH160)?;
    let payment_hash_vec = expect_data(iter)?;
    expect_op(iter, OP_EQUALVERIFY)?;
    expect_op(iter, OP_CHECKSIG)?;
    expect_op(iter, OP_ENDIF)?;
    if is_anchors {
        expect_op(iter, OP_PUSHNUM_1)?;
        expect_op(iter, OP_CSV)?;
        expect_op(iter, OP_DROP)?;
    }
    expect_op(iter, OP_ENDIF)?;
    expect_script_end(iter)?;
    Ok((revocation_hash, remote_htlc_pubkey, local_htlc_pubkey, payment_hash_vec))
}

pub(crate) fn parse_revokeable_redeemscript(
    script: &Script,
    _is_anchors: bool,
) -> Result<(Vec<u8>, i64, Vec<u8>), ValidationError> {
    let iter = &mut script.instructions();
    expect_op(iter, OP_IF)?;
    let revocation_key = expect_data(iter)?;
    expect_op(iter, OP_ELSE)?;
    let contest_delay = expect_number(iter)?;
    expect_op(iter, OP_CSV)?;
    expect_op(iter, OP_DROP)?;
    let delayed_pubkey = expect_data(iter)?;
    expect_op(iter, OP_ENDIF)?;
    expect_op(iter, OP_CHECKSIG)?;
    expect_script_end(iter)?;
    Ok((revocation_key, contest_delay, delayed_pubkey))
}

impl CommitmentInfo {
    #[cfg(test)]
    pub(crate) fn new_for_holder() -> Self {
        CommitmentInfo::new(false)
    }

    #[cfg(test)]
    pub(crate) fn new_for_counterparty() -> Self {
        CommitmentInfo::new(true)
    }

    /// Construct
    pub fn new(is_counterparty_broadcaster: bool) -> Self {
        CommitmentInfo {
            is_counterparty_broadcaster,
            to_countersigner_address: None,
            to_countersigner_pubkey: None,
            to_countersigner_value_sat: 0,
            to_countersigner_anchor_count: 0,
            revocation_pubkey: None,
            to_broadcaster_delayed_pubkey: None,
            to_broadcaster_value_sat: 0,
            to_self_delay: 0,
            to_broadcaster_anchor_count: 0,
            offered_htlcs: vec![],
            received_htlcs: vec![],
        }
    }

    pub(crate) fn has_to_broadcaster(&self) -> bool {
        self.to_broadcaster_delayed_pubkey.is_some()
    }

    pub(crate) fn has_to_countersigner(&self) -> bool {
        self.to_countersigner_address.is_some() || self.to_countersigner_pubkey.is_some()
    }

    /// The amount used by the broadcaster anchor
    pub fn to_broadcaster_anchor_value_sat(&self) -> Amount {
        if self.to_broadcaster_anchor_count == 1 {
            ANCHOR_SAT
        } else {
            Amount::ZERO
        }
    }

    /// The amount used by the countersigner anchor
    pub fn to_countersigner_anchor_value_sat(&self) -> Amount {
        if self.to_countersigner_anchor_count == 1 {
            ANCHOR_SAT
        } else {
            Amount::ZERO
        }
    }

    fn parse_to_broadcaster_script(
        &self,
        script: &Script,
    ) -> Result<(Vec<u8>, i64, Vec<u8>), ValidationError> {
        let iter = &mut script.instructions();
        expect_op(iter, OP_IF)?;
        let revocation_pubkey = expect_data(iter)?;
        expect_op(iter, OP_ELSE)?;
        let delay = expect_number(iter)?;
        expect_op(iter, OP_CSV)?;
        expect_op(iter, OP_DROP)?;
        let delayed_pubkey = expect_data(iter)?;
        expect_op(iter, OP_ENDIF)?;
        expect_op(iter, OP_CHECKSIG)?;
        expect_script_end(iter)?;
        Ok((revocation_pubkey, delay, delayed_pubkey))
    }

    fn handle_to_broadcaster_output(
        &mut self,
        out: &TxOut,
        vals: (Vec<u8>, i64, Vec<u8>),
    ) -> Result<(), ValidationError> {
        let (revocation_pubkey, delay, delayed_pubkey) = vals;
        // policy-commitment-singular-to-holder
        // policy-commitment-singular-to-counterparty
        if self.has_to_broadcaster() {
            return Err(transaction_format_error(
                "more than one to_broadcaster output".to_string(),
            ));
        }

        if delay < 0 {
            return Err(script_format_error("negative delay".to_string()));
        }
        if delay > MAX_DELAY {
            return Err(script_format_error(format!("delay too large: {} > {}", delay, MAX_DELAY)));
        }

        // This is safe because we checked for negative
        self.to_self_delay = delay as u16;
        self.to_broadcaster_value_sat = out.value.to_sat();
        self.to_broadcaster_delayed_pubkey = Some(
            PublicKey::from_slice(delayed_pubkey.as_slice())
                .map_err(|err| mismatch_error(format!("delayed_pubkey malformed: {}", err)))?,
        );
        self.revocation_pubkey = Some(
            PublicKey::from_slice(revocation_pubkey.as_slice())
                .map_err(|err| mismatch_error(format!("revocation_pubkey malformed: {}", err)))?,
        );

        Ok(())
    }

    fn parse_to_countersigner_delayed_script(
        &self,
        script: &Script,
    ) -> Result<Vec<u8>, ValidationError> {
        let iter = &mut script.instructions();
        let pubkey_data = expect_data(iter)?;
        expect_op(iter, OP_CHECKSIGVERIFY)?;
        expect_op(iter, OP_PUSHNUM_1)?;
        expect_op(iter, OP_CSV)?;
        expect_script_end(iter)?;
        Ok(pubkey_data)
    }

    /// 1 block delayed because of anchor usage
    fn handle_to_countersigner_delayed_output(
        &mut self,
        out: &TxOut,
        to_countersigner_delayed_pubkey_data: Vec<u8>,
    ) -> Result<(), ValidationError> {
        // policy-commitment-singular-to-holder
        // policy-commitment-singular-to-counterparty
        if self.has_to_countersigner() {
            return Err(transaction_format_error(
                "more than one to_countersigner output".to_string(),
            ));
        }
        self.to_countersigner_pubkey =
            Some(PublicKey::from_slice(to_countersigner_delayed_pubkey_data.as_slice()).map_err(
                |err| mismatch_error(format!("to_countersigner delayed pubkey malformed: {}", err)),
            )?);
        self.to_countersigner_value_sat = out.value.to_sat();
        Ok(())
    }

    fn handle_received_htlc_output(
        &mut self,
        out: &TxOut,
        vals: (Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>, i64),
    ) -> Result<(), ValidationError> {
        let (
            _revocation_hash,
            _remote_htlc_pubkey,
            payment_hash_vec,
            _local_htlc_pubkey,
            cltv_expiry,
        ) = vals;
        let payment_hash_hash = payment_hash_vec
            .as_slice()
            .try_into()
            .map_err(|_| mismatch_error("payment hash RIPEMD160 must be length 20".to_string()))?;

        if cltv_expiry < 0 {
            return Err(script_format_error("negative CLTV".to_string()));
        }

        let cltv_expiry = cltv_expiry as u32;

        let htlc = HTLCInfo { value_sat: out.value.to_sat(), payment_hash_hash, cltv_expiry };
        self.received_htlcs.push(htlc);

        Ok(())
    }

    fn handle_offered_htlc_output(
        &mut self,
        out: &TxOut,
        vals: (Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>),
    ) -> Result<(), ValidationError> {
        let (_revocation_hash, _remote_htlc_pubkey, _local_htlc_pubkey, payment_hash_vec) = vals;

        let payment_hash_hash = payment_hash_vec
            .as_slice()
            .try_into()
            .map_err(|_| mismatch_error("payment hash RIPEMD160 must be length 20".to_string()))?;

        let htlc = HTLCInfo { value_sat: out.value.to_sat(), payment_hash_hash, cltv_expiry: 0 };
        self.offered_htlcs.push(htlc);

        Ok(())
    }

    fn parse_anchor_script(&self, script: &Script) -> Result<Vec<u8>, ValidationError> {
        let iter = &mut script.instructions();
        let to_pubkey_data = expect_data(iter)?;
        expect_op(iter, OP_CHECKSIG)?;
        expect_op(iter, OP_IFDUP)?;
        expect_op(iter, OP_NOTIF)?;
        expect_op(iter, OP_PUSHNUM_16)?;
        expect_op(iter, OP_CSV)?;
        expect_op(iter, OP_ENDIF)?;
        expect_script_end(iter)?;
        Ok(to_pubkey_data)
    }

    fn handle_anchor_output(
        &mut self,
        keys: &VlsChannelSigner,
        setup: &ChannelSetup,
        out: &TxOut,
        to_pubkey_data: Vec<u8>,
    ) -> Result<(), ValidationError> {
        let secp_ctx = Secp256k1::new();
        let to_pubkey = PublicKey::from_slice(to_pubkey_data.as_slice())
            .map_err(|err| mismatch_error(format!("anchor to_pubkey malformed: {}", err)))?;

        let holder_funding_pubkey = keys.pubkeys(&secp_ctx).funding_pubkey;
        let counterparty_funding_pubkey = setup.counterparty_points.funding_pubkey;
        // These are dependent on which side owns this commitment.
        let (to_broadcaster_funding_pubkey, to_countersigner_funding_pubkey) =
            if self.is_counterparty_broadcaster {
                (counterparty_funding_pubkey, holder_funding_pubkey)
            } else {
                (holder_funding_pubkey, counterparty_funding_pubkey)
            };

        // policy-commitment-anchor-amount
        if out.value != ANCHOR_SAT {
            return Err(mismatch_error(format!("anchor wrong size: {}", out.value.to_sat())));
        }

        if to_pubkey == to_broadcaster_funding_pubkey {
            // local anchor
            self.to_broadcaster_anchor_count += 1;
        } else if to_pubkey == to_countersigner_funding_pubkey {
            // remote anchor
            self.to_countersigner_anchor_count += 1;
        } else {
            // policy-commitment-anchor-match-fundingkey
            return Err(mismatch_error(format!(
                "anchor to_pubkey {} doesn't match local or remote",
                to_pubkey_data.to_hex()
            )));
        }
        Ok(())
    }

    pub(crate) fn handle_output(
        &mut self,
        keys: &VlsChannelSigner,
        setup: &ChannelSetup,
        out: &TxOut,
        script_bytes: &[u8],
    ) -> Result<(), ValidationError> {
        if out.script_pubkey.is_p2wpkh() {
            if setup.is_anchors() {
                return Err(transaction_format_error(
                    "p2wpkh to_countersigner not valid with anchors".to_string(),
                ));
            }
            // policy-commitment-singular-to-holder
            // policy-commitment-singular-to-counterparty
            if self.has_to_countersigner() {
                return Err(transaction_format_error(
                    "more than one to_countersigner output".to_string(),
                ));
            }
            let address = Address::from_script(&out.script_pubkey, Network::Bitcoin);
            self.to_countersigner_address = address.ok();
            self.to_countersigner_value_sat = out.value.to_sat();
        } else if out.script_pubkey.is_p2wsh() {
            if script_bytes.is_empty() {
                return Err(transaction_format_error("missing witscript for p2wsh".to_string()));
            }
            let script = ScriptBuf::from(script_bytes.to_vec());
            // FIXME - Does this need it's own policy tag?
            if out.script_pubkey != script.to_p2wsh() {
                return Err(transaction_format_error(format!(
                    "script pubkey doesn't match inner script: {} != {}",
                    out.script_pubkey,
                    script.to_p2wsh()
                )));
            }
            if let Ok(vals) = self.parse_to_broadcaster_script(&script) {
                return self.handle_to_broadcaster_output(out, vals);
            }
            if let Ok(vals) = parse_received_htlc_script(&script, setup.is_anchors()) {
                return self.handle_received_htlc_output(out, vals);
            }
            if let Ok(vals) = parse_offered_htlc_script(&script, setup.is_anchors()) {
                return self.handle_offered_htlc_output(out, vals);
            }
            if let Ok(vals) = self.parse_anchor_script(&script) {
                return self.handle_anchor_output(keys, setup, out, vals);
            }
            if setup.is_anchors() {
                if let Ok(vals) = self.parse_to_countersigner_delayed_script(&script) {
                    return self.handle_to_countersigner_delayed_output(out, vals);
                }
            }
            // policy-commitment-no-unrecognized-outputs
            return Err(transaction_format_error("unknown p2wsh script".to_string()));
        } else {
            // policy-commitment-no-unrecognized-outputs
            return Err(transaction_format_error("unknown output type".to_string()));
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bitcoin::blockdata::script::Builder;
    use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
    use bitcoin::{Address, Amount, CompressedPublicKey, Network};
    use lightning::ln::chan_utils::get_revokeable_redeemscript;
    use lightning::ln::channel_keys::{DelayedPaymentKey, RevocationKey};
    use lightning::types::payment::PaymentHash;

    use crate::channel::CommitmentType;
    use crate::util::test_utils::key::make_test_pubkey;
    use crate::util::test_utils::{hex_encode, make_test_channel_keys, make_test_channel_setup};

    use test_log::test;

    #[test]
    fn htlc2_sorting() {
        // Defined in order ...
        let htlc0 =
            HTLCInfo2 { value_sat: 4000, payment_hash: PaymentHash([1; 32]), cltv_expiry: 2 << 16 };
        let htlc1 =
            HTLCInfo2 { value_sat: 4000, payment_hash: PaymentHash([1; 32]), cltv_expiry: 3 << 16 };
        let htlc2 =
            HTLCInfo2 { value_sat: 4000, payment_hash: PaymentHash([2; 32]), cltv_expiry: 3 << 16 };
        let htlc3 =
            HTLCInfo2 { value_sat: 5000, payment_hash: PaymentHash([2; 32]), cltv_expiry: 3 << 16 };
        let sorted = vec![&htlc0, &htlc1, &htlc2, &htlc3];

        // Reverse order
        let mut unsorted0 = vec![&htlc3, &htlc2, &htlc1, &htlc0];
        unsorted0.sort();
        assert_eq!(unsorted0, sorted);

        // Random order
        let mut unsorted1 = vec![&htlc2, &htlc0, &htlc3, &htlc1];
        unsorted1.sort();
        assert_eq!(unsorted1, sorted);
    }

    #[test]
    fn parse_test_err() {
        let info = CommitmentInfo::new_for_holder();
        let script = Builder::new().into_script();
        let err = info.parse_to_broadcaster_script(&script);
        assert!(err.is_err());
    }

    #[test]
    fn parse_test() {
        let secp_ctx = Secp256k1::signing_only();
        let mut info = CommitmentInfo::new_for_holder();
        let out = TxOut { value: Amount::from_sat(123), script_pubkey: Default::default() };
        let revocation_pubkey =
            PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[4u8; 32]).unwrap());
        let revocation_pubkey = RevocationKey(revocation_pubkey);
        let delayed_pubkey =
            PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[3u8; 32]).unwrap());
        let delayed_pubkey = DelayedPaymentKey(delayed_pubkey);
        let script = get_revokeable_redeemscript(&revocation_pubkey, 5, &delayed_pubkey);
        let vals = info.parse_to_broadcaster_script(&script).unwrap();
        let res = info.handle_to_broadcaster_output(&out, vals);
        assert!(res.is_ok());
        assert!(info.has_to_broadcaster());
        assert!(!info.has_to_countersigner());
        assert_eq!(info.revocation_pubkey.unwrap(), revocation_pubkey.to_public_key());
        assert_eq!(info.to_broadcaster_delayed_pubkey.unwrap(), delayed_pubkey.to_public_key());
        assert_eq!(info.to_self_delay, 5);
        assert_eq!(info.to_broadcaster_value_sat, 123);
        // Make sure you can't do it again (can't have two to_broadcaster outputs).
        let vals = info.parse_to_broadcaster_script(&script);
        let res = info.handle_to_broadcaster_output(&out, vals.unwrap());
        assert!(res.is_err());
        #[rustfmt::skip]
        assert_eq!(
            transaction_format_error("more than one to_broadcaster output".to_string()),
                    res.expect_err("expecting err")
        );
    }

    // policy-commitment-anchor-amount
    #[test]
    fn handle_anchor_wrong_size_test() {
        let mut info = CommitmentInfo::new_for_holder();
        let keys = make_test_channel_keys();
        let setup = make_test_channel_setup();
        let secp_ctx = Secp256k1::new();
        let out = TxOut { value: Amount::from_sat(329), script_pubkey: Default::default() };
        let to_pubkey_data = keys.pubkeys(&secp_ctx).funding_pubkey.serialize().to_vec();
        let res = info.handle_anchor_output(&keys, &setup, &out, to_pubkey_data);
        assert!(res.is_err());
        assert_eq!(
            res.unwrap_err(),
            mismatch_error(format!("anchor wrong size: {}", out.value.to_sat()))
        );
    }

    // policy-commitment-anchor-match-fundingkey
    #[test]
    fn handle_anchor_not_local_or_remote_test() {
        let mut info = CommitmentInfo::new_for_holder();
        let keys = make_test_channel_keys();
        let setup = make_test_channel_setup();
        let out = TxOut { value: Amount::from_sat(330), script_pubkey: Default::default() };
        let to_pubkey_data = make_test_pubkey(42).serialize().to_vec(); // doesn't match
        let res = info.handle_anchor_output(&keys, &setup, &out, to_pubkey_data.clone());
        assert!(res.is_err());
        assert_eq!(
            res.unwrap_err(),
            mismatch_error(format!(
                "anchor to_pubkey {} doesn\'t match local or remote",
                hex_encode(&to_pubkey_data)
            ))
        );
    }

    // policy-commitment-no-unrecognized-outputs
    #[test]
    fn handle_output_unknown_output_type_test() {
        let mut info = CommitmentInfo::new_for_counterparty();
        let keys = make_test_channel_keys();
        let setup = make_test_channel_setup();
        let out = TxOut { value: Amount::from_sat(42), script_pubkey: Default::default() };
        let script_bytes = [3u8; 30];
        let res = info.handle_output(&keys, &setup, &out, &script_bytes);
        assert!(res.is_err());
        assert_eq!(res.unwrap_err(), transaction_format_error("unknown output type".to_string()));
    }

    // policy-commitment-no-unrecognized-outputs
    #[test]
    fn handle_output_unknown_p2wsh_script_test() {
        let mut info = CommitmentInfo::new_for_counterparty();
        let keys = make_test_channel_keys();
        let setup = make_test_channel_setup();
        let script = Builder::new()
            .push_slice(&[0u8; 42]) // invalid
            .into_script();
        let out = TxOut {
            value: Amount::from_sat(42),
            script_pubkey: Address::p2wsh(&script, Network::Testnet).script_pubkey(),
        };
        let res = info.handle_output(&keys, &setup, &out, script.as_bytes());
        assert!(res.is_err());
        assert_eq!(res.unwrap_err(), transaction_format_error("unknown p2wsh script".to_string()));
    }

    #[test]
    fn handle_output_p2wpkh_to_countersigner_with_anchors_test() {
        let mut info = CommitmentInfo::new_for_counterparty();
        let keys = make_test_channel_keys();
        let mut setup = make_test_channel_setup();
        setup.commitment_type = CommitmentType::AnchorsZeroFeeHtlc;
        let pubkey =
            CompressedPublicKey::from_slice(&make_test_pubkey(43).serialize()[..]).unwrap();
        let out = TxOut {
            value: Amount::from_sat(42),
            script_pubkey: Address::p2wpkh(&pubkey, Network::Testnet).script_pubkey(),
        };
        let res = info.handle_output(&keys, &setup, &out, &[0u8; 0]);
        assert!(res.is_err());
        assert_eq!(
            res.unwrap_err(),
            transaction_format_error("p2wpkh to_countersigner not valid with anchors".to_string())
        );
    }

    #[test]
    fn handle_output_more_than_one_to_countersigner_test() {
        let mut info = CommitmentInfo::new_for_counterparty();
        let keys = make_test_channel_keys();
        let setup = make_test_channel_setup();
        let pubkey =
            CompressedPublicKey::from_slice(&make_test_pubkey(43).serialize()[..]).unwrap();
        let address = Address::p2wpkh(&pubkey, Network::Testnet);
        let out = TxOut { value: Amount::from_sat(42), script_pubkey: address.script_pubkey() };

        // Make the info look like a to_remote has already been seen.
        info.to_countersigner_address = Some(address);

        let res = info.handle_output(&keys, &setup, &out, &[0u8; 0]);
        assert!(res.is_err());
        assert_eq!(
            res.unwrap_err(),
            transaction_format_error("more than one to_countersigner output".to_string())
        );
    }

    #[test]
    fn handle_output_missing_witscript_test() {
        let mut info = CommitmentInfo::new_for_counterparty();
        let keys = make_test_channel_keys();
        let setup = make_test_channel_setup();
        let script = Builder::new().into_script();
        let out = TxOut {
            value: Amount::from_sat(42),
            script_pubkey: Address::p2wsh(&script, Network::Testnet).script_pubkey(),
        };
        let res = info.handle_output(&keys, &setup, &out, script.as_bytes());
        assert!(res.is_err());
        assert_eq!(
            res.unwrap_err(),
            transaction_format_error("missing witscript for p2wsh".to_string())
        );
    }

    #[test]
    fn handle_output_script_pubkey_doesnt_match_test() {
        let mut info = CommitmentInfo::new_for_counterparty();
        let keys = make_test_channel_keys();
        let setup = make_test_channel_setup();
        let script0 = Builder::new().into_script();
        let script1 = Builder::new().push_slice(&[0u8; 42]).into_script();
        let out = TxOut {
            value: Amount::from_sat(42),
            script_pubkey: Address::p2wsh(&script0, Network::Testnet).script_pubkey(),
        };
        let res = info.handle_output(&keys, &setup, &out, script1.as_bytes());
        assert!(res.is_err());
        assert_eq!(
            res.unwrap_err(),
            transaction_format_error("script pubkey doesn't match inner script: OP_0 OP_PUSHBYTES_32 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 != OP_0 OP_PUSHBYTES_32 3588420a18eae1ca84705137f9cccc52254273e9fb36577cb0ce7ebb4dd73a06".to_string())
        );
    }

    #[test]
    fn test_commitment_info2_htlc_balance_empty() {
        let info = CommitmentInfo2::new(false, 1000, 2000, vec![], vec![], 1000);
        let (received, offered, received_count, offered_count) = info.htlc_balance();
        assert_eq!(received, 0);
        assert_eq!(offered, 0);
        assert_eq!(received_count, 0);
        assert_eq!(offered_count, 0);

        let info_counterparty = CommitmentInfo2::new(true, 1000, 2000, vec![], vec![], 1000);
        let (received, offered, received_count, offered_count) = info_counterparty.htlc_balance();
        assert_eq!(received, 0);
        assert_eq!(offered, 0);
        assert_eq!(received_count, 0);
        assert_eq!(offered_count, 0);
    }

    #[test]
    #[should_panic]
    fn test_commitment_info2_htlc_balance_overflow() {
        let large_value = u64::MAX / 2 + 1;
        let offered = vec![
            HTLCInfo2 {
                value_sat: large_value,
                payment_hash: PaymentHash([1; 32]),
                cltv_expiry: 500000,
            },
            HTLCInfo2 {
                value_sat: large_value,
                payment_hash: PaymentHash([2; 32]),
                cltv_expiry: 500001,
            },
        ];
        let received = vec![HTLCInfo2 {
            value_sat: large_value,
            payment_hash: PaymentHash([3; 32]),
            cltv_expiry: 500002,
        }];

        let info = CommitmentInfo2::new(true, 1000, 2000, offered, received, 1000);
        let _ = info.htlc_balance();
    }

    #[test]
    fn test_commitment_info2_htlc_balance_non_broadcaster() {
        let offered = vec![
            HTLCInfo2 { value_sat: 1000, payment_hash: PaymentHash([1; 32]), cltv_expiry: 500000 },
            HTLCInfo2 { value_sat: 2000, payment_hash: PaymentHash([2; 32]), cltv_expiry: 500001 },
        ];
        let received = vec![HTLCInfo2 {
            value_sat: 3000,
            payment_hash: PaymentHash([3; 32]),
            cltv_expiry: 500002,
        }];
        let info = CommitmentInfo2::new(false, 1000, 2000, offered, received, 1000);
        let (received_sum, offered_sum, received_count, offered_count) = info.htlc_balance();
        assert_eq!(received_sum, 3000);
        assert_eq!(offered_sum, 3000);
        assert_eq!(received_count, 1);
        assert_eq!(offered_count, 2);
    }

    #[test]
    fn test_commitment_info2_htlc_balance_broadcaster() {
        let offered = vec![
            HTLCInfo2 { value_sat: 1000, payment_hash: PaymentHash([1; 32]), cltv_expiry: 500000 },
            HTLCInfo2 { value_sat: 2000, payment_hash: PaymentHash([2; 32]), cltv_expiry: 500001 },
        ];
        let received = vec![HTLCInfo2 {
            value_sat: 3000,
            payment_hash: PaymentHash([3; 32]),
            cltv_expiry: 500002,
        }];
        let info = CommitmentInfo2::new(true, 1000, 2000, offered, received, 1000);
        let (received_sum, offered_sum, received_count, offered_count) = info.htlc_balance();
        assert_eq!(received_sum, 1000 + 2000);
        assert_eq!(offered_sum, 3000);
        assert_eq!(received_count, 2);
        assert_eq!(offered_count, 1);
    }

    #[test]
    fn test_commitment_info_broadcaster_anchor_value() {
        let mut info = CommitmentInfo::new_for_holder();
        info.to_broadcaster_anchor_count = 0;
        assert_eq!(info.to_broadcaster_anchor_value_sat(), Amount::ZERO);

        info.to_broadcaster_anchor_count = 1;
        assert_eq!(info.to_broadcaster_anchor_value_sat(), ANCHOR_SAT);

        info.to_broadcaster_anchor_count = 2;
        assert_eq!(info.to_broadcaster_anchor_value_sat(), Amount::ZERO);
    }

    #[test]
    fn test_commitment_info_countersigner_anchor_value() {
        let mut info = CommitmentInfo::new_for_holder();
        info.to_countersigner_anchor_count = 0;
        assert_eq!(info.to_countersigner_anchor_value_sat(), Amount::ZERO);

        info.to_countersigner_anchor_count = 1;
        assert_eq!(info.to_countersigner_anchor_value_sat(), ANCHOR_SAT);

        info.to_countersigner_anchor_count = 2;
        assert_eq!(info.to_countersigner_anchor_value_sat(), Amount::ZERO);
    }
}