@@ -19,8 +19,9 @@ use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, Mes
19
19
use crate :: ln:: channel:: { EXPIRE_PREV_CONFIG_TICKS , commit_tx_fee_msat, get_holder_selected_channel_reserve_satoshis, ANCHOR_OUTPUT_VALUE_SATOSHI } ;
20
20
use crate :: ln:: channelmanager:: { BREAKDOWN_TIMEOUT , MPP_TIMEOUT_TICKS , MIN_CLTV_EXPIRY_DELTA , PaymentId , PaymentSendFailure , RecentPaymentDetails , RecipientOnionFields , HTLCForwardInfo , PendingHTLCRouting , PendingAddHTLCInfo } ;
21
21
use crate :: ln:: features:: { Bolt11InvoiceFeatures , ChannelTypeFeatures } ;
22
- use crate :: ln:: { msgs, ChannelId , PaymentSecret , PaymentPreimage } ;
22
+ use crate :: ln:: { msgs, ChannelId , PaymentHash , PaymentSecret , PaymentPreimage } ;
23
23
use crate :: ln:: msgs:: ChannelMessageHandler ;
24
+ use crate :: ln:: onion_utils;
24
25
use crate :: ln:: outbound_payment:: { IDEMPOTENCY_TIMEOUT_TICKS , Retry } ;
25
26
use crate :: routing:: gossip:: { EffectiveCapacity , RoutingFees } ;
26
27
use crate :: routing:: router:: { get_route, Path , PaymentParameters , Route , Router , RouteHint , RouteHintHop , RouteHop , RouteParameters , find_route} ;
@@ -31,10 +32,14 @@ use crate::util::errors::APIError;
31
32
use crate :: util:: ser:: Writeable ;
32
33
use crate :: util:: string:: UntrustedString ;
33
34
35
+ use bitcoin:: hashes:: Hash ;
36
+ use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
34
37
use bitcoin:: network:: constants:: Network ;
38
+ use bitcoin:: secp256k1:: { Secp256k1 , SecretKey } ;
35
39
36
40
use crate :: prelude:: * ;
37
41
42
+ use crate :: ln:: functional_test_utils;
38
43
use crate :: ln:: functional_test_utils:: * ;
39
44
use crate :: routing:: gossip:: NodeId ;
40
45
#[ cfg( feature = "std" ) ]
@@ -4194,3 +4199,59 @@ fn test_htlc_forward_considers_anchor_outputs_value() {
4194
4199
check_closed_broadcast ( & nodes[ 2 ] , 1 , true ) ;
4195
4200
check_added_monitors ( & nodes[ 2 ] , 1 ) ;
4196
4201
}
4202
+
4203
+ #[ test]
4204
+ fn peel_payment_onion_custom_tlvs ( ) {
4205
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
4206
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
4207
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
4208
+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
4209
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
4210
+ let secp_ctx = Secp256k1 :: new ( ) ;
4211
+
4212
+ let amt_msat = 1000 ;
4213
+ let payment_params = PaymentParameters :: for_keysend ( nodes[ 1 ] . node . get_our_node_id ( ) ,
4214
+ TEST_FINAL_CLTV , false ) ;
4215
+ let route_params = RouteParameters :: from_payment_params_and_value ( payment_params, amt_msat) ;
4216
+ let route = functional_test_utils:: get_route ( & nodes[ 0 ] , & route_params) . unwrap ( ) ;
4217
+ let mut recipient_onion = RecipientOnionFields :: spontaneous_empty ( )
4218
+ . with_custom_tlvs ( vec ! [ ( 414141 , vec![ 42 ; 1200 ] ) ] ) . unwrap ( ) ;
4219
+ let prng_seed = chanmon_cfgs[ 0 ] . keys_manager . get_secure_random_bytes ( ) ;
4220
+ let session_priv = SecretKey :: from_slice ( & prng_seed[ ..] ) . expect ( "RNG is busted" ) ;
4221
+ let keysend_preimage = PaymentPreimage ( [ 42 ; 32 ] ) ;
4222
+ let payment_hash = PaymentHash ( Sha256 :: hash ( & keysend_preimage. 0 ) . to_byte_array ( ) ) ;
4223
+
4224
+ let ( onion_routing_packet, first_hop_msat, cltv_expiry) = onion_utils:: create_payment_onion (
4225
+ & secp_ctx, & route. paths [ 0 ] , & session_priv, amt_msat, recipient_onion. clone ( ) ,
4226
+ nodes[ 0 ] . best_block_info ( ) . 1 , & payment_hash, & Some ( keysend_preimage) , prng_seed
4227
+ ) . unwrap ( ) ;
4228
+
4229
+ let update_add = msgs:: UpdateAddHTLC {
4230
+ channel_id : ChannelId ( [ 0 ; 32 ] ) ,
4231
+ htlc_id : 42 ,
4232
+ amount_msat : first_hop_msat,
4233
+ payment_hash,
4234
+ cltv_expiry,
4235
+ skimmed_fee_msat : None ,
4236
+ onion_routing_packet,
4237
+ blinding_point : None ,
4238
+ } ;
4239
+ let peeled_onion = crate :: ln:: onion_payment:: peel_payment_onion (
4240
+ & update_add, & & chanmon_cfgs[ 1 ] . keys_manager , & & chanmon_cfgs[ 1 ] . logger , & secp_ctx,
4241
+ nodes[ 1 ] . best_block_info ( ) . 1 , true , false
4242
+ ) . unwrap ( ) ;
4243
+ assert_eq ! ( peeled_onion. incoming_amt_msat, Some ( amt_msat) ) ;
4244
+ match peeled_onion. routing {
4245
+ PendingHTLCRouting :: ReceiveKeysend {
4246
+ payment_data, payment_metadata, custom_tlvs, ..
4247
+ } => {
4248
+ #[ cfg( not( c_bindings) ) ]
4249
+ assert_eq ! ( & custom_tlvs, recipient_onion. custom_tlvs( ) ) ;
4250
+ #[ cfg( c_bindings) ]
4251
+ assert_eq ! ( custom_tlvs, recipient_onion. custom_tlvs( ) ) ;
4252
+ assert ! ( payment_metadata. is_none( ) ) ;
4253
+ assert ! ( payment_data. is_none( ) ) ;
4254
+ } ,
4255
+ _ => panic ! ( )
4256
+ }
4257
+ }
0 commit comments