Content-addressed merkle tree storage library for Rust.
Simple over clever. SHA256 for hashing, CBOR for encoding. No multicodec, multibase, or CID versioning. One way to do things.
Core does one thing. Merkle trees over any key-value store. That's it. The library doesn't know about networks, peers, or protocols.
Composition over integration. Want WebRTC sync? Nostr discovery? Those are separate layers that use hashtree, not part of it.
- SHA256 hashing
- CBOR encoding for tree nodes
- File chunking with configurable size
- Directory support with nested trees
- Streaming append for large files
- Tree verification
- CHK (Content Hash Key) encryption (enabled by default)
- BEP52 (BitTorrent v2) compatible binary merkle algorithm (experimental)
use hashtree::{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 = tree.put(b"Hello, World!").await?;
// cid contains hash + encryption key
// Share cid.to_string() ("hash:key") to allow decryption
// Read it back
let data = tree.get(&cid).await?;
Ok(())
}For content that should be publicly readable without a key:
let tree = HashTree::new(HashTreeConfig::new(store).public());
let cid = tree.put(b"Public content").await?;
// cid.key is None, cid.to_string() is just the hashuse hashtree::DirEntry;
// Create files
let file1 = tree.put(b"content1").await?;
let file2 = tree.put(b"content2").await?;
// Create directory
let dir = tree.put_directory(vec![
DirEntry::new("a.txt", file1.hash).with_size(file1.size),
DirEntry::new("b.txt", file2.hash).with_size(file2.size),
], None).await?;
// List, resolve paths, walk trees
let entries = tree.list_directory(&dir).await?;
let resolved = tree.resolve_path(&dir, "a.txt").await?;hashtree- Core merkle tree libraryhashtree-bep52- BEP52 specific implementation (experimental)hashtree-lmdb- LMDB storage backendhashtree-resolver- Nostr-based ref resolutionhashtree-webrtc- P2P sync via WebRTChashtree-git- Git object compatibility layerhashtree-cli- Command-line interfacehashtree-relay- Nostr relay for hashtree eventsgit-remote-nostr- Git remote helper for Nostr
# Run tests
cargo test -p hashtree
# Run benchmarks
cargo bench -p hashtreeMIT