Skip to content

Spec for CIDs#530

Open
darobin wants to merge 4 commits intomainfrom
cid
Open

Spec for CIDs#530
darobin wants to merge 4 commits intomainfrom
cid

Conversation

@darobin
Copy link
Contributor

@darobin darobin commented Mar 12, 2026

No description provided.

@darobin darobin requested a review from lidel March 12, 2026 09:58
@github-actions
Copy link

github-actions bot commented Mar 12, 2026

🚀 Build Preview on IPFS ready

Robin Berjon and others added 3 commits March 12, 2026 11:58
Co-authored-by: Volker Mische <volker.mische@gmail.com>
Comment on lines +149 to +153
* If it's 34 bytes long with the leading bytes `[0x12, 0x20, ...]`, it's a CIDv0.
* The CID's multihash is `cid`.
* The CID's multicodec is DagProtobuf
* The CID's version is 0.
* Otherwise, let `N` be the first varint in `cid`. This is the CID's version.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using byte length anywhere in the binary decoding path isn't ideal and not how we do it in practice so I wouldn't want encourage this.

What we actually do, for both binary paths, is read the first bytes as a varint, which tells us what to do next: if the value is 0x12 then that's sha2-256 and this is a CIDv0 cause we've discovered the multihash and we proceed accordingly, if the value is 0x01 then it's a CIDv1 and we proceed accordingly.

One minor edge is that the JS decoder will also read the value 0x00 and decode the rest as a CIDv0 but that's probably not a good idea.

* The CID's multicodec is the second varint in `cid`
* The CID's multihash is the rest of the `cid` (after the second varint).
* The CID's version is 1.
* If `N == 0x02` (CIDv2), or `N == 0x03` (CIDv3), the CID version is reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to be said? if version isn't 0x01 then it's malformed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants