From: Wilmer Paulino Date: Thu, 28 Sep 2023 16:07:10 +0000 (-0700) Subject: Fix off-by-one max witness estimate for P2WPKH StaticPaymentDescriptor X-Git-Tag: v0.0.117-rc1~4^2~2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=3299d88595d6014dafa6f897a970330ee734e104;p=rust-lightning Fix off-by-one max witness estimate for P2WPKH StaticPaymentDescriptor We were not accounting for the extra byte denoting the number of items in the witness stack. --- diff --git a/lightning/src/events/bump_transaction.rs b/lightning/src/events/bump_transaction.rs index 35e9da605..55c12d23e 100644 --- a/lightning/src/events/bump_transaction.rs +++ b/lightning/src/events/bump_transaction.rs @@ -26,7 +26,7 @@ use crate::ln::chan_utils::{ use crate::ln::features::ChannelTypeFeatures; use crate::ln::PaymentPreimage; use crate::prelude::*; -use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner}; +use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner, P2WPKH_WITNESS_WEIGHT}; use crate::sync::Mutex; use crate::util::logger::Logger; @@ -384,12 +384,6 @@ pub struct Utxo { } impl Utxo { - const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ + - 1 /* sig length */ + - 73 /* sig including sighash flag */ + - 1 /* pubkey length */ + - 33 /* pubkey */; - /// Returns a `Utxo` with the `satisfaction_weight` estimate for a legacy P2PKH output. pub fn new_p2pkh(outpoint: OutPoint, value: u64, pubkey_hash: &PubkeyHash) -> Self { let script_sig_size = 1 /* script_sig length */ + @@ -419,7 +413,7 @@ impl Utxo { value, script_pubkey: Script::new_p2sh(&Script::new_v0_p2wpkh(pubkey_hash).script_hash()), }, - satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + Self::P2WPKH_WITNESS_WEIGHT, + satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + P2WPKH_WITNESS_WEIGHT, } } @@ -431,7 +425,7 @@ impl Utxo { value, script_pubkey: Script::new_v0_p2wpkh(pubkey_hash), }, - satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + Self::P2WPKH_WITNESS_WEIGHT, + satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT, } } } diff --git a/lightning/src/sign/mod.rs b/lightning/src/sign/mod.rs index 04652166f..9a3025304 100644 --- a/lightning/src/sign/mod.rs +++ b/lightning/src/sign/mod.rs @@ -107,6 +107,12 @@ impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, { (12, channel_value_satoshis, required), }); +pub(crate) const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ + + 1 /* sig length */ + + 73 /* sig including sighash flag */ + + 1 /* pubkey length */ + + 33 /* pubkey */; + /// Information about a spendable output to our "payment key". /// /// See [`SpendableOutputDescriptor::StaticPaymentOutput`] for more details on how to spend this. @@ -141,9 +147,7 @@ impl StaticPaymentOutputDescriptor { 1 /* num witness items */ + 1 /* sig push */ + 73 /* sig including sighash flag */ + 1 /* witness script push */ + witness_script_weight } else { - // Calculated as 1 byte legnth + 73 byte signature, 1 byte empty vec push, 1 byte length plus - // redeemscript push length. - 1 + 73 + 34 + P2WPKH_WITNESS_WEIGHT as usize } } }