@@ -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 ,
@@ -127,7 +139,7 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
127
139
128
140
/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
129
141
/// See [`OnionMessenger`] for example usage.
130
- pub fn send_onion_message ( & self , intermediate_nodes : & [ PublicKey ] , destination : Destination ) -> Result < ( ) , secp256k1 :: Error > {
142
+ pub fn send_onion_message ( & self , intermediate_nodes : & [ PublicKey ] , destination : Destination ) -> Result < ( ) , SendError > {
131
143
let blinding_secret_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
132
144
let blinding_secret = SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
133
145
let ( introduction_node_id, blinding_point) = if intermediate_nodes. len ( ) != 0 {
@@ -140,10 +152,12 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
140
152
}
141
153
} ;
142
154
let ( packet_payloads, packet_keys) = packet_payloads_and_keys (
143
- & self . secp_ctx , intermediate_nodes, destination, & blinding_secret) ?;
155
+ & self . secp_ctx , intermediate_nodes, destination, & blinding_secret)
156
+ . map_err ( |e| SendError :: Secp256k1 ( e) ) ?;
144
157
145
158
let prng_seed = self . keys_manager . get_secure_random_bytes ( ) ;
146
- let onion_packet = construct_onion_message_packet ( packet_payloads, packet_keys, prng_seed) ;
159
+ let onion_packet = construct_onion_message_packet (
160
+ packet_payloads, 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 ( ) ) ;
@@ -343,7 +357,8 @@ fn packet_payloads_and_keys<T: secp256k1::Signing + secp256k1::Verification>(
343
357
Ok ( ( payloads, onion_packet_keys) )
344
358
}
345
359
346
- fn construct_onion_message_packet ( payloads : Vec < ( Payload , [ u8 ; 32 ] ) > , onion_keys : Vec < onion_utils:: OnionKeys > , prng_seed : [ u8 ; 32 ] ) -> Packet {
360
+ /// Errors if the serialized payload size exceeds onion_message::BIG_PACKET_HOP_DATA_LEN
361
+ fn construct_onion_message_packet ( payloads : Vec < ( Payload , [ u8 ; 32 ] ) > , onion_keys : Vec < onion_utils:: OnionKeys > , prng_seed : [ u8 ; 32 ] ) -> Result < Packet , ( ) > {
347
362
// Spec rationale:
348
363
// "`len` allows larger messages to be sent than the standard 1300 bytes allowed for an HTLC
349
364
// onion, but this should be used sparingly as it is reduces anonymity set, hence the
@@ -353,7 +368,8 @@ fn construct_onion_message_packet(payloads: Vec<(Payload, [u8; 32])>, onion_keys
353
368
SMALL_PACKET_HOP_DATA_LEN
354
369
} else if payloads_ser_len <= BIG_PACKET_HOP_DATA_LEN {
355
370
BIG_PACKET_HOP_DATA_LEN
356
- } else { payloads_ser_len } ;
371
+ } else { return Err ( ( ) ) } ;
357
372
358
- onion_utils:: construct_onion_message_packet :: < _ , _ > ( payloads, onion_keys, prng_seed, hop_data_len)
373
+ Ok ( onion_utils:: construct_onion_message_packet :: < _ , _ > (
374
+ payloads, onion_keys, prng_seed, hop_data_len) )
359
375
}
0 commit comments