Skip to main content

hashtree_core/
lib.rs

1//! HashTree - Simple content-addressed merkle tree storage
2//!
3//! Rust-first library for building merkle trees with content-hash addressing:
4//! SHA256(content) -> content
5//!
6//! # Overview
7//!
8//! HashTree provides a simple, efficient way to build and traverse content-addressed
9//! merkle trees. It uses SHA256 for hashing and MessagePack for tree node encoding.
10//!
11//! Content is CHK (Content Hash Key) encrypted by default, enabling deduplication
12//! even for encrypted content. Use `.public()` config to disable encryption.
13//!
14//! # Core Concepts
15//!
16//! - **Blobs**: Raw data stored directly by their hash (SHA256(data) -> data)
17//! - **Tree Nodes**: MessagePack-encoded nodes with links to children (SHA256(msgpack(node)) -> msgpack(node))
18//! - **Links**: References to child nodes with optional name and size metadata
19//! - **Cid**: Content identifier with hash + optional encryption key
20//!
21//! # Example
22//!
23//! ```rust
24//! use hashtree_core::{HashTree, HashTreeConfig, MemoryStore};
25//! use std::sync::Arc;
26//!
27//! #[tokio::main]
28//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
29//!     let store = Arc::new(MemoryStore::new());
30//!     let tree = HashTree::new(HashTreeConfig::new(store));
31//!
32//!     // Store content (encrypted by default)
33//!     let (cid, _size) = tree.put(b"Hello, World!").await?;
34//!
35//!     // Read it back
36//!     let data = tree.get(&cid, None).await?;
37//!     assert_eq!(data, Some(b"Hello, World!".to_vec()));
38//!
39//!     Ok(())
40//! }
41//! ```
42
43pub mod blob_route;
44pub mod builder;
45pub mod codec;
46pub mod crypto;
47pub mod diff;
48mod directory;
49pub mod hash;
50pub mod hashtree;
51pub mod nhash;
52pub mod reader;
53pub mod store;
54pub mod types;
55pub mod visibility;
56
57// Re-exports for convenience
58pub use blob_route::{
59    decode_blob_reply_header, decode_blob_request, encode_blob_reply_header, encode_blob_request,
60    BlobCodecError, BlobReply, BlobReplyHeader, BlobRequest, BlobRoute, StoreBlobRoute,
61    BLOB_DEFAULT_HTL, BLOB_MAX_BYTES, BLOB_MAX_HTL, BLOB_REPLY_HEADER_BYTES, BLOB_REQUEST_BYTES,
62};
63
64// Main API - unified HashTree
65pub use hashtree::{verify_tree as hashtree_verify_tree, HashTree, HashTreeConfig, HashTreeError};
66
67// Constants
68pub use builder::{BEP52_CHUNK_SIZE, DEFAULT_CHUNK_SIZE, DEFAULT_MAX_LINKS};
69
70// Low-level codec
71pub use codec::{
72    decode_tree_node, encode_and_hash, encode_tree_node, get_node_type, is_directory_node,
73    is_tree_node, try_decode_tree_node, CodecError,
74};
75pub use hash::{sha256, verify};
76
77// Reader types (used by HashTree)
78pub use reader::{
79    verify_tree, verify_tree_integrity, ReaderError, TreeEntry, VerifyIntegrityResult,
80    VerifyResult, WalkEntry,
81};
82
83// Store
84pub use nhash::{
85    decode as nhash_decode_any, is_nhash, nhash_decode, nhash_encode, nhash_encode_full,
86    DecodeResult, NHashData, NHashError,
87};
88pub use store::{BufferedStore, MemoryStore, Store, StoreError};
89pub use types::{
90    from_hex, hash_equals, to_hex, Cid, CidParseError, DirEntry, Hash, Link, LinkType, PutResult,
91    TreeNode,
92};
93
94pub use crypto::{
95    content_hash, could_be_encrypted, decrypt, decrypt_chk, encrypt, encrypt_chk, encrypted_size,
96    encrypted_size_chk, generate_key, key_from_hex, key_to_hex, plaintext_size, CryptoError,
97    EncryptionKey,
98};
99pub use visibility::{xor_keys, TreeVisibility};
100
101// Tree diff operations
102pub use diff::{
103    collect_hashes, collect_hashes_with_progress, tree_diff, tree_diff_streaming,
104    tree_diff_with_old_hashes, DiffStats, TreeDiff,
105};