Content IDs (CIDs)

date2024-12-11
editorsRobin Berjon <robin@berjon.com>
Juan Caballero <bumblefudge@learningproof.xyz>
issueslist, new
abstract

DASL CIDs are a simple structured identifier format for content addressing. They encapsulate a hash with enough metadata to be extensible (to add new hash types in the future) and to indicate whether they are pointing to raw bytes or to structured data.

Introduction

DASL CIDs are a simple structured identifier format for content addressing. They encapsulate a hash with enough metadata to be extensible (to add new hash types in the future) and to indicate whether they are pointing to raw bytes or to structured data. If you're simply DASL CIDs as identifiers, you can almost certainly just use the string as an opaque ID and worry no further.

A DASL CID can be represented as a string or as an array of bytes. If you wish to understand the internals of a CID, it has the following structure:

  1. A b prefix (only in string form). This is an extensibility point for future CID encodings other than the current base32 to be supported. (Currently this is the only one.)
  2. A version number, which is currently always 1.
  3. A content codec, which is a flag indicating whether it is pointing to structured or raw data.
  4. A hash type, that is always SHA-256 ([sha256]).
  5. A hash size, indicating how many bytes long the digest is.
  6. A digest, which is the hash of the content being identified.

Parsing CIDs

Use the following steps to parse a CID string:

  1. Accept a string CID.
  2. Remove the first character from CID and store it in prefix.
  3. If prefix is not equal to b, throw an error.
  4. Decode the rest of CID using the base32 algorithm from RFC4648 with a lowercase alphabet and store the result in CID bytes ([rfc4648]).
  5. Return the result of applying the steps to decode a CID to CID bytes.

Use the following steps to parse a binary CID:

  1. Accept an array of bytes binary CID.
  2. Remove the first byte in binary CID and store it in prefix.
  3. If prefix is not equal to 0 (a null byte, the binary base256 prefix), throw an error.
  4. Store the rest of binary CID in CID bytes.
  5. Return the result of applying the steps to decode a CID to CID bytes.

Use the following steps to decode a CID:

  1. Accept an array of bytes CID bytes.
  2. Remove the first byte in CID bytes and store it in version.
  3. If version is not equal to 1, throw an error.
  4. Remove the next byte in CID bytes and store it in codec.
  5. If codec is not equal to 0x55 (raw) or 0x71 (dCBOR42), throw an error ([dcbor42]).
  6. Remove the next two bytes in CID bytes and store them in hash type and hash size, respectively.
  7. If hash type is not equal to 0x12 (SHA-256), throw an error ([sha256]).
  8. If the number of bytes left in CID bytes differs from hash size, throw an error. Store the remaining CID bytes in digest.
  9. Return version, codec, hash type, hash size, and digest.

Relationship to IPFS

You don't need to understand IPFS in order to use DASL. This section is for informational purposes only.

DASL CIDs are a strict subset of IPFS CIDs with the following properties:

References

[dcbor42]
Robin Berjon & Juan Caballero. Deterministic CBOR with tag 42 (dCBOR42). 2024-12-11. URL: https://dasl.ing/dcbor42.html
[rfc4648]
S. Josefsson. The Base16, Base32, and Base64 Data Encodings. October 2006. URL: https://www.rfc-editor.org/rfc/rfc4648
[sha256]
National Institute of Standards and Technology, Secure Hash Algorithm. NIST FIPS 180-2. August 2002.