Skip to content

Commit 9847c9e

Browse files
committed
f - add more constructors
1 parent 88c8c3b commit 9847c9e

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use bitcoin::secp256k1::Secp256k1;
3232
use bitcoin::secp256k1::{PublicKey, SecretKey};
3333
#[cfg(splicing)]
3434
use bitcoin::ScriptBuf;
35-
use bitcoin::{secp256k1, Sequence, SignedAmount};
35+
use bitcoin::{secp256k1, Sequence, SignedAmount, Weight};
3636

3737
use crate::blinded_path::message::MessageForwardNode;
3838
use crate::blinded_path::message::{AsyncPaymentsContext, OffersContext};
@@ -117,7 +117,10 @@ use crate::routing::router::{
117117
RouteParameters, RouteParametersConfig, Router,
118118
};
119119
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+
};
121124
#[cfg(any(feature = "_test_utils", test))]
122125
use crate::types::features::Bolt11InvoiceFeatures;
123126
use crate::types::features::{
@@ -221,26 +224,77 @@ pub struct FundingTxInput {
221224
}
222225

223226
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, ()> {
229231
Ok(FundingTxInput {
230232
utxo: Utxo {
231233
outpoint: bitcoin::OutPoint { txid: prevtx.compute_txid(), vout },
232234
output: prevtx
233235
.output
234236
.get(vout as usize)
235-
.filter(|output| output.script_pubkey.is_p2wpkh())
237+
.filter(|output| script_filter(&output.script_pubkey))
236238
.ok_or(())?
237239
.clone(),
238-
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT,
240+
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + witness_weight.to_wu(),
239241
},
240242
sequence,
241243
prevtx,
242244
})
243245
}
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+
}
244298
}
245299

246300
// We hold various information about HTLC relay in the HTLC objects in Channel itself:

0 commit comments

Comments
 (0)