@@ -108,6 +108,18 @@ impl Destination {
108
108
}
109
109
}
110
110
111
+ /// Errors that may occur when [sending an onion message].
112
+ ///
113
+ /// [sending an onion message]: OnionMessenger::send_onion_message
114
+ #[ derive( Debug , PartialEq ) ]
115
+ pub enum SendError {
116
+ /// Errored computing onion message packet keys.
117
+ Secp256k1 ( secp256k1:: Error ) ,
118
+ /// Because implementations such as Eclair will drop onion messages where the message packet
119
+ /// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
120
+ TooBigPacket ,
121
+ }
122
+
111
123
impl < Signer : Sign , K : Deref , L : Deref > OnionMessenger < Signer , K , L >
112
124
where K :: Target : KeysInterface < Signer = Signer > ,
113
125
L :: Target : Logger ,
@@ -126,7 +138,7 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
126
138
}
127
139
128
140
/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
129
- pub fn send_onion_message ( & self , intermediate_nodes : Vec < PublicKey > , destination : Destination ) -> Result < ( ) , secp256k1 :: Error > {
141
+ pub fn send_onion_message ( & self , intermediate_nodes : Vec < PublicKey > , destination : Destination ) -> Result < ( ) , SendError > {
130
142
let blinding_secret_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
131
143
let blinding_secret = SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
132
144
let ( introduction_node_id, blinding_point) = if intermediate_nodes. len ( ) != 0 {
@@ -139,11 +151,13 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
139
151
}
140
152
} ;
141
153
let ( control_tlvs_keys, onion_packet_keys) = construct_sending_keys (
142
- & self . secp_ctx , & intermediate_nodes, & destination, & blinding_secret) ?;
154
+ & self . secp_ctx , & intermediate_nodes, & destination, & blinding_secret)
155
+ . map_err ( |e| SendError :: Secp256k1 ( e) ) ?;
143
156
let payloads = build_payloads ( intermediate_nodes, destination, control_tlvs_keys) ;
144
157
145
158
let prng_seed = self . keys_manager . get_secure_random_bytes ( ) ;
146
- let onion_packet = construct_onion_message_packet ( payloads, onion_packet_keys, prng_seed) ;
159
+ let onion_packet = construct_onion_message_packet (
160
+ payloads, onion_packet_keys, prng_seed) . map_err ( |( ) | SendError :: TooBigPacket ) ?;
147
161
148
162
let mut pending_per_peer_msgs = self . pending_messages . lock ( ) . unwrap ( ) ;
149
163
let pending_msgs = pending_per_peer_msgs. entry ( introduction_node_id) . or_insert ( Vec :: new ( ) ) ;
@@ -359,19 +373,20 @@ fn build_payloads(intermediate_nodes: Vec<PublicKey>, destination: Destination,
359
373
payloads
360
374
}
361
375
362
- fn construct_onion_message_packet ( payloads : Vec < ( Payload , [ u8 ; 32 ] ) > , onion_keys : Vec < onion_utils:: OnionKeys > , prng_seed : [ u8 ; 32 ] ) -> Packet {
363
- let payloads_serialized_len = payloads. iter ( ) . map ( |p| p. serialized_length ( ) + 32 /* HMAC */ ) . sum ( ) ;
376
+ /// Errors if the serialized payload size exceeds onion_message::BIG_PACKET_HOP_DATA_LEN
377
+ fn construct_onion_message_packet ( payloads : Vec < ( Payload , [ u8 ; 32 ] ) > , onion_keys : Vec < onion_utils:: OnionKeys > , prng_seed : [ u8 ; 32 ] ) -> Result < Packet , ( ) > {
378
+ let payloads_serialized_len: usize = payloads. iter ( ) . map ( |p| p. serialized_length ( ) + 32 /* HMAC */ ) . sum ( ) ;
364
379
let hop_data_len = if payloads_serialized_len <= SMALL_PACKET_HOP_DATA_LEN {
365
380
SMALL_PACKET_HOP_DATA_LEN
366
381
} else if payloads_serialized_len <= BIG_PACKET_HOP_DATA_LEN {
367
382
BIG_PACKET_HOP_DATA_LEN
368
- } else { payloads_serialized_len } ;
383
+ } else { return Err ( ( ) ) } ;
369
384
370
385
let mut packet_data = vec ! [ 0 ; hop_data_len] ;
371
386
372
387
let mut chacha = ChaCha20 :: new ( & prng_seed, & [ 0 ; 8 ] ) ;
373
388
chacha. process_in_place ( & mut packet_data) ;
374
389
375
- onion_utils:: construct_onion_packet_with_init_noise :: < _ , _ > (
376
- payloads, onion_keys, packet_data, None )
390
+ Ok ( onion_utils:: construct_onion_packet_with_init_noise :: < _ , _ > (
391
+ payloads, onion_keys, packet_data, None ) )
377
392
}
0 commit comments