Skip to content

Commit 5df65d2

Browse files
committed
f Account for nLockTime given in spend_spendable_outputs
1 parent d9a9e18 commit 5df65d2

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/event.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use lightning::util::errors::APIError;
2222
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
2323

2424
use bitcoin::secp256k1::{PublicKey, Secp256k1};
25-
use bitcoin::OutPoint;
25+
use bitcoin::{LockTime, OutPoint, PackedLockTime};
2626
use rand::{thread_rng, Rng};
2727
use std::collections::VecDeque;
2828
use std::ops::Deref;
@@ -292,11 +292,15 @@ where
292292
// channel.
293293
let confirmation_target = ConfirmationTarget::Normal;
294294

295+
let cur_height = self.channel_manager.current_best_block().height();
296+
let locktime = LockTime::from_height(cur_height).unwrap_or(LockTime::ZERO);
297+
295298
// Sign the final funding transaction and broadcast it.
296299
match self.wallet.create_funding_transaction(
297300
output_script,
298301
channel_value_satoshis,
299302
confirmation_target,
303+
locktime,
300304
) {
301305
Ok(final_tx) => {
302306
// Give the funding transaction back to LDK for opening the channel.
@@ -552,11 +556,17 @@ where
552556
let output_descriptors = &outputs.iter().collect::<Vec<_>>();
553557
let tx_feerate =
554558
self.wallet.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
559+
560+
// We set nLockTime to the current height to discourage fee sniping.
561+
let cur_height = self.channel_manager.current_best_block().height();
562+
let locktime: PackedLockTime =
563+
LockTime::from_height(cur_height).map_or(PackedLockTime::ZERO, |l| l.into());
555564
let res = self.keys_manager.spend_spendable_outputs(
556565
output_descriptors,
557566
Vec::new(),
558567
destination_address.script_pubkey(),
559568
tx_feerate,
569+
Some(locktime),
560570
&Secp256k1::new(),
561571
);
562572
match res {

src/wallet.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bitcoin::bech32::u5;
2222
use bitcoin::secp256k1::ecdh::SharedSecret;
2323
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
2424
use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, Signing};
25-
use bitcoin::{Script, Transaction, TxOut, Txid};
25+
use bitcoin::{PackedLockTime, LockTime, Script, Transaction, TxOut, Txid};
2626

2727
use std::collections::HashMap;
2828
use std::sync::{Arc, Condvar, Mutex, RwLock};
@@ -156,14 +156,14 @@ where
156156
}
157157

158158
pub(crate) fn create_funding_transaction(
159-
&self, output_script: Script, value_sats: u64, confirmation_target: ConfirmationTarget,
159+
&self, output_script: Script, value_sats: u64, confirmation_target: ConfirmationTarget, locktime: LockTime,
160160
) -> Result<Transaction, Error> {
161161
let fee_rate = self.estimate_fee_rate(confirmation_target);
162162

163163
let locked_wallet = self.inner.lock().unwrap();
164164
let mut tx_builder = locked_wallet.build_tx();
165165

166-
tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).enable_rbf();
166+
tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).nlocktime(locktime).enable_rbf();
167167

168168
let mut psbt = match tx_builder.finish() {
169169
Ok((psbt, _)) => {
@@ -363,7 +363,7 @@ where
363363
pub fn spend_spendable_outputs<C: Signing>(
364364
&self, descriptors: &[&SpendableOutputDescriptor], outputs: Vec<TxOut>,
365365
change_destination_script: Script, feerate_sat_per_1000_weight: u32,
366-
secp_ctx: &Secp256k1<C>,
366+
locktime: Option<PackedLockTime>, secp_ctx: &Secp256k1<C>,
367367
) -> Result<Transaction, ()> {
368368
let only_non_static = &descriptors
369369
.iter()
@@ -375,6 +375,7 @@ where
375375
outputs,
376376
change_destination_script,
377377
feerate_sat_per_1000_weight,
378+
locktime,
378379
secp_ctx,
379380
)
380381
}

0 commit comments

Comments
 (0)