Skip to content

Commit

Permalink
Update the spec from the implementation.
Browse files Browse the repository at this point in the history
1. CIDv0 only supports SHA256 multihashes.
2. In CIDv0, the multibase *can* be specified but defaults to base58btc.

This commit also describes the proper algorithm for decoding CIDs as it's
non-obvious.

Fixes #11
  • Loading branch information
Stebalien committed Aug 30, 2017
1 parent 5b8255f commit b3215dc
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ For example:
### CIDv0

CIDv0 is a backwards-compatible version, where:
- the `multibase` is always `base58btc` and implicit (not written)
- the `multibase` defaults to `base58btc` (if 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,26 @@ 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), decode it into raw bytes:
* If it is 46 characters long starts with `Qm...`, it's a CIDv0 without a multibase prefix. Decode it as base58btc.
* Otherwise, decode it according to the multibase spec.
2. Given the decoded (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

0 comments on commit b3215dc

Please sign in to comment.