Skip to content

Commit 37f3469

Browse files
committed
Support signing to_remote anchors variant for StaticPaymentOutput
`to_remote` outputs on commitment transactions with anchor outputs have an additional `1 CSV` constraint on its spending condition, transitioning away from the previous P2WPKH script to a P2WSH. Since our `ChannelMonitor` was never updated to track the proper `to_remote` script on anchor outputs channels, we also missed updating our signer to handle the new script changes.
1 parent cc55553 commit 37f3469

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,6 +4033,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40334033
output: outp.clone(),
40344034
channel_keys_id: self.channel_keys_id,
40354035
channel_value_satoshis: self.channel_value_satoshis,
4036+
channel_transaction_parameters: Some(self.onchain_tx_handler.channel_transaction_parameters.clone()),
40364037
}));
40374038
break;
40384039
}

lightning/src/sign/mod.rs

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,34 @@ pub struct StaticPaymentOutputDescriptor {
121121
pub channel_keys_id: [u8; 32],
122122
/// The value of the channel which this transactions spends.
123123
pub channel_value_satoshis: u64,
124+
/// The necessary channel parameters that need to be provided to the re-derived signer through
125+
/// [`ChannelSigner::provide_channel_parameters`].
126+
// Added as optional, but always set, in 0.0.117.
127+
pub channel_transaction_parameters: Option<ChannelTransactionParameters>,
124128
}
125129
impl StaticPaymentOutputDescriptor {
126130
/// The maximum length a well-formed witness spending one of these should have.
127131
/// Note: If you have the grind_signatures feature enabled, this will be at least 1 byte
128132
/// shorter.
129-
// Calculated as 1 byte legnth + 73 byte signature, 1 byte empty vec push, 1 byte length plus
130-
// redeemscript push length.
131-
pub const MAX_WITNESS_LENGTH: usize = 1 + 73 + 34;
133+
pub fn max_witness_length(&self) -> usize {
134+
if self.channel_transaction_parameters.as_ref()
135+
.map(|channel_params| channel_params.channel_type_features.supports_anchors_zero_fee_htlc_tx())
136+
.unwrap_or(false)
137+
{
138+
1 /* num witness items */ + 1 /* sig push */ + 72 /* sig */ + 1 /* witness script push */ + 37 /* witness script */
139+
} else {
140+
// Calculated as 1 byte legnth + 73 byte signature, 1 byte empty vec push, 1 byte length plus
141+
// redeemscript push length.
142+
1 + 73 + 34
143+
}
144+
}
132145
}
133146
impl_writeable_tlv_based!(StaticPaymentOutputDescriptor, {
134147
(0, outpoint, required),
135148
(2, output, required),
136149
(4, channel_keys_id, required),
137150
(6, channel_value_satoshis, required),
151+
(7, channel_transaction_parameters, option),
138152
});
139153

140154
/// Describes the necessary information to spend a spendable output.
@@ -280,13 +294,22 @@ impl SpendableOutputDescriptor {
280294
match outp {
281295
SpendableOutputDescriptor::StaticPaymentOutput(descriptor) => {
282296
if !output_set.insert(descriptor.outpoint) { return Err(()); }
297+
let sequence =
298+
if descriptor.channel_transaction_parameters.as_ref()
299+
.map(|channel_params| channel_params.channel_type_features.supports_anchors_zero_fee_htlc_tx())
300+
.unwrap_or(false)
301+
{
302+
Sequence::from_consensus(1)
303+
} else {
304+
Sequence::ZERO
305+
};
283306
input.push(TxIn {
284307
previous_output: descriptor.outpoint.into_bitcoin_outpoint(),
285308
script_sig: Script::new(),
286-
sequence: Sequence::ZERO,
309+
sequence,
287310
witness: Witness::new(),
288311
});
289-
witness_weight += StaticPaymentOutputDescriptor::MAX_WITNESS_LENGTH;
312+
witness_weight += descriptor.max_witness_length();
290313
#[cfg(feature = "grind_signatures")]
291314
{ witness_weight -= 1; } // Guarantees a low R signature
292315
input_value += descriptor.output.value;
@@ -891,18 +914,30 @@ impl InMemorySigner {
891914
if !spend_tx.input[input_idx].script_sig.is_empty() { return Err(()); }
892915
if spend_tx.input[input_idx].previous_output != descriptor.outpoint.into_bitcoin_outpoint() { return Err(()); }
893916

894-
let remotepubkey = self.pubkeys().payment_point;
895-
let witness_script = bitcoin::Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: remotepubkey}, Network::Testnet).script_pubkey();
917+
let remotepubkey = bitcoin::PublicKey::new(self.pubkeys().payment_point);
918+
let witness_script = if self.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
919+
chan_utils::get_to_countersignatory_with_anchors_redeemscript(&remotepubkey.inner)
920+
} else {
921+
Script::new_p2pkh(&remotepubkey.pubkey_hash())
922+
};
896923
let sighash = hash_to_message!(&sighash::SighashCache::new(spend_tx).segwit_signature_hash(input_idx, &witness_script, descriptor.output.value, EcdsaSighashType::All).unwrap()[..]);
897924
let remotesig = sign_with_aux_rand(secp_ctx, &sighash, &self.payment_key, &self);
898-
let payment_script = bitcoin::Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, inner: remotepubkey}, Network::Bitcoin).unwrap().script_pubkey();
925+
let payment_script = if self.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
926+
witness_script.to_v0_p2wsh()
927+
} else {
928+
Script::new_v0_p2wpkh(&remotepubkey.wpubkey_hash().unwrap())
929+
};
899930

900931
if payment_script != descriptor.output.script_pubkey { return Err(()); }
901932

902933
let mut witness = Vec::with_capacity(2);
903934
witness.push(remotesig.serialize_der().to_vec());
904935
witness[0].push(EcdsaSighashType::All as u8);
905-
witness.push(remotepubkey.serialize().to_vec());
936+
if self.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
937+
witness.push(witness_script.to_bytes());
938+
} else {
939+
witness.push(remotepubkey.to_bytes());
940+
}
906941
Ok(witness)
907942
}
908943

@@ -1353,9 +1388,11 @@ impl KeysManager {
13531388
SpendableOutputDescriptor::StaticPaymentOutput(descriptor) => {
13541389
let input_idx = psbt.unsigned_tx.input.iter().position(|i| i.previous_output == descriptor.outpoint.into_bitcoin_outpoint()).ok_or(())?;
13551390
if keys_cache.is_none() || keys_cache.as_ref().unwrap().1 != descriptor.channel_keys_id {
1356-
keys_cache = Some((
1357-
self.derive_channel_keys(descriptor.channel_value_satoshis, &descriptor.channel_keys_id),
1358-
descriptor.channel_keys_id));
1391+
let mut signer = self.derive_channel_keys(descriptor.channel_value_satoshis, &descriptor.channel_keys_id);
1392+
if let Some(channel_params) = descriptor.channel_transaction_parameters.as_ref() {
1393+
signer.provide_channel_parameters(channel_params);
1394+
}
1395+
keys_cache = Some((signer, descriptor.channel_keys_id));
13591396
}
13601397
let witness = Witness::from_vec(keys_cache.as_ref().unwrap().0.sign_counterparty_payment_input(&psbt.unsigned_tx, input_idx, &descriptor, &secp_ctx)?);
13611398
psbt.inputs[input_idx].final_script_witness = Some(witness);

0 commit comments

Comments
 (0)