@@ -41,6 +41,11 @@ use chain;
41
41
42
42
const HTLC_OUTPUT_IN_COMMITMENT_SIZE : usize = 1 + 8 + 4 + 32 + 5 ;
43
43
44
+ pub ( crate ) const MAX_HTLCS : u16 = 483 ;
45
+
46
+ // This checks that the buffer size is greater than the maximum possible size for serialized HTLCS
47
+ const _EXCESS_BUFFER_SIZE: usize = MAX_BUF_SIZE - MAX_HTLCS as usize * HTLC_OUTPUT_IN_COMMITMENT_SIZE ;
48
+
44
49
pub ( super ) const HTLC_SUCCESS_TX_WEIGHT : u64 = 703 ;
45
50
pub ( super ) const HTLC_TIMEOUT_TX_WEIGHT : u64 = 663 ;
46
51
@@ -298,7 +303,7 @@ pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp2
298
303
///
299
304
/// These keys are assumed to be good, either because the code derived them from
300
305
/// channel basepoints via the new function, or they were obtained via
301
- /// PreCalculatedTxCreationKeys.trust_key_derivation because we trusted the source of the
306
+ /// CommitmentTransaction.untrusted_key_derivation because we trusted the source of the
302
307
/// pre-calculated keys.
303
308
#[ derive( PartialEq , Clone ) ]
304
309
pub struct TxCreationKeys {
@@ -545,9 +550,10 @@ pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, contest_del
545
550
}
546
551
547
552
/// Per-channel data used to build transactions in conjunction with the per-commitment data (CommitmentTransaction).
553
+ /// The fields are organized by holder/counterparty.
548
554
///
549
- /// Normally, this is converted to the owner-independent DirectedChannelTransactionParameters
550
- /// before use, via the as_holder_directed and as_counterparty_directed functions.
555
+ /// Normally, this is converted to the broadcaster/countersignatory-organized DirectedChannelTransactionParameters
556
+ /// before use, via the as_holder_broadcastable and as_counterparty_broadcastable functions.
551
557
#[ derive( Clone ) ]
552
558
pub struct ChannelTransactionParameters {
553
559
/// Holder public keys
@@ -583,8 +589,8 @@ impl ChannelTransactionParameters {
583
589
/// given that the holder is the broadcaster.
584
590
///
585
591
/// self.is_populated() must be true before calling this function.
586
- pub fn as_holder_directed ( & self ) -> DirectedChannelTransactionParameters {
587
- assert ! ( self . is_populated( ) , "self.late_parameters must be set before using as_holder_directed " ) ;
592
+ pub fn as_holder_broadcastable ( & self ) -> DirectedChannelTransactionParameters {
593
+ assert ! ( self . is_populated( ) , "self.late_parameters must be set before using as_holder_broadcastable " ) ;
588
594
DirectedChannelTransactionParameters {
589
595
inner : self ,
590
596
holder_is_broadcaster : true
@@ -595,8 +601,8 @@ impl ChannelTransactionParameters {
595
601
/// given that the counterparty is the broadcaster.
596
602
///
597
603
/// self.is_populated() must be true before calling this function.
598
- pub fn as_counterparty_directed ( & self ) -> DirectedChannelTransactionParameters {
599
- assert ! ( self . is_populated( ) , "self.late_parameters must be set before using as_counterparty_directed " ) ;
604
+ pub fn as_counterparty_broadcastable ( & self ) -> DirectedChannelTransactionParameters {
605
+ assert ! ( self . is_populated( ) , "self.late_parameters must be set before using as_counterparty_broadcastable " ) ;
600
606
DirectedChannelTransactionParameters {
601
607
inner : self ,
602
608
holder_is_broadcaster : false
@@ -617,11 +623,11 @@ impl_writeable!(ChannelTransactionParameters, 0, {
617
623
funding_outpoint
618
624
} ) ;
619
625
620
- /// Static channel fields used to build transactions given per-commitment fields, independent of
621
- /// transaction ownership .
626
+ /// Static channel fields used to build transactions given per-commitment fields, organized by
627
+ /// broadcaster/countersignatory .
622
628
///
623
629
/// This is derived from the owner-specific ChannelTransactionParameters via the
624
- /// as_holder_directed and as_counterparty_directed functions.
630
+ /// as_holder_broadcastable and as_counterparty_broadcastable functions.
625
631
pub struct DirectedChannelTransactionParameters < ' a > {
626
632
/// The holder's channel static parameters
627
633
inner : & ' a ChannelTransactionParameters ,
@@ -725,7 +731,7 @@ impl HolderCommitmentTransaction {
725
731
funding_outpoint : Some ( chain:: transaction:: OutPoint { txid : Default :: default ( ) , index : 0 } )
726
732
} ;
727
733
let aux: Vec < ( ) > = Vec :: new ( ) ;
728
- let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , 0 , 0 , keys, 0 , Vec :: new ( ) , aux, & channel_parameters. as_counterparty_directed ( ) ) . 0 ;
734
+ let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , 0 , 0 , keys, 0 , Vec :: new ( ) , aux, & channel_parameters. as_counterparty_broadcastable ( ) ) . 0 ;
729
735
HolderCommitmentTransaction {
730
736
inner,
731
737
counterparty_sig : dummy_sig,
@@ -734,9 +740,9 @@ impl HolderCommitmentTransaction {
734
740
}
735
741
}
736
742
737
- // Trust the pre-built commitment transaction and return its ID.
738
- pub ( crate ) fn trust_txid ( & self ) -> Txid {
739
- self . inner . trust_txid ( )
743
+ // Get pre-built commitment transaction and return its ID.
744
+ pub ( crate ) fn untrusted_txid ( & self ) -> Txid {
745
+ self . inner . untrusted_txid ( )
740
746
}
741
747
742
748
/// Gets a signed HTLC transaction given a preimage (for !htlc.offered) and the holder HTLC transaction signature.
@@ -757,8 +763,8 @@ impl HolderCommitmentTransaction {
757
763
758
764
/// The pre-calculated transaction creation public keys.
759
765
/// An external validating signer should not trust these keys.
760
- pub fn trust_key_derivation ( & self ) -> & TxCreationKeys {
761
- self . inner . trust_key_derivation ( )
766
+ pub fn untrusted_key_derivation ( & self ) -> & TxCreationKeys {
767
+ self . inner . untrusted_key_derivation ( )
762
768
}
763
769
}
764
770
@@ -798,7 +804,7 @@ pub struct BuiltCommitmentTransaction {
798
804
impl_writeable ! ( BuiltCommitmentTransaction , 0 , { transaction, txid } ) ;
799
805
800
806
impl BuiltCommitmentTransaction {
801
- /// Get the SIGHASH_ALL sighash value and the transaction.
807
+ /// Get the SIGHASH_ALL sighash value of the transaction.
802
808
///
803
809
/// This can be used to verify a signature.
804
810
pub fn get_sighash_all ( & self , funding_redeemscript : & Script , channel_value_satoshis : u64 ) -> Message {
@@ -808,7 +814,7 @@ impl BuiltCommitmentTransaction {
808
814
809
815
/// Sign a transaction, either because we are counter-signing the counterparty's transaction or
810
816
/// because we are about to broadcast a holder transaction.
811
- pub fn sign < T : secp256k1:: Signing + secp256k1 :: Verification > ( & self , funding_key : & SecretKey , funding_redeemscript : & Script , channel_value_satoshis : u64 , secp_ctx : & Secp256k1 < T > ) -> Signature {
817
+ pub fn sign < T : secp256k1:: Signing > ( & self , funding_key : & SecretKey , funding_redeemscript : & Script , channel_value_satoshis : u64 , secp_ctx : & Secp256k1 < T > ) -> Signature {
812
818
let sighash = self . get_sighash_all ( funding_redeemscript, channel_value_satoshis) ;
813
819
secp_ctx. sign ( & sighash, funding_key)
814
820
}
@@ -829,7 +835,7 @@ pub struct CommitmentTransaction {
829
835
htlcs : Vec < HTLCOutputInCommitment > ,
830
836
// A cache of the parties' pubkeys required to construct the transaction
831
837
keys : TxCreationKeys ,
832
- // For access to the built transaction, see doc for trust_built_transaction
838
+ // For access to the pre- built transaction, see doc for untrusted_built_transaction
833
839
built : BuiltCommitmentTransaction ,
834
840
}
835
841
@@ -888,6 +894,10 @@ impl CommitmentTransaction {
888
894
/// Also keeps track of auxiliary HTLC data and returns it along with the mutated and sorted HTLCs.
889
895
/// This allows the caller to match the HTLC output index with the auxiliary data.
890
896
/// This auxiliary data is not stored in this object.
897
+ ///
898
+ /// Only include HTLCs that are above the dust limit for the channel.
899
+ ///
900
+ /// Panics if the length of htlcs and aux are different.
891
901
pub fn new_with_auxiliary_htlc_data < T > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , keys : TxCreationKeys , feerate_per_kw : u32 , htlcs : Vec < HTLCOutputInCommitment > , aux : Vec < T > , channel_parameters : & DirectedChannelTransactionParameters ) -> ( CommitmentTransaction , Vec < ( HTLCOutputInCommitment , T ) > ) {
892
902
// Sort outputs and populate output indices while keeping track of the auxiliary data
893
903
let mut txouts = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, & htlcs, aux, channel_parameters) . unwrap ( ) ;
@@ -921,16 +931,16 @@ impl CommitmentTransaction {
921
931
( info, result_htlcs_with_aux)
922
932
}
923
933
924
- // Trust the pre-built commitment transaction and return its ID.
925
- pub ( crate ) fn trust_txid ( & self ) -> Txid {
934
+ // Get the pre-built commitment transaction's ID.
935
+ pub ( crate ) fn untrusted_txid ( & self ) -> Txid {
926
936
self . built . txid
927
937
}
928
938
929
- /// Trust the pre-built commitment transaction.
939
+ /// Get the pre-built commitment transaction.
930
940
///
931
941
/// This should only be used if you fully trust the builder of this object. It should not
932
942
/// be used by an external signer - instead use the build function.
933
- pub fn trust_built_transaction ( & self ) -> & BuiltCommitmentTransaction {
943
+ pub fn untrusted_built_transaction ( & self ) -> & BuiltCommitmentTransaction {
934
944
& self . built
935
945
}
936
946
@@ -1068,8 +1078,8 @@ impl CommitmentTransaction {
1068
1078
/// The returned Vec has one entry for each HTLC, and in the same order. For HTLCs which were
1069
1079
/// considered dust and not included, a None entry exists, for all others a signature is
1070
1080
/// included.
1071
- pub fn get_htlc_sigs < T : secp256k1:: Signing + secp256k1 :: Verification > ( & self , htlc_base_key : & SecretKey , channel_parameters : & DirectedChannelTransactionParameters , secp_ctx : & Secp256k1 < T > ) -> Result < Vec < Option < Signature > > , ( ) > {
1072
- let txid = self . trust_txid ( ) ;
1081
+ pub fn get_htlc_sigs < T : secp256k1:: Signing > ( & self , htlc_base_key : & SecretKey , channel_parameters : & DirectedChannelTransactionParameters , secp_ctx : & Secp256k1 < T > ) -> Result < Vec < Option < Signature > > , ( ) > {
1082
+ let txid = self . untrusted_txid ( ) ;
1073
1083
let mut ret = Vec :: with_capacity ( self . htlcs . len ( ) ) ;
1074
1084
let holder_htlc_key = derive_private_key ( secp_ctx, & self . keys . per_commitment_point , htlc_base_key) . map_err ( |_| ( ) ) ?;
1075
1085
@@ -1090,7 +1100,7 @@ impl CommitmentTransaction {
1090
1100
1091
1101
/// Gets a signed HTLC transaction given a preimage (for !htlc.offered) and the holder HTLC transaction signature.
1092
1102
pub ( crate ) fn get_signed_htlc_tx ( & self , channel_parameters : & DirectedChannelTransactionParameters , htlc_index : usize , counterparty_signature : & Signature , signature : & Signature , preimage : & Option < PaymentPreimage > ) -> Transaction {
1093
- let txid = self . trust_txid ( ) ;
1103
+ let txid = self . untrusted_txid ( ) ;
1094
1104
let this_htlc = & self . htlcs [ htlc_index] ;
1095
1105
assert ! ( this_htlc. transaction_output_index. is_some( ) ) ;
1096
1106
// if we don't have preimage for an HTLC-Success, we can't generate an HTLC transaction.
@@ -1123,7 +1133,7 @@ impl CommitmentTransaction {
1123
1133
1124
1134
/// The pre-calculated transaction creation public keys.
1125
1135
/// An external validating signer should not trust these keys.
1126
- pub fn trust_key_derivation ( & self ) -> & TxCreationKeys {
1136
+ pub fn untrusted_key_derivation ( & self ) -> & TxCreationKeys {
1127
1137
& self . keys
1128
1138
}
1129
1139
0 commit comments