@@ -365,6 +365,11 @@ impl Destination {
365365pub enum SendError {
366366 /// Errored computing onion message packet keys.
367367 Secp256k1 ( secp256k1:: Error ) ,
368+ /// The provided [destination] was an invalid [blinded route], due to having 0 blinded hops.
369+ ///
370+ /// [destination]: Destination
371+ /// [blinded route]: BlindedRoute
372+ MissingBlindedHops ,
368373 /// Because implementations such as Eclair will drop onion messages where the message packet
369374 /// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
370375 TooBigPacket ,
@@ -457,6 +462,11 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
457462
458463 /// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
459464 pub fn send_onion_message ( & self , intermediate_nodes : Vec < PublicKey > , destination : Destination ) -> Result < ( ) , SendError > {
465+ if let Destination :: BlindedRoute ( BlindedRoute { ref blinded_hops, .. } ) = destination {
466+ if blinded_hops. len ( ) == 0 {
467+ return Err ( SendError :: MissingBlindedHops ) ;
468+ }
469+ }
460470 let blinding_secret_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
461471 let blinding_secret = SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
462472 let ( introduction_node_id, blinding_point) = if intermediate_nodes. len ( ) != 0 {
@@ -806,6 +816,7 @@ mod tests {
806816 use bitcoin:: network:: constants:: Network ;
807817 use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
808818
819+ use core:: mem;
809820 use sync:: Arc ;
810821
811822 struct MessengerNode {
@@ -911,4 +922,19 @@ mod tests {
911922 let err = nodes[ 0 ] . messenger . send_onion_message ( hops, Destination :: Node ( hop_node_id) ) . unwrap_err ( ) ;
912923 assert_eq ! ( err, SendError :: TooBigPacket ) ;
913924 }
925+
926+ #[ test]
927+ fn invalid_blinded_route_error ( ) {
928+ // Make sure we error as expected if a provided blinded route has 0 hops.
929+ let mut nodes = create_nodes ( 3 ) ;
930+ let ( node1, node2, node3) = ( nodes. remove ( 0 ) , nodes. remove ( 0 ) , nodes. remove ( 0 ) ) ;
931+
932+ let secp_ctx = Secp256k1 :: new ( ) ;
933+ let mut blinded_route = BlindedRoute :: new ( vec ! [ node2. get_node_pk( ) , node3. get_node_pk( ) ] , & node3. keys_manager , & secp_ctx) . unwrap ( ) ;
934+ let mut empty_hops = Vec :: new ( ) ;
935+ mem:: swap ( & mut empty_hops, & mut blinded_route. blinded_hops ) ;
936+
937+ let err = node1. messenger . send_onion_message ( vec ! [ ] , Destination :: BlindedRoute ( blinded_route) ) . unwrap_err ( ) ;
938+ assert_eq ! ( err, SendError :: MissingBlindedHops ) ;
939+ }
914940}
0 commit comments