Skip to main content

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
4//! communication. For high-level client operations, use the `ant-client`
5//! crate instead.
6//!
7//! # Architecture
8//!
9//! As of 0.11, the shared protocol types and helpers live in the
10//! [`ant_protocol`] crate. This module re-exports them so existing
11//! callers of `ant_node::client::*` continue to compile; new code
12//! should prefer `ant_protocol::*` directly.
13//!
14//! # Example
15//!
16//! ```rust,ignore
17//! use ant_client::Client;
18//!
19//! #[tokio::main]
20//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
21//!     let client = Client::connect(&bootstrap_peers, Default::default()).await?;
22//!     let address = client.chunk_put(bytes::Bytes::from("hello world")).await?;
23//!     let chunk = client.chunk_get(&address).await?;
24//!     Ok(())
25//! }
26//! ```
27
28pub use ant_protocol::chunk_protocol::send_and_await_chunk_response;
29pub use ant_protocol::data_types::{
30    compute_address, peer_id_to_xor_name, xor_distance, ChunkStats, DataChunk,
31};
32pub use ant_protocol::XorName;
33
34use crate::error::{Error, Result};
35use evmlib::EncodedPeerId;
36
37/// Convert a hex-encoded 32-byte node ID to an [`EncodedPeerId`].
38///
39/// Peer IDs are 64-character hex strings representing 32 raw bytes.
40/// This function decodes the hex string and wraps the raw bytes directly
41/// into an `EncodedPeerId`.
42///
43/// # Errors
44///
45/// Returns an error if the hex string is invalid or not exactly 32 bytes.
46pub fn hex_node_id_to_encoded_peer_id(hex_id: &str) -> Result<EncodedPeerId> {
47    let raw_bytes = hex::decode(hex_id)
48        .map_err(|e| Error::Payment(format!("Invalid hex peer ID '{hex_id}': {e}")))?;
49    let bytes: [u8; 32] = raw_bytes.try_into().map_err(|v: Vec<u8>| {
50        let len = v.len();
51        Error::Payment(format!("Peer ID must be 32 bytes, got {len}"))
52    })?;
53    Ok(EncodedPeerId::new(bytes))
54}