From 1f5a51b8b28cd772f74368c46bb04d64b9d0ca60 Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:48:47 -0700 Subject: [PATCH] lxmf: links can open with prepared request keys --- crates/lxmf-core/src/link_delivery.rs | 22 +++++++++++++++++++++- crates/lxmf-core/src/propagation_client.rs | 20 +++++++++++++++++++- crates/lxmf-core/src/propagation_sync.rs | 22 +++++++++++++++++++++- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/crates/lxmf-core/src/link_delivery.rs b/crates/lxmf-core/src/link_delivery.rs index b75ca60..93aab55 100644 --- a/crates/lxmf-core/src/link_delivery.rs +++ b/crates/lxmf-core/src/link_delivery.rs @@ -18,6 +18,7 @@ use std::time::{Duration, Instant}; use bytes::Bytes; use rns_crypto::ed25519::{Ed25519PrivateKey, Ed25519PublicKey}; use rns_link::constants::ESTABLISHMENT_TIMEOUT_PER_HOP; +use rns_link::handshake::EphemeralKeys; use rns_link::link::{CloseReason, Link, LinkAction, LinkState}; use rns_link::stamp::{LinkRequestStamper, LinkStampPolicy}; use rns_protocol::resource::{ @@ -45,6 +46,8 @@ use crate::propagation::hex_encode; /// Upstream LXMF keeps reusable Direct links open for ten minutes of data /// inactivity before tearing them down (`LXMRouter.LINK_MAX_INACTIVITY`). +/// Links opened with prepared keys signal the base MTU, as the stamper does. +pub(crate) const BASE_LINK_MTU: u32 = rns_wire::constants::MTU as u32; const LINK_MAX_INACTIVITY: Duration = Duration::from_secs(600); const BACKCHANNEL_SEND_COMMAND_TIMEOUT: Duration = Duration::from_secs(10); const BACKCHANNEL_DELIVERY_TIMEOUT: Duration = Duration::from_secs(360); @@ -940,6 +943,7 @@ pub struct LinkDeliveryManager { delivery_events: VecDeque, resource_counters: ResourceDispatchCounters, link_stamper: LinkRequestStamper, + prepared_link_keys: HashMap<[u8; 16], EphemeralKeys>, } impl LinkDeliveryManager { @@ -989,9 +993,16 @@ impl LinkDeliveryManager { delivery_events: VecDeque::new(), resource_counters: ResourceDispatchCounters::default(), link_stamper: LinkRequestStamper::without_recall(LinkStampPolicy::default()), + prepared_link_keys: HashMap::new(), } } + /// Open the next Link to `dest_hash` with `keys`, such as stamped keys + /// generated off the caller's task, instead of the stamper's. + pub fn provide_link_request_keys(&mut self, dest_hash: [u8; 16], keys: EphemeralKeys) { + self.prepared_link_keys.insert(dest_hash, keys); + } + /// Stamp new link requests with this stamper's policy and announced costs. /// The default stamps only when `always_stamp_lrs` is set. pub fn set_link_request_stamper(&mut self, stamper: LinkRequestStamper) { @@ -1684,7 +1695,16 @@ impl LinkDeliveryManager { } = options; let msg_hash = Some(message.message_id()); let packed_override_kind = packed_override.is_some(); - let Ok((mut link, request_data)) = self.link_stamper.new_initiator(dest_hash, hops) else { + let initiator = match self.prepared_link_keys.remove(&dest_hash) { + Some(keys) => Ok(Link::new_initiator_with_keys( + dest_hash, + hops, + keys, + BASE_LINK_MTU, + )), + None => self.link_stamper.new_initiator(dest_hash, hops), + }; + let Ok((mut link, request_data)) = initiator else { return Err(LinkDeliveryStartFailure { error: LinkDeliveryStartError::StampFailure, message: Box::new(message), diff --git a/crates/lxmf-core/src/propagation_client.rs b/crates/lxmf-core/src/propagation_client.rs index 8345bdd..83ed6bc 100644 --- a/crates/lxmf-core/src/propagation_client.rs +++ b/crates/lxmf-core/src/propagation_client.rs @@ -29,6 +29,7 @@ use std::time::{Duration, Instant}; use bytes::Bytes; use rns_crypto::ed25519::{Ed25519PrivateKey, Ed25519PublicKey}; +use rns_link::handshake::EphemeralKeys; use rns_link::link::{CloseReason, Link, LinkAction, LinkState}; use rns_link::stamp::{LinkRequestStamper, LinkStampPolicy}; use rns_protocol::resource::{ @@ -233,6 +234,7 @@ pub struct PropagationClient { identity_pub: Option<[u8; 64]>, identity_key: Option, link_stamper: LinkRequestStamper, + prepared_link_keys: Option<([u8; 16], EphemeralKeys)>, /// Phase 1 response: transient IDs the server has. available_messages: Vec>, /// Messages we already have locally. @@ -315,6 +317,7 @@ impl PropagationClient { identity_pub, identity_key, link_stamper: LinkRequestStamper::without_recall(LinkStampPolicy::default()), + prepared_link_keys: None, available_messages: Vec::new(), local_messages: HashSet::new(), local_message_expiry: HashMap::new(), @@ -363,6 +366,12 @@ impl PropagationClient { self.link_stamper = stamper; } + /// Open the next Link to `node` with `keys`, such as stamped keys + /// generated off the caller's task, instead of the stamper's. + pub fn provide_link_request_keys(&mut self, node: [u8; 16], keys: EphemeralKeys) { + self.prepared_link_keys = Some((node, keys)); + } + pub fn set_propagation_node(&mut self, dest_hash: [u8; 16]) { self.outbound_propagation_node = Some(dest_hash); } @@ -746,7 +755,16 @@ impl PropagationClient { } fn establish_link(&mut self, node_hash: [u8; 16]) -> bool { - let Ok((link, request_data)) = self.link_stamper.new_initiator(node_hash, 1) else { + let initiator = match self.prepared_link_keys.take() { + Some((node, keys)) if node == node_hash => Ok(Link::new_initiator_with_keys( + node_hash, + 1, + keys, + crate::link_delivery::BASE_LINK_MTU, + )), + _ => self.link_stamper.new_initiator(node_hash, 1), + }; + let Ok((link, request_data)) = initiator else { tracing::warn!("propagation client link request stamp generation failed"); return false; }; diff --git a/crates/lxmf-core/src/propagation_sync.rs b/crates/lxmf-core/src/propagation_sync.rs index 8b4a069..410ede4 100644 --- a/crates/lxmf-core/src/propagation_sync.rs +++ b/crates/lxmf-core/src/propagation_sync.rs @@ -19,6 +19,7 @@ use std::time::{Duration, Instant}; use bytes::Bytes; use rns_crypto::ed25519::{Ed25519PrivateKey, Ed25519PublicKey}; +use rns_link::handshake::EphemeralKeys; use rns_link::link::{CloseReason, Link, LinkAction, LinkState}; use rns_link::stamp::{LinkRequestStamper, LinkStampPolicy}; use rns_protocol::resource::{ @@ -186,6 +187,7 @@ pub struct PropagationSyncTask { identity_pub: Option<[u8; 64]>, identity_key: Option, link_stamper: LinkRequestStamper, + prepared_link_keys: Option<([u8; 16], EphemeralKeys)>, active_offer_generation: Option, generation_exhausted: bool, terminal_result: Option, @@ -245,6 +247,7 @@ impl PropagationSyncTask { identity_pub: None, identity_key: None, link_stamper: LinkRequestStamper::without_recall(LinkStampPolicy::default()), + prepared_link_keys: None, active_offer_generation: None, generation_exhausted: false, terminal_result: None, @@ -312,6 +315,7 @@ impl PropagationSyncTask { identity_pub: None, identity_key: None, link_stamper: LinkRequestStamper::without_recall(LinkStampPolicy::default()), + prepared_link_keys: None, active_offer_generation: None, generation_exhausted: false, terminal_result: None, @@ -373,6 +377,7 @@ impl PropagationSyncTask { identity_pub: None, identity_key: None, link_stamper: LinkRequestStamper::without_recall(LinkStampPolicy::default()), + prepared_link_keys: None, active_offer_generation: None, generation_exhausted: false, terminal_result: None, @@ -440,6 +445,12 @@ impl PropagationSyncTask { self.link_stamper = stamper; } + /// Open the next Link to `node` with `keys`, such as stamped keys + /// generated off the caller's task, instead of the stamper's. + pub fn provide_link_request_keys(&mut self, node: [u8; 16], keys: EphemeralKeys) { + self.prepared_link_keys = Some((node, keys)); + } + /// Configure the local identity used for Link identification. Production /// peer sync must set this before requesting a sync; compatibility callers /// without identity material retain the historical unidentified behavior. @@ -1603,7 +1614,16 @@ impl PropagationSyncTask { self.active_transfer_ids.clear(); self.active_offer_generation = None; self.generation_exhausted = false; - let Ok((link, request_data)) = self.link_stamper.new_initiator(node_hash, 1) else { + let initiator = match self.prepared_link_keys.take() { + Some((node, keys)) if node == node_hash => Ok(Link::new_initiator_with_keys( + node_hash, + 1, + keys, + crate::link_delivery::BASE_LINK_MTU, + )), + _ => self.link_stamper.new_initiator(node_hash, 1), + }; + let Ok((link, request_data)) = initiator else { tracing::warn!("propagation sync link request stamp generation failed"); self.state = SyncTaskState::Failed; return false;