Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the spec from the implementation. #14

Merged
merged 2 commits into from
May 12, 2018
Merged
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
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ CIDv0 is a backwards-compatible version, where:
- the `multibase` is always `base58btc` and implicit (not written)
- the `multicodec` is always `protobuf-mdag` and implicit (not written)
- the `cid-version` is always `cidv0` and implicit (not written)
- the `multihash` is written as is.
- the `multihash` is written as is but is always a full (length 32) sha256 hash.

```
cidv0 ::= <multihash-content-address>
Expand All @@ -103,6 +103,28 @@ See the section: [How does it work? - Protocol Description](#how-does-it-work-pr
<cidv1> ::= <multibase-prefix><cid-version><multicodec-packed-content-type><multihash-content-address>
```

## Decoding Algorithm

To decode a CID, follow the following algorithm:

1. If it's a string (ASCII/UTF-8):
* If it is 46 characters long and starts with `Qm...`, it's a CIDv0. Decode it as base58btc and continue to step 2.
* Otherwise, decode it according to the multibase spec and:
* If the first decoded byte is 0x12, return an error. CIDv0 CIDs may not be multibase encoded and there will be no CIDv18 (0x12 = 18) to prevent ambiguity with decoded CIDv0s.
* Otherwise, you now have a binary CID. Continue to step 2.
2. Given a (binary) CID (`cid`):
* 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.
* If `N == 1` (CIDv1):
* 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 <= 0`, the CID is malformed.
* If `N > 1`, the CID version is reserved.

## Implementations

- [go-cid](https://github.com/ipfs/go-cid)
Expand Down