Skip to main content

MerkleDAG implementation in Python

Project description

py-ipld-dag

PyPI version Documentation Python 3.10+

IPLD DAG implementation for Python — codecs for DAG-CBOR, DAG-JSON, DAG-PB, and Raw, aligned with the js-multiformats ecosystem.

Provides:

  • DAG-CBOR (0x71) – Deterministic CBOR with CID links (CBOR tag 42)
  • DAG-JSON (0x0129) – Deterministic JSON with CID links ({"/": "bafy..."})
  • DAG-PB (0x70) – Protobuf-based Merkle DAG nodes (legacy IPFS/UnixFS)
  • Raw (0x55) – Identity codec for raw binary data
  • Block APIBlock.encode() / Block.decode() / Block.create()
  • Codec registry – Pluggable codec architecture with auto-registration
  • IPLD Data Model – Full support for Null, Bool, Int, Float, String, Bytes, List, Map, Link kinds
  • CID integration – Seamless use of py-cid for content addressing

Read more in the documentation on ReadTheDocs. View the release notes.

Quick Start

from dag import Block
from dag.codecs import dag_cbor, dag_json, dag_pb, raw

block = Block.encode(value={"hello": "world", "n": 42}, codec=dag_cbor.codec)
print(block.cid)
print(block.bytes)

restored = Block.decode(data=block.bytes, codec=dag_cbor.codec)
assert restored.value == {"hello": "world", "n": 42}

child = Block.encode(value={"child": True}, codec=dag_cbor.codec)
parent = Block.encode(
    value={"link": child.cid, "type": "parent"},
    codec=dag_cbor.codec,
)

for path, cid in parent.links():
    print(f"{path} -> {cid}")

json_block = Block.encode(
    value={"ref": child.cid, "data": b"\xde\xad"},
    codec=dag_json.codec,
)

pb_block = Block.encode(
    value={
        "Data": b"hello UnixFS",
        "Links": [{"Hash": child.cid, "Name": "child", "Tsize": 10}],
    },
    codec=dag_pb.codec,
)

raw_block = Block.encode(value=b"raw binary", codec=raw.codec)

Examples

Example scripts are available in examples/, including Python equivalents of js-multiformats interface demos:

  • examples/block_interface/block_interface.py
  • examples/cid_interface/cid_interface.py
  • examples/multicodec_interface/multicodec_interface.py
  • examples/multihash_interface/multihash_interface.py

Run them from the repository root:

source venv/bin/activate
python -m examples.block_interface.block_interface
python -m examples.cid_interface.cid_interface
python -m examples.multicodec_interface.multicodec_interface
python -m examples.multihash_interface.multihash_interface

Installation and usage

Installation (venv, pip/uv, stable and development) and usage are documented in the docs — see Installation and Usage in the table of contents.

Installation

From PyPI (stable):

pip install py-ipld-dag

From source (development):

Same as in CONTRIBUTING.md. With uv (recommended):

git clone https://github.com/ipld/py-ipld-dag.git
cd py-ipld-dag
uv venv && source venv/bin/activate   # Windows: venv\Scripts\activate
uv pip install --upgrade pip
uv pip install --group dev -e .
pre-commit install

With pip (requires pip >= 25.1):

python -m venv venv && source venv/bin/activate   # Windows: venv\Scripts\activate
pip install --upgrade pip
pip install --group dev -e .
pre-commit install

Or run make install-dev after activating a venv (uses uv).

Full usage, API reference, and examples: Documentation.

Development

  • Tests: pytest or make test
  • Lint: make lint or pre-commit run --all-files
  • Docs: make docs-ci (build docs with Sphinx)

Contributing

See CONTRIBUTING.md for setup and pull request guidelines.

Release notes

Changelog and release notes are generated from newsfragments with Towncrier and published in the docs: Release notes.

Project details


Download files

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

Source Distribution

py_ipld_dag-0.2.0.tar.gz (40.8 kB view details)

Uploaded Source

Built Distribution

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

py_ipld_dag-0.2.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file py_ipld_dag-0.2.0.tar.gz.

File metadata

  • Download URL: py_ipld_dag-0.2.0.tar.gz
  • Upload date:
  • Size: 40.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for py_ipld_dag-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1dd1bddc91c2d04f42b437607e2902e277d58851e48a581d50bd29030a04ec1c
MD5 ad4dbd97a3bcc42bfce8b2a2f74a7ed9
BLAKE2b-256 c87e20d238a933fd9a404afd101f289137a1cc8e84967d6fc6e53ce73542be3a

See more details on using hashes here.

File details

Details for the file py_ipld_dag-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: py_ipld_dag-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for py_ipld_dag-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d8ff2b62f8ea300c287a7c0c737c75202f6e184c8cd27b7c8c081febf00a59e7
MD5 640bb456d04cc1f8080e218d926bd25e
BLAKE2b-256 771a93226c645c495c378a6f6ea0b48716c54e6a461f67f9286dd3036b1569dd

See more details on using hashes here.

Supported by

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