• Stars
    star
    415
  • Rank 104,301 (Top 3 %)
  • Language
  • License
    Other
  • Created about 8 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Self-describing content-addressed identifiers for distributed systems

CID (Content IDentifier) Specification

Self-describing content-addressed identifiers for distributed systems

Table of Contents

Motivation

CID is a format for referencing content in distributed information systems, like IPFS. It leverages content addressing, cryptographic hashing, and self-describing formats. It is the core identifier used by IPFS and IPLD. It uses a multicodec to indicate its version, making it fully self describing.

You can read an in-depth discussion on why this format was needed in IPFS here: ipfs/specs#130 (first post reproduced here)

What is it?

A CID is a self-describing content-addressed identifier. It uses cryptographic hashes to achieve content addressing. It uses several multiformats to achieve flexible self-description, namely multihash for hashes, multicodec for data content types, and multibase to encode the CID itself into strings.

Concretely, it's a typed content address: a tuple of (content-type, content-address).

How does it work?

Current version: CIDv1

A CIDv1 has four parts:

<cidv1> ::= <mb><multicodec-cidv1><mc><mh>
# or, expanded:
<cidv1> ::= <multibase-prefix><multicodec-cidv1><multicodec-content-type><multihash-content-address>

Where

  • <multibase-prefix> is a multibase code (1 Unicode codepoint), to ease encoding CIDs into various bases. NOTE: Binary (not text-based) protocols and formats may omit the multibase prefix when the encoding is unambiguous.
  • <multicodec-cidv1> is a multicodec representing the version of CID, here for upgradability purposes.
  • <multicodec-content-type> is a multicodec code representing the content type or format of the data being addressed.
  • <multihash-content-address> is a 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.

That's it!

Design Considerations

CIDs design takes into account many difficult tradeoffs encountered while building IPFS. These are mostly coming from the multiformats project.

  • 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 URIs.
  • 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 base58btc to yield shorter and easily-copy-pastable hashes.
  • Versatility: CIDs are meant to be able to represent values of any format with any cryptographic hash.
  • Avoid Lock-in: CIDs prevent lock-in to old, potentially-outdated decisions.
  • Upgradability: CIDs encode a version to ensure the CID format itself can evolve.

Human Readable CIDs

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:

<hr-cid> ::= <hr-mbc> "-" <hr-cid-mc> "-" <hr-mc> "-" <hr-mh>

Where each sub-component is represented with its own human-readable form:

  • <hr-mbc> is a human-readable multibase code (eg base58btc)
  • <hr-cid-mc> is the string cidv# (eg cidv1 or cidv2)
  • <hr-mc> is a human-readable multicodec code (eg cbor)
  • <hr-mh> is a human-readable multihash (eg sha2-256-256-abcdef0123456789...)

For example:

# example CID
zb2rhe5P4gXftAwvA4eXQ5HJwsER2owDyS9sKaQRRVQPn93bA
# corresponding human readable CID
base58btc - cidv1 - raw - sha2-256-256-6e6ff7950a36187a801613426e858dce686cd7d7e3c0fc42ee0330072d245c95

See: https://cid.ipfs.io/#zb2rhe5P4gXftAwvA4eXQ5HJwsER2owDyS9sKaQRRVQPn93bA

Versions

CIDv0

CIDv0 is a backwards-compatible version, where:

  • the multibase of the string representation is always base58btc and implicit (not written)
  • the multicodec is always dag-pb and implicit (not written)
  • the cid-version is always cidv0 and implicit (not written)
  • the multihash is written as is but is always a full (length 32) sha256 hash.
cidv0 ::= <multihash-content-address>

CIDv1

See the section: How does it work?

<cidv1> ::= <multibase-prefix><multicodec-cidv1><multicodec-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.
  1. 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 == 0x01 (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 == 0x02 (CIDv2), or N == 0x03 (CIDv3), the CID version is reserved.
      • If N is equal to some other multicodec, the CID is malformed.

Implementations

FAQ

Q. I have questions on multicodec, multibase, or multihash.

Please check their repositories: multicodec, multibase, multihash.

Q. Why does CID exist?

We were using base58btc 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: ipfs/specs#130

Q. Is the use of multicodec similar to file extensions?

Yes, kind of! like a file extension, the multicodec 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.

Q. What formats (multicodec codes) does CID support?

We are figuring this out at this time. It will likely be a subset of multicodecs for secure distributed systems. So far, we want to address IPFS's UnixFS and raw blocks (dag-pb, raw), IPNS's libp2p-key, and IPLD's dag-json/dag-cbor formats.

Q. What is the process for updating CID specification (e.g., adding a new version)?

CIDs are a well established standard. IPFS uses CIDs for content-addressing and IPNS. Making changes to such key protocol requires a careful review which should include feedback from implementers and stakeholders across ecosystem.

Due to this, changes to CID specification MUST be submitted as an improvement proposal to ipfs/specs repository (PR with IPIP document), and follow the IPIP process described there.

Maintainers

Captain: @jbenet.

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to IPLD are subject to the IPFS Code of Conduct.

Small note: If editing the README, please conform to the standard-readme specification.

License

This repository is only for documents. These are licensed under a CC-BY 3.0 Unported License Β© 2016 Protocol Labs Inc.

More Repositories

1

multihash

Self describing hashes - for future proofing
Shell
884
star
2

multiformats

The main repository for discussing multiformats.
543
star
3

multiaddr

Composable and future-proof network addresses
Go
421
star
4

multicodec

Compact self-describing codecs. Save space by using predefined multicodec tables.
Python
335
star
5

multibase

Self identifying base encodings
277
star
6

go-multiaddr

Composable and future-proof network addresses
Go
263
star
7

go-multihash

Multihash implementation in Go
Go
234
star
8

js-multiformats

Multiformats interface (multihash, multicodec, multibase and CID)
TypeScript
224
star
9

rust-multihash

multihash implementation in Rust
Rust
150
star
10

js-multihash

multihash implementation in JavaScript
JavaScript
119
star
11

js-multiaddr

JavaScript implementation of multiaddr
TypeScript
109
star
12

js-cid

CID implementation in JavaScript
JavaScript
97
star
13

rust-cid

CID in rust
Rust
86
star
14

rust-multiaddr

multiaddr implementation in rust
Rust
86
star
15

unsigned-varint

unsigned varint in use in multiformat specs
77
star
16

multistream

Make data and streams self-described by prefixing them with human readable codecs.
62
star
17

multistream-select

Friendly protocol negotiation. It enables a multicodec to be negotiated between two entities.
62
star
18

cs-multihash

Multihash implementation in C#
C#
48
star
19

rust-multibase

Multibase in rust
Rust
46
star
20

java-multihash

A Java implementation of Multihash
Java
42
star
21

clj-multihash

Clojure implementation of the Multihash spec
Clojure
40
star
22

go-multistream

an implementation of the multistream protocol in go
Go
39
star
23

java-multibase

A Java implementation of multibase
Java
35
star
24

go-multiaddr-net

DEPRECATED: Please use the "net" subpackage in https://github.com/multiformats/go-multiaddr.
Go
34
star
25

go-multicodec

Go constants for the multicodec table
Go
34
star
26

py-multiaddr

multiaddr implementation in Python
Python
33
star
27

cid-utils-website

A website for decoding CIDs
HTML
33
star
28

go-multibase

Implementation of multibase parser in go
Go
32
star
29

js-multihashing-async

The fast version of js-multihashing
JavaScript
29
star
30

multigram

Protocol negotiation and multiplexing over datagrams
29
star
31

js-multistream-select

JavaScript implementation of multistream-select
JavaScript
29
star
32

go-multiaddr-dns

Go library and CLI tool for /dns4, /dns6, /dnsaddr multiaddr resolution
Go
28
star
33

haskell-multihash

Multihash Haskell implementation
Haskell
27
star
34

specs

Specification work regarding multihash, multiaddr, and others
26
star
35

ex_multihash

Multihash implementation in Elixir
Elixir
24
star
36

js-multibase

JavaScript implementation of the multibase specification
JavaScript
23
star
37

py-multibase

Multibase implementation in Python
Python
22
star
38

ruby-multihash

A simple multihash (https://github.com/multiformats/multihash) implementation for ruby.
Ruby
22
star
39

js-multicodec

JavaScript implementation of the multicodec specification
JavaScript
21
star
40

website

The multiformats website
HTML
20
star
41

py-multicodec

Multicodec implementation in Python
Python
17
star
42

cs-multibase

Multibase implementation in C#
C#
16
star
43

java-multiaddr

Java implementation of multiaddr
Java
15
star
44

js-mafmt

javascript multiaddr validation
TypeScript
15
star
45

py-multihash

Multihash implementation in Python
Python
14
star
46

SwiftMultihash

Swift implementation of multihash
Swift
14
star
47

c-multihash

C implementation of Multihash parsing and encoding (but not hashing)
C
12
star
48

js-multihashing

Use all the functions in multihash.
JavaScript
11
star
49

php-multihash

PHP implementation of multihash
PHP
10
star
50

scala-multihash

Scala multihash implementation
Scala
9
star
51

js-cid-tool

A module and command line tool for converting, formatting and discovering properties of CIDs
JavaScript
8
star
52

SwiftMultiaddr

A Multiaddr implementation in Swift.
Swift
8
star
53

cs-multiaddress

Multiaddress implementation in C#
C#
8
star
54

js-multiaddr-to-uri

Convert a Multiaddr to a URI /dnsaddr/ipfs.io/http -> http://ipfs.io
TypeScript
7
star
55

go-base36

Go
7
star
56

MultiHash.Net

.Net implementation of multihash
PowerShell
7
star
57

go-multigram

Go implementation of multigram
6
star
58

go-varint

Go
6
star
59

clj-multistream

Clojure implementation of multistream codecs
Clojure
6
star
60

haskell-multibase

haskell implementation of the multibase multiformat (project by protocol labs)
Haskell
6
star
61

haskell-multicodec

An implementation of the multicodec specification in haskell.
Haskell
5
star
62

clj-varint

Simple wrapper around Bazel VarInt code.
Java
4
star
63

go-multiaddr-fmt

A declarative validator for multiaddrs.
Go
4
star
64

js-sha3

Multiformats hash functions for SHA3
JavaScript
4
star
65

go-multicodec-packed

DEPRECATED -- see go-multicodec
Go
4
star
66

ma-pipe

multiaddr powered pipes
Go
4
star
67

js-uri-to-multiaddr

Convert a URI to a Multiaddr: https://protocol.ai -> /dns4/protocol.ai/tcp/443/https
TypeScript
3
star
68

js-murmur3

Multiformats hash functions for MurmurHash3
JavaScript
3
star
69

js-blake2

BLAKE2 multihash hashers for JavaScript multiformats
JavaScript
3
star
70

cs-multicodec

Multicodec implementation i C#
C#
3
star
71

c-multihashing

Use all the functions in multihash, in C
3
star
72

docs

Multiformats documentation website
2
star
73

cs-multistream

Multistream
C#
2
star
74

github-mgmt

TypeScript
1
star
75

js-multicodec-table

@multiformats/multicodec-table a JavaScript form of the current multicodec table
1
star
76

js-dns

Resolve DNS queries with browser fallback
TypeScript
1
star