Expand description
HashTree - Simple content-addressed merkle tree storage
Rust-first library for building merkle trees with content-hash addressing: SHA256(content) -> content
§Overview
HashTree provides a simple, efficient way to build and traverse content-addressed merkle trees. It uses SHA256 for hashing and MessagePack for tree node encoding.
Content is CHK (Content Hash Key) encrypted by default, enabling deduplication
even for encrypted content. Use .public() config to disable encryption.
§Core Concepts
- Blobs: Raw data stored directly by their hash (SHA256(data) -> data)
- Tree Nodes: MessagePack-encoded nodes with links to children (SHA256(msgpack(node)) -> msgpack(node))
- Links: References to child nodes with optional name and size metadata
- Cid: Content identifier with hash + optional encryption key
§Example
use hashtree_core::{HashTree, HashTreeConfig, MemoryStore};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let store = Arc::new(MemoryStore::new());
let tree = HashTree::new(HashTreeConfig::new(store));
// Store content (encrypted by default)
let (cid, _size) = tree.put(b"Hello, World!").await?;
// Read it back
let data = tree.get(&cid).await?;
assert_eq!(data, Some(b"Hello, World!".to_vec()));
Ok(())
}Re-exports§
pub use hashtree::HashTree;pub use hashtree::HashTreeConfig;pub use hashtree::HashTreeError;pub use hashtree::verify_tree as hashtree_verify_tree;pub use builder::BEP52_CHUNK_SIZE;pub use builder::DEFAULT_CHUNK_SIZE;pub use builder::DEFAULT_MAX_LINKS;pub use codec::decode_tree_node;pub use codec::encode_and_hash;pub use codec::encode_tree_node;pub use codec::get_node_type;pub use codec::is_directory_node;pub use codec::is_tree_node;pub use codec::try_decode_tree_node;pub use codec::CodecError;pub use hash::sha256;pub use hash::verify;pub use reader::verify_tree;pub use reader::ReaderError;pub use reader::TreeEntry;pub use reader::VerifyResult;pub use reader::WalkEntry;pub use store::MemoryStore;pub use store::Store;pub use store::StoreError;pub use types::from_hex;pub use types::hash_equals;pub use types::to_hex;pub use types::Cid;pub use types::CidParseError;pub use types::DirEntry;pub use types::Hash;pub use types::Link;pub use types::LinkType;pub use types::PutResult;pub use types::TreeNode;pub use nhash::decode as nhash_or_nref_decode;pub use nhash::is_nhash;pub use nhash::is_nref;pub use nhash::nhash_decode;pub use nhash::nhash_encode;pub use nhash::nhash_encode_full;pub use nhash::nref_decode;pub use nhash::nref_encode;pub use nhash::DecodeResult;pub use nhash::NHashData;pub use nhash::NHashError;pub use nhash::NRefData;pub use crypto::content_hash;pub use crypto::could_be_encrypted;pub use crypto::decrypt;pub use crypto::decrypt_chk;pub use crypto::encrypt;pub use crypto::encrypt_chk;pub use crypto::encrypted_size;pub use crypto::encrypted_size_chk;pub use crypto::generate_key;pub use crypto::key_from_hex;pub use crypto::key_to_hex;pub use crypto::plaintext_size;pub use crypto::CryptoError;pub use crypto::EncryptionKey;pub use diff::collect_hashes;pub use diff::collect_hashes_with_progress;pub use diff::tree_diff;pub use diff::tree_diff_streaming;pub use diff::tree_diff_with_old_hashes;pub use diff::DiffStats;pub use diff::TreeDiff;
Modules§
- builder
- Tree builder with chunking and fanout support
- codec
- MessagePack encoding/decoding for tree nodes
- crypto
- Content Hash Key (CHK) encryption for HashTree
- diff
- Tree diff operations for efficient incremental updates
- hash
- Hashing utilities using SHA256
- hashtree
- HashTree - Unified merkle tree operations
- nhash
- Bech32-encoded identifiers for hashtree content
- reader
- Tree reader and traversal utilities
- store
- Content-addressed key-value store interfaces and implementations
- types
- HashTree - Simple content-addressed merkle tree