Consider anchor outputs value on inbound HTLCs
[rust-lightning] / lightning / src / ln / chan_utils.rs
index 18048d8efd87a50fea85cd037f375148b0f916c3..d1489e2716836f928d5bdc04450f3d8722d4fbc9 100644 (file)
@@ -19,7 +19,7 @@ use bitcoin::util::address::Payload;
 use bitcoin::hashes::{Hash, HashEngine};
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hashes::ripemd160::Hash as Ripemd160;
-use bitcoin::hash_types::{Txid, PubkeyHash};
+use bitcoin::hash_types::{Txid, PubkeyHash, WPubkeyHash};
 
 use crate::chain::chaininterface::fee_for_weight;
 use crate::chain::package::WEIGHT_REVOKED_OUTPUT;
@@ -450,7 +450,7 @@ pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp2
 /// channel basepoints via the new function, or they were obtained via
 /// CommitmentTransaction.trust().keys() because we trusted the source of the
 /// pre-calculated keys.
-#[derive(PartialEq, Eq, Clone)]
+#[derive(PartialEq, Eq, Clone, Debug)]
 pub struct TxCreationKeys {
        /// The broadcaster's per-commitment public key which was used to derive the other keys.
        pub per_commitment_point: PublicKey,
@@ -475,7 +475,7 @@ impl_writeable_tlv_based!(TxCreationKeys, {
 });
 
 /// One counterparty's public keys which do not change over the life of a channel.
-#[derive(Clone, Debug, PartialEq, Eq)]
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
 pub struct ChannelPublicKeys {
        /// The public key which is used to sign all commitment transactions, as it appears in the
        /// on-chain channel lock-in 2-of-2 multisig output.
@@ -556,6 +556,16 @@ pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, contest_delay: u1
        res
 }
 
+/// Returns the script for the counterparty's output on a holder's commitment transaction based on
+/// the channel type.
+pub fn get_counterparty_payment_script(channel_type_features: &ChannelTypeFeatures, payment_key: &PublicKey) -> Script {
+       if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
+               get_to_countersignatory_with_anchors_redeemscript(payment_key).to_v0_p2wsh()
+       } else {
+               Script::new_v0_p2wpkh(&WPubkeyHash::hash(&payment_key.serialize()))
+       }
+}
+
 /// Information about an HTLC as it appears in a commitment transaction
 #[derive(Clone, Debug, PartialEq, Eq)]
 pub struct HTLCOutputInCommitment {
@@ -853,7 +863,7 @@ pub fn build_anchor_input_witness(funding_key: &PublicKey, funding_sig: &Signatu
 ///
 /// Normally, this is converted to the broadcaster/countersignatory-organized DirectedChannelTransactionParameters
 /// before use, via the as_holder_broadcastable and as_counterparty_broadcastable functions.
-#[derive(Clone, Debug, PartialEq, Eq)]
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
 pub struct ChannelTransactionParameters {
        /// Holder public keys
        pub holder_pubkeys: ChannelPublicKeys,
@@ -873,7 +883,7 @@ pub struct ChannelTransactionParameters {
 }
 
 /// Late-bound per-channel counterparty data used to build transactions.
-#[derive(Clone, Debug, PartialEq, Eq)]
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
 pub struct CounterpartyChannelTransactionParameters {
        /// Counter-party public keys
        pub pubkeys: ChannelPublicKeys,
@@ -982,7 +992,7 @@ pub struct DirectedChannelTransactionParameters<'a> {
 
 impl<'a> DirectedChannelTransactionParameters<'a> {
        /// Get the channel pubkeys for the broadcaster
-       pub fn broadcaster_pubkeys(&self) -> &ChannelPublicKeys {
+       pub fn broadcaster_pubkeys(&self) -> &'a ChannelPublicKeys {
                if self.holder_is_broadcaster {
                        &self.inner.holder_pubkeys
                } else {
@@ -991,7 +1001,7 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
        }
 
        /// Get the channel pubkeys for the countersignatory
-       pub fn countersignatory_pubkeys(&self) -> &ChannelPublicKeys {
+       pub fn countersignatory_pubkeys(&self) -> &'a ChannelPublicKeys {
                if self.holder_is_broadcaster {
                        &self.inner.counterparty_parameters.as_ref().unwrap().pubkeys
                } else {
@@ -1020,7 +1030,7 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
        }
 
        /// Whether to use anchors for this channel
-       pub fn channel_type_features(&self) -> &ChannelTypeFeatures {
+       pub fn channel_type_features(&self) -> &'a ChannelTypeFeatures {
                &self.inner.channel_type_features
        }
 }
@@ -1028,7 +1038,7 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
 /// Information needed to build and sign a holder's commitment transaction.
 ///
 /// The transaction is only signed once we are ready to broadcast.
-#[derive(Clone)]
+#[derive(Clone, Debug)]
 pub struct HolderCommitmentTransaction {
        inner: CommitmentTransaction,
        /// Our counterparty's signature for the transaction
@@ -1134,7 +1144,7 @@ impl HolderCommitmentTransaction {
 }
 
 /// A pre-built Bitcoin commitment transaction and its txid.
-#[derive(Clone)]
+#[derive(Clone, Debug)]
 pub struct BuiltCommitmentTransaction {
        /// The commitment transaction
        pub transaction: Transaction,
@@ -1279,7 +1289,7 @@ impl<'a> Deref for TrustedClosingTransaction<'a> {
 
 impl<'a> TrustedClosingTransaction<'a> {
        /// The pre-built Bitcoin commitment transaction
-       pub fn built_transaction(&self) -> &Transaction {
+       pub fn built_transaction(&self) -> &'a Transaction {
                &self.inner.built
        }
 
@@ -1305,7 +1315,7 @@ impl<'a> TrustedClosingTransaction<'a> {
 ///
 /// This class can be used inside a signer implementation to generate a signature given the relevant
 /// secret key.
-#[derive(Clone)]
+#[derive(Clone, Debug)]
 pub struct CommitmentTransaction {
        commitment_number: u64,
        to_broadcaster_value_sat: u64,
@@ -1668,17 +1678,17 @@ impl<'a> TrustedCommitmentTransaction<'a> {
        }
 
        /// The pre-built Bitcoin commitment transaction
-       pub fn built_transaction(&self) -> &BuiltCommitmentTransaction {
+       pub fn built_transaction(&self) -> &'a BuiltCommitmentTransaction {
                &self.inner.built
        }
 
        /// The pre-calculated transaction creation public keys.
-       pub fn keys(&self) -> &TxCreationKeys {
+       pub fn keys(&self) -> &'a TxCreationKeys {
                &self.inner.keys
        }
 
        /// Should anchors be used.
-       pub fn channel_type_features(&self) -> &ChannelTypeFeatures {
+       pub fn channel_type_features(&self) -> &'a ChannelTypeFeatures {
                &self.inner.channel_type_features
        }