12
12
use crate :: Bolt11Invoice ;
13
13
use bitcoin:: hashes:: Hash ;
14
14
15
- use lightning:: ln:: types:: PaymentHash ;
16
15
use lightning:: ln:: channelmanager:: RecipientOnionFields ;
16
+ use lightning:: ln:: types:: PaymentHash ;
17
17
use lightning:: routing:: router:: { PaymentParameters , RouteParameters } ;
18
18
19
19
/// Builds the necessary parameters to pay or pre-flight probe the given zero-amount
@@ -28,8 +28,9 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};
28
28
///
29
29
/// [`ChannelManager::send_payment`]: lightning::ln::channelmanager::ChannelManager::send_payment
30
30
/// [`ChannelManager::send_preflight_probes`]: lightning::ln::channelmanager::ChannelManager::send_preflight_probes
31
- pub fn payment_parameters_from_zero_amount_invoice ( invoice : & Bolt11Invoice , amount_msat : u64 )
32
- -> Result < ( PaymentHash , RecipientOnionFields , RouteParameters ) , ( ) > {
31
+ pub fn payment_parameters_from_zero_amount_invoice (
32
+ invoice : & Bolt11Invoice , amount_msat : u64 ,
33
+ ) -> Result < ( PaymentHash , RecipientOnionFields , RouteParameters ) , ( ) > {
33
34
if invoice. amount_milli_satoshis ( ) . is_some ( ) {
34
35
Err ( ( ) )
35
36
} else {
@@ -48,27 +49,30 @@ pub fn payment_parameters_from_zero_amount_invoice(invoice: &Bolt11Invoice, amou
48
49
///
49
50
/// [`ChannelManager::send_payment`]: lightning::ln::channelmanager::ChannelManager::send_payment
50
51
/// [`ChannelManager::send_preflight_probes`]: lightning::ln::channelmanager::ChannelManager::send_preflight_probes
51
- pub fn payment_parameters_from_invoice ( invoice : & Bolt11Invoice )
52
- -> Result < ( PaymentHash , RecipientOnionFields , RouteParameters ) , ( ) > {
52
+ pub fn payment_parameters_from_invoice (
53
+ invoice : & Bolt11Invoice ,
54
+ ) -> Result < ( PaymentHash , RecipientOnionFields , RouteParameters ) , ( ) > {
53
55
if let Some ( amount_msat) = invoice. amount_milli_satoshis ( ) {
54
56
Ok ( params_from_invoice ( invoice, amount_msat) )
55
57
} else {
56
58
Err ( ( ) )
57
59
}
58
60
}
59
61
60
- fn params_from_invoice ( invoice : & Bolt11Invoice , amount_msat : u64 )
61
- -> ( PaymentHash , RecipientOnionFields , RouteParameters ) {
62
+ fn params_from_invoice (
63
+ invoice : & Bolt11Invoice , amount_msat : u64 ,
64
+ ) -> ( PaymentHash , RecipientOnionFields , RouteParameters ) {
62
65
let payment_hash = PaymentHash ( ( * invoice. payment_hash ( ) ) . to_byte_array ( ) ) ;
63
66
64
67
let mut recipient_onion = RecipientOnionFields :: secret_only ( * invoice. payment_secret ( ) ) ;
65
68
recipient_onion. payment_metadata = invoice. payment_metadata ( ) . map ( |v| v. clone ( ) ) ;
66
69
67
70
let mut payment_params = PaymentParameters :: from_node_id (
68
- invoice. recover_payee_pub_key ( ) ,
69
- invoice. min_final_cltv_expiry_delta ( ) as u32
70
- )
71
- . with_route_hints ( invoice. route_hints ( ) ) . unwrap ( ) ;
71
+ invoice. recover_payee_pub_key ( ) ,
72
+ invoice. min_final_cltv_expiry_delta ( ) as u32 ,
73
+ )
74
+ . with_route_hints ( invoice. route_hints ( ) )
75
+ . unwrap ( ) ;
72
76
if let Some ( expiry) = invoice. expires_at ( ) {
73
77
payment_params = payment_params. with_expiry_time ( expiry. as_secs ( ) ) ;
74
78
}
@@ -83,19 +87,18 @@ fn params_from_invoice(invoice: &Bolt11Invoice, amount_msat: u64)
83
87
#[ cfg( test) ]
84
88
mod tests {
85
89
use super :: * ;
86
- use crate :: { InvoiceBuilder , Currency } ;
90
+ use crate :: { Currency , InvoiceBuilder } ;
87
91
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
92
+ use core:: time:: Duration ;
88
93
use lightning:: ln:: types:: PaymentSecret ;
89
94
use lightning:: routing:: router:: Payee ;
90
- use secp256k1:: { SecretKey , PublicKey , Secp256k1 } ;
91
- use core:: time:: Duration ;
95
+ use secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
92
96
#[ cfg( feature = "std" ) ]
93
97
use std:: time:: SystemTime ;
94
98
95
99
fn duration_since_epoch ( ) -> Duration {
96
100
#[ cfg( feature = "std" ) ]
97
- let duration_since_epoch =
98
- SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
101
+ let duration_since_epoch = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
99
102
#[ cfg( not( feature = "std" ) ) ]
100
103
let duration_since_epoch = Duration :: from_secs ( 1234567 ) ;
101
104
duration_since_epoch
@@ -115,9 +118,7 @@ mod tests {
115
118
. duration_since_epoch ( duration_since_epoch ( ) )
116
119
. min_final_cltv_expiry_delta ( 144 )
117
120
. amount_milli_satoshis ( 128 )
118
- . build_signed ( |hash| {
119
- secp_ctx. sign_ecdsa_recoverable ( hash, & private_key)
120
- } )
121
+ . build_signed ( |hash| secp_ctx. sign_ecdsa_recoverable ( hash, & private_key) )
121
122
. unwrap ( ) ;
122
123
123
124
assert ! ( payment_parameters_from_zero_amount_invoice( & invoice, 42 ) . is_err( ) ) ;
@@ -147,14 +148,13 @@ mod tests {
147
148
. payment_secret ( PaymentSecret ( [ 0 ; 32 ] ) )
148
149
. duration_since_epoch ( duration_since_epoch ( ) )
149
150
. min_final_cltv_expiry_delta ( 144 )
150
- . build_signed ( |hash| {
151
- secp_ctx. sign_ecdsa_recoverable ( hash, & private_key)
152
- } )
153
- . unwrap ( ) ;
151
+ . build_signed ( |hash| secp_ctx. sign_ecdsa_recoverable ( hash, & private_key) )
152
+ . unwrap ( ) ;
154
153
155
154
assert ! ( payment_parameters_from_invoice( & invoice) . is_err( ) ) ;
156
155
157
- let ( hash, onion, params) = payment_parameters_from_zero_amount_invoice ( & invoice, 42 ) . unwrap ( ) ;
156
+ let ( hash, onion, params) =
157
+ payment_parameters_from_zero_amount_invoice ( & invoice, 42 ) . unwrap ( ) ;
158
158
assert_eq ! ( & hash. 0 [ ..] , & payment_hash[ ..] ) ;
159
159
assert_eq ! ( onion. payment_secret, Some ( PaymentSecret ( [ 0 ; 32 ] ) ) ) ;
160
160
assert_eq ! ( params. final_value_msat, 42 ) ;
@@ -170,9 +170,9 @@ mod tests {
170
170
#[ cfg( feature = "std" ) ]
171
171
fn payment_metadata_end_to_end ( ) {
172
172
use lightning:: events:: Event ;
173
- use lightning:: ln:: channelmanager:: { Retry , PaymentId } ;
174
- use lightning:: ln:: msgs:: ChannelMessageHandler ;
173
+ use lightning:: ln:: channelmanager:: { PaymentId , Retry } ;
175
174
use lightning:: ln:: functional_test_utils:: * ;
175
+ use lightning:: ln:: msgs:: ChannelMessageHandler ;
176
176
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
177
177
// the way out through the `PaymentClaimable` event.
178
178
let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
@@ -195,8 +195,10 @@ mod tests {
195
195
. amount_milli_satoshis ( 50_000 )
196
196
. payment_metadata ( payment_metadata. clone ( ) )
197
197
. build_signed ( |hash| {
198
- Secp256k1 :: new ( ) . sign_ecdsa_recoverable ( hash,
199
- & nodes[ 1 ] . keys_manager . backing . get_node_secret_key ( ) )
198
+ Secp256k1 :: new ( ) . sign_ecdsa_recoverable (
199
+ hash,
200
+ & nodes[ 1 ] . keys_manager . backing . get_node_secret_key ( ) ,
201
+ )
200
202
} )
201
203
. unwrap ( ) ;
202
204
@@ -216,7 +218,7 @@ mod tests {
216
218
Event :: PaymentClaimable { onion_fields, .. } => {
217
219
assert_eq ! ( Some ( payment_metadata) , onion_fields. unwrap( ) . payment_metadata) ;
218
220
} ,
219
- _ => panic ! ( "Unexpected event" )
221
+ _ => panic ! ( "Unexpected event" ) ,
220
222
}
221
223
}
222
224
}
0 commit comments