3
3
//! Primarily features [`peel_payment_onion`], which allows the decoding of an onion statelessly
4
4
//! and can be used to predict whether we'd accept a payment.
5
5
6
- use bitcoin:: hashes:: { Hash , HashEngine } ;
7
- use bitcoin:: hashes:: hmac:: { Hmac , HmacEngine } ;
6
+ use bitcoin:: hashes:: Hash ;
8
7
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
9
- use bitcoin:: secp256k1:: { self , PublicKey , Scalar , Secp256k1 } ;
8
+ use bitcoin:: secp256k1:: { self , PublicKey , Secp256k1 } ;
10
9
11
10
use crate :: blinded_path;
12
11
use crate :: blinded_path:: payment:: { PaymentConstraints , PaymentRelay } ;
@@ -385,16 +384,6 @@ where
385
384
return_malformed_err ! ( "invalid ephemeral pubkey" , 0x8000 | 0x4000 | 6 ) ;
386
385
}
387
386
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
-
398
387
if msg. onion_routing_packet . version != 0 {
399
388
//TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
400
389
//sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
@@ -404,23 +393,20 @@ where
404
393
//node knows the HMAC matched, so they already know what is there...
405
394
return_malformed_err ! ( "Unknown onion packet version" , 0x8000 | 0x4000 | 4 ) ;
406
395
}
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
- }
413
396
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 )
422
400
}
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
+ } ;
424
410
425
411
let next_hop = match onion_utils:: decode_next_payment_hop (
426
412
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
430
416
Err ( onion_utils:: OnionDecodeErr :: Malformed { err_msg, err_code } ) => {
431
417
return_malformed_err ! ( err_msg, err_code) ;
432
418
} ,
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 ] ) ;
435
421
} ,
436
422
} ;
437
423
438
424
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 , .. } => {
440
426
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 ( ) ) ;
442
428
Some ( NextPacketDetails {
443
429
next_packet_pubkey, outgoing_scid : short_channel_id,
444
430
outgoing_amt_msat : amt_to_forward, outgoing_cltv_value
445
431
} )
446
432
}
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 , .. } => {
448
434
let ( amt_to_forward, outgoing_cltv_value) = match check_blinded_forward (
449
435
msg. amount_msat , msg. cltv_expiry , & payment_relay, & payment_constraints, & features
450
436
) {
451
437
Ok ( ( amt, cltv) ) => ( amt, cltv) ,
452
438
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 ] ) ;
455
441
}
456
442
} ;
457
443
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 ( ) ) ;
459
445
Some ( NextPacketDetails {
460
446
next_packet_pubkey, outgoing_scid : short_channel_id, outgoing_amt_msat : amt_to_forward,
461
447
outgoing_cltv_value
0 commit comments