@@ -146,6 +146,11 @@ pub enum SendError {
146
146
/// Because implementations such as Eclair will drop onion messages where the message packet
147
147
/// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
148
148
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 ,
149
154
}
150
155
151
156
impl < Signer : Sign , K : Deref , L : Deref > OnionMessenger < Signer , K , L >
@@ -167,6 +172,11 @@ where K::Target: KeysInterface<Signer = Signer>,
167
172
168
173
/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
169
174
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
+ }
170
180
let blinding_secret_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
171
181
let blinding_secret = SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
172
182
let ( introduction_node_id, blinding_point) = if intermediate_nodes. len ( ) != 0 {
@@ -481,6 +491,7 @@ mod tests {
481
491
use bitcoin:: network:: constants:: Network ;
482
492
use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
483
493
494
+ use core:: mem;
484
495
use sync:: Arc ;
485
496
486
497
struct MessengerNode {
@@ -586,4 +597,19 @@ mod tests {
586
597
let err = nodes[ 0 ] . messenger . send_onion_message ( hops, Destination :: Node ( hop_node_id) ) . unwrap_err ( ) ;
587
598
assert_eq ! ( err, SendError :: TooBigPacket ) ;
588
599
}
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
+ }
589
615
}
0 commit comments