@@ -146,6 +146,11 @@ pub enum SendError {
146146 /// Because implementations such as Eclair will drop onion messages where the message packet
147147 /// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
148148 TooBigPacket ,
149+ /// The provided [destination] was an invalid [blinded route], due to having 0 blinded hops.
150+ ///
151+ /// [destination]: Destination
152+ /// [blinded route]: self::blinded_route::BlindedRoute
153+ MissingBlindedHops ,
149154}
150155
151156impl < Signer : Sign , K : Deref , L : Deref > OnionMessenger < Signer , K , L >
@@ -167,6 +172,11 @@ where K::Target: KeysInterface<Signer = Signer>,
167172
168173 /// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
169174 pub fn send_onion_message ( & self , intermediate_nodes : Vec < PublicKey > , destination : Destination ) -> Result < ( ) , SendError > {
175+ if let Destination :: BlindedRoute ( BlindedRoute { ref blinded_hops, .. } ) = destination {
176+ if blinded_hops. len ( ) == 0 {
177+ return Err ( SendError :: MissingBlindedHops ) ;
178+ }
179+ }
170180 let blinding_secret_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
171181 let blinding_secret = SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
172182 let ( introduction_node_id, blinding_point) = if intermediate_nodes. len ( ) != 0 {
@@ -481,6 +491,7 @@ mod tests {
481491 use bitcoin:: network:: constants:: Network ;
482492 use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
483493
494+ use core:: mem;
484495 use sync:: Arc ;
485496
486497 struct MessengerNode {
@@ -586,4 +597,19 @@ mod tests {
586597 let err = nodes[ 0 ] . messenger . send_onion_message ( hops, Destination :: Node ( hop_node_id) ) . unwrap_err ( ) ;
587598 assert_eq ! ( err, SendError :: TooBigPacket ) ;
588599 }
600+
601+ #[ test]
602+ fn invalid_blinded_route_error ( ) {
603+ // Make sure we error as expected if a provided blinded route has 0 hops.
604+ let mut nodes = create_nodes ( 3 ) ;
605+ let ( node1, node2, node3) = ( nodes. remove ( 0 ) , nodes. remove ( 0 ) , nodes. remove ( 0 ) ) ;
606+
607+ let secp_ctx = Secp256k1 :: new ( ) ;
608+ let mut blinded_route = BlindedRoute :: new ( vec ! [ node2. get_node_pk( ) , node3. get_node_pk( ) ] , & node3. keys_manager , & secp_ctx) . unwrap ( ) ;
609+ let mut empty_hops = Vec :: new ( ) ;
610+ mem:: swap ( & mut empty_hops, & mut blinded_route. blinded_hops ) ;
611+
612+ let err = node1. messenger . send_onion_message ( vec ! [ ] , Destination :: BlindedRoute ( blinded_route) ) . unwrap_err ( ) ;
613+ assert_eq ! ( err, SendError :: MissingBlindedHops ) ;
614+ }
589615}
0 commit comments