Skip to content

CIDv1 default encoding is base58btc instead of base32 #66

Description

@sumanjeet0012

CIDv1.encode() defaults to base58btc encoding. The CID specification recommends base32 as the default encoding for CIDv1 strings. Go's Cid.String() uses base32 for CIDv1.

Problem

In cid/cid.py:

class CIDv1(BaseCID):
    def encode(self, encoding: str | None = "base58btc") -> bytes:
        return multibase.encode(encoding, self.buffer)
cid = CIDv1("dag-pb", some_hash)
str(cid)  # → "zdj7Wh..." (base58btc)

Go's implementation:

func (c Cid) String() string {
    switch c.Version() {
    case 0:
        return c.Hash().B58String()
    case 1:
        mbstr, _ := mbase.Encode(mbase.Base32, c.Bytes())
        return mbstr  // → "bafybei..." (base32)
    }
}

The CID spec says:

"The default base encoding for CIDv1 is base32 (lowercase, no padding)."

Proposed Solution

Change the default encoding to base32:

class CIDv1(BaseCID):
    def encode(self, encoding: str | None = "base32") -> bytes:
        return multibase.encode(encoding, self.buffer)

⚠️ Breaking change: This changes the output of str(cid) for all CIDv1 objects. Consider:

  • Adding a deprecation warning for one release cycle
  • Or documenting this as a breaking change in the changelog

Update all tests that check CIDv1 string representations.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions