|
| 1 | +# CID - Content IDentifier |
| 2 | + |
| 3 | +[](http://ipn.io) |
| 4 | +[](http://github.com/ipld/ipld) |
| 5 | +[](http://webchat.freenode.net/?channels=%23ipfs) |
| 6 | + |
| 7 | +> self-describing content-addressed identifiers for distributed systems |
| 8 | +
|
| 9 | + |
| 10 | +<!-- |
| 11 | +
|
| 12 | +## Table of Contents |
| 13 | +
|
| 14 | +TODO |
| 15 | +
|
| 16 | +--> |
| 17 | + |
| 18 | +## Motivation |
| 19 | + |
| 20 | +[**CID**](https://github.com/ipld/cid) is a format for referencing content in distributed information systems, like [IPFS](https://ipfs.io). It leverages [content addressing](https://en.wikipedia.org/wiki/Content-addressable_storage), [cryptographic hashing](https://simple.wikipedia.org/wiki/Cryptographic_hash_function), and [self-describing formats](https://github.com/multiformats/multiformats). It is the core identifier used by [IPFS](https://ipfs.io) and [IPLD](https://ipld.io). |
| 21 | + |
| 22 | + |
| 23 | +## How does it work? - Protocol Description |
| 24 | + |
| 25 | +CID is a self-describing content-addressed identifier. It uses cryptographic hashes to achieve content addressing. It uses several [multiformats](https://github.com/multiformats/multiformats) to achieve flexible self-description, namely [multihash](https://github.com/multiformats/multihash) for hashes, [multicodec-packed](https://github.com/multiformats/multicodec/blob/master/multicodec-packed.md) for data content types, and [multibase](https://github.com/multiformats/multibase) to encode the CID itself into strings. |
| 26 | + |
| 27 | +Current version: CIDv1 |
| 28 | + |
| 29 | +A CIDv1 has four parts: |
| 30 | + |
| 31 | +```sh |
| 32 | +<cidv1> ::= <mb><version><mcp><mh> |
| 33 | +# or, expanded: |
| 34 | +<cidv1> ::= <multibase-prefix><cid-version><multicodec-packed-content-type><multihash-content-address> |
| 35 | +``` |
| 36 | +Where |
| 37 | + |
| 38 | +- `<multibase-prefix>` is a [multibase](https://github.com/multiformats/multibase) code (1 or 2 bytes), to ease encoding CIDs into various bases. |
| 39 | +- `<cid-version>` is a [varint](https://github.com/multiformats/unsigned-varint) representing the version of CID, here for upgradability purposes. |
| 40 | +- `<multicodec-packed-content-type> is a [multicodec-packed](https://github.com/multiformats/multicodec/blob/master/multicodec-packed.md) code representing the content type or format of the data being addressed. |
| 41 | +- `<multihash-content-address>` is a [multihash](https://github.com/multiformats/multihash) value, representing the cryptographic hash of the content being addressed. Multihash enables CIDs to use many different cryptographic hash function, for upgradability and protocol agility purposes. |
| 42 | + |
| 43 | +That's it! |
| 44 | + |
| 45 | +## Design Considerations |
| 46 | + |
| 47 | +CIDs design takes into account many difficult tradeoffs encountered while building [IPFS](https://ipfs.io). These are mostly coming from the multiformats project. |
| 48 | + |
| 49 | +- Compactness: CIDs are binary in nature to ensure these are as compact as possible, as they're meant to be part of longer path identifiers or URLs. |
| 50 | +- Transport friendliness (or "copy-pastability"): CIDs are encoded with multibase to allow choosing the best base for transporting. For example, CIDs can be encoded into base58 to yield shorter and easily-copy-pastable hashes. |
| 51 | +- Versatility: CIDs are meant to be able to represent values of any format with any cryptographic hash. |
| 52 | +- Avoid Lock-in: CIDs prevent lock-in to old, potentially-outdated decisions. |
| 53 | +- Upgradability: CIDs encode a version to ensure the CID format itself can evolve. |
| 54 | + |
| 55 | +## Human Readable CIDs |
| 56 | + |
| 57 | +It is advantageous to have a human readable description of a CID, solely for the purposes of debugging and explanation. We can easily transform a CID to a "Human Readable CID" as follows: |
| 58 | + |
| 59 | +``` |
| 60 | +<hr-cid> ::= <hr-mbc> "-" <hr-cid-version> "-" <hr-mcp> "-" <hr-mh> |
| 61 | +``` |
| 62 | +Where each sub-component is represented with its own human-readable form: |
| 63 | + |
| 64 | +- `<hr-mbc>` is a human-readable multibase code (eg `base58`) |
| 65 | +- `<hr-cid-version>` is the string `cidv#` (eg `cidv1` or `cidv2`) |
| 66 | +- `<hr-mcp>` is a human-readable multicodec-packed code (eg `cbor`) |
| 67 | +- `<hr-mh>` is a human-readanble multihash (eg `sha2-256-256-abcdef0123456789...`) |
| 68 | + |
| 69 | +For example: |
| 70 | + |
| 71 | +``` |
| 72 | +# TODO example |
| 73 | +# example CID |
| 74 | +# corresponding human readable CID |
| 75 | +``` |
| 76 | + |
| 77 | +## Versions |
| 78 | + |
| 79 | +### CIDv0 |
| 80 | + |
| 81 | +CIDv0 is a backwards-compatible version, where: |
| 82 | +- the `multibase` is always `base58` and implicit (not written) |
| 83 | +- the `multicodec` is always `protobuf-mdag` and implicit (not written) |
| 84 | +- the `cid-version` is always `cidv0` and implicit (not written) |
| 85 | +- the `multihash` is written as is. |
| 86 | + |
| 87 | +``` |
| 88 | +cidv0 ::= <multihash-content-address> |
| 89 | +``` |
| 90 | + |
| 91 | +### CIDv1 |
| 92 | + |
| 93 | +See the section: [How does it work? - Protocol Description](#how-does-it-work-protocol-description) |
| 94 | + |
| 95 | +``` |
| 96 | +<cidv1> ::= <multibase-prefix><cid-version><multicodec-packed-content-type><multihash-content-address> |
| 97 | +``` |
| 98 | + |
| 99 | +## Implementations |
| 100 | + |
| 101 | +- [go-cid](https://github.com/ipld/go-cid) |
| 102 | +- [js-cid](https://github.com/ipld/js-cid) |
| 103 | +- [Add yours today!](https://github.com/multiformats/multicodec/edit/master/multicodec-packed.md) |
| 104 | + |
| 105 | +## FAQ |
| 106 | + |
| 107 | +> **Q. I have questions on multicodec, multibase, or multihash.** |
| 108 | +
|
| 109 | +Please check their repositories: [multicodec](https://github.com/multiformats/multicodec), [multibase](https://github.com/multiformats/multibase), [multihash](https://github.com/multiformats/multihash). |
| 110 | + |
| 111 | +> **Q. Why does CID exist?** |
| 112 | +
|
| 113 | +We were using base58 encoded multihashes in IPFS, and then we needed to switch formats to IPLD. We struggled with lots of problems of addressing data with different formats until we created CIDs. You can read the history of this format here: https://github.com/ipfs/specs/issues/130 |
| 114 | + |
| 115 | +> **Q. Is the use of multicodec-packed similar to file extensions?** |
| 116 | +
|
| 117 | +Yes, kind of! like a file extension, the multicodec-packed identifier establishes the format of the data. Unlike file extensions, these are in the middle of the identifier and not meant to be changed by users. There is also a short table of supported formats. |
| 118 | + |
| 119 | +> **Q. What formats (multicodec-packed codes) does CID support?** |
| 120 | +
|
| 121 | +We are figuring this out at this time. It will likely be a table of formats for secure distributed systems. So far, we want to address: IPFS's original protobuf format, the new IPLD CBOR format, git, bitcoin, and ethereum objects. |
| 122 | + |
| 123 | +## Maintainers |
| 124 | + |
| 125 | +Captain: [@jbenet](https://github.com/jbenet). |
| 126 | + |
| 127 | +## Contribute |
| 128 | + |
| 129 | +Contributions welcome. Please check out [the issues](https://github.com/multiformats/multicodec/issues). |
| 130 | + |
| 131 | +Check out our [contributing document](https://github.com/multiformats/multiformats/blob/master/contributing.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md). |
| 132 | + |
| 133 | +## License |
| 134 | + |
| 135 | +[MIT](LICENSE) |
0 commit comments