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 } ;
@@ -385,16 +384,6 @@ where
385384 return_malformed_err ! ( "invalid ephemeral pubkey" , 0x8000 | 0x4000 | 6 ) ;
386385 }
387386
388- let blinded_node_id_tweak = msg. blinding_point . map ( |bp| {
389- let blinded_tlvs_ss = node_signer. ecdh ( Recipient :: Node , & bp, None ) . unwrap ( ) . secret_bytes ( ) ;
390- let mut hmac = HmacEngine :: < Sha256 > :: new ( b"blinded_node_id" ) ;
391- hmac. input ( blinded_tlvs_ss. as_ref ( ) ) ;
392- Scalar :: from_be_bytes ( Hmac :: from_engine ( hmac) . to_byte_array ( ) ) . unwrap ( )
393- } ) ;
394- let shared_secret = node_signer. ecdh (
395- Recipient :: Node , & msg. onion_routing_packet . public_key . unwrap ( ) , blinded_node_id_tweak. as_ref ( )
396- ) . unwrap ( ) . secret_bytes ( ) ;
397-
398387 if msg. onion_routing_packet . version != 0 {
399388 //TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
400389 //sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
@@ -404,23 +393,20 @@ where
404393 //node knows the HMAC matched, so they already know what is there...
405394 return_malformed_err ! ( "Unknown onion packet version" , 0x8000 | 0x4000 | 4 ) ;
406395 }
407- macro_rules! return_err {
408- ( $msg: expr, $err_code: expr, $data: expr) => {
409- {
410- if msg. blinding_point. is_some( ) {
411- return_malformed_err!( $msg, INVALID_ONION_BLINDING )
412- }
413396
414- log_info!( logger, "Failed to accept/forward incoming HTLC: {}" , $msg) ;
415- return Err ( HTLCFailureMsg :: Relay ( msgs:: UpdateFailHTLC {
416- channel_id: msg. channel_id,
417- htlc_id: msg. htlc_id,
418- reason: HTLCFailReason :: reason( $err_code, $data. to_vec( ) )
419- . get_encrypted_failure_packet( & shared_secret, & None ) ,
420- } ) ) ;
421- }
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 )
422400 }
423- }
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+ } ;
424410
425411 let next_hop = match onion_utils:: decode_next_payment_hop (
426412 Recipient :: Node , & msg. onion_routing_packet . public_key . unwrap ( ) , & msg. onion_routing_packet . hop_data [ ..] , msg. onion_routing_packet . hmac ,
@@ -430,32 +416,32 @@ where
430416 Err ( onion_utils:: OnionDecodeErr :: Malformed { err_msg, err_code } ) => {
431417 return_malformed_err ! ( err_msg, err_code) ;
432418 } ,
433- Err ( onion_utils:: OnionDecodeErr :: Relay { err_msg, err_code } ) => {
434- 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 ] ) ;
435421 } ,
436422 } ;
437423
438424 let next_packet_details = match next_hop {
439- 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 , .. } => {
440426 let next_packet_pubkey = onion_utils:: next_hop_pubkey ( secp_ctx,
441- msg. onion_routing_packet . public_key . unwrap ( ) , & shared_secret) ;
427+ msg. onion_routing_packet . public_key . unwrap ( ) , & shared_secret. secret_bytes ( ) ) ;
442428 Some ( NextPacketDetails {
443429 next_packet_pubkey, outgoing_scid : short_channel_id,
444430 outgoing_amt_msat : amt_to_forward, outgoing_cltv_value
445431 } )
446432 }
447- 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 , .. } => {
448434 let ( amt_to_forward, outgoing_cltv_value) = match check_blinded_forward (
449435 msg. amount_msat , msg. cltv_expiry , & payment_relay, & payment_constraints, & features
450436 ) {
451437 Ok ( ( amt, cltv) ) => ( amt, cltv) ,
452438 Err ( ( ) ) => {
453- return_err ! ( "Underflow calculating outbound amount or cltv value for blinded forward" ,
454- 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 ] ) ;
455441 }
456442 } ;
457443 let next_packet_pubkey = onion_utils:: next_hop_pubkey ( & secp_ctx,
458- msg. onion_routing_packet . public_key . unwrap ( ) , & shared_secret) ;
444+ msg. onion_routing_packet . public_key . unwrap ( ) , & shared_secret. secret_bytes ( ) ) ;
459445 Some ( NextPacketDetails {
460446 next_packet_pubkey, outgoing_scid : short_channel_id, outgoing_amt_msat : amt_to_forward,
461447 outgoing_cltv_value
0 commit comments