33//! Primarily features [`peel_payment_onion`], which allows the decoding of an onion statelessly
44//! and can be used to predict whether we'd accept a payment.
55
6- use bitcoin:: hashes:: { Hash , HashEngine } ;
7- use bitcoin:: hashes:: hmac:: { Hmac , HmacEngine } ;
6+ use bitcoin:: hashes:: Hash ;
87use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
9- use bitcoin:: secp256k1:: { self , PublicKey , Scalar , Secp256k1 } ;
8+ use bitcoin:: secp256k1:: { self , PublicKey , Secp256k1 } ;
109
1110use crate :: blinded_path;
1211use crate :: blinded_path:: payment:: { PaymentConstraints , PaymentRelay } ;
@@ -285,7 +284,7 @@ where
285284 NS :: Target : NodeSigner ,
286285 L :: Target : Logger ,
287286{
288- let ( hop, shared_secret , next_packet_details_opt) =
287+ let ( hop, next_packet_details_opt) =
289288 decode_incoming_update_add_htlc_onion ( msg, node_signer, logger, secp_ctx
290289 ) . map_err ( |e| {
291290 let ( err_code, err_data) = match e {
@@ -296,7 +295,8 @@ where
296295 InboundHTLCErr { msg, err_code, err_data }
297296 } ) ?;
298297 Ok ( match hop {
299- onion_utils:: Hop :: Forward { next_hop_hmac, new_packet_bytes, .. } | onion_utils:: Hop :: BlindedForward { next_hop_hmac, new_packet_bytes, .. } => {
298+ onion_utils:: Hop :: Forward { shared_secret, next_hop_hmac, new_packet_bytes, .. } |
299+ onion_utils:: Hop :: BlindedForward { shared_secret, next_hop_hmac, new_packet_bytes, .. } => {
300300 let inbound_onion_payload = match hop {
301301 onion_utils:: Hop :: Forward { next_hop_data, .. } => msgs:: InboundOnionPayload :: Forward ( next_hop_data) ,
302302 onion_utils:: Hop :: BlindedForward { next_hop_data, .. } => msgs:: InboundOnionPayload :: BlindedForward ( next_hop_data) ,
@@ -328,19 +328,19 @@ where
328328 // TODO: If this is potentially a phantom payment we should decode the phantom payment
329329 // onion here and check it.
330330 create_fwd_pending_htlc_info (
331- msg, inbound_onion_payload, next_hop_hmac, new_packet_bytes, shared_secret,
331+ msg, inbound_onion_payload, next_hop_hmac, new_packet_bytes, shared_secret. secret_bytes ( ) ,
332332 Some ( next_packet_pubkey) ,
333333 ) ?
334334 } ,
335- onion_utils:: Hop :: Receive ( received_data ) => {
335+ onion_utils:: Hop :: Receive { hop_data , shared_secret } => {
336336 create_recv_pending_htlc_info (
337- msgs:: InboundOnionPayload :: Receive ( received_data ) , shared_secret, msg. payment_hash , msg. amount_msat , msg. cltv_expiry ,
337+ msgs:: InboundOnionPayload :: Receive ( hop_data ) , shared_secret. secret_bytes ( ) , msg. payment_hash , msg. amount_msat , msg. cltv_expiry ,
338338 None , allow_skimmed_fees, msg. skimmed_fee_msat , cur_height,
339339 ) ?
340340 } ,
341- onion_utils:: Hop :: BlindedReceive ( received_data ) => {
341+ onion_utils:: Hop :: BlindedReceive { hop_data , shared_secret } => {
342342 create_recv_pending_htlc_info (
343- msgs:: InboundOnionPayload :: BlindedReceive ( received_data ) , shared_secret, msg. payment_hash , msg. amount_msat , msg. cltv_expiry ,
343+ msgs:: InboundOnionPayload :: BlindedReceive ( hop_data ) , shared_secret. secret_bytes ( ) , msg. payment_hash , msg. amount_msat , msg. cltv_expiry ,
344344 None , allow_skimmed_fees, msg. skimmed_fee_msat , cur_height,
345345 ) ?
346346 }
@@ -356,7 +356,7 @@ pub(super) struct NextPacketDetails {
356356
357357pub ( super ) fn decode_incoming_update_add_htlc_onion < NS : Deref , L : Deref , T : secp256k1:: Verification > (
358358 msg : & msgs:: UpdateAddHTLC , node_signer : NS , logger : L , secp_ctx : & Secp256k1 < T > ,
359- ) -> Result < ( onion_utils:: Hop , [ u8 ; 32 ] , Option < NextPacketDetails > ) , HTLCFailureMsg >
359+ ) -> Result < ( onion_utils:: Hop , Option < NextPacketDetails > ) , HTLCFailureMsg >
360360where
361361 NS :: Target : NodeSigner ,
362362 L :: Target : Logger ,
@@ -384,16 +384,6 @@ where
384384 return_malformed_err ! ( "invalid ephemeral pubkey" , 0x8000 | 0x4000 | 6 ) ;
385385 }
386386
387- let blinded_node_id_tweak = msg. blinding_point . map ( |bp| {
388- let blinded_tlvs_ss = node_signer. ecdh ( Recipient :: Node , & bp, None ) . unwrap ( ) . secret_bytes ( ) ;
389- let mut hmac = HmacEngine :: < Sha256 > :: new ( b"blinded_node_id" ) ;
390- hmac. input ( blinded_tlvs_ss. as_ref ( ) ) ;
391- Scalar :: from_be_bytes ( Hmac :: from_engine ( hmac) . to_byte_array ( ) ) . unwrap ( )
392- } ) ;
393- let shared_secret = node_signer. ecdh (
394- Recipient :: Node , & msg. onion_routing_packet . public_key . unwrap ( ) , blinded_node_id_tweak. as_ref ( )
395- ) . unwrap ( ) . secret_bytes ( ) ;
396-
397387 if msg. onion_routing_packet . version != 0 {
398388 //TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
399389 //sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
@@ -403,58 +393,55 @@ where
403393 //node knows the HMAC matched, so they already know what is there...
404394 return_malformed_err ! ( "Unknown onion packet version" , 0x8000 | 0x4000 | 4 ) ;
405395 }
406- macro_rules! return_err {
407- ( $msg: expr, $err_code: expr, $data: expr) => {
408- {
409- if msg. blinding_point. is_some( ) {
410- return_malformed_err!( $msg, INVALID_ONION_BLINDING )
411- }
412396
413- log_info!( logger, "Failed to accept/forward incoming HTLC: {}" , $msg) ;
414- return Err ( HTLCFailureMsg :: Relay ( msgs:: UpdateFailHTLC {
415- channel_id: msg. channel_id,
416- htlc_id: msg. htlc_id,
417- reason: HTLCFailReason :: reason( $err_code, $data. to_vec( ) )
418- . get_encrypted_failure_packet( & shared_secret, & None ) ,
419- } ) ) ;
420- }
397+ let encode_relay_error = |message : & str , err_code : u16 , shared_secret : [ u8 ; 32 ] , data : & [ u8 ] | {
398+ if msg. blinding_point . is_some ( ) {
399+ return_malformed_err ! ( message, INVALID_ONION_BLINDING )
421400 }
422- }
401+
402+ log_info ! ( logger, "Failed to accept/forward incoming HTLC: {}" , message) ;
403+ return Err ( HTLCFailureMsg :: Relay ( msgs:: UpdateFailHTLC {
404+ channel_id : msg. channel_id ,
405+ htlc_id : msg. htlc_id ,
406+ reason : HTLCFailReason :: reason ( err_code, data. to_vec ( ) )
407+ . get_encrypted_failure_packet ( & shared_secret, & None ) ,
408+ } ) ) ;
409+ } ;
423410
424411 let next_hop = match onion_utils:: decode_next_payment_hop (
425- shared_secret , & msg. onion_routing_packet . hop_data [ ..] , msg. onion_routing_packet . hmac ,
412+ Recipient :: Node , & msg . onion_routing_packet . public_key . unwrap ( ) , & msg. onion_routing_packet . hop_data [ ..] , msg. onion_routing_packet . hmac ,
426413 msg. payment_hash , msg. blinding_point , node_signer
427414 ) {
428415 Ok ( res) => res,
429416 Err ( onion_utils:: OnionDecodeErr :: Malformed { err_msg, err_code } ) => {
430417 return_malformed_err ! ( err_msg, err_code) ;
431418 } ,
432- Err ( onion_utils:: OnionDecodeErr :: Relay { err_msg, err_code } ) => {
433- return_err ! ( err_msg, err_code, & [ 0 ; 0 ] ) ;
419+ Err ( onion_utils:: OnionDecodeErr :: Relay { err_msg, err_code, shared_secret } ) => {
420+ return encode_relay_error ( err_msg, err_code, shared_secret . secret_bytes ( ) , & [ 0 ; 0 ] ) ;
434421 } ,
435422 } ;
436423
437424 let next_packet_details = match next_hop {
438- Hop :: Forward { next_hop_data : msgs:: InboundOnionForwardPayload { short_channel_id, amt_to_forward, outgoing_cltv_value } , .. } => {
425+ Hop :: Forward { next_hop_data : msgs:: InboundOnionForwardPayload { short_channel_id, amt_to_forward, outgoing_cltv_value } , shared_secret , .. } => {
439426 let next_packet_pubkey = onion_utils:: next_hop_pubkey ( secp_ctx,
440- msg. onion_routing_packet . public_key . unwrap ( ) , & shared_secret) ;
427+ msg. onion_routing_packet . public_key . unwrap ( ) , & shared_secret. secret_bytes ( ) ) ;
441428 Some ( NextPacketDetails {
442429 next_packet_pubkey, outgoing_scid : short_channel_id,
443430 outgoing_amt_msat : amt_to_forward, outgoing_cltv_value
444431 } )
445432 }
446- Hop :: BlindedForward { next_hop_data : msgs:: InboundOnionBlindedForwardPayload { short_channel_id, ref payment_relay, ref payment_constraints, ref features, .. } , .. } => {
433+ Hop :: BlindedForward { next_hop_data : msgs:: InboundOnionBlindedForwardPayload { short_channel_id, ref payment_relay, ref payment_constraints, ref features, .. } , shared_secret , .. } => {
447434 let ( amt_to_forward, outgoing_cltv_value) = match check_blinded_forward (
448435 msg. amount_msat , msg. cltv_expiry , & payment_relay, & payment_constraints, & features
449436 ) {
450437 Ok ( ( amt, cltv) ) => ( amt, cltv) ,
451438 Err ( ( ) ) => {
452- return_err ! ( "Underflow calculating outbound amount or cltv value for blinded forward" ,
453- INVALID_ONION_BLINDING , & [ 0 ; 32 ] ) ;
439+ return encode_relay_error ( "Underflow calculating outbound amount or cltv value for blinded forward" ,
440+ INVALID_ONION_BLINDING , shared_secret . secret_bytes ( ) , & [ 0 ; 32 ] ) ;
454441 }
455442 } ;
456443 let next_packet_pubkey = onion_utils:: next_hop_pubkey ( & secp_ctx,
457- msg. onion_routing_packet . public_key . unwrap ( ) , & shared_secret) ;
444+ msg. onion_routing_packet . public_key . unwrap ( ) , & shared_secret. secret_bytes ( ) ) ;
458445 Some ( NextPacketDetails {
459446 next_packet_pubkey, outgoing_scid : short_channel_id, outgoing_amt_msat : amt_to_forward,
460447 outgoing_cltv_value
@@ -463,7 +450,7 @@ where
463450 _ => None
464451 } ;
465452
466- Ok ( ( next_hop, shared_secret , next_packet_details) )
453+ Ok ( ( next_hop, next_packet_details) )
467454}
468455
469456pub ( super ) fn check_incoming_htlc_cltv (
0 commit comments