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 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 ant_evm::EncodedPeerId;
49
50/// Identity multihash code (stores raw bytes without hashing).
51const MULTIHASH_IDENTITY_CODE: u64 = 0x00;
52
53/// Convert a hex-encoded 32-byte node ID to an [`EncodedPeerId`].
54///
55/// Peer IDs are 64-character hex strings representing 32 raw bytes.
56/// libp2p `PeerId` expects a multihash-encoded identity. This function bridges the two
57/// formats by wrapping the raw bytes in an identity multihash (code 0x00) and then
58/// converting to `EncodedPeerId` via `From<PeerId>`.
59///
60/// # Errors
61///
62/// Returns an error if the hex string is invalid or the peer ID cannot be constructed.
63pub fn hex_node_id_to_encoded_peer_id(hex_id: &str) -> Result<EncodedPeerId> {
64    let raw_bytes = hex::decode(hex_id)
65        .map_err(|e| Error::Payment(format!("Invalid hex peer ID '{hex_id}': {e}")))?;
66
67    let multihash =
68        multihash::Multihash::<64>::wrap(MULTIHASH_IDENTITY_CODE, &raw_bytes).map_err(|e| {
69            Error::Payment(format!(
70                "Failed to create multihash for peer '{hex_id}': {e}"
71            ))
72        })?;
73
74    let peer_id = libp2p::PeerId::from_multihash(multihash).map_err(|_| {
75        Error::Payment(format!(
76            "Failed to create PeerId from multihash for peer '{hex_id}'"
77        ))
78    })?;
79
80    Ok(EncodedPeerId::from(peer_id))
81}