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).await?;
37//! assert_eq!(data, Some(b"Hello, World!".to_vec()));
38//!
39//! Ok(())
40//! }
41//! ```
42
43pub mod builder;
44pub mod codec;
45pub mod crypto;
46pub mod diff;
47pub mod hash;
48pub mod hashtree;
49pub mod nhash;
50pub mod reader;
51pub mod store;
52pub mod types;
53pub mod visibility;
54
55// Re-exports for convenience
56// Main API - unified HashTree
57pub use hashtree::{HashTree, HashTreeConfig, HashTreeError, verify_tree as hashtree_verify_tree};
58
59// Constants
60pub use builder::{BEP52_CHUNK_SIZE, DEFAULT_CHUNK_SIZE, DEFAULT_MAX_LINKS};
61
62// Low-level codec
63pub use codec::{
64 decode_tree_node, encode_and_hash, encode_tree_node, get_node_type, is_directory_node,
65 is_tree_node, try_decode_tree_node, CodecError,
66};
67pub use hash::{sha256, verify};
68
69// Reader types (used by HashTree)
70pub use reader::{verify_tree, ReaderError, TreeEntry, VerifyResult, WalkEntry};
71
72// Store
73pub use store::{MemoryStore, Store, StoreError};
74pub use types::{from_hex, hash_equals, to_hex, Cid, CidParseError, DirEntry, Hash, Link, LinkType, PutResult, TreeNode};
75pub use nhash::{
76 decode as nhash_or_nref_decode, is_nhash, is_nref, nhash_decode, nhash_encode,
77 nhash_encode_full, nref_decode, nref_encode, DecodeResult, NHashData, NHashError, NRefData,
78};
79
80pub use crypto::{
81 content_hash, could_be_encrypted, decrypt, decrypt_chk, encrypt, encrypt_chk, encrypted_size,
82 encrypted_size_chk, generate_key, key_from_hex, key_to_hex, plaintext_size, CryptoError,
83 EncryptionKey,
84};
85pub use visibility::{xor_keys, TreeVisibility};
86
87// Tree diff operations
88pub use diff::{collect_hashes, collect_hashes_with_progress, tree_diff, tree_diff_streaming, tree_diff_with_old_hashes, DiffStats, TreeDiff};