Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unwrap'ing channel_parameters in to_counterparty signing #2634

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,14 +947,20 @@ impl InMemorySigner {
if spend_tx.input[input_idx].previous_output != descriptor.outpoint.into_bitcoin_outpoint() { return Err(()); }

let remotepubkey = bitcoin::PublicKey::new(self.pubkeys().payment_point);
let witness_script = if self.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
// We cannot always assume that `channel_parameters` is set, so can't just call
TheBlueMatt marked this conversation as resolved.
Show resolved Hide resolved
// `self.channel_parameters()` or anything that relies on it
let supports_anchors_zero_fee_htlc_tx = self.channel_parameters.as_ref()
.map(|params| params.channel_type_features.supports_anchors_zero_fee_htlc_tx())
.unwrap_or(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a helper. We do this in create_spendable_outputs_psbt, too, and it looks like we have an unchecked use in sign_counterparty_commitment. Though maybe that is never reached in practice?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sign_counterparty_commitment shouldn't happen, we always provide the parameters there before signing commitment transactions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out the one in create_spendable_outputs_psbt is not on self but rather on StaticPaymentOutputDescriptor. While we could DRY up that usage and StaticPaymentOutputDescriptor::max_witness_length, it's probably not worth it because we may refactor the former as per #2605 (comment).


let witness_script = if supports_anchors_zero_fee_htlc_tx {
chan_utils::get_to_countersignatory_with_anchors_redeemscript(&remotepubkey.inner)
} else {
Script::new_p2pkh(&remotepubkey.pubkey_hash())
};
let sighash = hash_to_message!(&sighash::SighashCache::new(spend_tx).segwit_signature_hash(input_idx, &witness_script, descriptor.output.value, EcdsaSighashType::All).unwrap()[..]);
let remotesig = sign_with_aux_rand(secp_ctx, &sighash, &self.payment_key, &self);
let payment_script = if self.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
let payment_script = if supports_anchors_zero_fee_htlc_tx {
witness_script.to_v0_p2wsh()
} else {
Script::new_v0_p2wpkh(&remotepubkey.wpubkey_hash().unwrap())
Expand All @@ -965,7 +971,7 @@ impl InMemorySigner {
let mut witness = Vec::with_capacity(2);
witness.push(remotesig.serialize_der().to_vec());
witness[0].push(EcdsaSighashType::All as u8);
if self.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
if supports_anchors_zero_fee_htlc_tx {
witness.push(witness_script.to_bytes());
} else {
witness.push(remotepubkey.to_bytes());
Expand Down