py-cid: Self-describing content-addressed identifiers

0.5.0 · active · verified Thu Apr 16

py-cid is a Python library that provides an implementation of the Content Identifier (CID) specification. CIDs are self-describing content-addressed identifiers used in distributed systems like IPFS and IPLD, leveraging cryptographic hashing and multiformats for flexible self-description. The library is actively maintained, with version 0.5.0 being the latest release, supporting Python 3.10 and newer.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create CIDv0 and CIDv1 objects using `make_cid` from both base58 and multibase encoded strings. It also shows how to access their version, codec, multihash, and how to encode CIDv1 into different multibase formats.

from cid import make_cid, CIDv0, CIDv1

# Create a CIDv0 from a base58-encoded multihash
cid_v0_str = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'
cid0 = make_cid(cid_v0_str)
print(f"CIDv0: {cid0}")
print(f"Version: {cid0.version}, Codec: {cid0.codec}, Multihash: {cid0.multihash.hex()}")

# Create a CIDv1 from a multibase-encoded CID string
cid_v1_str = 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3fzmsyqhwfsgr2zfdnc2dsxze'
cid1 = make_cid(cid_v1_str)
print(f"CIDv1: {cid1}")
print(f"Version: {cid1.version}, Codec: {cid1.codec}, Multihash: {cid1.multihash.hex()}")

# Accessing components
print(f"CIDv0 string: {str(cid0)}")
print(f"CIDv1 encoded in base32: {cid1.encode('base32')}") # requires py-multibase

view raw JSON →