Skip to content

Commit ad61ec5

Browse files
committed
Fail payment retry if Invoice is expired
According to BOLT 11: - after the `timestamp` plus `expiry` has passed - SHOULD NOT attempt a payment Add a convenience method for checking if an Invoice has expired, and use it to short-circuit payment retries.
1 parent dced4c4 commit ad61ec5

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

lightning-invoice/src/lib.rs

+41
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,19 @@ impl Invoice {
11881188
.unwrap_or(Duration::from_secs(DEFAULT_EXPIRY_TIME))
11891189
}
11901190

1191+
/// Returns whether the invoice has expired.
1192+
pub fn is_expired(&self) -> bool {
1193+
Self::is_expired_from_epoch(self.timestamp(), self.expiry_time())
1194+
}
1195+
1196+
/// Returns whether the expiry time from the given epoch has passed.
1197+
pub(crate) fn is_expired_from_epoch(epoch: &SystemTime, expiry_time: Duration) -> bool {
1198+
match epoch.elapsed() {
1199+
Ok(elapsed) => elapsed > expiry_time,
1200+
Err(_) => false,
1201+
}
1202+
}
1203+
11911204
/// Returns the invoice's `min_final_cltv_expiry` time, if present, otherwise
11921205
/// [`DEFAULT_MIN_FINAL_CLTV_EXPIRY`].
11931206
pub fn min_final_cltv_expiry(&self) -> u64 {
@@ -1920,5 +1933,33 @@ mod test {
19201933

19211934
assert_eq!(invoice.min_final_cltv_expiry(), DEFAULT_MIN_FINAL_CLTV_EXPIRY);
19221935
assert_eq!(invoice.expiry_time(), Duration::from_secs(DEFAULT_EXPIRY_TIME));
1936+
assert!(!invoice.is_expired());
1937+
}
1938+
1939+
#[test]
1940+
fn test_expiration() {
1941+
use ::*;
1942+
use secp256k1::Secp256k1;
1943+
use secp256k1::key::SecretKey;
1944+
1945+
let timestamp = SystemTime::now()
1946+
.checked_sub(Duration::from_secs(DEFAULT_EXPIRY_TIME * 2))
1947+
.unwrap();
1948+
let signed_invoice = InvoiceBuilder::new(Currency::Bitcoin)
1949+
.description("Test".into())
1950+
.payment_hash(sha256::Hash::from_slice(&[0;32][..]).unwrap())
1951+
.payment_secret(PaymentSecret([0; 32]))
1952+
.timestamp(timestamp)
1953+
.build_raw()
1954+
.unwrap()
1955+
.sign::<_, ()>(|hash| {
1956+
let privkey = SecretKey::from_slice(&[41; 32]).unwrap();
1957+
let secp_ctx = Secp256k1::new();
1958+
Ok(secp_ctx.sign_recoverable(hash, &privkey))
1959+
})
1960+
.unwrap();
1961+
let invoice = Invoice::from_signed(signed_invoice).unwrap();
1962+
1963+
assert!(invoice.is_expired());
19231964
}
19241965
}

lightning-invoice/src/payment.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ use secp256k1::key::PublicKey;
114114
use std::collections::hash_map::{self, HashMap};
115115
use std::ops::Deref;
116116
use std::sync::Mutex;
117+
use std::time::{Duration, SystemTime};
117118

118119
/// A utility for paying [`Invoice]`s.
119120
pub struct InvoicePayer<P: Deref, R, L: Deref, E>
@@ -225,6 +226,7 @@ where
225226
hash_map::Entry::Vacant(entry) => {
226227
let payer = self.payer.node_id();
227228
let mut payee = Payee::new(invoice.recover_payee_pub_key())
229+
.with_expiry_time(expiry_time_from_unix_epoch(&invoice))
228230
.with_route_hints(invoice.route_hints());
229231
if let Some(features) = invoice.features() {
230232
payee = payee.with_features(features.clone());
@@ -272,6 +274,14 @@ where
272274
}
273275
}
274276

277+
fn expiry_time_from_unix_epoch(invoice: &Invoice) -> Duration {
278+
invoice.timestamp().duration_since(SystemTime::UNIX_EPOCH).unwrap() + invoice.expiry_time()
279+
}
280+
281+
fn has_expired(params: &RouteParameters) -> bool {
282+
Invoice::is_expired_from_epoch(&SystemTime::UNIX_EPOCH, params.payee.expiry_time.unwrap())
283+
}
284+
275285
impl<P: Deref, R, L: Deref, E> EventHandler for InvoicePayer<P, R, L, E>
276286
where
277287
P::Target: Payer,
@@ -296,6 +306,8 @@ where
296306
log_trace!(self.logger, "Payment {} exceeded maximum attempts; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
297307
} else if retry.is_none() {
298308
log_trace!(self.logger, "Payment {} missing retry params; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
309+
} else if has_expired(retry.as_ref().unwrap()) {
310+
log_trace!(self.logger, "Invoice expired for payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
299311
} else if self.retry_payment(*payment_id.as_ref().unwrap(), retry.as_ref().unwrap()).is_err() {
300312
log_trace!(self.logger, "Error retrying payment {}; not retrying (attempts: {})", log_bytes!(payment_hash.0), attempts);
301313
} else {
@@ -328,7 +340,7 @@ where
328340
#[cfg(test)]
329341
mod tests {
330342
use super::*;
331-
use crate::{InvoiceBuilder, Currency};
343+
use crate::{DEFAULT_EXPIRY_TIME, InvoiceBuilder, Currency};
332344
use bitcoin_hashes::sha256::Hash as Sha256;
333345
use lightning::ln::PaymentPreimage;
334346
use lightning::ln::features::{ChannelFeatures, NodeFeatures};
@@ -338,6 +350,7 @@ mod tests {
338350
use lightning::util::errors::APIError;
339351
use lightning::util::events::Event;
340352
use secp256k1::{SecretKey, PublicKey, Secp256k1};
353+
use std::time::{SystemTime, Duration};
341354

342355
fn invoice(payment_preimage: PaymentPreimage) -> Invoice {
343356
let payment_hash = Sha256::hash(&payment_preimage.0);
@@ -370,6 +383,25 @@ mod tests {
370383
.unwrap()
371384
}
372385

386+
fn expired_invoice(payment_preimage: PaymentPreimage) -> Invoice {
387+
let payment_hash = Sha256::hash(&payment_preimage.0);
388+
let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
389+
let timestamp = SystemTime::now()
390+
.checked_sub(Duration::from_secs(DEFAULT_EXPIRY_TIME * 2))
391+
.unwrap();
392+
InvoiceBuilder::new(Currency::Bitcoin)
393+
.description("test".into())
394+
.payment_hash(payment_hash)
395+
.payment_secret(PaymentSecret([0; 32]))
396+
.timestamp(timestamp)
397+
.min_final_cltv_expiry(144)
398+
.amount_milli_satoshis(128)
399+
.build_signed(|hash| {
400+
Secp256k1::new().sign_recoverable(hash, &private_key)
401+
})
402+
.unwrap()
403+
}
404+
373405
#[test]
374406
fn pays_invoice_on_first_attempt() {
375407
let event_handled = core::cell::RefCell::new(false);
@@ -524,6 +556,37 @@ mod tests {
524556
assert_eq!(*payer.attempts.borrow(), 1);
525557
}
526558

559+
#[test]
560+
fn fails_paying_invoice_after_expiration() {
561+
let event_handled = core::cell::RefCell::new(false);
562+
let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
563+
564+
let payer = TestPayer::new();
565+
let router = TestRouter {};
566+
let logger = TestLogger::new();
567+
let invoice_payer =
568+
InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
569+
570+
let payment_preimage = PaymentPreimage([1; 32]);
571+
let invoice = expired_invoice(payment_preimage);
572+
let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
573+
assert_eq!(*payer.attempts.borrow(), 1);
574+
575+
let event = Event::PaymentPathFailed {
576+
payment_id,
577+
payment_hash: PaymentHash(invoice.payment_hash().clone().into_inner()),
578+
network_update: None,
579+
rejected_by_dest: false,
580+
all_paths_failed: false,
581+
path: vec![],
582+
short_channel_id: None,
583+
retry: Some(TestRouter::retry_for_invoice(&invoice)),
584+
};
585+
invoice_payer.handle_event(&event);
586+
assert_eq!(*event_handled.borrow(), true);
587+
assert_eq!(*payer.attempts.borrow(), 1);
588+
}
589+
527590
#[test]
528591
fn fails_paying_invoice_after_retry_error() {
529592
let event_handled = core::cell::RefCell::new(false);
@@ -745,6 +808,7 @@ mod tests {
745808

746809
fn retry_for_invoice(invoice: &Invoice) -> RouteParameters {
747810
let mut payee = Payee::new(invoice.recover_payee_pub_key())
811+
.with_expiry_time(expiry_time_from_unix_epoch(invoice))
748812
.with_route_hints(invoice.route_hints());
749813
if let Some(features) = invoice.features() {
750814
payee = payee.with_features(features.clone());

lightning/src/routing/router.rs

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use prelude::*;
2727
use alloc::collections::BinaryHeap;
2828
use core::cmp;
2929
use core::ops::Deref;
30+
use core::time::Duration;
3031

3132
/// A hop in a route
3233
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
@@ -180,12 +181,16 @@ pub struct Payee {
180181

181182
/// Hints for routing to the payee, containing channels connecting the payee to public nodes.
182183
pub route_hints: Vec<RouteHint>,
184+
185+
/// Expiration of a payment to the payee, relative to a user-defined epoch.
186+
pub expiry_time: Option<Duration>,
183187
}
184188

185189
impl_writeable_tlv_based!(Payee, {
186190
(0, pubkey, required),
187191
(2, features, option),
188192
(4, route_hints, vec_type),
193+
(6, expiry_time, option),
189194
});
190195

191196
impl Payee {
@@ -195,6 +200,7 @@ impl Payee {
195200
pubkey,
196201
features: None,
197202
route_hints: vec![],
203+
expiry_time: None,
198204
}
199205
}
200206

@@ -216,6 +222,11 @@ impl Payee {
216222
pub fn with_route_hints(self, route_hints: Vec<RouteHint>) -> Self {
217223
Self { route_hints, ..self }
218224
}
225+
226+
/// Includes a payment expiration relative to a user-defined epoch.
227+
pub fn with_expiry_time(self, expiry_time: Duration) -> Self {
228+
Self { expiry_time: Some(expiry_time), ..self }
229+
}
219230
}
220231

221232
/// A list of hops along a payment path terminating with a channel to the recipient.

lightning/src/util/ser.rs

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use bitcoin::consensus::Encodable;
2727
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
2828
use bitcoin::hash_types::{Txid, BlockHash};
2929
use core::marker::Sized;
30+
use core::time::Duration;
3031
use ln::msgs::DecodeError;
3132
use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
3233

@@ -911,3 +912,16 @@ impl Readable for String {
911912
Ok(ret)
912913
}
913914
}
915+
916+
impl Writeable for Duration {
917+
#[inline]
918+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
919+
self.as_secs().write(w)
920+
}
921+
}
922+
impl Readable for Duration {
923+
#[inline]
924+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
925+
Ok(Duration::from_secs(Readable::read(r)?))
926+
}
927+
}

0 commit comments

Comments
 (0)