Multiformats
raw JSON → 0.3.1.post4 verified Fri May 01 auth: no python
Python implementation of the multiformats protocols (multihash, multiaddr, multibase, multicodec, CID). Version 0.3.1.post4. Active development; releases every few months.
pip install multiformats Common errors
error KeyError: 'sha2-256' ↓
cause The hash function is not registered because optional dependencies are missing.
fix
Install the required hash extra, e.g., pip install multiformats[sha2].
error ModuleNotFoundError: No module named 'multihash' ↓
cause Attempted to import multihash as a standalone package, but it is a submodule of multiformats.
fix
Use 'from multiformats import multihash' instead.
error TypeError: CID() got an unexpected keyword argument 'codec' ↓
cause Older code used 'codec' parameter; in v0.2.0+ the parameter is 'multicodec'.
fix
Use CID('base32', 'sha2-256', content) – the second positional argument is the multicodec name, not a keyword 'codec'.
Warnings
gotcha The package installs without optional dependencies by default. Required hash functions (e.g., SHA2, SHA3) are not available unless you install multiformats[full] or the specific extra like sha2, sha3, blake2, etc. ↓
fix Run 'pip install multiformats[full]' to get all hash implementations, or install specific extras (e.g., multiformats[sha2]).
breaking In v0.2.0, the API for creating CIDs changed. CID() now expects the multibase code as a string (e.g., 'base32'), not a number. Old code using integer codes will fail. ↓
fix Use string multibase names: CID('base32', 'sha2-256', content). See quickstart.
deprecated The multihash.decode() function returns a namedtuple; accessing fields by index is deprecated in future versions. ↓
fix Use named attributes: mh.code, mh.size, mh.digest instead of mh[0], mh[1], mh[2].
Install
pip install multiformats[full] Imports
- multihash wrong
import multihashcorrectfrom multiformats import multihash - multiaddr wrong
import multiaddrcorrectfrom multiformats import multiaddr - multibase wrong
import multibasecorrectfrom multiformats import multibase - multicodec wrong
import multicodeccorrectfrom multiformats import multicodec - CID wrong
from multiformats.cid import CIDcorrectfrom multiformats import CID
Quickstart
from multiformats import multihash, multibase, CID
# Create a multihash (SHA2-256, 32 bytes)
digest = b'\x12' + b'\x20' + (b'x' * 32)
mh = multihash.decode(digest)
print(f"Hash function: {mh.code}, digest size: {mh.size}")
# Encode to multibase (base58btc)
encoded = multibase.encode('base58btc', digest)
print(f"Encoded: {encoded}")
# Create a CIDv1
cid = CID('base32', 'sha2-256', b'x' * 32)
print(f"CID: {cid}")
# Validate
assert cid.version == 1