@@ -41,6 +41,9 @@ use lightning_invoice::RawBolt11Invoice;
4141use types:: features:: Features ;
4242use crate :: blinded_path:: BlindedHop ;
4343
44+ #[ cfg( trampoline) ]
45+ use crate :: routing:: router:: Route ;
46+
4447pub fn blinded_payment_path (
4548 payment_secret : PaymentSecret , intro_node_min_htlc : u64 , intro_node_max_htlc : u64 ,
4649 node_ids : Vec < PublicKey > , channel_upds : & [ & msgs:: UnsignedChannelUpdate ] ,
@@ -127,7 +130,7 @@ pub fn fail_blinded_htlc_backwards(
127130 let unblinded_node_updates = get_htlc_update_msgs ! ( nodes[ i] , nodes[ i-1 ] . node. get_our_node_id( ) ) ;
128131 assert_eq ! ( unblinded_node_updates. update_fail_htlcs. len( ) , 1 ) ;
129132 nodes[ i-1 ] . node . handle_update_fail_htlc (
130- nodes[ i] . node . get_our_node_id ( ) , & unblinded_node_updates. update_fail_htlcs [ i- 1 ]
133+ nodes[ i] . node . get_our_node_id ( ) , & unblinded_node_updates. update_fail_htlcs [ 0 ]
131134 ) ;
132135 do_commitment_signed_dance ( & nodes[ i-1 ] , & nodes[ i] , & unblinded_node_updates. commitment_signed , false , false ) ;
133136 } ,
@@ -1958,3 +1961,203 @@ fn test_trampoline_inbound_payment_decoding() {
19581961 panic ! ( ) ;
19591962 } ;
19601963}
1964+
1965+ #[ cfg( trampoline) ]
1966+ fn do_test_trampoline_single_hop_receive ( success : bool ) {
1967+ const TOTAL_NODE_COUNT : usize = 3 ;
1968+ let secp_ctx = Secp256k1 :: new ( ) ;
1969+
1970+ let chanmon_cfgs = create_chanmon_cfgs ( TOTAL_NODE_COUNT ) ;
1971+ let node_cfgs = create_node_cfgs ( TOTAL_NODE_COUNT , & chanmon_cfgs) ;
1972+ let node_chanmgrs = create_node_chanmgrs ( TOTAL_NODE_COUNT , & node_cfgs, & vec ! [ None ; TOTAL_NODE_COUNT ] ) ;
1973+ let mut nodes = create_network ( TOTAL_NODE_COUNT , & node_cfgs, & node_chanmgrs) ;
1974+
1975+ let ( _, _, chan_id_alice_bob, _) = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1_000_000 , 0 ) ;
1976+ let ( _, _, chan_id_bob_carol, _) = create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 1_000_000 , 0 ) ;
1977+
1978+ for i in 0 ..TOTAL_NODE_COUNT { // connect all nodes' blocks
1979+ connect_blocks ( & nodes[ i] , ( TOTAL_NODE_COUNT as u32 ) * CHAN_CONFIRM_DEPTH + 1 - nodes[ i] . best_block_info ( ) . 1 ) ;
1980+ }
1981+
1982+ let alice_node_id = nodes[ 0 ] . node ( ) . get_our_node_id ( ) ;
1983+ let bob_node_id = nodes[ 1 ] . node ( ) . get_our_node_id ( ) ;
1984+ let carol_node_id = nodes[ 2 ] . node ( ) . get_our_node_id ( ) ;
1985+
1986+ let alice_bob_scid = nodes[ 0 ] . node ( ) . list_channels ( ) . iter ( ) . find ( |c| c. channel_id == chan_id_alice_bob) . unwrap ( ) . short_channel_id . unwrap ( ) ;
1987+ let bob_carol_scid = nodes[ 1 ] . node ( ) . list_channels ( ) . iter ( ) . find ( |c| c. channel_id == chan_id_bob_carol) . unwrap ( ) . short_channel_id . unwrap ( ) ;
1988+
1989+ let amt_msat = 1000 ;
1990+ let ( payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash ( & nodes[ 2 ] , Some ( amt_msat) , None ) ;
1991+
1992+ let carol_alice_trampoline_session_priv = secret_from_hex ( "a0f4b8d7b6c2d0ffdfaf718f76e9decaef4d9fb38a8c4addb95c4007cc3eee03" ) ;
1993+ let carol_blinding_point = PublicKey :: from_secret_key ( & secp_ctx, & carol_alice_trampoline_session_priv) ;
1994+ let carol_blinded_hops = if success {
1995+ let payee_tlvs = UnauthenticatedReceiveTlvs {
1996+ payment_secret,
1997+ payment_constraints : PaymentConstraints {
1998+ max_cltv_expiry : u32:: max_value ( ) ,
1999+ htlc_minimum_msat : amt_msat,
2000+ } ,
2001+ payment_context : PaymentContext :: Bolt12Refund ( Bolt12RefundContext { } ) ,
2002+ } ;
2003+
2004+ let nonce = Nonce ( [ 42u8 ; 16 ] ) ;
2005+ let expanded_key = nodes[ 2 ] . keys_manager . get_inbound_payment_key ( ) ;
2006+ let payee_tlvs = payee_tlvs. authenticate ( nonce, & expanded_key) ;
2007+ let carol_unblinded_tlvs = payee_tlvs. encode ( ) ;
2008+
2009+ let path = [ ( carol_node_id, WithoutLength ( & carol_unblinded_tlvs) ) ] ;
2010+ blinded_path:: utils:: construct_blinded_hops (
2011+ & secp_ctx, path. into_iter ( ) , & carol_alice_trampoline_session_priv
2012+ ) . unwrap ( )
2013+ } else {
2014+ let payee_tlvs = blinded_path:: payment:: TrampolineForwardTlvs {
2015+ next_trampoline : alice_node_id,
2016+ payment_constraints : PaymentConstraints {
2017+ max_cltv_expiry : u32:: max_value ( ) ,
2018+ htlc_minimum_msat : amt_msat,
2019+ } ,
2020+ features : BlindedHopFeatures :: empty ( ) ,
2021+ payment_relay : PaymentRelay {
2022+ cltv_expiry_delta : 0 ,
2023+ fee_proportional_millionths : 0 ,
2024+ fee_base_msat : 0 ,
2025+ } ,
2026+ next_blinding_override : None ,
2027+ } ;
2028+
2029+ let carol_unblinded_tlvs = payee_tlvs. encode ( ) ;
2030+ let path = [ ( carol_node_id, WithoutLength ( & carol_unblinded_tlvs) ) ] ;
2031+ blinded_path:: utils:: construct_blinded_hops (
2032+ & secp_ctx, path. into_iter ( ) , & carol_alice_trampoline_session_priv
2033+ ) . unwrap ( )
2034+ } ;
2035+
2036+ let route = Route {
2037+ paths : vec ! [ Path {
2038+ hops: vec![
2039+ // Bob
2040+ RouteHop {
2041+ pubkey: bob_node_id,
2042+ node_features: NodeFeatures :: empty( ) ,
2043+ short_channel_id: alice_bob_scid,
2044+ channel_features: ChannelFeatures :: empty( ) ,
2045+ fee_msat: 1000 ,
2046+ cltv_expiry_delta: 48 ,
2047+ maybe_announced_channel: false ,
2048+ } ,
2049+
2050+ // Carol
2051+ RouteHop {
2052+ pubkey: carol_node_id,
2053+ node_features: NodeFeatures :: empty( ) ,
2054+ short_channel_id: bob_carol_scid,
2055+ channel_features: ChannelFeatures :: empty( ) ,
2056+ fee_msat: 0 ,
2057+ cltv_expiry_delta: 48 ,
2058+ maybe_announced_channel: false ,
2059+ }
2060+ ] ,
2061+ blinded_tail: Some ( BlindedTail {
2062+ trampoline_hops: vec![
2063+ // Carol
2064+ TrampolineHop {
2065+ pubkey: carol_node_id,
2066+ node_features: Features :: empty( ) ,
2067+ fee_msat: amt_msat,
2068+ cltv_expiry_delta: 24 ,
2069+ } ,
2070+ ] ,
2071+ hops: carol_blinded_hops,
2072+ blinding_point: carol_blinding_point,
2073+ excess_final_cltv_expiry_delta: 39 ,
2074+ final_value_msat: amt_msat,
2075+ } )
2076+ } ] ,
2077+ route_params : None ,
2078+ } ;
2079+
2080+ nodes[ 0 ] . node . send_payment_with_route ( route. clone ( ) , payment_hash, RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( payment_hash. 0 ) ) . unwrap ( ) ;
2081+
2082+ check_added_monitors ! ( & nodes[ 0 ] , 1 ) ;
2083+
2084+ if success {
2085+ pass_along_route ( & nodes[ 0 ] , & [ & [ & nodes[ 1 ] , & nodes[ 2 ] ] ] , amt_msat, payment_hash, payment_secret) ;
2086+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , payment_preimage) ;
2087+ } else {
2088+ let replacement_onion = {
2089+ // create a substitute onion where the last Trampoline hop is a forward
2090+ let trampoline_secret_key = secret_from_hex ( "0134928f7b7ca6769080d70f16be84c812c741f545b49a34db47ce338a205799" ) ;
2091+ let prng_seed = secret_from_hex ( "fe02b4b9054302a3ddf4e1e9f7c411d644aebbd295218ab009dca94435f775a9" ) ;
2092+ let recipient_onion_fields = RecipientOnionFields :: spontaneous_empty ( ) ;
2093+
2094+ let mut blinded_tail = route. paths [ 0 ] . blinded_tail . clone ( ) . unwrap ( ) ;
2095+
2096+ // append some dummy blinded hop so the intro hop looks like a forward
2097+ blinded_tail. hops . push ( BlindedHop {
2098+ blinded_node_id : alice_node_id,
2099+ encrypted_payload : vec ! [ ] ,
2100+ } ) ;
2101+
2102+ let ( mut trampoline_payloads, outer_total_msat, outer_starting_htlc_offset) = onion_utils:: build_trampoline_onion_payloads ( & blinded_tail, amt_msat, & recipient_onion_fields, 32 , & None ) . unwrap ( ) ;
2103+
2104+ // pop the last dummy hop
2105+ trampoline_payloads. pop ( ) ;
2106+
2107+ let trampoline_onion_keys = onion_utils:: construct_trampoline_onion_keys ( & secp_ctx, & route. paths [ 0 ] . blinded_tail . as_ref ( ) . unwrap ( ) , & trampoline_secret_key) . unwrap ( ) ;
2108+ let trampoline_packet = onion_utils:: construct_trampoline_onion_packet (
2109+ trampoline_payloads,
2110+ trampoline_onion_keys,
2111+ prng_seed. secret_bytes ( ) ,
2112+ & payment_hash,
2113+ None ,
2114+ ) . unwrap ( ) ;
2115+
2116+ let outer_session_priv = secret_from_hex ( "e52c20461ed7acd46c4e7b591a37610519179482887bd73bf3b94617f8f03677" ) ;
2117+
2118+ let ( outer_payloads, _, _) = onion_utils:: build_onion_payloads ( & route. paths [ 0 ] , outer_total_msat, & recipient_onion_fields, outer_starting_htlc_offset, & None , None , Some ( trampoline_packet) ) . unwrap ( ) ;
2119+ let outer_onion_keys = onion_utils:: construct_onion_keys ( & secp_ctx, & route. clone ( ) . paths [ 0 ] , & outer_session_priv) . unwrap ( ) ;
2120+ let outer_packet = onion_utils:: construct_onion_packet (
2121+ outer_payloads,
2122+ outer_onion_keys,
2123+ prng_seed. secret_bytes ( ) ,
2124+ & payment_hash,
2125+ ) . unwrap ( ) ;
2126+
2127+ outer_packet
2128+ } ;
2129+
2130+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
2131+ assert_eq ! ( events. len( ) , 1 ) ;
2132+ let mut first_message_event = remove_first_msg_event_to_node ( & nodes[ 1 ] . node . get_our_node_id ( ) , & mut events) ;
2133+ let mut update_message = match first_message_event {
2134+ MessageSendEvent :: UpdateHTLCs { ref mut updates, .. } => {
2135+ assert_eq ! ( updates. update_add_htlcs. len( ) , 1 ) ;
2136+ updates. update_add_htlcs . get_mut ( 0 )
2137+ } ,
2138+ _ => panic ! ( )
2139+ } ;
2140+ update_message. map ( |msg| {
2141+ msg. onion_routing_packet = replacement_onion. clone ( ) ;
2142+ } ) ;
2143+
2144+ let route: & [ & Node ] = & [ & nodes[ 1 ] , & nodes[ 2 ] ] ;
2145+ let args = PassAlongPathArgs :: new ( & nodes[ 0 ] , route, amt_msat, payment_hash, first_message_event)
2146+ . with_payment_preimage ( payment_preimage)
2147+ . without_claimable_event ( )
2148+ . expect_failure ( HTLCDestination :: InvalidOnion ) ;
2149+ do_pass_along_path ( args) ;
2150+
2151+ fail_blinded_htlc_backwards ( payment_hash, 1 , & [ & nodes[ 0 ] , & nodes[ 1 ] , & nodes[ 2 ] ] , false ) ;
2152+ }
2153+ }
2154+
2155+ #[ test]
2156+ #[ cfg( trampoline) ]
2157+ fn test_trampoline_single_hop_receive ( ) {
2158+ // Simulate a payment of A (0) -> B (1) -> C(Trampoline (blinded intro)) (2)
2159+ do_test_trampoline_single_hop_receive ( true ) ;
2160+
2161+ // Simulate a payment failure of A (0) -> B (1) -> C(Trampoline (blinded forward)) (2)
2162+ do_test_trampoline_single_hop_receive ( false ) ;
2163+ }
0 commit comments