Skip to main content

content-addressable

Data carries its own proof of integrity, intrinsically.

IPLD-native content addressing for Rust and Python. A content address is derived from the data itself, not assigned by an authority — give someone the bytes and the address, and they can recompute the address and know, with no trusted third party, that the bytes are exactly what the address names. The proof travels with the data.

This crate is deliberately small and honest: it is the instrument, not the sky.

It speaks the multiformats / IPLD stack, so its artifacts interoperate with the wider content-addressed world (IPFS, IPLD, libp2p). Every id is a CIDv1 with a fixed profile:

Field Value
CID version v1
Codec DAG-CBOR (0x71)
Multihash BLAKE3 (0x1e)
Digest 32 bytes
Encoding canonical DAG-CBOR (strict key order, definite lengths, tag-42 links)

Rust is the core implementation; the Python package is a PyO3 binding over that same Rust core, so an id computed in Python is byte-identical to the one Rust computes for the same canonical IPLD value.

Status & stability

The package is 0.1.0 — the first release that freezes the core contract. The core byte/wire and API contracts are locked for the whole 0.1.x line (changing any is a breaking release outside 0.1.x), while the optional unstable-merkle / unstable-store features are explicitly still moving and are named to say so.

Surface Default Stability
ContentId, canonical encoding, core errors, presentation, MSRV Yes Frozen for 0.1.x — changing any is a breaking release outside 0.1.x
Python core parity Separate package Same core byte profile
unstable-merkle feature No Experimental — serialized node bytes NOT frozen
unstable-store feature No Experimental — trait/API surface NOT frozen (no new wire format of its own)

Details and rationale: docs/STABILITY.md.

Installation

Rust:

[dependencies]
content-addressable = "0.1.0"

With the optional (default-off) features:

content-addressable = { version = "0.1.0", features = ["unstable-merkle"] }
content-addressable = { version = "0.1.0", features = ["unstable-store"] }
content-addressable = { version = "0.1.0", features = ["unstable-merkle", "unstable-store"] }

Python:

pip install content-addressable

The PyPI distribution is content-addressable (hyphen); the import name is content_addressable (underscore):

import content_addressable

Rust quick start

Implement ContentAddressable by providing canonical_form; content_id, verify, and ensure_content_id come for free:

use content_addressable::{canonical, ContentAddressable, ContentError};
use serde::Serialize;

#[derive(Serialize)]
struct Record {
    name: String,
}

impl ContentAddressable for Record {
    fn canonical_form(&self) -> Result<Vec<u8>, ContentError> {
        canonical::to_canonical_dagcbor(self)
    }
}

let record = Record { name: "alpha".into() };

let id = record.content_id()?;          // a CIDv1 (DAG-CBOR + BLAKE3)
assert!(record.verify(&id)?);           // self-certifying: re-derive and compare
println!("{id}");                       // "bafyr4i…" — the canonical text form
# Ok::<(), ContentError>(())

verify returns Ok(false) on a mismatch; its strict sibling ensure_content_id returns Err(ContentError::VerificationFailed) instead. The secondary digest and binary presentation forms are in Presentation forms.

Python quick start

The Python face exposes the same byte profile, but not the Rust ContentAddressable trait or verify — you canonicalize a native Python value and take its content_id directly. This block is mirrored by tests/test_readme.py (every call identical), so CI's python job proves it still works:

from content_addressable import (
    ContentId, content_id,
    to_canonical_dagcbor, from_canonical_dagcbor,
)

# A value's content id (CIDv1, DAG-CBOR + BLAKE3). Key order is irrelevant.
record = {"name": "alpha", "attrs": {}}
cid = content_id(record)

assert str(cid).startswith("b")                # base32-lower multibase text
assert len(cid.digest_hex()) == 64             # 64-char bare-digest-hex
assert isinstance(to_canonical_dagcbor(record), bytes)

# Canonical bytes round-trip; equal values -> equal bytes -> equal ids.
raw = to_canonical_dagcbor(record)
assert content_id(record) == ContentId.from_canonical_bytes(raw)
assert from_canonical_dagcbor(raw) == record
assert content_id({"attrs": {}, "name": "alpha"}) == cid  # order-independent

# Parse an id back from its text / binary forms.
assert ContentId.parse(str(cid)) == cid
assert ContentId.from_bytes(cid.to_bytes()) == cid

# Wrap an already-computed 32-byte BLAKE3 digest with NO re-hash.
assert ContentId.from_blake3_content_digest(cid.digest_bytes()) == cid
assert len(cid.digest_bytes()) == 32           # the raw BLAKE3 hash

ContentId implements __eq__ / __hash__, so an id is usable as a dict key or set member.

Choosing a construction path

Prefer the safe path. from_canonical_bytes is fast but carries a real precondition — it is not universally safe.

Use case API (Rust / Python) Contract
Hash a normal value value.content_id() / content_id(value) Preferred safe path
Encode a value to bytes canonical::to_canonical_dagcbor(v) / to_canonical_dagcbor(v) Produces canonical DAG-CBOR
Accept foreign / untrusted bytes Rust: ContentId::from_canonical_bytes_checked(b) · Python: no single checked constructor yet Validates DAG-CBOR canonicality; errors on non-canonical
Hash already-trusted canonical bytes ContentId::from_canonical_bytes(b) Unchecked precondition: caller asserts b is canonical DAG-CBOR
Wrap an existing BLAKE3 digest ContentId::from_blake3_content_digest(d) No rehash; caller asserts the digest is BLAKE3 over canonical DAG-CBOR

Presentation forms

A ContentId names four distinct presentation forms so callers can't confuse them; each is frozen (changing any is a breaking release outside 0.1.x):

Form Rust Python What it is
Canonical text Display / to_string() str(id) multibase base32-lower (b…) — the IPLD-canonical CID string
Binary envelope to_bytes() / from_bytes() to_bytes() / from_bytes() the full CID binary form (version + codec + multihash + digest)
Bare digest digest_bytes() -> [u8; 32] digest_bytes() -> bytes the raw 32-byte BLAKE3 hash, no envelope
Bare-digest-hex digest_hex() -> String digest_hex() -> str lower-hex of the 32-byte digest (64 chars, no prefix)

Display is the inverse of FromStr for base32-lower, and that round-trip is frozen and tested. Full CID bytes can be hex-encoded by a caller directly (hex::encode(id.to_bytes())) — the crate deliberately does not bless a second "hex" method; see docs/STABILITY.md for why.

Experimental features

Both features are default-off and exercised in CI via --all-features. Do not depend on the unstable-merkle node bytes yet.

merkle — content-addressed DAG nodes

MerkleNode<T> is a payload: T plus parents: BTreeSet<ContentId>; its id is derived from both the payload and the parent links, so a root id plus the node bytes determines the whole DAG. Because parents are a BTreeSet, they are deduplicated and ordered by content-derived Ord — equal parent sets always produce equal bytes regardless of insertion order, and each parent serializes as a real DAG-CBOR tag-42 link.

use content_addressable::merkle::MerkleNode; // feature = "unstable-merkle"

let root = MerkleNode::genesis("hello");
let root_id = root.id()?;
let child = MerkleNode::new("world", [root_id]);
assert!(child.parents().contains(&root_id));
# Ok::<(), content_addressable::ContentError>(())

The serialized node layout is experimental and NOT frozen — pinning it (Merkle conformance vectors) is post-0.1.0 work.

store — the CID-addressed node store seam

A narrow, backend-agnostic seam: get/put by ContentId, with a verified read path the extension-trait implementation establishes. The pieces:

  • NodeStore — the raw backend seam (two dumb ops: get_unverified, insert). Backends implement only this.
  • NodeStoreExt — blanket-implemented, sealed-by-coherence verified operations (get, get_node, put, put_checked, …). A backend cannot re-implement them.
  • VerifiedStore<B> — the recommended capability-safe facade: it exposes only the verified operations (dispatched via UFCS), so a backend's own inherent method cannot intercept a call made through it. Use this unless you have a reason to drop to raw ops.
  • MemoryStore — the grow-only in-memory reference backend.
  • AddressedBytes — an unforgeable, address-consistent (id, bytes) pair; it is the only thing insert accepts, so a backend can't be handed a mismatched pair.

Typed writes/reads are the strict doors:

use content_addressable::store::{MemoryStore, VerifiedStore};
use content_addressable::{canonical, ContentAddressable, ContentError};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Record {
    name: String,
}

impl ContentAddressable for Record {
    fn canonical_form(&self) -> Result<Vec<u8>, ContentError> {
        canonical::to_canonical_dagcbor(self)
    }
}

let mut store = VerifiedStore::new(MemoryStore::new());
let record = Record { name: "alpha".into() };

let id = store.put_node(&record)?;               // strict: rejects non-canonical
let recovered: Record = store.get_node(&id)?;    // identity-preserving typed read
assert_eq!(record, recovered);
# Ok::<(), content_addressable::store::StoreError>(())
  • put_node strictly validates canonical DAG-CBOR before insertion; put is unchecked with respect to canonicality (put_checked / put_node are the strict doors).
  • get_node performs an identity-preserving typed read (decode, re-encode, and require the value to be the one named by the id). Raw get proves only that the returned bytes hash to the requested CID — not that they are canonical.

Trust boundary. The seam derives addresses on write and verifies them on read (seam theorems). Successful persistence, durability, no-rebind, and grow-only behavior are backend obligations (backend refinement laws), not seam theorems — MemoryStore discharges its documented in-memory obligations. The formal Lean/TLA+ artifacts are deferred proof targets (tracked in #71); the store module docs in src/store.rs carry the full proof-obligation catalog. The store trait/API is experimental and NOT frozen.

Stability details

The frozen 0.1.x contracts — CID profile, presentation, serde representation, error policy, verify/ensure_content_id, crate-root exports, MSRV/edition, the no-rehash digest bridge, and the experimental-feature exclusions — are recorded in docs/STABILITY.md, with issue provenance. Treat the frozen surfaces as durable.

Development

just check            # the local gate: fmt + clippy + test + docs + leaf-deps

A pre-push hook runs the same checks; the individual cargo fmt / cargo clippy / cargo test --all-features steps work directly too.

Releasing

Tag-driven; see RELEASING.md for the wheel matrix, PyPI Trusted Publishing, and crates.io steps.

License

Apache-2.0.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

content_addressable-0.1.0.tar.gz (147.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

content_addressable-0.1.0-cp39-abi3-win_amd64.whl (276.4 kB view details)

Uploaded CPython 3.9+Windows x86-64

content_addressable-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (431.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

content_addressable-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (416.6 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

content_addressable-0.1.0-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (751.6 kB view details)

Uploaded CPython 3.9+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file content_addressable-0.1.0.tar.gz.

File metadata

  • Download URL: content_addressable-0.1.0.tar.gz
  • Upload date:
  • Size: 147.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for content_addressable-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6496ee598276f85135b7aa68ae22945f6b4000e4757ef1098b367026b36bb2f4
MD5 e04e19d9315fd2f028328c8ce79e4dcc
BLAKE2b-256 ea49bfb8d6a612ebf677c40a5e7d8ffe4f56fd1d0ed130d5caa9eaaf316343f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for content_addressable-0.1.0.tar.gz:

Publisher: release.yml on hartsock/content-addressable

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file content_addressable-0.1.0-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for content_addressable-0.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 3b8ce1b09d80fbcf108819ccf6bdd464eb7327e96761a096085028470267be92
MD5 a2001097517bd8618a0c9044d000a502
BLAKE2b-256 98f7579fc8b0c7673885ec86456fcaf677dc6661f9fb1af8c1ee41573c4d5b07

See more details on using hashes here.

Provenance

The following attestation bundles were made for content_addressable-0.1.0-cp39-abi3-win_amd64.whl:

Publisher: release.yml on hartsock/content-addressable

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file content_addressable-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for content_addressable-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6b239f27c666306fff46845c5b8c6451e8dd963d3cf9805e49287ff3b5be2149
MD5 10e8159bb2ee86e8b6b717978b400939
BLAKE2b-256 6465a55372ad87d2d7e16954d8fe7a2a86ef2463e5894debfe30c0493329b163

See more details on using hashes here.

Provenance

The following attestation bundles were made for content_addressable-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on hartsock/content-addressable

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file content_addressable-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for content_addressable-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0a112338cce8b060aef24a3df2ec846fb8d51ba8e914ea53e7b90d335619072c
MD5 9abdceaac2429d7fcb8f68d6aa1590fc
BLAKE2b-256 4d4fc9ec22d1dc2a0ed63c5650a4a4aba67e8f99dedd335c5ece9c3ec0207b19

See more details on using hashes here.

Provenance

The following attestation bundles were made for content_addressable-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on hartsock/content-addressable

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file content_addressable-0.1.0-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for content_addressable-0.1.0-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 72f9efecd403a745822862ff14420155daeb2e9801d990683b0176250cc6e47b
MD5 ee5911760272524704fff77868bd929c
BLAKE2b-256 502ebb899489c09c9c3bfdc0676a7e48389cf436b67ad0ef9b988c5cc2998340

See more details on using hashes here.

Provenance

The following attestation bundles were made for content_addressable-0.1.0-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: release.yml on hartsock/content-addressable

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page