Skip to content

Commit a505df1

Browse files
committed
Fix to_remote output redeemscript when anchors enabled
1 parent 2bf39a6 commit a505df1

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,17 @@ pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, conte
608608
}
609609
}
610610

611+
/// Gets the witnessScript for the to_remote output when anchors are enabled.
612+
#[inline]
613+
pub(crate) fn get_to_countersignatory_with_anchors_redeemscript(payment_point: &PublicKey) -> Script {
614+
Builder::new()
615+
.push_slice(&payment_point.serialize()[..])
616+
.push_opcode(opcodes::all::OP_CHECKSIGVERIFY)
617+
.push_int(1)
618+
.push_opcode(opcodes::all::OP_CSV)
619+
.into_script()
620+
}
621+
611622
/// Gets the witnessScript for an anchor output from the funding public key.
612623
/// The witness in the spending input must be:
613624
/// <BIP 143 funding_signature>
@@ -1130,7 +1141,11 @@ impl CommitmentTransaction {
11301141
let mut txouts: Vec<(TxOut, Option<&mut HTLCOutputInCommitment>)> = Vec::new();
11311142

11321143
if to_countersignatory_value_sat > 0 {
1133-
let script = script_for_p2wpkh(&countersignatory_pubkeys.payment_point);
1144+
let script = if opt_anchors {
1145+
get_to_countersignatory_with_anchors_redeemscript(&countersignatory_pubkeys.payment_point).to_v0_p2wsh()
1146+
} else {
1147+
script_for_p2wpkh(&countersignatory_pubkeys.payment_point)
1148+
};
11341149
txouts.push((
11351150
TxOut {
11361151
script_pubkey: script.clone(),
@@ -1435,7 +1450,7 @@ mod tests {
14351450
use super::CounterpartyCommitmentSecrets;
14361451
use ::{hex, chain};
14371452
use prelude::*;
1438-
use ln::chan_utils::{CommitmentTransaction, TxCreationKeys, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment};
1453+
use ln::chan_utils::{get_to_countersignatory_with_anchors_redeemscript, script_for_p2wpkh, CommitmentTransaction, TxCreationKeys, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment};
14391454
use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
14401455
use util::test_utils;
14411456
use chain::keysinterface::{KeysInterface, BaseSign};
@@ -1478,6 +1493,7 @@ mod tests {
14781493
&mut htlcs_with_aux, &channel_parameters.as_holder_broadcastable()
14791494
);
14801495
assert_eq!(tx.built.transaction.output.len(), 2);
1496+
assert_eq!(tx.built.transaction.output[1].script_pubkey, script_for_p2wpkh(&counterparty_pubkeys.payment_point));
14811497

14821498
// Generate broadcaster and counterparty outputs as well as two anchors
14831499
let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
@@ -1489,6 +1505,7 @@ mod tests {
14891505
&mut htlcs_with_aux, &channel_parameters.as_holder_broadcastable()
14901506
);
14911507
assert_eq!(tx.built.transaction.output.len(), 4);
1508+
assert_eq!(tx.built.transaction.output[3].script_pubkey, get_to_countersignatory_with_anchors_redeemscript(&counterparty_pubkeys.payment_point).to_v0_p2wsh());
14921509

14931510
// Generate broadcaster output and anchor
14941511
let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(

0 commit comments

Comments
 (0)