@@ -403,6 +403,18 @@ impl Destination {
403
403
}
404
404
}
405
405
406
+ /// Errors that may occur when [sending an onion message].
407
+ ///
408
+ /// [sending an onion message]: OnionMessenger::send_onion_message
409
+ #[ derive( Debug ) ]
410
+ pub enum SendError {
411
+ /// Errored computing onion message packet keys.
412
+ Secp256k1 ( secp256k1:: Error ) ,
413
+ /// Because implementations such as Eclair will drop onion messages where the message packet
414
+ /// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
415
+ TooBigPacket ,
416
+ }
417
+
406
418
/// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
407
419
/// used to retrieve invoices and fulfill invoice requests from [offers].
408
420
///
@@ -438,7 +450,7 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
438
450
}
439
451
440
452
/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
441
- pub fn send_onion_message ( & self , intermediate_nodes : Vec < PublicKey > , destination : Destination ) -> Result < ( ) , secp256k1 :: Error > {
453
+ pub fn send_onion_message ( & self , intermediate_nodes : Vec < PublicKey > , destination : Destination ) -> Result < ( ) , SendError > {
442
454
let blinding_secret_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
443
455
let blinding_secret = SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
444
456
let ( introduction_node_id, blinding_point) = if intermediate_nodes. len ( ) != 0 {
@@ -451,9 +463,17 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
451
463
}
452
464
} ;
453
465
let ( encrypted_data_keys, onion_packet_keys) = construct_sending_keys (
454
- & self . secp_ctx , & intermediate_nodes, & destination, & blinding_secret) ?;
466
+ & self . secp_ctx , & intermediate_nodes, & destination, & blinding_secret)
467
+ . map_err ( |e| SendError :: Secp256k1 ( e) ) ?;
455
468
let payloads = build_payloads ( intermediate_nodes, destination, encrypted_data_keys) ;
456
469
470
+ // Check whether the onion message is too big to send.
471
+ let payloads_serialized_len = payloads. iter ( )
472
+ . fold ( 0 , |total, next_payload| total + next_payload. serialized_length ( ) + 32 /* HMAC */ ) ;
473
+ if payloads_serialized_len > BIG_PACKET_HOP_DATA_LEN {
474
+ return Err ( SendError :: TooBigPacket )
475
+ }
476
+
457
477
let prng_seed = self . keys_manager . get_secure_random_bytes ( ) ;
458
478
let onion_packet = onion_utils:: construct_onion_message_packet ( payloads, onion_packet_keys, prng_seed) ;
459
479
0 commit comments