lxmf: centralize opportunistic encryption packing

This commit is contained in:
DeFiDude committed 2026-05-15 16:35:06 -06:00
1 parent d0efe3a82d
commit 56fb3fea94
2 files changed
+89 -37

No files matched your search

+46
View File
@@ -340,6 +340,25 @@ impl LxMessage {
self.pack_propagated_encrypted_inner(encrypt_fn, Some(target_cost))
}
/// Pack the payload that Reticulum carries for Opportunistic delivery.
///
/// Python `LXMessage.__as_packet()` encrypts the complete LXMF packet after
/// the leading destination hash because the RNS packet header already
/// carries that destination. Keeping this helper in core prevents callers
/// from reimplementing the destination-prefix stripping differently.
pub fn pack_opportunistic_encrypted<F>(&self, encrypt_fn: F) -> Result<Vec<u8>, MessageError>
where
F: FnOnce(&[u8]) -> Result<Vec<u8>, MessageError>,
{
let packed = self.pack()?;
if packed.len() <= DESTINATION_LENGTH {
return Err(MessageError::PackFailed(
"packed LXMF message missing opportunistic payload tail".to_string(),
));
}
encrypt_fn(&packed[DESTINATION_LENGTH..])
}
fn pack_propagated_encrypted_inner<F>(
&mut self,
encrypt_fn: F,
@@ -2027,6 +2046,33 @@ mod tests {
assert_eq!(&lxmf_data[..16], &[0xAA; 16]);
}
#[test]
fn test_pack_opportunistic_encrypted_strips_destination_hash() {
let key = rns_crypto::ed25519::Ed25519PrivateKey::generate();
let mut msg = LxMessage::new(
[0xAA; 16],
[0xBB; 16],
"Opportunistic",
"Content body here",
DeliveryMethod::Opportunistic,
);
msg.sign(&key).unwrap();
let packed = msg.pack().unwrap();
let expected_tail = packed[DESTINATION_LENGTH..].to_vec();
let encrypted = msg
.pack_opportunistic_encrypted(|plaintext| {
assert_eq!(plaintext, expected_tail.as_slice());
let mut out = vec![0xEE];
out.extend_from_slice(plaintext);
Ok(out)
})
.unwrap();
assert_eq!(encrypted[0], 0xEE);
assert_eq!(&encrypted[1..], expected_tail.as_slice());
}
#[test]
fn test_propagation_transient_id() {
let lxmf_data = vec![0xAA; 100];
+43 -37
View File
@@ -1755,43 +1755,49 @@ impl LxmdRunner {
}
let msg_hash = message.hash;
let packed = match message.pack() {
Ok(p) => p,
Err(_) => continue,
};
// Opportunistic delivery strips the dest_hash prefix
// (Python LXMessage.py:629).
let encrypt_data = if is_opportunistic && packed.len() > 16 {
&packed[16..]
} else {
&packed
};
let payload = if let Some(ct) = self.encrypt_for_destination(&dest_hex, encrypt_data) {
tracing::info!(
dest = %dest_hex,
packed_len = packed.len(),
encrypted_len = ct.len(),
"outbound LXMF: encrypted"
);
ct
} else {
// Destination key unknown; re-queue for later.
tracing::warn!(
dest = %dest_hex,
attempts = message.delivery_attempts,
"destination key unknown, re-queuing"
);
requeue_after_path_request(
&mut self.router,
&self.transport_tx,
message,
dest_hash,
"opportunistic destination identity unknown",
true,
);
continue;
let mut missing_identity = false;
let payload = match message.pack_opportunistic_encrypted(|plaintext| {
self.encrypt_for_destination(&dest_hex, plaintext)
.ok_or_else(|| {
missing_identity = true;
lxmf_core::message::MessageError::PackFailed(format!(
"no identity key for destination {dest_hex}"
))
})
}) {
Ok(ct) => {
tracing::info!(
dest = %dest_hex,
encrypted_len = ct.len(),
"outbound LXMF: encrypted opportunistic payload"
);
ct
}
Err(err) if missing_identity => {
tracing::warn!(
dest = %dest_hex,
attempts = message.delivery_attempts,
error = %err,
"destination key unknown, re-queuing"
);
requeue_after_path_request(
&mut self.router,
&self.transport_tx,
message,
dest_hash,
"opportunistic destination identity unknown",
true,
);
continue;
}
Err(err) => {
tracing::warn!(
dest = %dest_hex,
error = %err,
"failed to pack opportunistic LXMF message"
);
continue;
}
};
let flags = rns_wire::flags::PacketFlags {