9
9
10
10
use bitcoin:: secp256k1:: Secp256k1 ;
11
11
use crate :: blinded_path:: message:: { MessageContext , OffersContext } ;
12
- use crate :: events:: { Event , MessageSendEventsProvider , PaymentFailureReason } ;
13
- use crate :: ln:: channelmanager:: PaymentId ;
12
+ use crate :: events:: { Event , HTLCDestination , MessageSendEventsProvider , PaymentFailureReason } ;
13
+ use crate :: ln:: blinded_payment_tests:: { blinded_payment_path, get_blinded_route_parameters} ;
14
+ use crate :: ln:: channelmanager;
15
+ use crate :: ln:: channelmanager:: { PaymentId , RecipientOnionFields } ;
14
16
use crate :: ln:: functional_test_utils:: * ;
17
+ use crate :: ln:: inbound_payment;
18
+ use crate :: ln:: msgs:: ChannelMessageHandler ;
15
19
use crate :: ln:: msgs:: OnionMessageHandler ;
16
20
use crate :: ln:: offers_tests;
21
+ use crate :: ln:: onion_utils:: INVALID_ONION_BLINDING ;
17
22
use crate :: ln:: outbound_payment:: Retry ;
18
23
use crate :: offers:: nonce:: Nonce ;
19
24
use crate :: onion_message:: async_payments:: {
@@ -23,12 +28,156 @@ use crate::onion_message::messenger::{Destination, MessageRouter, MessageSendIns
23
28
use crate :: onion_message:: offers:: OffersMessage ;
24
29
use crate :: onion_message:: packet:: ParsedOnionMessageContents ;
25
30
use crate :: prelude:: * ;
31
+ use crate :: routing:: router:: { PaymentParameters , RouteParameters } ;
32
+ use crate :: sign:: NodeSigner ;
26
33
use crate :: types:: features:: Bolt12InvoiceFeatures ;
34
+ use crate :: types:: payment:: { PaymentPreimage , PaymentSecret } ;
35
+ use crate :: util:: config:: UserConfig ;
27
36
28
37
use core:: convert:: Infallible ;
29
38
30
39
#[ 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 = create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 1_000_000 , 0 ) . 0 . contents ;
47
+
48
+ let inbound_payment_key = nodes[ 2 ] . keys_manager . get_inbound_payment_key ( ) ;
49
+ let payment_secret = inbound_payment:: create_for_spontaneous_payment (
50
+ & inbound_payment_key, None , u32:: MAX , nodes[ 2 ] . node . duration_since_epoch ( ) . as_secs ( ) , None
51
+ ) . unwrap ( ) ;
52
+
53
+ let amt_msat = 5000 ;
54
+ let keysend_preimage = PaymentPreimage ( [ 42 ; 32 ] ) ;
55
+ let route_params = get_blinded_route_parameters ( amt_msat, payment_secret, 1 ,
56
+ 1_0000_0000 ,
57
+ nodes. iter ( ) . skip ( 1 ) . map ( |n| n. node . get_our_node_id ( ) ) . collect ( ) ,
58
+ & [ & chan_upd_1_2] , & chanmon_cfgs[ 2 ] . keys_manager ) ;
59
+
60
+ let payment_hash = nodes[ 0 ] . node . send_spontaneous_payment ( Some ( keysend_preimage) , RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( keysend_preimage. 0 ) , route_params, Retry :: Attempts ( 0 ) ) . unwrap ( ) ;
61
+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
62
+
63
+ let expected_route: & [ & [ & Node ] ] = & [ & [ & nodes[ 1 ] , & nodes[ 2 ] ] ] ;
64
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
65
+ assert_eq ! ( events. len( ) , 1 ) ;
66
+
67
+ let ev = remove_first_msg_event_to_node ( & nodes[ 1 ] . node . get_our_node_id ( ) , & mut events) ;
68
+ pass_along_path ( & nodes[ 0 ] , expected_route[ 0 ] , amt_msat, payment_hash, Some ( payment_secret) , ev. clone ( ) , true , Some ( keysend_preimage) ) ;
69
+ claim_payment_along_route (
70
+ ClaimAlongRouteArgs :: new ( & nodes[ 0 ] , expected_route, keysend_preimage)
71
+ ) ;
72
+ }
73
+
74
+ #[ test]
75
+ fn blinded_mpp_keysend ( ) {
76
+ let chanmon_cfgs = create_chanmon_cfgs ( 4 ) ;
77
+ let node_cfgs = create_node_cfgs ( 4 , & chanmon_cfgs) ;
78
+ let node_chanmgrs = create_node_chanmgrs ( 4 , & node_cfgs, & [ None , None , None , None ] ) ;
79
+ let nodes = create_network ( 4 , & node_cfgs, & node_chanmgrs) ;
80
+
81
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
82
+ create_announced_chan_between_nodes ( & nodes, 0 , 2 ) ;
83
+ let chan_1_3 = create_announced_chan_between_nodes ( & nodes, 1 , 3 ) ;
84
+ let chan_2_3 = create_announced_chan_between_nodes ( & nodes, 2 , 3 ) ;
85
+
86
+ let inbound_payment_key = nodes[ 3 ] . keys_manager . get_inbound_payment_key ( ) ;
87
+ let payment_secret = inbound_payment:: create_for_spontaneous_payment (
88
+ & inbound_payment_key, None , u32:: MAX , nodes[ 3 ] . node . duration_since_epoch ( ) . as_secs ( ) , None
89
+ ) . unwrap ( ) ;
90
+
91
+ let amt_msat = 15_000_000 ;
92
+ let keysend_preimage = PaymentPreimage ( [ 42 ; 32 ] ) ;
93
+ let route_params = {
94
+ let pay_params = PaymentParameters :: blinded (
95
+ vec ! [
96
+ blinded_payment_path( payment_secret, 1 , 1_0000_0000 ,
97
+ vec![ nodes[ 1 ] . node. get_our_node_id( ) , nodes[ 3 ] . node. get_our_node_id( ) ] , & [ & chan_1_3. 0 . contents] ,
98
+ & chanmon_cfgs[ 3 ] . keys_manager
99
+ ) ,
100
+ blinded_payment_path( payment_secret, 1 , 1_0000_0000 ,
101
+ vec![ nodes[ 2 ] . node. get_our_node_id( ) , nodes[ 3 ] . node. get_our_node_id( ) ] , & [ & chan_2_3. 0 . contents] ,
102
+ & chanmon_cfgs[ 3 ] . keys_manager
103
+ ) ,
104
+ ]
105
+ )
106
+ . with_bolt12_features ( channelmanager:: provided_bolt12_invoice_features ( & UserConfig :: default ( ) ) )
107
+ . unwrap ( ) ;
108
+ RouteParameters :: from_payment_params_and_value ( pay_params, amt_msat)
109
+ } ;
110
+
111
+ let payment_hash = nodes[ 0 ] . node . send_spontaneous_payment ( Some ( keysend_preimage) , RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( keysend_preimage. 0 ) , route_params, Retry :: Attempts ( 0 ) ) . unwrap ( ) ;
112
+ check_added_monitors ! ( nodes[ 0 ] , 2 ) ;
113
+
114
+ let expected_route: & [ & [ & Node ] ] = & [ & [ & nodes[ 1 ] , & nodes[ 3 ] ] , & [ & nodes[ 2 ] , & nodes[ 3 ] ] ] ;
115
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
116
+ assert_eq ! ( events. len( ) , 2 ) ;
117
+
118
+ let ev = remove_first_msg_event_to_node ( & nodes[ 1 ] . node . get_our_node_id ( ) , & mut events) ;
119
+ pass_along_path ( & nodes[ 0 ] , expected_route[ 0 ] , amt_msat, payment_hash. clone ( ) ,
120
+ Some ( payment_secret) , ev. clone ( ) , false , Some ( keysend_preimage) ) ;
121
+
122
+ let ev = remove_first_msg_event_to_node ( & nodes[ 2 ] . node . get_our_node_id ( ) , & mut events) ;
123
+ pass_along_path ( & nodes[ 0 ] , expected_route[ 1 ] , amt_msat, payment_hash. clone ( ) ,
124
+ Some ( payment_secret) , ev. clone ( ) , true , Some ( keysend_preimage) ) ;
125
+ claim_payment_along_route (
126
+ ClaimAlongRouteArgs :: new ( & nodes[ 0 ] , expected_route, keysend_preimage)
127
+ ) ;
128
+ }
129
+
130
+ #[ test]
131
+ fn invalid_keysend_payment_secret ( ) {
132
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
133
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
134
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
135
+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
136
+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1_000_000 , 0 ) ;
137
+ let chan_upd_1_2 = create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 1_000_000 , 0 ) . 0 . contents ;
138
+
139
+ let invalid_payment_secret = PaymentSecret ( [ 42 ; 32 ] ) ;
140
+ let amt_msat = 5000 ;
141
+ let keysend_preimage = PaymentPreimage ( [ 42 ; 32 ] ) ;
142
+ let route_params = get_blinded_route_parameters (
143
+ amt_msat, invalid_payment_secret, 1 , 1_0000_0000 ,
144
+ nodes. iter ( ) . skip ( 1 ) . map ( |n| n. node . get_our_node_id ( ) ) . collect ( ) , & [ & chan_upd_1_2] ,
145
+ & chanmon_cfgs[ 2 ] . keys_manager
146
+ ) ;
147
+
148
+ let payment_hash = nodes[ 0 ] . node . send_spontaneous_payment ( Some ( keysend_preimage) , RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( keysend_preimage. 0 ) , route_params, Retry :: Attempts ( 0 ) ) . unwrap ( ) ;
149
+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
150
+
151
+ let expected_route: & [ & [ & Node ] ] = & [ & [ & nodes[ 1 ] , & nodes[ 2 ] ] ] ;
152
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
153
+ assert_eq ! ( events. len( ) , 1 ) ;
154
+
155
+ let ev = remove_first_msg_event_to_node ( & nodes[ 1 ] . node . get_our_node_id ( ) , & mut events) ;
156
+ let args = PassAlongPathArgs :: new (
157
+ & nodes[ 0 ] , & expected_route[ 0 ] , amt_msat, payment_hash, ev. clone ( )
158
+ )
159
+ . with_payment_secret ( invalid_payment_secret)
160
+ . with_payment_preimage ( keysend_preimage)
161
+ . expect_failure ( HTLCDestination :: FailedPayment { payment_hash } ) ;
162
+ do_pass_along_path ( args) ;
163
+
164
+ let updates_2_1 = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
165
+ assert_eq ! ( updates_2_1. update_fail_malformed_htlcs. len( ) , 1 ) ;
166
+ let update_malformed = & updates_2_1. update_fail_malformed_htlcs [ 0 ] ;
167
+ assert_eq ! ( update_malformed. sha256_of_onion, [ 0 ; 32 ] ) ;
168
+ assert_eq ! ( update_malformed. failure_code, INVALID_ONION_BLINDING ) ;
169
+ nodes[ 1 ] . node . handle_update_fail_malformed_htlc ( nodes[ 2 ] . node . get_our_node_id ( ) , update_malformed) ;
170
+ do_commitment_signed_dance ( & nodes[ 1 ] , & nodes[ 2 ] , & updates_2_1. commitment_signed , true , false ) ;
171
+
172
+ let updates_1_0 = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
173
+ assert_eq ! ( updates_1_0. update_fail_htlcs. len( ) , 1 ) ;
174
+ nodes[ 0 ] . node . handle_update_fail_htlc ( nodes[ 1 ] . node . get_our_node_id ( ) , & updates_1_0. update_fail_htlcs [ 0 ] ) ;
175
+ do_commitment_signed_dance ( & nodes[ 0 ] , & nodes[ 1 ] , & updates_1_0. commitment_signed , false , false ) ;
176
+ expect_payment_failed_conditions ( & nodes[ 0 ] , payment_hash, false ,
177
+ PaymentFailedConditions :: new ( ) . expected_htlc_error_data ( INVALID_ONION_BLINDING , & [ 0 ; 32 ] ) ) ;
178
+ }
179
+
180
+ #[ test]
32
181
fn static_invoice_unknown_required_features ( ) {
33
182
// Test that we will fail to pay a static invoice with unsupported required features.
34
183
let secp_ctx = Secp256k1 :: new ( ) ;
0 commit comments