libipld

3.3.2 · active · verified Mon Apr 13

libipld provides Python bindings to a high-performance Rust IPLD library, enabling fast encoding and decoding of IPLD data structures such as DAG-CBOR, CAR files, and CIDs. It's built for efficiency, often used in decentralized web and blockchain applications. The current version is 3.3.2, with active development and frequent minor releases addressing performance and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates encoding a Python dictionary into DAG-CBOR format, decoding it back, and generating a Content Identifier (CID) from the encoded data. This showcases core `libipld` functionality for structured data serialization and content addressing.

from libipld import encode_dag_cbor, decode_dag_cbor, CID

data = {
    'name': 'IPLD Example',
    'version': 1,
    'active': True,
    'items': [
        {'id': 1, 'value': 'A'},
        {'id': 2, 'value': 'B'}
    ]
}

# Encode Python dict to DAG-CBOR bytes
encoded_bytes = encode_dag_cbor(data)
print(f"Encoded DAG-CBOR (first 20 bytes): {encoded_bytes[:20]}...")

# Decode DAG-CBOR bytes back to Python dict
decoded_data = decode_dag_cbor(encoded_bytes)
print(f"Decoded data: {decoded_data}")

# Create a CID from the encoded bytes (defaults to DAG-CBOR codec, sha2-256 multihash)
content_id = CID.from_bytes(encoded_bytes)
print(f"Generated CID: {content_id}")
print(f"CID version: {content_id.version}")
print(f"CID codec: {content_id.codec}")
print(f"CID multihash: {content_id.multihash.name}")

view raw JSON →