Skip to content

Commit 7189ae9

Browse files
committed
Require min_final_cltv_expiry in invoice
1 parent bf0a5e7 commit 7189ae9

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

lightning-invoice/src/lib.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub fn check_platform() {
146146
/// .description("Coins pls!".into())
147147
/// .payment_hash(payment_hash)
148148
/// .current_timestamp()
149+
/// .min_final_cltv_expiry(144)
149150
/// .build_signed(|hash| {
150151
/// Secp256k1::new().sign_recoverable(hash, &private_key)
151152
/// })
@@ -162,7 +163,7 @@ pub fn check_platform() {
162163
/// * `H`: exactly one `PaymentHash`
163164
/// * `T`: the timestamp is set
164165
#[derive(Eq, PartialEq, Debug, Clone)]
165-
pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool, T: tb::Bool> {
166+
pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool> {
166167
currency: Currency,
167168
amount: Option<u64>,
168169
si_prefix: Option<SiPrefix>,
@@ -173,6 +174,7 @@ pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool, T: tb::Bool> {
173174
phantom_d: std::marker::PhantomData<D>,
174175
phantom_h: std::marker::PhantomData<H>,
175176
phantom_t: std::marker::PhantomData<T>,
177+
phantom_c: std::marker::PhantomData<C>,
176178
}
177179

178180
/// Represents a syntactically and semantically correct lightning BOLT11 invoice.
@@ -416,7 +418,7 @@ pub mod constants {
416418
pub const TAG_FEATURES: u8 = 5;
417419
}
418420

419-
impl InvoiceBuilder<tb::False, tb::False, tb::False> {
421+
impl InvoiceBuilder<tb::False, tb::False, tb::False, tb::False> {
420422
/// Construct new, empty `InvoiceBuilder`. All necessary fields have to be filled first before
421423
/// `InvoiceBuilder::build(self)` becomes available.
422424
pub fn new(currrency: Currency) -> Self {
@@ -431,14 +433,15 @@ impl InvoiceBuilder<tb::False, tb::False, tb::False> {
431433
phantom_d: std::marker::PhantomData,
432434
phantom_h: std::marker::PhantomData,
433435
phantom_t: std::marker::PhantomData,
436+
phantom_c: std::marker::PhantomData,
434437
}
435438
}
436439
}
437440

438-
impl<D: tb::Bool, H: tb::Bool, T: tb::Bool> InvoiceBuilder<D, H, T> {
441+
impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool> InvoiceBuilder<D, H, T, C> {
439442
/// Helper function to set the completeness flags.
440-
fn set_flags<DN: tb::Bool, HN: tb::Bool, TN: tb::Bool>(self) -> InvoiceBuilder<DN, HN, TN> {
441-
InvoiceBuilder::<DN, HN, TN> {
443+
fn set_flags<DN: tb::Bool, HN: tb::Bool, TN: tb::Bool, CN: tb::Bool>(self) -> InvoiceBuilder<DN, HN, TN, CN> {
444+
InvoiceBuilder::<DN, HN, TN, CN> {
442445
currency: self.currency,
443446
amount: self.amount,
444447
si_prefix: self.si_prefix,
@@ -449,6 +452,7 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool> InvoiceBuilder<D, H, T> {
449452
phantom_d: std::marker::PhantomData,
450453
phantom_h: std::marker::PhantomData,
451454
phantom_t: std::marker::PhantomData,
455+
phantom_c: std::marker::PhantomData,
452456
}
453457
}
454458

@@ -484,12 +488,6 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool> InvoiceBuilder<D, H, T> {
484488
self
485489
}
486490

487-
/// Sets `min_final_cltv_expiry`.
488-
pub fn min_final_cltv_expiry(mut self, min_final_cltv_expiry: u64) -> Self {
489-
self.tagged_fields.push(TaggedField::MinFinalCltvExpiry(MinFinalCltvExpiry(min_final_cltv_expiry)));
490-
self
491-
}
492-
493491
/// Adds a fallback address.
494492
pub fn fallback(mut self, fallback: Fallback) -> Self {
495493
self.tagged_fields.push(TaggedField::Fallback(fallback));
@@ -513,7 +511,7 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool> InvoiceBuilder<D, H, T> {
513511
}
514512
}
515513

516-
impl<D: tb::Bool, H: tb::Bool> InvoiceBuilder<D, H, tb::True> {
514+
impl<D: tb::Bool, H: tb::Bool, C: tb::Bool> InvoiceBuilder<D, H, tb::True, C> {
517515
/// Builds a `RawInvoice` if no `CreationError` occurred while construction any of the fields.
518516
pub fn build_raw(self) -> Result<RawInvoice, CreationError> {
519517

@@ -546,9 +544,9 @@ impl<D: tb::Bool, H: tb::Bool> InvoiceBuilder<D, H, tb::True> {
546544
}
547545
}
548546

549-
impl<H: tb::Bool, T: tb::Bool> InvoiceBuilder<tb::False, H, T> {
547+
impl<H: tb::Bool, T: tb::Bool, C: tb::Bool> InvoiceBuilder<tb::False, H, T, C> {
550548
/// Set the description. This function is only available if no description (hash) was set.
551-
pub fn description(mut self, description: String) -> InvoiceBuilder<tb::True, H, T> {
549+
pub fn description(mut self, description: String) -> InvoiceBuilder<tb::True, H, T, C> {
552550
match Description::new(description) {
553551
Ok(d) => self.tagged_fields.push(TaggedField::Description(d)),
554552
Err(e) => self.error = Some(e),
@@ -557,23 +555,23 @@ impl<H: tb::Bool, T: tb::Bool> InvoiceBuilder<tb::False, H, T> {
557555
}
558556

559557
/// Set the description hash. This function is only available if no description (hash) was set.
560-
pub fn description_hash(mut self, description_hash: sha256::Hash) -> InvoiceBuilder<tb::True, H, T> {
558+
pub fn description_hash(mut self, description_hash: sha256::Hash) -> InvoiceBuilder<tb::True, H, T, C> {
561559
self.tagged_fields.push(TaggedField::DescriptionHash(Sha256(description_hash)));
562560
self.set_flags()
563561
}
564562
}
565563

566-
impl<D: tb::Bool, T: tb::Bool> InvoiceBuilder<D, tb::False, T> {
564+
impl<D: tb::Bool, T: tb::Bool, C: tb::Bool> InvoiceBuilder<D, tb::False, T, C> {
567565
/// Set the payment hash. This function is only available if no payment hash was set.
568-
pub fn payment_hash(mut self, hash: sha256::Hash) -> InvoiceBuilder<D, tb::True, T> {
566+
pub fn payment_hash(mut self, hash: sha256::Hash) -> InvoiceBuilder<D, tb::True, T, C> {
569567
self.tagged_fields.push(TaggedField::PaymentHash(Sha256(hash)));
570568
self.set_flags()
571569
}
572570
}
573571

574-
impl<D: tb::Bool, H: tb::Bool> InvoiceBuilder<D, H, tb::False> {
572+
impl<D: tb::Bool, H: tb::Bool, C: tb::Bool> InvoiceBuilder<D, H, tb::False, C> {
575573
/// Sets the timestamp.
576-
pub fn timestamp(mut self, time: SystemTime) -> InvoiceBuilder<D, H, tb::True> {
574+
pub fn timestamp(mut self, time: SystemTime) -> InvoiceBuilder<D, H, tb::True, C> {
577575
match PositiveTimestamp::from_system_time(time) {
578576
Ok(t) => self.timestamp = Some(t),
579577
Err(e) => self.error = Some(e),
@@ -583,14 +581,22 @@ impl<D: tb::Bool, H: tb::Bool> InvoiceBuilder<D, H, tb::False> {
583581
}
584582

585583
/// Sets the timestamp to the current UNIX timestamp.
586-
pub fn current_timestamp(mut self) -> InvoiceBuilder<D, H, tb::True> {
584+
pub fn current_timestamp(mut self) -> InvoiceBuilder<D, H, tb::True, C> {
587585
let now = PositiveTimestamp::from_system_time(SystemTime::now());
588586
self.timestamp = Some(now.expect("for the foreseeable future this shouldn't happen"));
589587
self.set_flags()
590588
}
591589
}
592590

593-
impl InvoiceBuilder<tb::True, tb::True, tb::True> {
591+
impl<D: tb::Bool, H: tb::Bool, T: tb::Bool> InvoiceBuilder<D, H, T, tb::False> {
592+
/// Sets `min_final_cltv_expiry`.
593+
pub fn min_final_cltv_expiry(mut self, min_final_cltv_expiry: u64) -> InvoiceBuilder<D, H, T, tb::True> {
594+
self.tagged_fields.push(TaggedField::MinFinalCltvExpiry(MinFinalCltvExpiry(min_final_cltv_expiry)));
595+
self.set_flags()
596+
}
597+
}
598+
599+
impl InvoiceBuilder<tb::True, tb::True, tb::True, tb::True> {
594600
/// Builds and signs an invoice using the supplied `sign_function`. This function MAY NOT fail
595601
/// and MUST produce a recoverable signature valid for the given hash and if applicable also for
596602
/// the included payee public key.
@@ -1465,7 +1471,8 @@ mod test {
14651471

14661472
let builder = InvoiceBuilder::new(Currency::Bitcoin)
14671473
.payment_hash(sha256::Hash::from_slice(&[0;32][..]).unwrap())
1468-
.current_timestamp();
1474+
.current_timestamp()
1475+
.min_final_cltv_expiry(144);
14691476

14701477
let too_long_string = String::from_iter(
14711478
(0..1024).map(|_| '?')
@@ -1582,7 +1589,6 @@ mod test {
15821589
.payee_pub_key(public_key.clone())
15831590
.expiry_time(Duration::from_secs(54321))
15841591
.min_final_cltv_expiry(144)
1585-
.min_final_cltv_expiry(143)
15861592
.fallback(Fallback::PubKeyHash([0;20]))
15871593
.route(route_1.clone())
15881594
.route(route_2.clone())
@@ -1594,7 +1600,7 @@ mod test {
15941600
}).unwrap();
15951601

15961602
assert!(invoice.check_signature().is_ok());
1597-
assert_eq!(invoice.tagged_fields().count(), 9);
1603+
assert_eq!(invoice.tagged_fields().count(), 8);
15981604

15991605
assert_eq!(invoice.amount_pico_btc(), Some(123));
16001606
assert_eq!(invoice.currency(), Currency::BitcoinTestnet);

lightning-invoice/tests/ser_de.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ fn get_test_tuples() -> Vec<(String, SignedRawInvoice, Option<SemanticError>)> {
110110
.amount_pico_btc(20000000000)
111111
.timestamp(UNIX_EPOCH + Duration::from_secs(1496314658))
112112
.payment_secret(PaymentSecret([42; 32]))
113-
.build_signed(|msg_hash| {
113+
.build_raw()
114+
.unwrap()
115+
.sign::<_, ()>(|msg_hash| {
114116
let privkey = SecretKey::from_slice(&[41; 32]).unwrap();
115117
let secp_ctx = Secp256k1::new();
116-
secp_ctx.sign_recoverable(msg_hash, &privkey)
118+
Ok(secp_ctx.sign_recoverable(msg_hash, &privkey))
117119
})
118-
.unwrap()
119-
.into_signed_raw(),
120+
.unwrap(),
120121
None
121122
)
122123
]

0 commit comments

Comments
 (0)