@@ -556,21 +556,30 @@ pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, contest_del
556
556
}
557
557
}
558
558
559
- /// Static channel fields used to build transactions given per-commitment fields (CommitmentTransactionInfo)
559
+ // Static channel fields used to build transactions given per-commitment fields (CommitmentTransactionInfo)
560
560
#[ derive( Clone ) ]
561
- pub struct ChannelStaticInfo {
562
- /// Holder public keys
563
- pub holder_pubkeys : ChannelPublicKeys ,
564
- /// Counter-party public keys
565
- pub counterparty_pubkeys : ChannelPublicKeys ,
566
- /// The contest delay selected by the holder
567
- pub holder_selected_contest_delay : u16 ,
568
- /// The contest delay selected by the counterparty
569
- pub counterparty_selected_contest_delay : u16 ,
570
- /// The funding outpoint
571
- pub funding_outpoint : OutPoint ,
572
- /// Whether the channel is outbound from the point of view of the holder
573
- pub is_outbound_from_holder : bool ,
561
+ pub ( crate ) struct ChannelStaticInfo {
562
+ // Holder public keys
563
+ pub ( crate ) holder_pubkeys : ChannelPublicKeys ,
564
+ // Counter-party public keys
565
+ pub ( crate ) counterparty_pubkeys : ChannelPublicKeys ,
566
+ // The contest delay selected by the holder
567
+ pub ( crate ) holder_selected_contest_delay : u16 ,
568
+ // The contest delay selected by the counterparty
569
+ pub ( crate ) counterparty_selected_contest_delay : u16 ,
570
+ // The funding outpoint
571
+ pub ( crate ) funding_outpoint : OutPoint ,
572
+ // Whether the channel is outbound from the point of view of the holder
573
+ pub ( crate ) is_outbound_from_holder : bool ,
574
+ }
575
+
576
+ impl ChannelStaticInfo {
577
+ pub ( crate ) fn to_directed ( & self , holder : bool ) -> DirectedChannelStaticInfo {
578
+ DirectedChannelStaticInfo {
579
+ info : self ,
580
+ broadcaster_is_holder : holder
581
+ }
582
+ }
574
583
}
575
584
576
585
impl_writeable ! ( ChannelStaticInfo , 0 , {
@@ -582,25 +591,38 @@ impl_writeable!(ChannelStaticInfo, 0, {
582
591
is_outbound_from_holder
583
592
} ) ;
584
593
585
- impl ChannelStaticInfo {
586
- /// Get the pubkeys for the selected side and the opposing side
587
- pub fn pubkeys ( & self , holder : bool ) -> ( & ChannelPublicKeys , & ChannelPublicKeys ) {
588
- if holder {
589
- ( & self . holder_pubkeys , & self . counterparty_pubkeys )
594
+ /// Static channel fields used to build transactions given per-commitment fields (CommitmentTransactionInfo)
595
+ pub struct DirectedChannelStaticInfo < ' a > {
596
+ /// The holder's channel static info
597
+ info : & ' a ChannelStaticInfo ,
598
+ /// Whether the holder is the broadcaster
599
+ broadcaster_is_holder : bool ,
600
+ }
601
+
602
+ impl < ' a > DirectedChannelStaticInfo < ' a > {
603
+ /// Get the pubkeys for the broadcaster and countersignatory
604
+ pub fn pubkeys ( & self ) -> ( & ChannelPublicKeys , & ChannelPublicKeys ) {
605
+ if self . broadcaster_is_holder {
606
+ ( & self . info . holder_pubkeys , & self . info . counterparty_pubkeys )
590
607
} else {
591
- ( & self . counterparty_pubkeys , & self . holder_pubkeys )
608
+ ( & self . info . counterparty_pubkeys , & self . info . holder_pubkeys )
592
609
}
593
610
}
594
611
595
- /// Get the contest delay applicable to the selected side's transactions.
596
- /// Note that the contest delay was selected by the opposite party .
597
- pub fn contest_delay ( & self , holder : bool ) -> u16 {
598
- if holder { self . counterparty_selected_contest_delay } else { self . holder_selected_contest_delay }
612
+ /// Get the contest delay applicable to the transactions.
613
+ /// Note that the contest delay was selected by the countersignatory .
614
+ pub fn contest_delay ( & self ) -> u16 {
615
+ if self . broadcaster_is_holder { self . info . counterparty_selected_contest_delay } else { self . info . holder_selected_contest_delay }
599
616
}
600
617
601
- /// Whether the channel is outbound from the perspective of the selected party
602
- pub fn is_outbound ( & self , holder : bool ) -> bool {
603
- return if holder { self . is_outbound_from_holder } else { !self . is_outbound_from_holder } ;
618
+ /// Whether the channel is outbound from the broadcaster
619
+ pub fn is_outbound ( & self ) -> bool {
620
+ return if self . broadcaster_is_holder { self . info . is_outbound_from_holder } else { !self . info . is_outbound_from_holder } ;
621
+ }
622
+
623
+ /// The funding outpoint
624
+ pub fn funding_outpoint ( & self ) -> OutPoint {
625
+ self . info . funding_outpoint
604
626
}
605
627
}
606
628
@@ -654,8 +676,8 @@ impl HolderCommitmentTransactionInfo {
654
676
counterparty_htlc_sigs : Vec :: new ( )
655
677
}
656
678
}
657
- pub ( crate ) fn txid < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_static_info : & ChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> Txid {
658
- self . info . txid ( true , channel_static_info, secp_ctx)
679
+ pub ( crate ) fn txid < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_static_info : & DirectedChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> Txid {
680
+ self . info . txid ( channel_static_info, secp_ctx)
659
681
}
660
682
}
661
683
@@ -664,7 +686,7 @@ impl HolderCommitmentTransactionInfo {
664
686
let opt_sigs: Vec < Option < Signature > > = self . counterparty_htlc_sigs . iter ( ) . map ( |s| Some ( s. clone ( ) ) ) . collect ( ) ;
665
687
let mut htlcs = self . info . htlcs . clone ( ) ;
666
688
let htlcs_with_sig = htlcs. drain ( ..) . zip ( opt_sigs) . collect ( ) ;
667
- self . info . to_holder_commitment_tx ( self . counterparty_sig , htlcs_with_sig, channel_static_info, secp_ctx)
689
+ self . info . to_holder_commitment_tx ( self . counterparty_sig , htlcs_with_sig, & channel_static_info. to_directed ( true ) , secp_ctx)
668
690
}
669
691
}
670
692
@@ -735,9 +757,9 @@ impl CommitmentTransactionInfo {
735
757
/// Also keeps track of auxiliary HTLC data and returns it along with the mutated and sorted HTLCs.
736
758
/// This allows the caller to match the HTLC output index with the auxiliary data.
737
759
/// This auxiliary data is not stored in this object.
738
- pub fn new_with_auxiliary_htlc_data < T : Copy > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , keys : TxCreationKeys , feerate_per_kw : u32 , htlcs_with_aux : Vec < ( HTLCOutputInCommitment , T ) > , holder : bool , channel_static_info : & ChannelStaticInfo , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> ( CommitmentTransactionInfo , Vec < ( HTLCOutputInCommitment , T ) > ) {
760
+ pub fn new_with_auxiliary_htlc_data < T : Copy > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , keys : TxCreationKeys , feerate_per_kw : u32 , htlcs_with_aux : Vec < ( HTLCOutputInCommitment , T ) > , channel_static_info : & DirectedChannelStaticInfo , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> ( CommitmentTransactionInfo , Vec < ( HTLCOutputInCommitment , T ) > ) {
739
761
// Sort outputs and populate output indices while keeping track of the auxiliary data
740
- let mut txouts = Self :: do_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, & htlcs_with_aux, holder , channel_static_info, & secp_ctx) . unwrap ( ) ;
762
+ let mut txouts = Self :: do_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, & htlcs_with_aux, channel_static_info, & secp_ctx) . unwrap ( ) ;
741
763
let mut result_htlcs_with_aux = Vec :: new ( ) ;
742
764
let mut htlcs = Vec :: new ( ) ;
743
765
for ( idx, mut out) in txouts. drain ( ..) . enumerate ( ) {
@@ -759,16 +781,16 @@ impl CommitmentTransactionInfo {
759
781
( info, result_htlcs_with_aux)
760
782
}
761
783
762
- pub ( crate ) fn txid < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , holder : bool , channel_static_info : & ChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> Txid {
763
- let tx = self . build ( holder , channel_static_info, secp_ctx) . unwrap ( ) . 0 ;
784
+ pub ( crate ) fn txid < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_static_info : & DirectedChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> Txid {
785
+ let tx = self . build ( channel_static_info, secp_ctx) . unwrap ( ) . 0 ;
764
786
tx. txid ( )
765
787
}
766
788
767
789
/// Build the Bitcoin transaction
768
- pub fn build < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , holder : bool , channel_static_info : & ChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> Result < ( bitcoin:: Transaction , Vec < HTLCOutputInCommitment > , Vec < Script > ) , ( ) > {
769
- let ( obscured_commitment_transaction_number, txins) = self . build_inputs ( holder , channel_static_info) ;
790
+ pub fn build < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_static_info : & DirectedChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> Result < ( bitcoin:: Transaction , Vec < HTLCOutputInCommitment > , Vec < Script > ) , ( ) > {
791
+ let ( obscured_commitment_transaction_number, txins) = self . build_inputs ( channel_static_info) ;
770
792
771
- let mut txouts = self . build_outputs ( holder , channel_static_info, secp_ctx) ?;
793
+ let mut txouts = self . build_outputs ( channel_static_info, secp_ctx) ?;
772
794
773
795
let mut outputs = Vec :: with_capacity ( txouts. len ( ) ) ;
774
796
let mut scripts = Vec :: with_capacity ( txouts. len ( ) ) ;
@@ -794,16 +816,16 @@ impl CommitmentTransactionInfo {
794
816
) )
795
817
}
796
818
797
- fn build_outputs < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , holder : bool , channel_static_info : & ChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> Result < Vec < ( TxOut , ( Script , Option < HTLCOutputInCommitment > ) ) > , ( ) > {
819
+ fn build_outputs < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_static_info : & DirectedChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> Result < Vec < ( TxOut , ( Script , Option < HTLCOutputInCommitment > ) ) > , ( ) > {
798
820
let htlcs = self . htlcs . iter ( ) . map ( |h| ( h. clone ( ) , ( ) ) ) . collect ( ) ;
799
- let mut txouts = Self :: do_build_outputs ( & self . keys , self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & htlcs, holder , channel_static_info, secp_ctx) ?;
821
+ let mut txouts = Self :: do_build_outputs ( & self . keys , self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & htlcs, channel_static_info, secp_ctx) ?;
800
822
let outs = txouts. drain ( ..) . map ( |( out, ( s, extra) ) | ( out, ( s, extra. map ( |( p, _) | p) ) ) ) . collect ( ) ;
801
823
Ok ( outs)
802
824
}
803
825
804
- fn do_build_outputs < T : Copy , S : secp256k1:: Signing + secp256k1:: Verification > ( keys : & TxCreationKeys , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , htlcs : & Vec < ( HTLCOutputInCommitment , T ) > , holder : bool , channel_static_info : & ChannelStaticInfo , secp_ctx : & Secp256k1 < S > ) -> Result < Vec < ( TxOut , ( Script , Option < ( HTLCOutputInCommitment , T ) > ) ) > , ( ) > {
805
- let ( broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info. pubkeys ( holder ) ;
806
- let contest_delay = channel_static_info. contest_delay ( holder ) ;
826
+ fn do_build_outputs < T : Copy , S : secp256k1:: Signing + secp256k1:: Verification > ( keys : & TxCreationKeys , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , htlcs : & Vec < ( HTLCOutputInCommitment , T ) > , channel_static_info : & DirectedChannelStaticInfo , secp_ctx : & Secp256k1 < S > ) -> Result < Vec < ( TxOut , ( Script , Option < ( HTLCOutputInCommitment , T ) > ) ) > , ( ) > {
827
+ let ( broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info. pubkeys ( ) ;
828
+ let contest_delay = channel_static_info. contest_delay ( ) ;
807
829
808
830
let per_commitment_point = & keys. per_commitment_point ;
809
831
let to_broadcaster_delayed_pubkey = derive_public_key (
@@ -871,12 +893,12 @@ impl CommitmentTransactionInfo {
871
893
Ok ( txouts)
872
894
}
873
895
874
- fn build_inputs ( & self , holder : bool , channel_static_info : & ChannelStaticInfo ) -> ( u64 , Vec < TxIn > ) {
875
- let ( broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info. pubkeys ( holder ) ;
896
+ fn build_inputs ( & self , channel_static_info : & DirectedChannelStaticInfo ) -> ( u64 , Vec < TxIn > ) {
897
+ let ( broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info. pubkeys ( ) ;
876
898
let commitment_transaction_number_obscure_factor = get_commitment_transaction_number_obscure_factor (
877
899
& broadcaster_pubkeys. payment_point ,
878
900
& countersignatory_pubkeys. payment_point ,
879
- channel_static_info. is_outbound ( holder ) ,
901
+ channel_static_info. is_outbound ( ) ,
880
902
) ;
881
903
882
904
let obscured_commitment_transaction_number =
@@ -885,7 +907,7 @@ impl CommitmentTransactionInfo {
885
907
let txins = {
886
908
let mut ins: Vec < TxIn > = Vec :: new ( ) ;
887
909
ins. push ( TxIn {
888
- previous_output : channel_static_info. funding_outpoint . clone ( ) ,
910
+ previous_output : channel_static_info. funding_outpoint ( ) ,
889
911
script_sig : Script :: new ( ) ,
890
912
sequence : ( ( 0x80 as u32 ) << 8 * 3 )
891
913
| ( ( obscured_commitment_transaction_number >> 3 * 8 ) as u32 ) ,
@@ -898,25 +920,25 @@ impl CommitmentTransactionInfo {
898
920
899
921
/// Sign a transaction, either because we are counter-signing the counterparty's transaction or
900
922
/// because we are about to broadcast a holder transaction.
901
- pub fn get_signature < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , holder : bool , channel_static_info : & ChannelStaticInfo , funding_key : & SecretKey , funding_redeemscript : & Script , channel_value_satoshis : u64 , secp_ctx : & Secp256k1 < T > ) -> Signature {
902
- let sighash = self . get_sighash ( holder , channel_static_info, funding_redeemscript, channel_value_satoshis, secp_ctx) . 0 ;
923
+ pub fn get_signature < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_static_info : & DirectedChannelStaticInfo , funding_key : & SecretKey , funding_redeemscript : & Script , channel_value_satoshis : u64 , secp_ctx : & Secp256k1 < T > ) -> Signature {
924
+ let sighash = self . get_sighash ( channel_static_info, funding_redeemscript, channel_value_satoshis, secp_ctx) . 0 ;
903
925
secp_ctx. sign ( & sighash, funding_key)
904
926
}
905
927
906
928
/// Get the SIGHASH_ALL sighash value and the transaction.
907
929
///
908
930
/// Builds the transaction and computes the sighash. This can be used to verify a signature.
909
- pub fn get_sighash < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , holder : bool , channel_static_info : & ChannelStaticInfo , funding_redeemscript : & Script , channel_value_satoshis : u64 , secp_ctx : & Secp256k1 < T > ) -> ( Message , Transaction ) {
910
- let ( unsigned_tx, _, _) = self . build ( holder , channel_static_info, secp_ctx) . unwrap ( ) ;
931
+ pub fn get_sighash < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_static_info : & DirectedChannelStaticInfo , funding_redeemscript : & Script , channel_value_satoshis : u64 , secp_ctx : & Secp256k1 < T > ) -> ( Message , Transaction ) {
932
+ let ( unsigned_tx, _, _) = self . build ( channel_static_info, secp_ctx) . unwrap ( ) ;
911
933
let sighash = hash_to_message ! ( & bip143:: SigHashCache :: new( & unsigned_tx)
912
934
. signature_hash( 0 , funding_redeemscript, channel_value_satoshis, SigHashType :: All ) [ ..] ) ;
913
935
( sighash, unsigned_tx)
914
936
}
915
937
916
938
// TODO(devrandom): remove this and subsume the HolderCommitmentTransaction signing functionality
917
- pub ( crate ) fn to_holder_commitment_tx < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , counterparty_sig : Signature , htlcs_with_sig : Vec < ( HTLCOutputInCommitment , Option < Signature > ) > , channel_static_info : & ChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> HolderCommitmentTransaction {
918
- let ( broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info. pubkeys ( true ) ;
919
- let ( tx, _, _) = self . build ( true , channel_static_info, secp_ctx) . unwrap ( ) ;
939
+ pub ( crate ) fn to_holder_commitment_tx < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , counterparty_sig : Signature , htlcs_with_sig : Vec < ( HTLCOutputInCommitment , Option < Signature > ) > , channel_static_info : & DirectedChannelStaticInfo , secp_ctx : & Secp256k1 < T > ) -> HolderCommitmentTransaction {
940
+ let ( broadcaster_pubkeys, countersignatory_pubkeys) = channel_static_info. pubkeys ( ) ;
941
+ let ( tx, _, _) = self . build ( channel_static_info, secp_ctx) . unwrap ( ) ;
920
942
HolderCommitmentTransaction :: new_missing_holder_sig ( tx, counterparty_sig, & broadcaster_pubkeys. funding_pubkey , & countersignatory_pubkeys. funding_pubkey , self . keys . clone ( ) , self . feerate_per_kw , htlcs_with_sig)
921
943
}
922
944
}
0 commit comments