8
8
// licenses.
9
9
10
10
use crate :: blinded_path:: message:: { MessageContext , OffersContext } ;
11
- use crate :: events:: { Event , MessageSendEventsProvider , PaymentFailureReason } ;
12
- use crate :: ln:: channelmanager:: PaymentId ;
11
+ use crate :: events:: { Event , HTLCDestination , MessageSendEventsProvider , PaymentFailureReason } ;
12
+ use crate :: ln:: blinded_payment_tests:: { blinded_payment_path, get_blinded_route_parameters} ;
13
+ use crate :: ln:: channelmanager;
14
+ use crate :: ln:: channelmanager:: { PaymentId , RecipientOnionFields } ;
13
15
use crate :: ln:: functional_test_utils:: * ;
16
+ use crate :: ln:: inbound_payment;
17
+ use crate :: ln:: msgs:: ChannelMessageHandler ;
14
18
use crate :: ln:: msgs:: OnionMessageHandler ;
15
19
use crate :: ln:: offers_tests;
20
+ use crate :: ln:: onion_utils:: INVALID_ONION_BLINDING ;
16
21
use crate :: ln:: outbound_payment:: Retry ;
17
22
use crate :: offers:: nonce:: Nonce ;
18
23
use crate :: onion_message:: async_payments:: {
@@ -22,14 +27,251 @@ use crate::onion_message::messenger::{Destination, MessageRouter, MessageSendIns
22
27
use crate :: onion_message:: offers:: OffersMessage ;
23
28
use crate :: onion_message:: packet:: ParsedOnionMessageContents ;
24
29
use crate :: prelude:: * ;
30
+ use crate :: routing:: router:: { PaymentParameters , RouteParameters } ;
31
+ use crate :: sign:: NodeSigner ;
25
32
use crate :: types:: features:: Bolt12InvoiceFeatures ;
33
+ use crate :: types:: payment:: { PaymentPreimage , PaymentSecret } ;
34
+ use crate :: util:: config:: UserConfig ;
26
35
use bitcoin:: secp256k1:: Secp256k1 ;
27
36
28
37
use core:: convert:: Infallible ;
29
38
use core:: time:: Duration ;
30
39
31
40
#[ test]
32
- #[ cfg( async_payments) ]
41
+ fn blinded_keysend ( ) {
42
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
43
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
44
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
45
+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
46
+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1_000_000 , 0 ) ;
47
+ let chan_upd_1_2 =
48
+ create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 1_000_000 , 0 ) . 0 . contents ;
49
+
50
+ let inbound_payment_key = nodes[ 2 ] . keys_manager . get_inbound_payment_key ( ) ;
51
+ let payment_secret = inbound_payment:: create_for_spontaneous_payment (
52
+ & inbound_payment_key,
53
+ None ,
54
+ u32:: MAX ,
55
+ nodes[ 2 ] . node . duration_since_epoch ( ) . as_secs ( ) ,
56
+ None ,
57
+ )
58
+ . unwrap ( ) ;
59
+
60
+ let amt_msat = 5000 ;
61
+ let keysend_preimage = PaymentPreimage ( [ 42 ; 32 ] ) ;
62
+ let route_params = get_blinded_route_parameters (
63
+ amt_msat,
64
+ payment_secret,
65
+ 1 ,
66
+ 1_0000_0000 ,
67
+ nodes. iter ( ) . skip ( 1 ) . map ( |n| n. node . get_our_node_id ( ) ) . collect ( ) ,
68
+ & [ & chan_upd_1_2] ,
69
+ & chanmon_cfgs[ 2 ] . keys_manager ,
70
+ ) ;
71
+
72
+ let payment_hash = nodes[ 0 ]
73
+ . node
74
+ . send_spontaneous_payment (
75
+ Some ( keysend_preimage) ,
76
+ RecipientOnionFields :: spontaneous_empty ( ) ,
77
+ PaymentId ( keysend_preimage. 0 ) ,
78
+ route_params,
79
+ Retry :: Attempts ( 0 ) ,
80
+ )
81
+ . unwrap ( ) ;
82
+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
83
+
84
+ let expected_route: & [ & [ & Node ] ] = & [ & [ & nodes[ 1 ] , & nodes[ 2 ] ] ] ;
85
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
86
+ assert_eq ! ( events. len( ) , 1 ) ;
87
+
88
+ let ev = remove_first_msg_event_to_node ( & nodes[ 1 ] . node . get_our_node_id ( ) , & mut events) ;
89
+ pass_along_path (
90
+ & nodes[ 0 ] ,
91
+ expected_route[ 0 ] ,
92
+ amt_msat,
93
+ payment_hash,
94
+ Some ( payment_secret) ,
95
+ ev. clone ( ) ,
96
+ true ,
97
+ Some ( keysend_preimage) ,
98
+ ) ;
99
+ claim_payment_along_route ( ClaimAlongRouteArgs :: new (
100
+ & nodes[ 0 ] ,
101
+ expected_route,
102
+ keysend_preimage,
103
+ ) ) ;
104
+ }
105
+
106
+ #[ test]
107
+ fn blinded_mpp_keysend ( ) {
108
+ let chanmon_cfgs = create_chanmon_cfgs ( 4 ) ;
109
+ let node_cfgs = create_node_cfgs ( 4 , & chanmon_cfgs) ;
110
+ let node_chanmgrs = create_node_chanmgrs ( 4 , & node_cfgs, & [ None , None , None , None ] ) ;
111
+ let nodes = create_network ( 4 , & node_cfgs, & node_chanmgrs) ;
112
+
113
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
114
+ create_announced_chan_between_nodes ( & nodes, 0 , 2 ) ;
115
+ let chan_1_3 = create_announced_chan_between_nodes ( & nodes, 1 , 3 ) ;
116
+ let chan_2_3 = create_announced_chan_between_nodes ( & nodes, 2 , 3 ) ;
117
+
118
+ let inbound_payment_key = nodes[ 3 ] . keys_manager . get_inbound_payment_key ( ) ;
119
+ let payment_secret = inbound_payment:: create_for_spontaneous_payment (
120
+ & inbound_payment_key,
121
+ None ,
122
+ u32:: MAX ,
123
+ nodes[ 3 ] . node . duration_since_epoch ( ) . as_secs ( ) ,
124
+ None ,
125
+ )
126
+ . unwrap ( ) ;
127
+
128
+ let amt_msat = 15_000_000 ;
129
+ let keysend_preimage = PaymentPreimage ( [ 42 ; 32 ] ) ;
130
+ let route_params = {
131
+ let pay_params = PaymentParameters :: blinded ( vec ! [
132
+ blinded_payment_path(
133
+ payment_secret,
134
+ 1 ,
135
+ 1_0000_0000 ,
136
+ vec![ nodes[ 1 ] . node. get_our_node_id( ) , nodes[ 3 ] . node. get_our_node_id( ) ] ,
137
+ & [ & chan_1_3. 0 . contents] ,
138
+ & chanmon_cfgs[ 3 ] . keys_manager,
139
+ ) ,
140
+ blinded_payment_path(
141
+ payment_secret,
142
+ 1 ,
143
+ 1_0000_0000 ,
144
+ vec![ nodes[ 2 ] . node. get_our_node_id( ) , nodes[ 3 ] . node. get_our_node_id( ) ] ,
145
+ & [ & chan_2_3. 0 . contents] ,
146
+ & chanmon_cfgs[ 3 ] . keys_manager,
147
+ ) ,
148
+ ] )
149
+ . with_bolt12_features ( channelmanager:: provided_bolt12_invoice_features (
150
+ & UserConfig :: default ( ) ,
151
+ ) )
152
+ . unwrap ( ) ;
153
+ RouteParameters :: from_payment_params_and_value ( pay_params, amt_msat)
154
+ } ;
155
+
156
+ let payment_hash = nodes[ 0 ]
157
+ . node
158
+ . send_spontaneous_payment (
159
+ Some ( keysend_preimage) ,
160
+ RecipientOnionFields :: spontaneous_empty ( ) ,
161
+ PaymentId ( keysend_preimage. 0 ) ,
162
+ route_params,
163
+ Retry :: Attempts ( 0 ) ,
164
+ )
165
+ . unwrap ( ) ;
166
+ check_added_monitors ! ( nodes[ 0 ] , 2 ) ;
167
+
168
+ let expected_route: & [ & [ & Node ] ] = & [ & [ & nodes[ 1 ] , & nodes[ 3 ] ] , & [ & nodes[ 2 ] , & nodes[ 3 ] ] ] ;
169
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
170
+ assert_eq ! ( events. len( ) , 2 ) ;
171
+
172
+ let ev = remove_first_msg_event_to_node ( & nodes[ 1 ] . node . get_our_node_id ( ) , & mut events) ;
173
+ pass_along_path (
174
+ & nodes[ 0 ] ,
175
+ expected_route[ 0 ] ,
176
+ amt_msat,
177
+ payment_hash. clone ( ) ,
178
+ Some ( payment_secret) ,
179
+ ev. clone ( ) ,
180
+ false ,
181
+ Some ( keysend_preimage) ,
182
+ ) ;
183
+
184
+ let ev = remove_first_msg_event_to_node ( & nodes[ 2 ] . node . get_our_node_id ( ) , & mut events) ;
185
+ pass_along_path (
186
+ & nodes[ 0 ] ,
187
+ expected_route[ 1 ] ,
188
+ amt_msat,
189
+ payment_hash. clone ( ) ,
190
+ Some ( payment_secret) ,
191
+ ev. clone ( ) ,
192
+ true ,
193
+ Some ( keysend_preimage) ,
194
+ ) ;
195
+ claim_payment_along_route ( ClaimAlongRouteArgs :: new (
196
+ & nodes[ 0 ] ,
197
+ expected_route,
198
+ keysend_preimage,
199
+ ) ) ;
200
+ }
201
+
202
+ #[ test]
203
+ fn invalid_keysend_payment_secret ( ) {
204
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
205
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
206
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
207
+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
208
+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1_000_000 , 0 ) ;
209
+ let chan_upd_1_2 =
210
+ create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 1_000_000 , 0 ) . 0 . contents ;
211
+
212
+ let invalid_payment_secret = PaymentSecret ( [ 42 ; 32 ] ) ;
213
+ let amt_msat = 5000 ;
214
+ let keysend_preimage = PaymentPreimage ( [ 42 ; 32 ] ) ;
215
+ let route_params = get_blinded_route_parameters (
216
+ amt_msat,
217
+ invalid_payment_secret,
218
+ 1 ,
219
+ 1_0000_0000 ,
220
+ nodes. iter ( ) . skip ( 1 ) . map ( |n| n. node . get_our_node_id ( ) ) . collect ( ) ,
221
+ & [ & chan_upd_1_2] ,
222
+ & chanmon_cfgs[ 2 ] . keys_manager ,
223
+ ) ;
224
+
225
+ let payment_hash = nodes[ 0 ]
226
+ . node
227
+ . send_spontaneous_payment (
228
+ Some ( keysend_preimage) ,
229
+ RecipientOnionFields :: spontaneous_empty ( ) ,
230
+ PaymentId ( keysend_preimage. 0 ) ,
231
+ route_params,
232
+ Retry :: Attempts ( 0 ) ,
233
+ )
234
+ . unwrap ( ) ;
235
+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
236
+
237
+ let expected_route: & [ & [ & Node ] ] = & [ & [ & nodes[ 1 ] , & nodes[ 2 ] ] ] ;
238
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
239
+ assert_eq ! ( events. len( ) , 1 ) ;
240
+
241
+ let ev = remove_first_msg_event_to_node ( & nodes[ 1 ] . node . get_our_node_id ( ) , & mut events) ;
242
+ let args =
243
+ PassAlongPathArgs :: new ( & nodes[ 0 ] , & expected_route[ 0 ] , amt_msat, payment_hash, ev. clone ( ) )
244
+ . with_payment_secret ( invalid_payment_secret)
245
+ . with_payment_preimage ( keysend_preimage)
246
+ . expect_failure ( HTLCDestination :: FailedPayment { payment_hash } ) ;
247
+ do_pass_along_path ( args) ;
248
+
249
+ let updates_2_1 = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
250
+ assert_eq ! ( updates_2_1. update_fail_malformed_htlcs. len( ) , 1 ) ;
251
+ let update_malformed = & updates_2_1. update_fail_malformed_htlcs [ 0 ] ;
252
+ assert_eq ! ( update_malformed. sha256_of_onion, [ 0 ; 32 ] ) ;
253
+ assert_eq ! ( update_malformed. failure_code, INVALID_ONION_BLINDING ) ;
254
+ nodes[ 1 ]
255
+ . node
256
+ . handle_update_fail_malformed_htlc ( nodes[ 2 ] . node . get_our_node_id ( ) , update_malformed) ;
257
+ do_commitment_signed_dance ( & nodes[ 1 ] , & nodes[ 2 ] , & updates_2_1. commitment_signed , true , false ) ;
258
+
259
+ let updates_1_0 = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
260
+ assert_eq ! ( updates_1_0. update_fail_htlcs. len( ) , 1 ) ;
261
+ nodes[ 0 ] . node . handle_update_fail_htlc (
262
+ nodes[ 1 ] . node . get_our_node_id ( ) ,
263
+ & updates_1_0. update_fail_htlcs [ 0 ] ,
264
+ ) ;
265
+ do_commitment_signed_dance ( & nodes[ 0 ] , & nodes[ 1 ] , & updates_1_0. commitment_signed , false , false ) ;
266
+ expect_payment_failed_conditions (
267
+ & nodes[ 0 ] ,
268
+ payment_hash,
269
+ false ,
270
+ PaymentFailedConditions :: new ( ) . expected_htlc_error_data ( INVALID_ONION_BLINDING , & [ 0 ; 32 ] ) ,
271
+ ) ;
272
+ }
273
+
274
+ #[ test]
33
275
fn static_invoice_unknown_required_features ( ) {
34
276
// Test that we will fail to pay a static invoice with unsupported required features.
35
277
let secp_ctx = Secp256k1 :: new ( ) ;
0 commit comments