Skip to content

Commit c888a90

Browse files
committed
Add min_final_cltv_expiry parameter to invoice utils
All utility functions for invoice construction will now also accept an Option<>al `min_final_cltv_expiry_delta` which is useful for things like swaps etc. The `min_final_cltv_expiry_delta` will default back to `MIN_FINAL_CLTV_EXPIRY_DELTA` if ` None is provided.
1 parent 9048d14 commit c888a90

File tree

5 files changed

+99
-44
lines changed

5 files changed

+99
-44
lines changed

lightning-invoice/src/de.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use secp256k1;
2222
use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
2323
use secp256k1::PublicKey;
2424

25-
use super::{Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiry, Fallback, PayeePubKey, InvoiceSignature, PositiveTimestamp,
25+
use super::{Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiryDelta, Fallback, PayeePubKey, InvoiceSignature, PositiveTimestamp,
2626
SemanticError, PrivateRoute, ParseError, ParseOrSemanticError, Description, RawTaggedField, Currency, RawHrp, SiPrefix, RawInvoice,
2727
constants, SignedRawInvoice, RawDataPart, InvoiceFeatures};
2828

@@ -452,7 +452,7 @@ impl FromBase32 for TaggedField {
452452
constants::TAG_EXPIRY_TIME =>
453453
Ok(TaggedField::ExpiryTime(ExpiryTime::from_base32(field_data)?)),
454454
constants::TAG_MIN_FINAL_CLTV_EXPIRY_DELTA =>
455-
Ok(TaggedField::MinFinalCltvExpiry(MinFinalCltvExpiry::from_base32(field_data)?)),
455+
Ok(TaggedField::MinFinalCltvExpiryDelta(MinFinalCltvExpiryDelta::from_base32(field_data)?)),
456456
constants::TAG_FALLBACK =>
457457
Ok(TaggedField::Fallback(Fallback::from_base32(field_data)?)),
458458
constants::TAG_PRIVATE_ROUTE =>
@@ -523,13 +523,13 @@ impl FromBase32 for ExpiryTime {
523523
}
524524
}
525525

526-
impl FromBase32 for MinFinalCltvExpiry {
526+
impl FromBase32 for MinFinalCltvExpiryDelta {
527527
type Err = ParseError;
528528

529-
fn from_base32(field_data: &[u5]) -> Result<MinFinalCltvExpiry, ParseError> {
529+
fn from_base32(field_data: &[u5]) -> Result<MinFinalCltvExpiryDelta, ParseError> {
530530
let expiry = parse_int_be::<u64, u5>(field_data, 32);
531531
if let Some(expiry) = expiry {
532-
Ok(MinFinalCltvExpiry(expiry))
532+
Ok(MinFinalCltvExpiryDelta(expiry))
533533
} else {
534534
Err(ParseError::IntegerOverflowError)
535535
}
@@ -841,13 +841,13 @@ mod test {
841841

842842
#[test]
843843
fn test_parse_min_final_cltv_expiry_delta() {
844-
use crate::MinFinalCltvExpiry;
844+
use crate::MinFinalCltvExpiryDelta;
845845
use bech32::FromBase32;
846846

847847
let input = from_bech32("pr".as_bytes());
848-
let expected = Ok(MinFinalCltvExpiry(35));
848+
let expected = Ok(MinFinalCltvExpiryDelta(35));
849849

850-
assert_eq!(MinFinalCltvExpiry::from_base32(&input), expected);
850+
assert_eq!(MinFinalCltvExpiryDelta::from_base32(&input), expected);
851851
}
852852

853853
#[test]

lightning-invoice/src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ pub enum TaggedField {
410410
PayeePubKey(PayeePubKey),
411411
DescriptionHash(Sha256),
412412
ExpiryTime(ExpiryTime),
413-
MinFinalCltvExpiry(MinFinalCltvExpiry),
413+
MinFinalCltvExpiryDelta(MinFinalCltvExpiryDelta),
414414
Fallback(Fallback),
415415
PrivateRoute(PrivateRoute),
416416
PaymentSecret(PaymentSecret),
@@ -440,7 +440,7 @@ pub struct ExpiryTime(Duration);
440440

441441
/// `min_final_cltv_expiry_delta` to use for the last HTLC in the route
442442
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
443-
pub struct MinFinalCltvExpiry(pub u64);
443+
pub struct MinFinalCltvExpiryDelta(pub u64);
444444

445445
// TODO: better types instead onf byte arrays
446446
/// Fallback address in case no LN payment is possible
@@ -656,7 +656,7 @@ impl<D: tb::Bool, H: tb::Bool, C: tb::Bool, S: tb::Bool> InvoiceBuilder<D, H, tb
656656
impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, S: tb::Bool> InvoiceBuilder<D, H, T, tb::False, S> {
657657
/// Sets `min_final_cltv_expiry_delta`.
658658
pub fn min_final_cltv_expiry_delta(mut self, min_final_cltv_expiry_delta: u64) -> InvoiceBuilder<D, H, T, tb::True, S> {
659-
self.tagged_fields.push(TaggedField::MinFinalCltvExpiry(MinFinalCltvExpiry(min_final_cltv_expiry_delta)));
659+
self.tagged_fields.push(TaggedField::MinFinalCltvExpiryDelta(MinFinalCltvExpiryDelta(min_final_cltv_expiry_delta)));
660660
self.set_flags()
661661
}
662662
}
@@ -925,8 +925,8 @@ impl RawInvoice {
925925
find_extract!(self.known_tagged_fields(), TaggedField::ExpiryTime(ref x), x)
926926
}
927927

928-
pub fn min_final_cltv_expiry_delta(&self) -> Option<&MinFinalCltvExpiry> {
929-
find_extract!(self.known_tagged_fields(), TaggedField::MinFinalCltvExpiry(ref x), x)
928+
pub fn min_final_cltv_expiry_delta(&self) -> Option<&MinFinalCltvExpiryDelta> {
929+
find_extract!(self.known_tagged_fields(), TaggedField::MinFinalCltvExpiryDelta(ref x), x)
930930
}
931931

932932
pub fn payment_secret(&self) -> Option<&PaymentSecret> {
@@ -1297,7 +1297,7 @@ impl TaggedField {
12971297
TaggedField::PayeePubKey(_) => constants::TAG_PAYEE_PUB_KEY,
12981298
TaggedField::DescriptionHash(_) => constants::TAG_DESCRIPTION_HASH,
12991299
TaggedField::ExpiryTime(_) => constants::TAG_EXPIRY_TIME,
1300-
TaggedField::MinFinalCltvExpiry(_) => constants::TAG_MIN_FINAL_CLTV_EXPIRY_DELTA,
1300+
TaggedField::MinFinalCltvExpiryDelta(_) => constants::TAG_MIN_FINAL_CLTV_EXPIRY_DELTA,
13011301
TaggedField::Fallback(_) => constants::TAG_FALLBACK,
13021302
TaggedField::PrivateRoute(_) => constants::TAG_PRIVATE_ROUTE,
13031303
TaggedField::PaymentSecret(_) => constants::TAG_PAYMENT_SECRET,
@@ -1444,6 +1444,9 @@ pub enum CreationError {
14441444
///
14451445
/// [phantom invoices]: crate::utils::create_phantom_invoice
14461446
MissingRouteHints,
1447+
1448+
/// The provided `min_final_cltv_expiry_delta` was less than `MIN_FINAL_CLTV_EXPIRY_DELTA`.
1449+
MinFinalCltvExpiryDeltaTooShort,
14471450
}
14481451

14491452
impl Display for CreationError {
@@ -1454,6 +1457,8 @@ impl Display for CreationError {
14541457
CreationError::TimestampOutOfBounds => f.write_str("The Unix timestamp of the supplied date is less than zero or greater than 35-bits"),
14551458
CreationError::InvalidAmount => f.write_str("The supplied millisatoshi amount was greater than the total bitcoin supply"),
14561459
CreationError::MissingRouteHints => f.write_str("The invoice required route hints and they weren't provided"),
1460+
CreationError::MinFinalCltvExpiryDeltaTooShort => f.write_str(
1461+
"The supplied final CLTV expiry delta was less than LDK's `MIN_FINAL_CLTV_EXPIRY_DELTA`"),
14571462
}
14581463
}
14591464
}

lightning-invoice/src/payment.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,7 @@ mod tests {
20822082

20832083
assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager_and_duration_since_epoch(
20842084
&nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::Bitcoin,
2085-
Some(100_010_000), "Invoice".to_string(), duration_since_epoch(), 3600).unwrap())
2085+
Some(100_010_000), "Invoice".to_string(), duration_since_epoch(), 3600, None).unwrap())
20862086
.is_ok());
20872087
let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
20882088
assert_eq!(htlc_msgs.len(), 2);
@@ -2127,7 +2127,7 @@ mod tests {
21272127

21282128
assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager_and_duration_since_epoch(
21292129
&nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::Bitcoin,
2130-
Some(100_010_000), "Invoice".to_string(), duration_since_epoch(), 3600).unwrap())
2130+
Some(100_010_000), "Invoice".to_string(), duration_since_epoch(), 3600, None).unwrap())
21312131
.is_ok());
21322132
let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
21332133
assert_eq!(htlc_msgs.len(), 2);
@@ -2208,7 +2208,7 @@ mod tests {
22082208

22092209
assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager_and_duration_since_epoch(
22102210
&nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::Bitcoin,
2211-
Some(100_010_000), "Invoice".to_string(), duration_since_epoch(), 3600).unwrap())
2211+
Some(100_010_000), "Invoice".to_string(), duration_since_epoch(), 3600, None).unwrap())
22122212
.is_ok());
22132213
let htlc_updates = SendEvent::from_node(&nodes[0]);
22142214
check_added_monitors!(nodes[0], 1);

lightning-invoice/src/ser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::fmt::{Display, Formatter};
33
use bech32::{ToBase32, u5, WriteBase32, Base32Len};
44
use crate::prelude::*;
55

6-
use super::{Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiry, Fallback, PayeePubKey, InvoiceSignature, PositiveTimestamp,
6+
use super::{Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiryDelta, Fallback, PayeePubKey, InvoiceSignature, PositiveTimestamp,
77
PrivateRoute, Description, RawTaggedField, Currency, RawHrp, SiPrefix, constants, SignedRawInvoice, RawDataPart};
88

99
/// Converts a stream of bytes written to it to base32. On finalization the according padding will
@@ -313,13 +313,13 @@ impl Base32Len for ExpiryTime {
313313
}
314314
}
315315

316-
impl ToBase32 for MinFinalCltvExpiry {
316+
impl ToBase32 for MinFinalCltvExpiryDelta {
317317
fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
318318
writer.write(&encode_int_be_base32(self.0))
319319
}
320320
}
321321

322-
impl Base32Len for MinFinalCltvExpiry {
322+
impl Base32Len for MinFinalCltvExpiryDelta {
323323
fn base32_len(&self) -> usize {
324324
encoded_int_be_base32_size(self.0)
325325
}
@@ -434,7 +434,7 @@ impl ToBase32 for TaggedField {
434434
TaggedField::ExpiryTime(ref duration) => {
435435
write_tagged_field(writer, constants::TAG_EXPIRY_TIME, duration)
436436
},
437-
TaggedField::MinFinalCltvExpiry(ref expiry) => {
437+
TaggedField::MinFinalCltvExpiryDelta(ref expiry) => {
438438
write_tagged_field(writer, constants::TAG_MIN_FINAL_CLTV_EXPIRY_DELTA, expiry)
439439
},
440440
TaggedField::Fallback(ref fallback_address) => {

0 commit comments

Comments
 (0)