ant_node/client/mod.rs
1//! Protocol helpers for ant-node client operations.
2//!
3//! This module provides low-level protocol support for client-node communication.
4//! For high-level client operations, use the `ant-client` crate instead.
5//!
6//! # Architecture
7//!
8//! This module contains:
9//!
10//! 1. **Protocol message handlers**: Send/await pattern for chunks
11//! 2. **Data types**: Common types like `XorName`, `DataChunk`, address computation
12//!
13//! # Migration Note
14//!
15//! The `QuantumClient` has been deprecated and consolidated into `ant-client::Client`.
16//! Use `ant-client` for all client operations.
17//!
18//! # Example
19//!
20//! ```rust,ignore
21//! use ant_client::Client; // Use ant-client instead of QuantumClient
22//!
23//! #[tokio::main]
24//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
25//! // High-level client API
26//! let client = Client::connect(&bootstrap_peers, Default::default()).await?;
27//!
28//! // Store data with payment
29//! let address = client.chunk_put(bytes::Bytes::from("hello world")).await?;
30//!
31//! // Retrieve data
32//! let chunk = client.chunk_get(&address).await?;
33//!
34//! Ok(())
35//! }
36//! ```
37
38mod chunk_protocol;
39mod data_types;
40
41pub use chunk_protocol::send_and_await_chunk_response;
42pub use data_types::{
43 compute_address, peer_id_to_xor_name, xor_distance, ChunkStats, DataChunk, XorName,
44};
45
46// Re-export hex_node_id_to_encoded_peer_id for payment operations
47use crate::error::{Error, Result};
48use evmlib::EncodedPeerId;
49
50/// Convert a hex-encoded 32-byte node ID to an [`EncodedPeerId`].
51///
52/// Peer IDs are 64-character hex strings representing 32 raw bytes.
53/// This function decodes the hex string and wraps the raw bytes directly
54/// into an `EncodedPeerId`.
55///
56/// # Errors
57///
58/// Returns an error if the hex string is invalid or not exactly 32 bytes.
59pub fn hex_node_id_to_encoded_peer_id(hex_id: &str) -> Result<EncodedPeerId> {
60 let raw_bytes = hex::decode(hex_id)
61 .map_err(|e| Error::Payment(format!("Invalid hex peer ID '{hex_id}': {e}")))?;
62 let bytes: [u8; 32] = raw_bytes.try_into().map_err(|v: Vec<u8>| {
63 let len = v.len();
64 Error::Payment(format!("Peer ID must be 32 bytes, got {len}"))
65 })?;
66 Ok(EncodedPeerId::new(bytes))
67}