Skip to content

Commit cf43d54

Browse files
Add SendError enum for onion messages and error on too-big packets
XXX test
1 parent c205bf9 commit cf43d54

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

lightning/src/onion_message.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,18 @@ impl Destination {
403403
}
404404
}
405405

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+
406418
/// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
407419
/// used to retrieve invoices and fulfill invoice requests from [offers].
408420
///
@@ -438,7 +450,7 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
438450
}
439451

440452
/// 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> {
442454
let blinding_secret_bytes = self.keys_manager.get_secure_random_bytes();
443455
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
444456
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>
451463
}
452464
};
453465
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))?;
455468
let payloads = build_payloads(intermediate_nodes, destination, encrypted_data_keys);
456469

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+
457477
let prng_seed = self.keys_manager.get_secure_random_bytes();
458478
let onion_packet = onion_utils::construct_onion_message_packet(payloads, onion_packet_keys, prng_seed);
459479

0 commit comments

Comments
 (0)