Skip to content

Commit e8484d3

Browse files
committed
f Account for nLockTime given in spend_spendable_outputs
1 parent cc502a2 commit e8484d3

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
@@ -20,7 +20,7 @@ use lightning::util::errors::APIError;
2020
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
2121

2222
use bitcoin::secp256k1::{PublicKey, Secp256k1};
23-
use bitcoin::OutPoint;
23+
use bitcoin::{LockTime, OutPoint, PackedLockTime};
2424
use rand::{thread_rng, Rng};
2525
use std::collections::VecDeque;
2626
use std::ops::Deref;
@@ -270,11 +270,15 @@ where
270270
// channel.
271271
let confirmation_target = ConfirmationTarget::Normal;
272272

273+
let cur_height = self.channel_manager.current_best_block().height();
274+
let locktime = LockTime::from_height(cur_height).unwrap_or(LockTime::ZERO);
275+
273276
// Sign the final funding transaction and broadcast it.
274277
match self.wallet.create_funding_transaction(
275278
output_script,
276279
channel_value_satoshis,
277280
confirmation_target,
281+
locktime,
278282
) {
279283
Ok(final_tx) => {
280284
// Give the funding transaction back to LDK for opening the channel.
@@ -556,11 +560,17 @@ where
556560
let output_descriptors = &outputs.iter().collect::<Vec<_>>();
557561
let tx_feerate =
558562
self.wallet.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
563+
564+
// We set nLockTime to the current height to discourage fee sniping.
565+
let cur_height = self.channel_manager.current_best_block().height();
566+
let locktime: PackedLockTime =
567+
LockTime::from_height(cur_height).map_or(PackedLockTime::ZERO, |l| l.into());
559568
let res = self.keys_manager.spend_spendable_outputs(
560569
output_descriptors,
561570
Vec::new(),
562571
destination_address.script_pubkey(),
563572
tx_feerate,
573+
Some(locktime),
564574
&Secp256k1::new(),
565575
);
566576
match res {

src/wallet.rs

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

2929
use std::collections::HashMap;
3030
use std::ops::Deref;
@@ -153,14 +153,14 @@ where
153153
}
154154

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

160160
let locked_wallet = self.inner.lock().unwrap();
161161
let mut tx_builder = locked_wallet.build_tx();
162162

163-
tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).enable_rbf();
163+
tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).nlocktime(locktime).enable_rbf();
164164

165165
let mut psbt = match tx_builder.finish() {
166166
Ok((psbt, _)) => {
@@ -372,7 +372,7 @@ where
372372
pub fn spend_spendable_outputs<C: Signing>(
373373
&self, descriptors: &[&SpendableOutputDescriptor], outputs: Vec<TxOut>,
374374
change_destination_script: Script, feerate_sat_per_1000_weight: u32,
375-
secp_ctx: &Secp256k1<C>,
375+
locktime: Option<PackedLockTime>, secp_ctx: &Secp256k1<C>,
376376
) -> Result<Transaction, ()> {
377377
let only_non_static = &descriptors
378378
.iter()
@@ -384,6 +384,7 @@ where
384384
outputs,
385385
change_destination_script,
386386
feerate_sat_per_1000_weight,
387+
locktime,
387388
secp_ctx,
388389
)
389390
}

0 commit comments

Comments
 (0)