@@ -21,7 +21,6 @@ use secp256k1::PublicKey;
21
21
use core:: ops:: Deref ;
22
22
use core:: time:: Duration ;
23
23
24
- #[ cfg( feature = "std" ) ]
25
24
/// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
26
25
/// See [`PhantomKeysManager`] for more information on phantom node payments.
27
26
///
@@ -50,6 +49,9 @@ use core::time::Duration;
50
49
/// Note that the provided `keys_manager`'s `NodeSigner` implementation must support phantom
51
50
/// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
52
51
/// requirement).
52
+ ///
53
+ /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
54
+ /// available and the current time is supplied by the caller.
53
55
///
54
56
/// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
55
57
/// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
@@ -60,7 +62,7 @@ use core::time::Duration;
60
62
pub fn create_phantom_invoice < ES : Deref , NS : Deref , L : Deref > (
61
63
amt_msat : Option < u64 > , payment_hash : Option < PaymentHash > , description : String ,
62
64
invoice_expiry_delta_secs : u32 , phantom_route_hints : Vec < PhantomRouteHints > , entropy_source : ES ,
63
- node_signer : NS , logger : L , network : Currency , min_final_cltv_expiry_delta : Option < u16 > ,
65
+ node_signer : NS , logger : L , network : Currency , min_final_cltv_expiry_delta : Option < u16 > , duration_since_epoch : Duration ,
64
66
) -> Result < Invoice , SignOrCreationError < ( ) > >
65
67
where
66
68
ES :: Target : EntropySource ,
@@ -71,11 +73,10 @@ where
71
73
let description = InvoiceDescription :: Direct ( & description, ) ;
72
74
_create_phantom_invoice :: < ES , NS , L > (
73
75
amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints,
74
- entropy_source, node_signer, logger, network, min_final_cltv_expiry_delta,
76
+ entropy_source, node_signer, logger, network, min_final_cltv_expiry_delta, duration_since_epoch ,
75
77
)
76
78
}
77
79
78
- #[ cfg( feature = "std" ) ]
79
80
/// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
80
81
/// See [`PhantomKeysManager`] for more information on phantom node payments.
81
82
///
@@ -101,6 +102,9 @@ where
101
102
/// Note that the provided `keys_manager`'s `NodeSigner` implementation must support phantom
102
103
/// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
103
104
/// requirement).
105
+ ///
106
+ /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
107
+ /// available and the current time is supplied by the caller.
104
108
///
105
109
/// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
106
110
/// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
@@ -110,7 +114,7 @@ where
110
114
pub fn create_phantom_invoice_with_description_hash < ES : Deref , NS : Deref , L : Deref > (
111
115
amt_msat : Option < u64 > , payment_hash : Option < PaymentHash > , invoice_expiry_delta_secs : u32 ,
112
116
description_hash : Sha256 , phantom_route_hints : Vec < PhantomRouteHints > , entropy_source : ES ,
113
- node_signer : NS , logger : L , network : Currency , min_final_cltv_expiry_delta : Option < u16 > ,
117
+ node_signer : NS , logger : L , network : Currency , min_final_cltv_expiry_delta : Option < u16 > , duration_since_epoch : Duration ,
114
118
) -> Result < Invoice , SignOrCreationError < ( ) > >
115
119
where
116
120
ES :: Target : EntropySource ,
@@ -120,22 +124,20 @@ where
120
124
_create_phantom_invoice :: < ES , NS , L > (
121
125
amt_msat, payment_hash, InvoiceDescription :: Hash ( & description_hash) ,
122
126
invoice_expiry_delta_secs, phantom_route_hints, entropy_source, node_signer, logger, network,
123
- min_final_cltv_expiry_delta,
127
+ min_final_cltv_expiry_delta, duration_since_epoch ,
124
128
)
125
129
}
126
130
127
- #[ cfg( feature = "std" ) ]
128
131
fn _create_phantom_invoice < ES : Deref , NS : Deref , L : Deref > (
129
132
amt_msat : Option < u64 > , payment_hash : Option < PaymentHash > , description : InvoiceDescription ,
130
133
invoice_expiry_delta_secs : u32 , phantom_route_hints : Vec < PhantomRouteHints > , entropy_source : ES ,
131
- node_signer : NS , logger : L , network : Currency , min_final_cltv_expiry_delta : Option < u16 > ,
134
+ node_signer : NS , logger : L , network : Currency , min_final_cltv_expiry_delta : Option < u16 > , duration_since_epoch : Duration ,
132
135
) -> Result < Invoice , SignOrCreationError < ( ) > >
133
136
where
134
137
ES :: Target : EntropySource ,
135
138
NS :: Target : NodeSigner ,
136
139
L :: Target : Logger ,
137
140
{
138
- use std:: time:: { SystemTime , UNIX_EPOCH } ;
139
141
140
142
if phantom_route_hints. len ( ) == 0 {
141
143
return Err ( SignOrCreationError :: CreationError (
@@ -162,9 +164,7 @@ where
162
164
amt_msat,
163
165
payment_hash,
164
166
invoice_expiry_delta_secs,
165
- SystemTime :: now ( )
166
- . duration_since ( UNIX_EPOCH )
167
- . expect ( "Time must be > 1970" )
167
+ duration_since_epoch
168
168
. as_secs ( ) ,
169
169
min_final_cltv_expiry_delta,
170
170
)
@@ -176,9 +176,7 @@ where
176
176
amt_msat,
177
177
invoice_expiry_delta_secs,
178
178
& entropy_source,
179
- SystemTime :: now ( )
180
- . duration_since ( UNIX_EPOCH )
181
- . expect ( "Time must be > 1970" )
179
+ duration_since_epoch
182
180
. as_secs ( ) ,
183
181
min_final_cltv_expiry_delta,
184
182
)
@@ -1072,7 +1070,7 @@ mod test {
1072
1070
crate :: utils:: create_phantom_invoice :: < & test_utils:: TestKeysInterface , & test_utils:: TestKeysInterface , & test_utils:: TestLogger > (
1073
1071
Some ( payment_amt) , payment_hash, "test" . to_string ( ) , non_default_invoice_expiry_secs,
1074
1072
route_hints, & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . logger ,
1075
- Currency :: BitcoinTestnet , None ,
1073
+ Currency :: BitcoinTestnet , None , Duration :: from_secs ( 1234567 )
1076
1074
) . unwrap ( ) ;
1077
1075
let ( payment_hash, payment_secret) = ( PaymentHash ( invoice. payment_hash ( ) . into_inner ( ) ) , * invoice. payment_secret ( ) ) ;
1078
1076
let payment_preimage = if user_generated_pmt_hash {
@@ -1182,7 +1180,7 @@ mod test {
1182
1180
let invoice = crate :: utils:: create_phantom_invoice :: < & test_utils:: TestKeysInterface ,
1183
1181
& test_utils:: TestKeysInterface , & test_utils:: TestLogger > ( Some ( payment_amt) , Some ( payment_hash) ,
1184
1182
"test" . to_string ( ) , 3600 , route_hints, & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . keys_manager ,
1185
- & nodes[ 1 ] . logger , Currency :: BitcoinTestnet , None ) . unwrap ( ) ;
1183
+ & nodes[ 1 ] . logger , Currency :: BitcoinTestnet , None , Duration :: from_secs ( 1234567 ) ) . unwrap ( ) ;
1186
1184
1187
1185
let chan_0_1 = & nodes[ 1 ] . node . list_usable_channels ( ) [ 0 ] ;
1188
1186
assert_eq ! ( invoice. route_hints( ) [ 0 ] . 0 [ 0 ] . htlc_minimum_msat, chan_0_1. inbound_htlc_minimum_msat) ;
@@ -1214,7 +1212,7 @@ mod test {
1214
1212
> (
1215
1213
Some ( payment_amt) , None , non_default_invoice_expiry_secs, description_hash,
1216
1214
route_hints, & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . logger ,
1217
- Currency :: BitcoinTestnet , None ,
1215
+ Currency :: BitcoinTestnet , None , Duration :: from_secs ( 1234567 ) ,
1218
1216
)
1219
1217
. unwrap ( ) ;
1220
1218
assert_eq ! ( invoice. amount_pico_btc( ) , Some ( 200_000 ) ) ;
@@ -1240,10 +1238,11 @@ mod test {
1240
1238
let payment_hash = Some ( PaymentHash ( Sha256 :: hash ( & user_payment_preimage. 0 [ ..] ) . into_inner ( ) ) ) ;
1241
1239
let non_default_invoice_expiry_secs = 4200 ;
1242
1240
let min_final_cltv_expiry_delta = Some ( 100 ) ;
1241
+ let duration_since_epoch = Duration :: from_secs ( 1234567 ) ;
1243
1242
let invoice = crate :: utils:: create_phantom_invoice :: < & test_utils:: TestKeysInterface ,
1244
1243
& test_utils:: TestKeysInterface , & test_utils:: TestLogger > ( Some ( payment_amt) , payment_hash,
1245
1244
"" . to_string ( ) , non_default_invoice_expiry_secs, route_hints, & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . keys_manager ,
1246
- & nodes[ 1 ] . logger , Currency :: BitcoinTestnet , min_final_cltv_expiry_delta) . unwrap ( ) ;
1245
+ & nodes[ 1 ] . logger , Currency :: BitcoinTestnet , min_final_cltv_expiry_delta, duration_since_epoch ) . unwrap ( ) ;
1247
1246
assert_eq ! ( invoice. amount_pico_btc( ) , Some ( 200_000 ) ) ;
1248
1247
assert_eq ! ( invoice. min_final_cltv_expiry_delta( ) , ( min_final_cltv_expiry_delta. unwrap( ) + 3 ) as u64 ) ;
1249
1248
assert_eq ! ( invoice. expiry_time( ) , Duration :: from_secs( non_default_invoice_expiry_secs. into( ) ) ) ;
@@ -1557,7 +1556,7 @@ mod test {
1557
1556
let invoice = crate :: utils:: create_phantom_invoice :: < & test_utils:: TestKeysInterface ,
1558
1557
& test_utils:: TestKeysInterface , & test_utils:: TestLogger > ( invoice_amt, None , "test" . to_string ( ) ,
1559
1558
3600 , phantom_route_hints, & invoice_node. keys_manager , & invoice_node. keys_manager ,
1560
- & invoice_node. logger , Currency :: BitcoinTestnet , None ) . unwrap ( ) ;
1559
+ & invoice_node. logger , Currency :: BitcoinTestnet , None , Duration :: from_secs ( 1234567 ) ) . unwrap ( ) ;
1561
1560
1562
1561
let invoice_hints = invoice. private_routes ( ) ;
1563
1562
0 commit comments