Merge pull request #2633 from TheBlueMatt/2023-09-expose-onion-parse
[rust-lightning] / lightning / src / sign / mod.rs
index 04652166f05978b6dd8bb1ab0a4b4c2ed9c37d5f..39f1001c480fd45926aed14355d78068028bfdc5 100644 (file)
@@ -107,6 +107,12 @@ impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, {
        (12, channel_value_satoshis, required),
 });
 
+pub(crate) const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
+       1 /* sig length */ +
+       73 /* sig including sighash flag */ +
+       1 /* pubkey length */ +
+       33 /* pubkey */;
+
 /// Information about a spendable output to our "payment key".
 ///
 /// See [`SpendableOutputDescriptor::StaticPaymentOutput`] for more details on how to spend this.
@@ -128,6 +134,22 @@ pub struct StaticPaymentOutputDescriptor {
        pub channel_transaction_parameters: Option<ChannelTransactionParameters>,
 }
 impl StaticPaymentOutputDescriptor {
+       /// Returns the `witness_script` of the spendable output.
+       ///
+       /// Note that this will only return `Some` for [`StaticPaymentOutputDescriptor`]s that
+       /// originated from an anchor outputs channel, as they take the form of a P2WSH script.
+       pub fn witness_script(&self) -> Option<Script> {
+               self.channel_transaction_parameters.as_ref()
+                       .and_then(|channel_params|
+                                if channel_params.channel_type_features.supports_anchors_zero_fee_htlc_tx() {
+                                       let payment_point = channel_params.holder_pubkeys.payment_point;
+                                       Some(chan_utils::get_to_countersignatory_with_anchors_redeemscript(&payment_point))
+                                } else {
+                                        None
+                                }
+                       )
+       }
+
        /// The maximum length a well-formed witness spending one of these should have.
        /// Note: If you have the grind_signatures feature enabled, this will be at least 1 byte
        /// shorter.
@@ -141,9 +163,7 @@ impl StaticPaymentOutputDescriptor {
                        1 /* num witness items */ + 1 /* sig push */ + 73 /* sig including sighash flag */ +
                                1 /* witness script push */ + witness_script_weight
                } else {
-                       // Calculated as 1 byte legnth + 73 byte signature, 1 byte empty vec push, 1 byte length plus
-                       // redeemscript push length.
-                       1 + 73 + 34
+                       P2WPKH_WITNESS_WEIGHT as usize
                }
        }
 }