diff --git a/cid/cid.py b/cid/cid.py index 07b1bed..6f8ad8b 100644 --- a/cid/cid.py +++ b/cid/cid.py @@ -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 """ diff --git a/newsfragments/66.breaking.rst b/newsfragments/66.breaking.rst new file mode 100644 index 0000000..4390717 --- /dev/null +++ b/newsfragments/66.breaking.rst @@ -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. diff --git a/tests/test_cid.py b/tests/test_cid.py index 9554eb0..da7b6f3 100644 --- a/tests/test_cid.py +++ b/tests/test_cid.py @@ -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):