1111//! largely of interest for those implementing the traits on [`crate::sign`] by hand.
1212
1313use bitcoin:: amount:: Amount ;
14+ use bitcoin:: constants:: WITNESS_SCALE_FACTOR ;
1415use bitcoin:: opcodes;
1516use bitcoin:: script:: { Builder , Script , ScriptBuf } ;
1617use bitcoin:: sighash;
@@ -89,12 +90,18 @@ pub const ANCHOR_INPUT_WITNESS_WEIGHT: u64 = 114;
8990#[ cfg( not( feature = "grind_signatures" ) ) ]
9091pub const ANCHOR_INPUT_WITNESS_WEIGHT : u64 = 115 ;
9192
92- /// The weight of a P2A anchor witness .
93- pub const P2A_ANCHOR_INPUT_WITNESS_WEIGHT : u64 = 1 ;
93+ /// The weight of an empty witness; used to spend a P2A output .
94+ pub const EMPTY_WITNESS_WEIGHT : u64 = 1 ;
9495
9596/// The maximum value of a P2A anchor.
9697pub const P2A_MAX_VALUE : u64 = 240 ;
9798
99+ /// The maximum weight of a TRUC transaction, see BIP431.
100+ pub const TRUC_MAX_WEIGHT : u64 = 10_000 * WITNESS_SCALE_FACTOR as u64 ;
101+
102+ /// The maximum weight of a TRUC transaction with an unconfirmed TRUC ancestor, see BIP431.
103+ pub const TRUC_CHILD_MAX_WEIGHT : u64 = 1000 * WITNESS_SCALE_FACTOR as u64 ;
104+
98105/// The upper bound weight of an HTLC timeout input from a commitment transaction with keyed anchor outputs.
99106pub const HTLC_TIMEOUT_INPUT_KEYED_ANCHOR_WITNESS_WEIGHT : u64 = 288 ;
100107/// The upper bound weight of an HTLC timeout input from a commitment transaction with a p2a anchor output.
@@ -125,6 +132,15 @@ pub const FUNDING_TRANSACTION_WITNESS_WEIGHT: u64 = 1 + // number_of_witness_ele
125132 1 + // witness_script_length
126133 MULTISIG_SCRIPT_SIZE ;
127134
135+ pub ( crate ) const BASE_TX_SIZE : u64 = 4 /* version */ + 1 /* input count */ + 1 /* output count */ + 4 /* locktime */ ;
136+ pub ( crate ) const SEGWIT_MARKER_FLAG_WEIGHT : u64 = 2 ;
137+ pub ( crate ) const EMPTY_SCRIPT_SIG_WEIGHT : u64 =
138+ 1 /* empty script_sig */ * WITNESS_SCALE_FACTOR as u64 ;
139+ pub ( crate ) const BASE_INPUT_SIZE : u64 = 32 /* txid */ + 4 /* vout */ + 4 /* sequence */ ;
140+ pub ( crate ) const BASE_INPUT_WEIGHT : u64 = BASE_INPUT_SIZE * WITNESS_SCALE_FACTOR as u64 ;
141+ pub ( crate ) const P2WSH_TXOUT_WEIGHT : u64 =
142+ ( 8 /* value */ + 1 /* var_int */ + 34 /* p2wsh spk */ ) * WITNESS_SCALE_FACTOR as u64 ;
143+
128144/// Gets the weight for an HTLC-Success transaction.
129145#[ inline]
130146#[ rustfmt:: skip]
@@ -134,6 +150,18 @@ pub fn htlc_success_tx_weight(channel_type_features: &ChannelTypeFeatures) -> u6
134150 if channel_type_features. supports_anchors_zero_fee_htlc_tx ( ) { HTLC_SUCCESS_ANCHOR_TX_WEIGHT } else { HTLC_SUCCESS_TX_WEIGHT }
135151}
136152
153+ /// Gets the weight of a single input-output pair in externally funded HTLC-success transactions
154+ pub fn aggregated_htlc_success_input_output_pair_weight (
155+ channel_type_features : & ChannelTypeFeatures ,
156+ ) -> u64 {
157+ let satisfaction_weight = if channel_type_features. supports_anchors_zero_fee_htlc_tx ( ) {
158+ EMPTY_SCRIPT_SIG_WEIGHT + HTLC_SUCCESS_INPUT_KEYED_ANCHOR_WITNESS_WEIGHT
159+ } else {
160+ EMPTY_SCRIPT_SIG_WEIGHT + HTLC_SUCCESS_INPUT_P2A_ANCHOR_WITNESS_WEIGHT
161+ } ;
162+ BASE_INPUT_WEIGHT + P2WSH_TXOUT_WEIGHT + satisfaction_weight
163+ }
164+
137165/// Gets the weight for an HTLC-Timeout transaction.
138166#[ inline]
139167#[ rustfmt:: skip]
@@ -143,6 +171,18 @@ pub fn htlc_timeout_tx_weight(channel_type_features: &ChannelTypeFeatures) -> u6
143171 if channel_type_features. supports_anchors_zero_fee_htlc_tx ( ) { HTLC_TIMEOUT_ANCHOR_TX_WEIGHT } else { HTLC_TIMEOUT_TX_WEIGHT }
144172}
145173
174+ /// Gets the weight of a single input-output pair in externally funded HTLC-timeout transactions
175+ pub fn aggregated_htlc_timeout_input_output_pair_weight (
176+ channel_type_features : & ChannelTypeFeatures ,
177+ ) -> u64 {
178+ let satisfaction_weight = if channel_type_features. supports_anchors_zero_fee_htlc_tx ( ) {
179+ EMPTY_SCRIPT_SIG_WEIGHT + HTLC_TIMEOUT_INPUT_KEYED_ANCHOR_WITNESS_WEIGHT
180+ } else {
181+ EMPTY_SCRIPT_SIG_WEIGHT + HTLC_TIMEOUT_INPUT_P2A_ANCHOR_WITNESS_WEIGHT
182+ } ;
183+ BASE_INPUT_WEIGHT + P2WSH_TXOUT_WEIGHT + satisfaction_weight
184+ }
185+
146186/// Describes the type of HTLC claim as determined by analyzing the witness.
147187#[ derive( PartialEq , Eq ) ]
148188pub enum HTLCClaim {
0 commit comments