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 } ;
@@ -285,7 +284,7 @@ where
285
284
NS :: Target : NodeSigner ,
286
285
L :: Target : Logger ,
287
286
{
288
- let ( hop, shared_secret , next_packet_details_opt) =
287
+ let ( hop, next_packet_details_opt) =
289
288
decode_incoming_update_add_htlc_onion ( msg, node_signer, logger, secp_ctx
290
289
) . map_err ( |e| {
291
290
let ( err_code, err_data) = match e {
@@ -296,7 +295,8 @@ where
296
295
InboundHTLCErr { msg, err_code, err_data }
297
296
} ) ?;
298
297
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, .. } => {
300
300
let inbound_onion_payload = match hop {
301
301
onion_utils:: Hop :: Forward { next_hop_data, .. } => msgs:: InboundOnionPayload :: Forward ( next_hop_data) ,
302
302
onion_utils:: Hop :: BlindedForward { next_hop_data, .. } => msgs:: InboundOnionPayload :: BlindedForward ( next_hop_data) ,
@@ -328,19 +328,19 @@ where
328
328
// TODO: If this is potentially a phantom payment we should decode the phantom payment
329
329
// onion here and check it.
330
330
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 ( ) ,
332
332
Some ( next_packet_pubkey) ,
333
333
) ?
334
334
} ,
335
- onion_utils:: Hop :: Receive ( received_data ) => {
335
+ onion_utils:: Hop :: Receive { hop_data , shared_secret } => {
336
336
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 ,
338
338
None , allow_skimmed_fees, msg. skimmed_fee_msat , cur_height,
339
339
) ?
340
340
} ,
341
- onion_utils:: Hop :: BlindedReceive ( received_data ) => {
341
+ onion_utils:: Hop :: BlindedReceive { hop_data , shared_secret } => {
342
342
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 ,
344
344
None , allow_skimmed_fees, msg. skimmed_fee_msat , cur_height,
345
345
) ?
346
346
}
@@ -356,7 +356,7 @@ pub(super) struct NextPacketDetails {
356
356
357
357
pub ( super ) fn decode_incoming_update_add_htlc_onion < NS : Deref , L : Deref , T : secp256k1:: Verification > (
358
358
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 >
360
360
where
361
361
NS :: Target : NodeSigner ,
362
362
L :: Target : Logger ,
@@ -384,16 +384,6 @@ where
384
384
return_malformed_err ! ( "invalid ephemeral pubkey" , 0x8000 | 0x4000 | 6 ) ;
385
385
}
386
386
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
-
397
387
if msg. onion_routing_packet . version != 0 {
398
388
//TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
399
389
//sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
@@ -403,58 +393,55 @@ where
403
393
//node knows the HMAC matched, so they already know what is there...
404
394
return_malformed_err ! ( "Unknown onion packet version" , 0x8000 | 0x4000 | 4 ) ;
405
395
}
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
- }
412
396
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 )
421
400
}
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
+ } ;
423
410
424
411
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 ,
426
413
msg. payment_hash , msg. blinding_point , node_signer
427
414
) {
428
415
Ok ( res) => res,
429
416
Err ( onion_utils:: OnionDecodeErr :: Malformed { err_msg, err_code } ) => {
430
417
return_malformed_err ! ( err_msg, err_code) ;
431
418
} ,
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 ] ) ;
434
421
} ,
435
422
} ;
436
423
437
424
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 , .. } => {
439
426
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 ( ) ) ;
441
428
Some ( NextPacketDetails {
442
429
next_packet_pubkey, outgoing_scid : short_channel_id,
443
430
outgoing_amt_msat : amt_to_forward, outgoing_cltv_value
444
431
} )
445
432
}
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 , .. } => {
447
434
let ( amt_to_forward, outgoing_cltv_value) = match check_blinded_forward (
448
435
msg. amount_msat , msg. cltv_expiry , & payment_relay, & payment_constraints, & features
449
436
) {
450
437
Ok ( ( amt, cltv) ) => ( amt, cltv) ,
451
438
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 ] ) ;
454
441
}
455
442
} ;
456
443
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 ( ) ) ;
458
445
Some ( NextPacketDetails {
459
446
next_packet_pubkey, outgoing_scid : short_channel_id, outgoing_amt_msat : amt_to_forward,
460
447
outgoing_cltv_value
@@ -463,7 +450,7 @@ where
463
450
_ => None
464
451
} ;
465
452
466
- Ok ( ( next_hop, shared_secret , next_packet_details) )
453
+ Ok ( ( next_hop, next_packet_details) )
467
454
}
468
455
469
456
pub ( super ) fn check_incoming_htlc_cltv (
0 commit comments