@@ -11,7 +11,6 @@ use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
11
11
use lightning:: chain:: keysinterface:: { Recipient , NodeSigner , SignerProvider , EntropySource } ;
12
12
use lightning:: ln:: { PaymentHash , PaymentPreimage , PaymentSecret } ;
13
13
use lightning:: ln:: channelmanager:: { ChannelDetails , ChannelManager , PaymentId , PaymentSendFailure , MIN_FINAL_CLTV_EXPIRY_DELTA } ;
14
- #[ cfg( feature = "std" ) ]
15
14
use lightning:: ln:: channelmanager:: { PhantomRouteHints , MIN_CLTV_EXPIRY_DELTA } ;
16
15
use lightning:: ln:: inbound_payment:: { create, create_from_hash, ExpandedKey } ;
17
16
use lightning:: routing:: gossip:: RoutingFees ;
@@ -21,7 +20,6 @@ use secp256k1::PublicKey;
21
20
use core:: ops:: Deref ;
22
21
use core:: time:: Duration ;
23
22
24
- #[ cfg( feature = "std" ) ]
25
23
/// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
26
24
/// See [`PhantomKeysManager`] for more information on phantom node payments.
27
25
///
@@ -41,6 +39,11 @@ use core::time::Duration;
41
39
///
42
40
/// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
43
41
/// in excess of the current time.
42
+ ///
43
+ /// 'duration_since_epoch' is the current time since epoch in seconds.
44
+ ///
45
+ /// ['std::time::SystemTime'] has been removed to allow this function to be used in a 'no_std' environment,
46
+ /// where [`std::time::SystemTime`] is not available and the current time is supplied by the caller.
44
47
///
45
48
/// You can specify a custom `min_final_cltv_expiry_delta`, or let LDK default it to
46
49
/// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]. The provided expiry must be at least [`MIN_FINAL_CLTV_EXPIRY_DELTA`] - 3.
@@ -60,7 +63,7 @@ use core::time::Duration;
60
63
pub fn create_phantom_invoice < ES : Deref , NS : Deref , L : Deref > (
61
64
amt_msat : Option < u64 > , payment_hash : Option < PaymentHash > , description : String ,
62
65
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 > ,
66
+ node_signer : NS , logger : L , network : Currency , min_final_cltv_expiry_delta : Option < u16 > , duration_since_epoch : Duration ,
64
67
) -> Result < Invoice , SignOrCreationError < ( ) > >
65
68
where
66
69
ES :: Target : EntropySource ,
@@ -71,11 +74,10 @@ where
71
74
let description = InvoiceDescription :: Direct ( & description, ) ;
72
75
_create_phantom_invoice :: < ES , NS , L > (
73
76
amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints,
74
- entropy_source, node_signer, logger, network, min_final_cltv_expiry_delta,
77
+ entropy_source, node_signer, logger, network, min_final_cltv_expiry_delta, duration_since_epoch ,
75
78
)
76
79
}
77
80
78
- #[ cfg( feature = "std" ) ]
79
81
/// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
80
82
/// See [`PhantomKeysManager`] for more information on phantom node payments.
81
83
///
97
99
///
98
100
/// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
99
101
/// in excess of the current time.
102
+ ///
103
+ /// 'duration_since_epoch' is the current time since epoch in seconds.
104
+ ///
105
+ /// ['std::time::SystemTime'] has been removed to allow this function to be used in a 'no_std' environment,
106
+ /// where [`std::time::SystemTime`] is not available and the current time is supplied by the caller.
100
107
///
101
108
/// Note that the provided `keys_manager`'s `NodeSigner` implementation must support phantom
102
109
/// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
@@ -110,7 +117,7 @@ where
110
117
pub fn create_phantom_invoice_with_description_hash < ES : Deref , NS : Deref , L : Deref > (
111
118
amt_msat : Option < u64 > , payment_hash : Option < PaymentHash > , invoice_expiry_delta_secs : u32 ,
112
119
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 > ,
120
+ node_signer : NS , logger : L , network : Currency , min_final_cltv_expiry_delta : Option < u16 > , duration_since_epoch : Duration ,
114
121
) -> Result < Invoice , SignOrCreationError < ( ) > >
115
122
where
116
123
ES :: Target : EntropySource ,
@@ -120,22 +127,20 @@ where
120
127
_create_phantom_invoice :: < ES , NS , L > (
121
128
amt_msat, payment_hash, InvoiceDescription :: Hash ( & description_hash) ,
122
129
invoice_expiry_delta_secs, phantom_route_hints, entropy_source, node_signer, logger, network,
123
- min_final_cltv_expiry_delta,
130
+ min_final_cltv_expiry_delta, duration_since_epoch ,
124
131
)
125
132
}
126
133
127
- #[ cfg( feature = "std" ) ]
128
134
fn _create_phantom_invoice < ES : Deref , NS : Deref , L : Deref > (
129
135
amt_msat : Option < u64 > , payment_hash : Option < PaymentHash > , description : InvoiceDescription ,
130
136
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 > ,
137
+ node_signer : NS , logger : L , network : Currency , min_final_cltv_expiry_delta : Option < u16 > , duration_since_epoch : Duration ,
132
138
) -> Result < Invoice , SignOrCreationError < ( ) > >
133
139
where
134
140
ES :: Target : EntropySource ,
135
141
NS :: Target : NodeSigner ,
136
142
L :: Target : Logger ,
137
143
{
138
- use std:: time:: { SystemTime , UNIX_EPOCH } ;
139
144
140
145
if phantom_route_hints. len ( ) == 0 {
141
146
return Err ( SignOrCreationError :: CreationError (
@@ -162,9 +167,7 @@ where
162
167
amt_msat,
163
168
payment_hash,
164
169
invoice_expiry_delta_secs,
165
- SystemTime :: now ( )
166
- . duration_since ( UNIX_EPOCH )
167
- . expect ( "Time must be > 1970" )
170
+ duration_since_epoch
168
171
. as_secs ( ) ,
169
172
min_final_cltv_expiry_delta,
170
173
)
@@ -176,9 +179,7 @@ where
176
179
amt_msat,
177
180
invoice_expiry_delta_secs,
178
181
& entropy_source,
179
- SystemTime :: now ( )
180
- . duration_since ( UNIX_EPOCH )
181
- . expect ( "Time must be > 1970" )
182
+ duration_since_epoch
182
183
. as_secs ( ) ,
183
184
min_final_cltv_expiry_delta,
184
185
)
@@ -189,7 +190,7 @@ where
189
190
phantom_route_hints. len( ) , log_bytes!( payment_hash. 0 ) ) ;
190
191
191
192
let mut invoice = invoice
192
- . current_timestamp ( )
193
+ . timestamp_no_std ( duration_since_epoch )
193
194
. payment_hash ( Hash :: from_slice ( & payment_hash. 0 ) . unwrap ( ) )
194
195
. payment_secret ( payment_secret)
195
196
. min_final_cltv_expiry_delta (
@@ -1072,7 +1073,7 @@ mod test {
1072
1073
crate :: utils:: create_phantom_invoice :: < & test_utils:: TestKeysInterface , & test_utils:: TestKeysInterface , & test_utils:: TestLogger > (
1073
1074
Some ( payment_amt) , payment_hash, "test" . to_string ( ) , non_default_invoice_expiry_secs,
1074
1075
route_hints, & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . logger ,
1075
- Currency :: BitcoinTestnet , None ,
1076
+ Currency :: BitcoinTestnet , None , Duration :: from_secs ( 1234567 )
1076
1077
) . unwrap ( ) ;
1077
1078
let ( payment_hash, payment_secret) = ( PaymentHash ( invoice. payment_hash ( ) . into_inner ( ) ) , * invoice. payment_secret ( ) ) ;
1078
1079
let payment_preimage = if user_generated_pmt_hash {
@@ -1182,7 +1183,7 @@ mod test {
1182
1183
let invoice = crate :: utils:: create_phantom_invoice :: < & test_utils:: TestKeysInterface ,
1183
1184
& test_utils:: TestKeysInterface , & test_utils:: TestLogger > ( Some ( payment_amt) , Some ( payment_hash) ,
1184
1185
"test" . to_string ( ) , 3600 , route_hints, & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . keys_manager ,
1185
- & nodes[ 1 ] . logger , Currency :: BitcoinTestnet , None ) . unwrap ( ) ;
1186
+ & nodes[ 1 ] . logger , Currency :: BitcoinTestnet , None , Duration :: from_secs ( 1234567 ) ) . unwrap ( ) ;
1186
1187
1187
1188
let chan_0_1 = & nodes[ 1 ] . node . list_usable_channels ( ) [ 0 ] ;
1188
1189
assert_eq ! ( invoice. route_hints( ) [ 0 ] . 0 [ 0 ] . htlc_minimum_msat, chan_0_1. inbound_htlc_minimum_msat) ;
@@ -1214,7 +1215,7 @@ mod test {
1214
1215
> (
1215
1216
Some ( payment_amt) , None , non_default_invoice_expiry_secs, description_hash,
1216
1217
route_hints, & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . keys_manager , & nodes[ 1 ] . logger ,
1217
- Currency :: BitcoinTestnet , None ,
1218
+ Currency :: BitcoinTestnet , None , Duration :: from_secs ( 1234567 ) ,
1218
1219
)
1219
1220
. unwrap ( ) ;
1220
1221
assert_eq ! ( invoice. amount_pico_btc( ) , Some ( 200_000 ) ) ;
@@ -1240,10 +1241,11 @@ mod test {
1240
1241
let payment_hash = Some ( PaymentHash ( Sha256 :: hash ( & user_payment_preimage. 0 [ ..] ) . into_inner ( ) ) ) ;
1241
1242
let non_default_invoice_expiry_secs = 4200 ;
1242
1243
let min_final_cltv_expiry_delta = Some ( 100 ) ;
1244
+ let duration_since_epoch = Duration :: from_secs ( 1234567 ) ;
1243
1245
let invoice = crate :: utils:: create_phantom_invoice :: < & test_utils:: TestKeysInterface ,
1244
1246
& test_utils:: TestKeysInterface , & test_utils:: TestLogger > ( Some ( payment_amt) , payment_hash,
1245
1247
"" . 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 ( ) ;
1248
+ & nodes[ 1 ] . logger , Currency :: BitcoinTestnet , min_final_cltv_expiry_delta, duration_since_epoch ) . unwrap ( ) ;
1247
1249
assert_eq ! ( invoice. amount_pico_btc( ) , Some ( 200_000 ) ) ;
1248
1250
assert_eq ! ( invoice. min_final_cltv_expiry_delta( ) , ( min_final_cltv_expiry_delta. unwrap( ) + 3 ) as u64 ) ;
1249
1251
assert_eq ! ( invoice. expiry_time( ) , Duration :: from_secs( non_default_invoice_expiry_secs. into( ) ) ) ;
@@ -1557,7 +1559,7 @@ mod test {
1557
1559
let invoice = crate :: utils:: create_phantom_invoice :: < & test_utils:: TestKeysInterface ,
1558
1560
& test_utils:: TestKeysInterface , & test_utils:: TestLogger > ( invoice_amt, None , "test" . to_string ( ) ,
1559
1561
3600 , phantom_route_hints, & invoice_node. keys_manager , & invoice_node. keys_manager ,
1560
- & invoice_node. logger , Currency :: BitcoinTestnet , None ) . unwrap ( ) ;
1562
+ & invoice_node. logger , Currency :: BitcoinTestnet , None , Duration :: from_secs ( 1234567 ) ) . unwrap ( ) ;
1561
1563
1562
1564
let invoice_hints = invoice. private_routes ( ) ;
1563
1565
0 commit comments