项目作者: nocursor

项目描述 :
Elixir library for creating self-describing content-addressed identifiers for distributed systems (CIDs).
高级语言: Elixir
项目地址: git://github.com/nocursor/ex-cid.git
创建时间: 2018-11-22T14:00:31Z
项目社区:https://github.com/nocursor/ex-cid

开源协议:MIT License

下载


Elixir CID

Elixir version of CID. CID is currently being used as part of IPFS for identifying distributed content.

Self-describing content-addressed identifiers for distributed systems

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.

Features

  • Encode/Decode CIDs
    • Encode a CID to a string
    • Encode a CID buffer to later Multibase encode
    • Decode a CID string to a CID and optionally, the Multibase encoding used
  • Human readable CIDs for debugging and checking
  • Error handling and exception versions of most major API functions
  • Current support for all Multibase and Multicodec encodings
  • Immutable Elixir struct for CID data
    • Easy comparisons, construction, validation, etc.
    • Send over the wire
    • Simple debugging and inspecting
  • Consistent API
  • Support for CID v0 and CID v1
  • Tests

Usage

The following examples show the basic usage of using this library to encode, decode, and introspect CIDs.

Note: that in the case of codecs (Multicodec), multihashes (Multihash), and encodings (Multibase), we may select values for demonstration purposes. In other words, some usage may not match reality - pragmatic examples are the goal.

Read more about Multicodec, Multihash, and Multibase to learn more.

Create a CID

  1. # first let's create a Multihash to use for our examples
  2. {:ok, multihash} = Multihash.encode(:sha2_256, :crypto.hash(:sha256, "like common people"))
  3. {:ok,
  4. <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92, 130, 235, 240,
  5. 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153, 175, 31, 65,
  6. 149>>}
  7. # the default version is the latest (CID v1) and the default codec is "dag-pb" which is used for CID v0
  8. CID.cid(multihash)
  9. {:ok,
  10. %CID{
  11. codec: "dag-pb",
  12. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  13. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  14. 175, 31, 65, 149>>,
  15. version: 1
  16. }}
  17. # we can also use an exception raising version
  18. # let's also change the codec too and explicitly pass the version
  19. CID.cid!(multihash, "cbor", 1)
  20. %CID{
  21. codec: "cbor",
  22. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  23. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  24. 175, 31, 65, 149>>,
  25. version: 1
  26. }
  27. # if for some reason we want a CID v0 ID, we can do it like so
  28. {:ok, v0_multihash} = Multihash.encode(:sha2_256, :crypto.hash(:sha256, "like common people"), 32)
  29. {:ok,
  30. <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92, 130, 235, 240,
  31. 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153, 175, 31, 65,
  32. 149>>}
  33. CID.cid!(v0_multihash, "dag-pb", 0)
  34. %CID{
  35. codec: "dag-pb",
  36. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  37. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  38. 175, 31, 65, 149>>,
  39. version: 0
  40. }
  41. # thankfully if we mess up, our lib stops us before catastrophe strikes
  42. CID.cid(v0_multihash, "cbor", 0)
  43. {:error, :invalid_multicodec}
  44. CID.cid(multihash, "barrel of feet", 1)
  45. {:error, :invalid_multicodec}

Encode a CID

Now let’s actually encode a CID to a string, for use in an application perhaps like IPFS:

  1. CID.cid!(multihash, "dag-json", 1) |> CID.encode()
  2. {:ok, "z4EBG9jACDDa7TeGCBUUk4ziuRsGmGnjefMx5GEofptsFxTaH8Q"}
  3. # we also have our exception raising version
  4. CID.cid!(multihash, "cbor", 1) |> CID.encode!()
  5. "zadi4ekZWb9uwiPgSM1Fki5UAfvggkGC27Qy6acaYZ49jycsv"
  6. # and we can do the same for a v0 CID
  7. CID.cid!(v0_multihash, "dag-pb", 0) |> CID.encode()
  8. {:ok, "QmYR6e57B15cM77ankTPqxsQVbFVzcYSGrFGr5WtmR8jdn"}
  9. # we can only encode a v0 CID with :base58_btc
  10. CID.cid!(v0_multihash, "dag-pb", 0) |> CID.encode(:base64)
  11. {:error, :invalid_encoding}
  12. # with CID v1 we can control the encoding - let's choose :base64
  13. CID.cid!(multihash, "dag-pb", 1) |> CID.encode!(:base64)
  14. "mAXASIJW4gxsH5vZxGi3rXILr8Fhj0K2zMchrK63Ip5mvH0GV"
  15. # if we just want the buffer, we can also do this
  16. CID.cid!(multihash, "cbor", 1) |> CID.encode_buffer()
  17. {:ok,
  18. <<1, 81, 18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92, 130,
  19. 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153, 175,
  20. 31, 65, 149>>}

Decode a CID

Decode a CID string:

  1. # let's decode some of our earlier strings
  2. CID.decode("zadi4ekZWb9uwiPgSM1Fki5UAfvggkGC27Qy6acaYZ49jycsv")
  3. # notice we also get the Multibase used
  4. {:ok,
  5. {%CID{
  6. codec: "cbor",
  7. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  8. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167,
  9. 153, 175, 31, 65, 149>>,
  10. version: 1
  11. }, :base58_btc}}
  12. # we have our exception raising version again
  13. CID.decode!("zadi4ekZWb9uwiPgSM1Fki5UAfvggkGC27Qy6acaYZ49jycsv")
  14. {%CID{
  15. codec: "cbor",
  16. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  17. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  18. 175, 31, 65, 149>>,
  19. version: 1
  20. }, :base58_btc}
  21. # we can also get only the CID, exception raising version available
  22. CID.decode_cid!("mAXASIJW4gxsH5vZxGi3rXILr8Fhj0K2zMchrK63Ip5mvH0GV")
  23. %CID{
  24. codec: "dag-pb",
  25. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  26. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  27. 175, 31, 65, 149>>,
  28. version: 1
  29. }
  30. # fortunately our error handling versions have some sanity checks too
  31. CID.decode_cid("Why would a company named Franco American make Italian food?")
  32. {:error, "unable to decode CID string"}
  33. # let's tamper with a string by adding "Q" to prove it
  34. CID.decode("QmAXASIJW4gxsH5vZxGi3rXILr8Fhj0K2zMchrK63Ip5mvH0GV")
  35. {:error, "unable to decode CID string"}

Human Readable CID Strings

Sometimes we want to better understand exactly what is in our CID. Fortunately, we can humanize a CID string like so:

  1. # help me make sense of all this
  2. CID.humanize("zadi4ekZWb9uwiPgSM1Fki5UAfvggkGC27Qy6acaYZ49jycsv")
  3. {:ok,
  4. "base58_btc - CIDv1 - cbor - sha2_256 - 95b8831b07e6f6711a2deb5c82ebf05863d0adb331c86b2badc8a799af1f4195"}
  5. # a custom separator
  6. CID.humanize("z4EBG9jACDDa7TeGCBUUk4ziuRsGmGnjefMx5GEofptsFxTaH8Q", ":")
  7. {:ok,
  8. "base58_btc:CIDv1:dag-json:sha2_256:95b8831b07e6f6711a2deb5c82ebf05863d0adb331c86b2badc8a799af1f4195"}
  9. # we can get sassy too with our own custom separators
  10. CID.humanize("QmYR6e57B15cM77ankTPqxsQVbFVzcYSGrFGr5WtmR8jdn", "\_(ツ)_/¯")
  11. {:ok,
  12. "base58_btc_(ツ)_/¯CIDv0_(ツ)_/¯dag-pb_(ツ)_/¯sha2_256_(ツ)_/¯95b8831b07e6f6711a2deb5c82ebf05863d0adb331c86b2badc8a799af1f"}

CID Validity

What if want to know whether or not a CID is valid? No problem:

  1. # vindication
  2. CID.cid?("z4EBG9jACDDa7TeGCBUUk4ziuRsGmGnjefMx5GEofptsFxTaH8Q")
  3. true
  4. # sadly, not a valid ID
  5. CID.cid?("I want to persuade you that I am an ID")
  6. false

Convert a CID

If we have some need to convert CID representations, we can do it. This is more useful in the long-term. A typical use-case is to convert a CID v0 to a CID v1.

The simplest route would just be to decode and re-encode, however if we are still working with our struct or if we already decoded it, we have a simple option:

  1. CID.cid!(multihash, "dag-pb", 0) |> CID.to_version(1)
  2. {:ok,
  3. %CID{
  4. codec: "dag-pb",
  5. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  6. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  7. 175, 31, 65, 149>>,
  8. version: 1
  9. }}
  10. # we can't convert if we break the rules though of a version, such as CID v1 -> CID v0
  11. CID.cid!(multihash, "dag-cbor", 1) |> CID.to_version(0)
  12. {:error, :unsupported_conversion}
  13. # we can convert from CID v1 to CID v0 though if we follow the rules
  14. CID.cid!(v0_multihash, "dag-pb", 1) |> CID.to_version(0)
  15. # notice it works because the codec was "dag-pb"
  16. {:ok,
  17. %CID{
  18. codec: "dag-pb",
  19. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  20. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  21. 175, 31, 65, 149>>,
  22. version: 0
  23. }}

Summary - Encode and Decode Round-Trip

Finally, to review let’s round trip some data:

  1. # take a CID v1, encode it, then decode it back again
  2. CID.cid!(multihash, "dag-pb", 1) |> CID.encode!() |> CID.decode!()
  3. {%CID{
  4. codec: "dag-pb",
  5. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  6. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  7. 175, 31, 65, 149>>,
  8. version: 1
  9. }, :base58_btc}
  10. # changing the default encoding while round tripping
  11. CID.cid!(multihash, "cbor") |> CID.encode!(:base32_z) |> CID.decode!()
  12. {%CID{
  13. codec: "cbor",
  14. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  15. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  16. 175, 31, 65, 149>>,
  17. version: 1
  18. }, :base32_z}
  19. # round-tripping a v0 CID with some options
  20. CID.cid!(v0_multihash, "dag-pb", 0) |> CID.encode!() |> CID.decode_cid!()
  21. %CID{
  22. codec: "dag-pb",
  23. multihash: <<18, 32, 149, 184, 131, 27, 7, 230, 246, 113, 26, 45, 235, 92,
  24. 130, 235, 240, 88, 99, 208, 173, 179, 49, 200, 107, 43, 173, 200, 167, 153,
  25. 175, 31, 65, 149>>,
  26. version: 0
  27. }

Installation

Available in Hex. The package can be installed by adding cid to your list of dependencies in mix.exs:

  1. def deps do
  2. [
  3. {:cid, "~> 0.0.1"}
  4. ]
  5. end

API Documentation can be found at https://hexdocs.pm/cid.

Issues

In general, most questions are best answered via the official CID project and by its members.

If you have Elixir specific issues, please open an issue here. Do not open Multibase, Multicodec, Multihash, or generic CID issues here.

You can also check the FAQ in the docs folder.

Acknowledgments