Fix to_remote output redeemscript when anchors enabled
authorKen Sedgwick <ken@bonsai.com>
Wed, 10 Nov 2021 08:09:24 +0000 (00:09 -0800)
committerKen Sedgwick <ken@bonsai.com>
Thu, 11 Nov 2021 23:08:15 +0000 (15:08 -0800)
lightning/src/ln/chan_utils.rs

index 994e1c276c4427168bca70a6aa1369bc7f1a741f..83cbd1206155992d358c9656f30345f9ab3a2cf5 100644 (file)
@@ -608,6 +608,17 @@ pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, conte
        }
 }
 
+/// Gets the witnessScript for the to_remote output when anchors are enabled.
+#[inline]
+pub(crate) fn get_to_countersignatory_with_anchors_redeemscript(payment_point: &PublicKey) -> Script {
+       Builder::new()
+               .push_slice(&payment_point.serialize()[..])
+               .push_opcode(opcodes::all::OP_CHECKSIGVERIFY)
+               .push_int(1)
+               .push_opcode(opcodes::all::OP_CSV)
+               .into_script()
+}
+
 /// Gets the witnessScript for an anchor output from the funding public key.
 /// The witness in the spending input must be:
 /// <BIP 143 funding_signature>
@@ -1130,7 +1141,11 @@ impl CommitmentTransaction {
                let mut txouts: Vec<(TxOut, Option<&mut HTLCOutputInCommitment>)> = Vec::new();
 
                if to_countersignatory_value_sat > 0 {
-                       let script = script_for_p2wpkh(&countersignatory_pubkeys.payment_point);
+                       let script = if opt_anchors {
+                           get_to_countersignatory_with_anchors_redeemscript(&countersignatory_pubkeys.payment_point).to_v0_p2wsh()
+                       } else {
+                           script_for_p2wpkh(&countersignatory_pubkeys.payment_point)
+                       };
                        txouts.push((
                                TxOut {
                                        script_pubkey: script.clone(),
@@ -1435,7 +1450,7 @@ mod tests {
        use super::CounterpartyCommitmentSecrets;
        use ::{hex, chain};
        use prelude::*;
-       use ln::chan_utils::{CommitmentTransaction, TxCreationKeys, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment};
+       use ln::chan_utils::{get_to_countersignatory_with_anchors_redeemscript, script_for_p2wpkh, CommitmentTransaction, TxCreationKeys, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment};
        use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
        use util::test_utils;
        use chain::keysinterface::{KeysInterface, BaseSign};
@@ -1478,6 +1493,7 @@ mod tests {
                        &mut htlcs_with_aux, &channel_parameters.as_holder_broadcastable()
                );
                assert_eq!(tx.built.transaction.output.len(), 2);
+               assert_eq!(tx.built.transaction.output[1].script_pubkey, script_for_p2wpkh(&counterparty_pubkeys.payment_point));
 
                // Generate broadcaster and counterparty outputs as well as two anchors
                let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
@@ -1489,6 +1505,7 @@ mod tests {
                        &mut htlcs_with_aux, &channel_parameters.as_holder_broadcastable()
                );
                assert_eq!(tx.built.transaction.output.len(), 4);
+               assert_eq!(tx.built.transaction.output[3].script_pubkey, get_to_countersignatory_with_anchors_redeemscript(&counterparty_pubkeys.payment_point).to_v0_p2wsh());
 
                // Generate broadcaster output and anchor
                let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(