API Reference

Helper functions

cid.make_cid(*args: str | bytes | int) CIDv0 | CIDv1[source]

Creates a cid.CIDv0 or cid.CIDv1 object based on the given parameters

The function supports the following signatures:

make_cid(<base58 encoded multihash CID>) -> CIDv0

make_cid(<multihash CID>) -> CIDv0

make_cid(<multibase encoded multihash CID>) -> CIDv1

make_cid(<version>, <codec>, <multihash>) -> CIDv1

Parameters:

args

  • base58-encoded multihash (str or bytes)

  • multihash (str or bytes)

  • multibase-encoded multihash (str or bytes)

  • version:int, codec(str), multihash(str or bytes)

Returns:

the respective CID object

Return type:

cid.CIDv0 or cid.CIDv1

Raises:
  • ValueError – if the number of arguments is not 1 or 3

  • ValueError – if the only argument passed is not a str or a byte

  • ValueError – if the string provided is not a valid base58 encoded hash

  • ValueError – if 3 arguments are passed and version is not 0 or 1

  • ValueError – if 3 arguments are passed and the codec is not supported by multicodec

  • ValueError – if 3 arguments are passed and the multihash is not str or byte

  • ValueError – if 3 arguments are passed with version 0 and codec is not dag-pb

cid.is_cid(cidstr: str | bytes) bool[source]

Checks if a given input string is valid encoded CID or not. It takes same input as cid.make_cid method with a single argument

Parameters:

cidstr (str or bytes) –

input string which can be a

  • base58-encoded multihash

  • multihash

  • multibase-encoded multihash

Returns:

if the value is a valid CID or not

Return type:

bool

cid.from_string(cidstr: str) CIDv0 | CIDv1[source]

Creates a CID object from a encoded form

Parameters:

cidstr (str) –

can be

  • base58-encoded multihash

  • multihash

  • multibase-encoded multihash

Returns:

a CID object

Return type:

cid.CIDv0 or cid.CIDv1

cid.from_bytes(cidbytes: bytes) CIDv0 | CIDv1[source]

Creates a CID object from a encoded form

Parameters:

cidbytes (bytes) –

can be

  • base58-encoded multihash

  • multihash

  • multibase-encoded multihash

Returns:

a CID object

Return type:

cid.CIDv0 or cid.CIDv1

Raises:

ValueError if the base58-encoded string is not a valid string

Raises:

ValueError if the length of the argument is zero

Raises:

ValueError if the length of decoded CID is invalid

CID classes

class cid.CIDv0(multihash: str | bytes)[source]

Bases: BaseCID

CID version 0 object

CODEC = 'dag-pb'
property buffer: bytes

The raw representation that will be encoded.

Returns:

the multihash

Return type:

bytes

property codec: str

CID codec

encode(encoding: str | None = None) bytes[source]

base58-encoded buffer

Returns:

encoded representation or CID

Return type:

bytes

property multihash: bytes

CID multihash

to_v1() CIDv1[source]

Get an equivalent cid.CIDv1 object.

Returns:

cid.CIDv1 object

Return type:

cid.CIDv1

property version: int

CID version

class cid.CIDv1(codec: str, multihash: str | bytes)[source]

Bases: BaseCID

CID version 1 object

property buffer: bytes

The raw representation of the CID

Returns:

raw representation of the CID

Return type:

bytes

property codec: str

CID codec

encode(encoding: str | None = 'base58btc') bytes[source]

Encoded version of the raw representation

Parameters:

encoding (str) – the encoding to use to encode the raw representation, should be supported by py-multibase

Returns:

encoded raw representation with the given encoding

Return type:

bytes

property multihash: bytes

CID multihash

to_v0() CIDv0[source]

Get an equivalent cid.CIDv0 object.

Returns:

cid.CIDv0 object

Return type:

cid.CIDv0

Raises:

ValueError – if the codec is not ‘dag-pb’

property version: int

CID version