@@ -32,7 +32,7 @@ use bitcoin::secp256k1::Secp256k1;
32
32
use bitcoin::secp256k1::{PublicKey, SecretKey};
33
33
#[cfg(splicing)]
34
34
use bitcoin::ScriptBuf;
35
- use bitcoin::{secp256k1, Sequence, SignedAmount};
35
+ use bitcoin::{secp256k1, Sequence, SignedAmount, Weight };
36
36
37
37
use crate::blinded_path::message::MessageForwardNode;
38
38
use crate::blinded_path::message::{AsyncPaymentsContext, OffersContext};
@@ -117,7 +117,10 @@ use crate::routing::router::{
117
117
RouteParameters, RouteParametersConfig, Router,
118
118
};
119
119
use crate::sign::ecdsa::EcdsaChannelSigner;
120
- use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider, P2WPKH_WITNESS_WEIGHT};
120
+ use crate::sign::{
121
+ EntropySource, NodeSigner, Recipient, SignerProvider, P2TR_KEY_PATH_WITNESS_WEIGHT,
122
+ P2WPKH_WITNESS_WEIGHT,
123
+ };
121
124
#[cfg(any(feature = "_test_utils", test))]
122
125
use crate::types::features::Bolt11InvoiceFeatures;
123
126
use crate::types::features::{
@@ -221,26 +224,77 @@ pub struct FundingTxInput {
221
224
}
222
225
223
226
impl FundingTxInput {
224
- /// Creates an input spending an P2WPKH output from the given `prevtx` at index `vout` using the
225
- /// provided `sequence` number.
226
- ///
227
- /// Returns `Err` if no such output exists in `prevtx` at index `vout`.
228
- pub fn new_p2wpkh(prevtx: Transaction, vout: u32, sequence: Sequence) -> Result<Self, ()> {
227
+ fn new<F: FnOnce(&bitcoin::Script) -> bool>(
228
+ prevtx: Transaction, vout: u32, sequence: Sequence, witness_weight: Weight,
229
+ script_filter: F,
230
+ ) -> Result<Self, ()> {
229
231
Ok(FundingTxInput {
230
232
utxo: Utxo {
231
233
outpoint: bitcoin::OutPoint { txid: prevtx.compute_txid(), vout },
232
234
output: prevtx
233
235
.output
234
236
.get(vout as usize)
235
- .filter(|output| output.script_pubkey.is_p2wpkh( ))
237
+ .filter(|output| script_filter(& output.script_pubkey))
236
238
.ok_or(())?
237
239
.clone(),
238
- satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT ,
240
+ satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + witness_weight.to_wu() ,
239
241
},
240
242
sequence,
241
243
prevtx,
242
244
})
243
245
}
246
+
247
+ /// Creates an input spending a P2WPKH output from the given `prevtx` at index `vout` using the
248
+ /// provided `sequence` number.
249
+ ///
250
+ /// Returns `Err` if no such output exists in `prevtx` at index `vout`.
251
+ pub fn new_p2wpkh(prevtx: Transaction, vout: u32, sequence: Sequence) -> Result<Self, ()> {
252
+ let witness_weight = Weight::from_wu(P2WPKH_WITNESS_WEIGHT);
253
+ FundingTxInput::new(prevtx, vout, sequence, witness_weight, bitcoin::Script::is_p2wpkh)
254
+ }
255
+
256
+ /// Creates an input spending a P2WSH output from the given `prevtx` at index `vout` using the
257
+ /// provided `sequence` number.
258
+ ///
259
+ /// Requires passing the weight of witness needed to satisfy the output's script.
260
+ ///
261
+ /// Returns `Err` if no such output exists in `prevtx` at index `vout`.
262
+ pub fn new_p2wsh(
263
+ prevtx: Transaction, vout: u32, sequence: Sequence, witness_weight: Weight,
264
+ ) -> Result<Self, ()> {
265
+ FundingTxInput::new(prevtx, vout, sequence, witness_weight, bitcoin::Script::is_p2wsh)
266
+ }
267
+
268
+ /// Creates an input spending a P2TR output from the given `prevtx` at index `vout` using the
269
+ /// provided `sequence` number.
270
+ ///
271
+ /// This is meant for inputs spending a taproot output using the key path. See
272
+ /// [`new_p2tr_script_spend`] for when spending using a script path.
273
+ ///
274
+ /// Returns `Err` if no such output exists in `prevtx` at index `vout`.
275
+ ///
276
+ /// [`new_p2tr_script_spend`]: Self::new_p2tr_script_spend
277
+ pub fn new_p2tr_key_spend(
278
+ prevtx: Transaction, vout: u32, sequence: Sequence,
279
+ ) -> Result<Self, ()> {
280
+ let witness_weight = Weight::from_wu(P2TR_KEY_PATH_WITNESS_WEIGHT);
281
+ FundingTxInput::new(prevtx, vout, sequence, witness_weight, bitcoin::Script::is_p2tr)
282
+ }
283
+
284
+ /// Creates an input spending a P2TR output from the given `prevtx` at index `vout` using the
285
+ /// provided `sequence` number.
286
+ ///
287
+ /// Requires passing the weight of witness needed to satisfy a script path of the taproot
288
+ /// output. See [`new_p2tr_key_spend`] for when spending using the key path.
289
+ ///
290
+ /// Returns `Err` if no such output exists in `prevtx` at index `vout`.
291
+ ///
292
+ /// [`new_p2tr_key_spend`]: Self::new_p2tr_key_spend
293
+ pub fn new_p2tr_script_spend(
294
+ prevtx: Transaction, vout: u32, sequence: Sequence, witness_weight: Weight,
295
+ ) -> Result<Self, ()> {
296
+ FundingTxInput::new(prevtx, vout, sequence, witness_weight, bitcoin::Script::is_p2tr)
297
+ }
244
298
}
245
299
246
300
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
0 commit comments