API Reference¶
Helper functions¶
- cid.make_cid(*args: str | bytes | int) CIDv0 | CIDv1[source]¶
Creates a
cid.CIDv0orcid.CIDv1object based on the given parametersThe 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:
- Raises:
ValueError – if the number of arguments is not 1 or 3
ValueError – if the only argument passed is not a
stror abyteValueError – 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
codecis not supported bymulticodecValueError – if 3 arguments are passed and the
multihashis notstrorbyteValueError – 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_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:
- 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:
BaseCIDCID 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
- property version: int¶
CID version
- class cid.CIDv1(codec: str, multihash: str | bytes)[source]¶
Bases:
BaseCIDCID 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
- property version: int¶
CID version