Files
nym/sdk/rust/nym-sdk/examples/builder_with_storage.rs
mfahampshire a70e68c7bd Max/smolmix docs (#6716)
* Smolmix documentation

* Add smolmix docs: landing page, tutorials, and developer page links

* Add Exit Gateway services page (NR vs IPR) and link from existing docs

* Update auto-generated command and API outputs

* Reorg of tutorials and architecture pages

* License information + remove TODO from docs.rs visibile comment + reorg
readme

* Add versions file for doc-wide versioning

* Relative -> absolute links

* Relative -> absolute links

* Update license + add old tutorial code as examples

* Streamline smolmix docs

* Clippy

* Clean up doc comments

* Last pass

* Add larger file download to list

* set new versions

* Clippy

* Remove blake pin from docs + add version range to root Cargo.toml

* Format example logging

* Remove crate blocked component

* Loose whitespace

* Add doc verification script for inline mdx

* Formatting

* Components regen

* Reorg + tighten text

* Voicing cohesion pass + remove bloated examples

* Voicing cont.

* Reduce max download size

* Small suggested clarifications

* Max/docs voicing consistency (#6769)

* Reduce max download size

* voicing consistency across docs

* New landing order w smolmix

* Tweaks

* Final tweaks
2026-05-13 11:19:44 +00:00

60 lines
2.0 KiB
Rust

//! Using `MixnetClientBuilder` with persistent on-disk key storage.
//!
//! Keys are generated on the first run, then loaded from disk on
//! subsequent runs so the client keeps the same Nym address.
//!
//! ## What this demonstrates
//!
//! - `StoragePaths::new_from_dir()` points to a directory for key material
//! - `MixnetClientBuilder::new_with_default_storage()` builds a client that
//! persists its identity (ed25519 + x25519 keypairs) to disk
//! - Run this example twice; the Nym address stays the same
//! - Use this pattern for any real application; ephemeral clients
//! (`connect_new()`) are only for quick experiments
//!
//! ```sh
//! cargo run --example builder_with_storage
//! ```
use nym_sdk::mixnet;
use nym_sdk::mixnet::MixnetMessageSender;
use std::path::PathBuf;
#[tokio::main]
async fn main() {
nym_bin_common::logging::setup_tracing_logger();
// Point storage at a directory.
// If keys exist there they are loaded; otherwise new ones are generated.
let config_dir = PathBuf::from("/tmp/mixnet-client");
let storage_paths = mixnet::StoragePaths::new_from_dir(&config_dir).unwrap();
// Build the client with on-disk persistent storage.
let client = mixnet::MixnetClientBuilder::new_with_default_storage(storage_paths)
.await
.unwrap()
.build()
.unwrap();
// Connect to the mixnet.
let mut client = client.connect_to_mixnet().await.unwrap();
let our_address = client.nym_address();
println!("Our client nym address is: {our_address}");
// Send a message to ourselves and wait for it.
client
.send_plain_message(*our_address, "hello there")
.await
.unwrap();
println!("Waiting for message");
if let Some(received) = client.wait_for_messages().await {
for r in received {
println!("Received: {}", String::from_utf8_lossy(&r.message));
}
}
// Always disconnect for clean shutdown.
client.disconnect().await;
}