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 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::{verify_tree as hashtree_verify_tree, HashTree, HashTreeConfig, HashTreeError};
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::{
71    verify_tree, verify_tree_integrity, ReaderError, TreeEntry, VerifyIntegrityResult,
72    VerifyResult, WalkEntry,
73};
74
75// Store
76pub use nhash::{
77    decode as nhash_decode_any, is_nhash, nhash_decode, nhash_encode, nhash_encode_full,
78    DecodeResult, NHashData, NHashError,
79};
80pub use store::{MemoryStore, Store, StoreError};
81pub use types::{
82    from_hex, hash_equals, to_hex, Cid, CidParseError, DirEntry, Hash, Link, LinkType, PutResult,
83    TreeNode,
84};
85
86pub use crypto::{
87    content_hash, could_be_encrypted, decrypt, decrypt_chk, encrypt, encrypt_chk, encrypted_size,
88    encrypted_size_chk, generate_key, key_from_hex, key_to_hex, plaintext_size, CryptoError,
89    EncryptionKey,
90};
91pub use visibility::{xor_keys, TreeVisibility};
92
93// Tree diff operations
94pub use diff::{
95    collect_hashes, collect_hashes_with_progress, tree_diff, tree_diff_streaming,
96    tree_diff_with_old_hashes, DiffStats, TreeDiff,
97};