@@ -45,6 +45,7 @@ pub use de::{ParseError, ParseOrSemanticError};
45
45
/// let invoice = InvoiceBuilder::new(Currency::Bitcoin)
46
46
/// .description("Coins pls!".into())
47
47
/// .payment_hash([0u8; 32])
48
+ /// .current_timestamp()
48
49
/// .build_signed(|hash| {
49
50
/// Secp256k1::new().sign_recoverable(hash, &private_key)
50
51
/// })
@@ -59,8 +60,9 @@ pub use de::{ParseError, ParseOrSemanticError};
59
60
/// given field:
60
61
/// * `D`: exactly one `Description` or `DescriptionHash`
61
62
/// * `H`: exactly one `PaymentHash`
63
+ /// * `T`: the timestamp is set
62
64
#[ derive( Eq , PartialEq , Debug , Clone ) ]
63
- pub struct InvoiceBuilder < D : tb:: Bool , H : tb:: Bool > {
65
+ pub struct InvoiceBuilder < D : tb:: Bool , H : tb:: Bool , T : tb :: Bool > {
64
66
currency : Currency ,
65
67
amount : Option < u64 > ,
66
68
si_prefix : Option < SiPrefix > ,
@@ -70,6 +72,7 @@ pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool> {
70
72
71
73
phantom_d : std:: marker:: PhantomData < D > ,
72
74
phantom_h : std:: marker:: PhantomData < H > ,
75
+ phantom_t : std:: marker:: PhantomData < T > ,
73
76
}
74
77
75
78
/// Represents a syntactically and semantically correct lightning BOLT11 invoice.
@@ -293,7 +296,7 @@ fn as_u5(tag: u8) -> u5 {
293
296
u5:: try_from_u8 ( tag) . unwrap ( )
294
297
}
295
298
296
- impl InvoiceBuilder < tb:: False , tb:: False > {
299
+ impl InvoiceBuilder < tb:: False , tb:: False , tb :: False > {
297
300
/// Construct new, empty `InvoiceBuilder`. All necessary fields have to be filled first before
298
301
/// `InvoiceBuilder::build(self)` becomes available.
299
302
pub fn new ( currrency : Currency ) -> Self {
@@ -307,14 +310,15 @@ impl InvoiceBuilder<tb::False, tb::False> {
307
310
308
311
phantom_d : std:: marker:: PhantomData ,
309
312
phantom_h : std:: marker:: PhantomData ,
313
+ phantom_t : std:: marker:: PhantomData ,
310
314
}
311
315
}
312
316
}
313
317
314
- impl < D : tb:: Bool , H : tb:: Bool > InvoiceBuilder < D , H > {
318
+ impl < D : tb:: Bool , H : tb:: Bool , T : tb :: Bool > InvoiceBuilder < D , H , T > {
315
319
/// Helper function to set the completeness flags.
316
- fn set_flags < DN : tb:: Bool , HN : tb:: Bool > ( self ) -> InvoiceBuilder < DN , HN > {
317
- InvoiceBuilder :: < DN , HN > {
320
+ fn set_flags < DN : tb:: Bool , HN : tb:: Bool , TN : tb :: Bool > ( self ) -> InvoiceBuilder < DN , HN , TN > {
321
+ InvoiceBuilder :: < DN , HN , TN > {
318
322
currency : self . currency ,
319
323
amount : self . amount ,
320
324
si_prefix : self . si_prefix ,
@@ -324,6 +328,7 @@ impl<D: tb::Bool, H: tb::Bool> InvoiceBuilder<D, H> {
324
328
325
329
phantom_d : std:: marker:: PhantomData ,
326
330
phantom_h : std:: marker:: PhantomData ,
331
+ phantom_t : std:: marker:: PhantomData ,
327
332
}
328
333
}
329
334
@@ -338,12 +343,6 @@ impl<D: tb::Bool, H: tb::Bool> InvoiceBuilder<D, H> {
338
343
self
339
344
}
340
345
341
- /// Sets the timestamp. `time` is a UNIX timestamp.
342
- pub fn timestamp ( mut self , time : u64 ) -> Self {
343
- self . timestamp = Some ( time) ;
344
- self
345
- }
346
-
347
346
/// Sets the payee's public key.
348
347
pub fn payee_pub_key ( mut self , pub_key : PublicKey ) -> Self {
349
348
self . tagged_fields . push ( TaggedField :: PayeePubKey ( PayeePubKey ( pub_key) ) ) ;
@@ -415,9 +414,9 @@ impl<D: tb::Bool, H: tb::Bool> InvoiceBuilder<D, H> {
415
414
}
416
415
}
417
416
418
- impl < H : tb:: Bool > InvoiceBuilder < tb:: False , H > {
417
+ impl < H : tb:: Bool , T : tb :: Bool > InvoiceBuilder < tb:: False , H , T > {
419
418
/// Set the description. This function is only available if no description (hash) was set.
420
- pub fn description ( mut self , description : String ) -> InvoiceBuilder < tb:: True , H > {
419
+ pub fn description ( mut self , description : String ) -> InvoiceBuilder < tb:: True , H , T > {
421
420
match Description :: new ( description) {
422
421
Ok ( d) => self . tagged_fields . push ( TaggedField :: Description ( d) ) ,
423
422
Err ( e) => self . error = Some ( e) ,
@@ -426,21 +425,38 @@ impl<H: tb::Bool> InvoiceBuilder<tb::False, H> {
426
425
}
427
426
428
427
/// Set the description hash. This function is only available if no description (hash) was set.
429
- pub fn description_hash ( mut self , description_hash : [ u8 ; 32 ] ) -> InvoiceBuilder < tb:: True , H > {
428
+ pub fn description_hash ( mut self , description_hash : [ u8 ; 32 ] ) -> InvoiceBuilder < tb:: True , H , T > {
430
429
self . tagged_fields . push ( TaggedField :: DescriptionHash ( Sha256 ( description_hash) ) ) ;
431
430
self . set_flags ( )
432
431
}
433
432
}
434
433
435
- impl < D : tb:: Bool > InvoiceBuilder < D , tb:: False > {
434
+ impl < D : tb:: Bool , T : tb :: Bool > InvoiceBuilder < D , tb:: False , T > {
436
435
/// Set the payment hash. This function is only available if no payment hash was set.
437
- pub fn payment_hash ( mut self , hash : [ u8 ; 32 ] ) -> InvoiceBuilder < D , tb:: True > {
436
+ pub fn payment_hash ( mut self , hash : [ u8 ; 32 ] ) -> InvoiceBuilder < D , tb:: True , T > {
438
437
self . tagged_fields . push ( TaggedField :: PaymentHash ( Sha256 ( hash) ) ) ;
439
438
self . set_flags ( )
440
439
}
441
440
}
442
441
443
- impl InvoiceBuilder < tb:: True , tb:: True > {
442
+ impl < D : tb:: Bool , H : tb:: Bool > InvoiceBuilder < D , H , tb:: False > {
443
+ /// Sets the timestamp. `time` is a UNIX timestamp.
444
+ pub fn timestamp ( mut self , time : u64 ) -> InvoiceBuilder < D , H , tb:: True > {
445
+ self . timestamp = Some ( time) ;
446
+ self . set_flags ( )
447
+ }
448
+
449
+ /// Sets the timestamp to the current UNIX timestamp.
450
+ pub fn current_timestamp ( mut self ) -> InvoiceBuilder < D , H , tb:: True > {
451
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
452
+ let now = SystemTime :: now ( ) ;
453
+ let since_unix_epoch = now. duration_since ( UNIX_EPOCH ) . expect ( "it won't be 1970 ever again" ) ;
454
+ self . timestamp = Some ( since_unix_epoch. as_secs ( ) as u64 ) ;
455
+ self . set_flags ( )
456
+ }
457
+ }
458
+
459
+ impl InvoiceBuilder < tb:: True , tb:: True , tb:: True > {
444
460
/// Builds and signs an invoice using the supplied `sign_function`. This function MAY NOT fail
445
461
/// and MUST produce a recoverable signature valid for the given hash and if applicable also for
446
462
/// the included payee public key.
@@ -1150,6 +1166,7 @@ mod test {
1150
1166
1151
1167
let sign_error_res = builder. clone ( )
1152
1168
. description ( "Test" . into ( ) )
1169
+ . current_timestamp ( )
1153
1170
. try_build_signed ( |_| {
1154
1171
Err ( "ImaginaryError" )
1155
1172
} ) ;
0 commit comments