Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cid/cid.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,15 @@ def buffer(self) -> bytes:
"""
return b"".join([bytes([self.version]), multicodec.add_prefix(self.codec, self.multihash)])

def encode(self, encoding: str | None = "base58btc") -> bytes:
def encode(self, encoding: str | None = "base32") -> bytes:
"""
Encoded version of the raw representation
Encoded version of the raw representation.

The default encoding is ``base32`` (CID specification default for CIDv1).
Callers that need the previous default can pass ``"base58btc"`` explicitly.

:param str encoding: the encoding to use to encode the raw representation,
should be supported by ``py-multibase``
should be supported by ``py-multibase``. Defaults to ``base32``.
:return: encoded raw representation with the given encoding
:rtype: bytes
"""
Expand Down
3 changes: 3 additions & 0 deletions newsfragments/66.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Change CIDv1.encode() default encoding from base58btc to base32 to conform with the CID specification.

This also changes ``str(cid)`` for CIDv1 objects (which uses ``encode()``), and therefore IPLD JSON serialization via ``to_json_dict()`` / ``CIDJSONEncoder``, which emit the string form. Callers that require base58btc must pass ``encode("base58btc")`` explicitly.
4 changes: 2 additions & 2 deletions tests/test_cid.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def test_buffer(self, cid):
assert buffer[1:] == multicodec.add_prefix(self.TEST_CODEC, cid.multihash)

def test_encode_default(self, cid):
"""#encode defaults to base58btc encoding"""
assert cid.encode() == b"zdj7WhuEjrB52m1BisYCtmjH1hSKa7yZ3jEZ9JcXaFRD51wVz"
"""#encode defaults to base32 encoding"""
assert cid.encode() == b"bafybeifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e"

@pytest.mark.parametrize("codec", ENCODINGS)
def test_encode_encoding(self, cid, codec):
Expand Down
Loading