@@ -19,16 +19,18 @@ use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1919use super :: async_payments:: AsyncPaymentsMessage ;
2020use super :: async_payments:: AsyncPaymentsMessageHandler ;
2121use super :: dns_resolution:: { DNSResolverMessage , DNSResolverMessageHandler } ;
22- use super :: offers:: OffersMessageHandler ;
22+ use super :: offers:: { OffersMessage , OffersMessageHandler } ;
2323use super :: packet:: OnionMessageContents ;
2424use super :: packet:: ParsedOnionMessageContents ;
2525use super :: packet:: {
2626 ForwardControlTlvs , Packet , Payload , ReceiveControlTlvs , BIG_PACKET_HOP_DATA_LEN ,
2727 SMALL_PACKET_HOP_DATA_LEN ,
2828} ;
29+ #[ cfg( async_payments) ]
30+ use crate :: blinded_path:: message:: AsyncPaymentsContext ;
2931use crate :: blinded_path:: message:: {
30- BlindedMessagePath , ForwardTlvs , MessageContext , MessageForwardNode , NextMessageHop ,
31- ReceiveTlvs ,
32+ BlindedMessagePath , DNSResolverContext , ForwardTlvs , MessageContext , MessageForwardNode ,
33+ NextMessageHop , OffersContext , ReceiveTlvs ,
3234} ;
3335use crate :: blinded_path:: utils;
3436use crate :: blinded_path:: { IntroductionNode , NodeIdLookUp } ;
@@ -897,8 +899,15 @@ pub trait CustomOnionMessageHandler {
897899pub enum PeeledOnion < T : OnionMessageContents > {
898900 /// Forwarded onion, with the next node id and a new onion
899901 Forward ( NextMessageHop , OnionMessage ) ,
900- /// Received onion message, with decrypted contents, context, and reply path
901- Receive ( ParsedOnionMessageContents < T > , Option < MessageContext > , Option < BlindedMessagePath > ) ,
902+ /// Received offers onion message, with decrypted contents, context, and reply path
903+ Offers ( OffersMessage , Option < OffersContext > , Option < BlindedMessagePath > ) ,
904+ /// Received async payments onion message, with decrypted contents, context, and reply path
905+ #[ cfg( async_payments) ]
906+ AsyncPayments ( AsyncPaymentsMessage , AsyncPaymentsContext , Option < BlindedMessagePath > ) ,
907+ /// Received DNS resolver onion message, with decrypted contents, context, and reply path
908+ DNSResolver ( DNSResolverMessage , DNSResolverContext , Option < BlindedMessagePath > ) ,
909+ /// Received custom onion message, with decrypted contents, context, and reply path
910+ Custom ( T , Option < Vec < u8 > > , Option < BlindedMessagePath > ) ,
902911}
903912
904913/// Creates an [`OnionMessage`] with the given `contents` for sending to the destination of
@@ -1073,26 +1082,32 @@ where
10731082 reply_path,
10741083 } ,
10751084 None ,
1076- ) ) => match ( & message, & context) {
1077- ( _, None ) => Ok ( PeeledOnion :: Receive ( message, None , reply_path) ) ,
1078- ( ParsedOnionMessageContents :: Offers ( _) , Some ( MessageContext :: Offers ( _) ) ) => {
1079- Ok ( PeeledOnion :: Receive ( message, context, reply_path) )
1085+ ) ) => match ( message, context) {
1086+ ( ParsedOnionMessageContents :: Offers ( msg) , Some ( MessageContext :: Offers ( ctx) ) ) => {
1087+ Ok ( PeeledOnion :: Offers ( msg, Some ( ctx) , reply_path) )
1088+ } ,
1089+ ( ParsedOnionMessageContents :: Offers ( msg) , None ) => {
1090+ Ok ( PeeledOnion :: Offers ( msg, None , reply_path) )
10801091 } ,
10811092 #[ cfg( async_payments) ]
10821093 (
1083- ParsedOnionMessageContents :: AsyncPayments ( _ ) ,
1084- Some ( MessageContext :: AsyncPayments ( _ ) ) ,
1085- ) => Ok ( PeeledOnion :: Receive ( message , context , reply_path) ) ,
1086- ( ParsedOnionMessageContents :: Custom ( _ ) , Some ( MessageContext :: Custom ( _ ) ) ) => {
1087- Ok ( PeeledOnion :: Receive ( message , context , reply_path) )
1094+ ParsedOnionMessageContents :: AsyncPayments ( msg ) ,
1095+ Some ( MessageContext :: AsyncPayments ( ctx ) ) ,
1096+ ) => Ok ( PeeledOnion :: AsyncPayments ( msg , ctx , reply_path) ) ,
1097+ ( ParsedOnionMessageContents :: Custom ( msg ) , Some ( MessageContext :: Custom ( ctx ) ) ) => {
1098+ Ok ( PeeledOnion :: Custom ( msg , Some ( ctx ) , reply_path) )
10881099 } ,
1089- ( ParsedOnionMessageContents :: DNSResolver ( _ ) , Some ( MessageContext :: DNSResolver ( _ ) ) ) => {
1090- Ok ( PeeledOnion :: Receive ( message , context , reply_path) )
1100+ ( ParsedOnionMessageContents :: Custom ( msg ) , None ) => {
1101+ Ok ( PeeledOnion :: Custom ( msg , None , reply_path) )
10911102 } ,
1103+ (
1104+ ParsedOnionMessageContents :: DNSResolver ( msg) ,
1105+ Some ( MessageContext :: DNSResolver ( ctx) ) ,
1106+ ) => Ok ( PeeledOnion :: DNSResolver ( msg, ctx, reply_path) ) ,
10921107 _ => {
10931108 log_trace ! (
10941109 logger,
1095- "Received message was sent on a blinded path with the wrong context."
1110+ "Received message was sent on a blinded path with wrong or missing context."
10961111 ) ;
10971112 Err ( ( ) )
10981113 } ,
@@ -1894,98 +1909,68 @@ where
18941909{
18951910 fn handle_onion_message ( & self , peer_node_id : PublicKey , msg : & OnionMessage ) {
18961911 let logger = WithContext :: from ( & self . logger , Some ( peer_node_id) , None , None ) ;
1897- match self . peel_onion_message ( msg ) {
1898- Ok ( PeeledOnion :: Receive ( message, context , reply_path ) ) => {
1912+ macro_rules! log_receive {
1913+ ( $ message: expr , $with_reply_path : expr ) => {
18991914 log_trace!(
19001915 logger,
19011916 "Received an onion message with {} reply_path: {:?}" ,
1902- if reply_path . is_some ( ) { "a" } else { "no" } ,
1903- message
1917+ if $with_reply_path { "a" } else { "no" } ,
1918+ $ message
19041919 ) ;
1920+ } ;
1921+ }
19051922
1923+ match self . peel_onion_message ( msg) {
1924+ Ok ( PeeledOnion :: Offers ( message, context, reply_path) ) => {
1925+ log_receive ! ( message, reply_path. is_some( ) ) ;
1926+ let responder = reply_path. map ( Responder :: new) ;
1927+ let response_instructions =
1928+ self . offers_handler . handle_message ( message, context, responder) ;
1929+ if let Some ( ( msg, instructions) ) = response_instructions {
1930+ let _ = self . handle_onion_message_response ( msg, instructions) ;
1931+ }
1932+ } ,
1933+ #[ cfg( async_payments) ]
1934+ Ok ( PeeledOnion :: AsyncPayments ( message, context, reply_path) ) => {
1935+ log_receive ! ( message, reply_path. is_some( ) ) ;
19061936 let responder = reply_path. map ( Responder :: new) ;
19071937 match message {
1908- ParsedOnionMessageContents :: Offers ( msg) => {
1909- let context = match context {
1910- None => None ,
1911- Some ( MessageContext :: Offers ( context) ) => Some ( context) ,
1912- _ => {
1913- debug_assert ! ( false , "Checked in peel_onion_message" ) ;
1914- return ;
1915- } ,
1916- } ;
1917- let response_instructions =
1918- self . offers_handler . handle_message ( msg, context, responder) ;
1919- if let Some ( ( msg, instructions) ) = response_instructions {
1920- let _ = self . handle_onion_message_response ( msg, instructions) ;
1921- }
1922- } ,
1923- #[ cfg( async_payments) ]
1924- ParsedOnionMessageContents :: AsyncPayments (
1925- AsyncPaymentsMessage :: HeldHtlcAvailable ( msg) ,
1926- ) => {
1927- let context = match context {
1928- Some ( MessageContext :: AsyncPayments ( context) ) => context,
1929- Some ( _) => {
1930- debug_assert ! ( false , "Checked in peel_onion_message" ) ;
1931- return ;
1932- } ,
1933- None => return ,
1934- } ;
1938+ AsyncPaymentsMessage :: HeldHtlcAvailable ( msg) => {
19351939 let response_instructions = self
19361940 . async_payments_handler
19371941 . handle_held_htlc_available ( msg, context, responder) ;
19381942 if let Some ( ( msg, instructions) ) = response_instructions {
19391943 let _ = self . handle_onion_message_response ( msg, instructions) ;
19401944 }
19411945 } ,
1942- #[ cfg( async_payments) ]
1943- ParsedOnionMessageContents :: AsyncPayments (
1944- AsyncPaymentsMessage :: ReleaseHeldHtlc ( msg) ,
1945- ) => {
1946- let context = match context {
1947- Some ( MessageContext :: AsyncPayments ( context) ) => context,
1948- Some ( _) => {
1949- debug_assert ! ( false , "Checked in peel_onion_message" ) ;
1950- return ;
1951- } ,
1952- None => return ,
1953- } ;
1946+ AsyncPaymentsMessage :: ReleaseHeldHtlc ( msg) => {
19541947 self . async_payments_handler . handle_release_held_htlc ( msg, context) ;
19551948 } ,
1956- ParsedOnionMessageContents :: DNSResolver ( DNSResolverMessage :: DNSSECQuery (
1957- msg,
1958- ) ) => {
1949+ }
1950+ } ,
1951+ Ok ( PeeledOnion :: DNSResolver ( message, context, reply_path) ) => {
1952+ log_receive ! ( message, reply_path. is_some( ) ) ;
1953+ let responder = reply_path. map ( Responder :: new) ;
1954+ match message {
1955+ DNSResolverMessage :: DNSSECQuery ( msg) => {
19591956 let response_instructions =
19601957 self . dns_resolver_handler . handle_dnssec_query ( msg, responder) ;
19611958 if let Some ( ( msg, instructions) ) = response_instructions {
19621959 let _ = self . handle_onion_message_response ( msg, instructions) ;
19631960 }
19641961 } ,
1965- ParsedOnionMessageContents :: DNSResolver ( DNSResolverMessage :: DNSSECProof (
1966- msg,
1967- ) ) => {
1968- let context = match context {
1969- Some ( MessageContext :: DNSResolver ( context) ) => context,
1970- _ => return ,
1971- } ;
1962+ DNSResolverMessage :: DNSSECProof ( msg) => {
19721963 self . dns_resolver_handler . handle_dnssec_proof ( msg, context) ;
19731964 } ,
1974- ParsedOnionMessageContents :: Custom ( msg) => {
1975- let context = match context {
1976- None => None ,
1977- Some ( MessageContext :: Custom ( data) ) => Some ( data) ,
1978- _ => {
1979- debug_assert ! ( false , "Checked in peel_onion_message" ) ;
1980- return ;
1981- } ,
1982- } ;
1983- let response_instructions =
1984- self . custom_handler . handle_custom_message ( msg, context, responder) ;
1985- if let Some ( ( msg, instructions) ) = response_instructions {
1986- let _ = self . handle_onion_message_response ( msg, instructions) ;
1987- }
1988- } ,
1965+ }
1966+ } ,
1967+ Ok ( PeeledOnion :: Custom ( message, context, reply_path) ) => {
1968+ log_receive ! ( message, reply_path. is_some( ) ) ;
1969+ let responder = reply_path. map ( Responder :: new) ;
1970+ let response_instructions =
1971+ self . custom_handler . handle_custom_message ( message, context, responder) ;
1972+ if let Some ( ( msg, instructions) ) = response_instructions {
1973+ let _ = self . handle_onion_message_response ( msg, instructions) ;
19891974 }
19901975 } ,
19911976 Ok ( PeeledOnion :: Forward ( next_hop, onion_message) ) => {
0 commit comments