Send and inspect LXMF messages in Rust
For LXMF message construction, signing, serialization, unpacking, and signature verification, start with lxmf_core::message_api (MSRV Rust 1.89+).
lxmf-core is candidate-stable, with lxmf_core::message_api as the canonical application codec boundary. Higher-level router ownership (lxmf_core::router::LxmRouter), delivery orchestration (lxmf_core::link_delivery::LinkDeliveryManager), propagation clients/nodes, handlers, and persistence internals remain provisional and module-qualified — pin exact release tags and read api/migrations/1.5.md when depending on them.
1. Add dependencies to Cargo.toml
Match the rsLXMF and rsReticulum release line:
[dependencies]
lxmf-core = { git = "https://github.com/ratspeak/rsLXMF.git", tag = "v1.5.1" }
rns-crypto = { git = "https://github.com/ratspeak/rsReticulum.git", tag = "v1.5.1" }
2. Construct, sign, unpack, and verify a message
Adapted directly from the CI-compiled rsLXMF/crates/lxmf-core/examples/message.rs:
use lxmf_core::message_api::{DeliveryMethod, LxMessage, LxMessageDraft, MessageState};
use rns_crypto::ed25519::Ed25519PrivateKey;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let signing_key = Ed25519PrivateKey::generate();
// Placeholder hashes for local packing; real senders MUST derive
// source_hash from their lxmf.delivery Destination bound to signing_key.
let mut draft = LxMessageDraft::new(
[0x11; 16], // destination_hash (recipient lxmf.delivery)
[0x22; 16], // source_hash (sender lxmf.delivery)
"Hello",
"LXMF from Rust",
DeliveryMethod::Direct,
)?;
// Attach a raw binary application field
draft.set_field(0x105_u64, vec![1, 2, 3])?;
// Sign and freeze into an immutable LxMessage (None = no PoW stamp)
let message = draft.sign(&signing_key)?.finalize(None)?;
// Unpack wire bytes on the receiver side
let unpacked = LxMessage::unpack(message.wire_bytes())?;
assert_eq!(unpacked.title_text(), "Hello");
assert_eq!(unpacked.state, MessageState::Generating);
assert_eq!(unpacked.wire_bytes(), message.wire_bytes());
// Cryptographically verify the Ed25519 signature against the sender's public key
assert!(unpacked.verify_signature(&signing_key.public_key()));
Ok(())
}
Run the upstream example and generate local rustdoc from a checked-out rsLXMF workspace:
cargo run -p lxmf-core --example message
cargo doc --workspace --no-deps --open
3. Binary vs. native MessagePack fields (set_field vs. set_msgpack_field)
LXMF fields carry typed MessagePack values. Choosing the wrong setter silently breaks wire interoperability with Python LXMF clients:
draft.set_field(id, bytes)wrapsbytesas aFieldValue::Binary(MessagePackbin) — use this for opaque binary blobs.draft.set_msgpack_field(id, encoded_bytes)validates and storesencoded_bytesas canonical native MessagePack (FieldValue::MessagePack) — required for structured protocol fields such asFIELD_TICKET,FIELD_REACTION,FIELD_RESULTS, and LRGP envelopes (0xFBstring marker +0xFDmap).
// Example: embedding a pre-encoded MessagePack value (e.g. a ticket or LRGP field)
let mut encoded = Vec::new();
rmpv::encode::write_value(&mut encoded, &rmpv::Value::String("lrgp.v1".into()))?;
draft.set_msgpack_field(0xFB_u8, encoded)?;
set_fieldCalling set_field(0xFB, encoded) puts bin("lrgp.v1") on the wire instead of str("lrgp.v1"). Python LXMF and LRGP peers will ignore or reject the field.
4. Delivery methods, size budgets & stamps
DeliveryMethod | Wire transport | Practical limits & notes |
|---|---|---|
Opportunistic | Single encrypted Reticulum packet (no Link) | Default content ceiling is 287 bytes before title, fields, serialization, and stamp overhead; requires a known recipient identity/ratchet. |
Direct | Encrypted Reticulum Link (auto-promotes to Resource if larger than Link MDU) | Reuses an open Link to the same destination; compresses only when the recipient announces compression support. |
Propagated | Encrypted envelope deposited at an lxmd / lxmd-rs propagation node | Subject to the chosen propagation node's propagation_message_max_accepted_size and PoW stamp cost. |
Stamps, tickets & untrusted metadata
- Fractional stamp costs (
f64): SincersLXMF 1.5.0(LXMF1.3.0), stamp costs and values aref64(for example16.0or fractional values). - Signature ≠ valid stamp:
LxMessage::unpackandverify_signaturedo not enforce your application's stamp cost or replay policy. Validate stamps (or matching recipient-issued tickets) against your router policy before accepting inbound traffic. - Sanitize display names: Announce display names are passed through verbatim from the wire. Always run untrusted names through
lxmf_core::presentation::sanitize_namebefore displaying them in a UI.
5. Integrating with provisional router & delivery APIs
When embedding full sending/receiving loops (beyond pure message packing), lxmf-core exposes caller-driven state machines rather than a hidden background daemon:
- Event-driven Direct delivery (
LinkDeliveryManager): Callpoll_readyto wait for inbound delivery packets, local admission acknowledgements, and endpoint lifecycle events; drain bounded batches withdrain_eventsand drive protocol deadlines withtick. - Exact admission receipts: Wire
LinkDeliveryManager::set_link_endpoint_dispatch_handlewith the handle from the sameReticulumHandleruntime so local FIFO wait and measured-RTT proof wait stay distinct. - Inbound memory reservations: Opt into
set_inbound_resource_completion_handler(synchronous, non-blocking callback) to take ownership of completed inbound Resource payloads without cloning. - SQLite state store:
rsLXMF 1.5+persists delivered message IDs, stamp costs, and tickets instorage/lxmf/state.db(matching Python LXMF 1.3.x).
Next steps
- Build structured turn-based apps over LXMF fields in Building games with LRGP.
- For
no_stdmicrocontrollers, see Embedded Lite & Handheld FFI. - Review daemon config and storage behavior in rsLXMF reference.