@@ -365,6 +365,11 @@ impl Destination {
365
365
pub enum SendError {
366
366
/// Errored computing onion message packet keys.
367
367
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 ,
368
373
/// Because implementations such as Eclair will drop onion messages where the message packet
369
374
/// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
370
375
TooBigPacket ,
@@ -457,6 +462,11 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
457
462
458
463
/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
459
464
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
+ }
460
470
let blinding_secret_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
461
471
let blinding_secret = SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
462
472
let ( introduction_node_id, blinding_point) = if intermediate_nodes. len ( ) != 0 {
@@ -806,6 +816,7 @@ mod tests {
806
816
use bitcoin:: network:: constants:: Network ;
807
817
use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
808
818
819
+ use core:: mem;
809
820
use sync:: Arc ;
810
821
811
822
struct MessengerNode {
@@ -911,4 +922,19 @@ mod tests {
911
922
let err = nodes[ 0 ] . messenger . send_onion_message ( hops, Destination :: Node ( hop_node_id) ) . unwrap_err ( ) ;
912
923
assert_eq ! ( err, SendError :: TooBigPacket ) ;
913
924
}
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
+ }
914
940
}
0 commit comments