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