Exosphere.ATProto.CID (Exosphere v0.6.0)

Copy Markdown View Source

Content Identifier (CID) handling for Exosphere.ATProto.

CIDs are self-describing content-addressed identifiers used throughout Exosphere.ATProto for referencing data objects (DAG-CBOR) and blobs (raw bytes).

Exosphere.ATProto CID Requirements

Exosphere.ATProto uses a specific "blessed" CID format:

  • CIDv1
  • Multibase: base32 for string encoding, binary for DAG-CBOR
  • Multicodec: dag-cbor (0x71) for data objects, raw (0x55) for blobs
  • Multihash: sha-256 (0x12) with 256 bits

Examples

# Create a CID from data
iex> {:ok, cid} = Exosphere.ATProto.CID.create(%{"hello" => "world"})
iex> to_string(cid)
"bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae"

# Parse a CID string
iex> {:ok, cid} = Exosphere.ATProto.CID.decode("bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae")
iex> cid.codec
:dag_cbor

# Create a CID for a blob
iex> {:ok, cid} = Exosphere.ATProto.CID.create_raw(<<binary_data>>)
iex> cid.codec
:raw

Summary

Functions

Create a CID for a DAG-CBOR encoded term.

Create a CID for a DAG-CBOR term, raising on error.

Create a CID for raw binary data (blobs).

Create a CID for raw binary data, raising on error.

Check if a CID uses the dag-cbor codec.

Decode a CID from its string representation.

Decode a CID string, raising on error.

Encode a CID to its string representation using base32.

Create a CID from raw bytes (without multibase prefix).

Create a CID from raw bytes, raising on error.

Check if a CID uses the raw codec.

Convert a CID to its raw byte representation (without multibase prefix).

Types

t()

@type t() :: %Exosphere.ATProto.CID{
  codec: :dag_cbor | :raw,
  hash: binary(),
  version: 1
}

Functions

create(term)

@spec create(term()) :: {:ok, t()} | {:error, term()}

Create a CID for a DAG-CBOR encoded term.

The term is encoded with Exosphere.ATProto.CBOR.encode/1 and hashed with SHA-256.

Examples

iex> {:ok, cid} = Exosphere.ATProto.CID.create(%{"foo" => "bar"})
iex> cid.codec
:dag_cbor

create!(term)

@spec create!(term()) :: t()

Create a CID for a DAG-CBOR term, raising on error.

create_raw(data)

@spec create_raw(binary()) :: {:ok, t()}

Create a CID for raw binary data (blobs).

Examples

iex> {:ok, cid} = Exosphere.ATProto.CID.create_raw(<<1, 2, 3>>)
iex> cid.codec
:raw

create_raw!(data)

@spec create_raw!(binary()) :: t()

Create a CID for raw binary data, raising on error.

dag_cbor?(arg1)

@spec dag_cbor?(t()) :: boolean()

Check if a CID uses the dag-cbor codec.

decode(arg1)

@spec decode(String.t()) :: {:ok, t()} | {:error, term()}

Decode a CID from its string representation.

Supports base32-encoded CIDv1 strings (prefix 'b').

Examples

iex> Exosphere.ATProto.CID.decode("bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae")
{:ok, %Exosphere.ATProto.CID{...}}

decode!(string)

@spec decode!(String.t()) :: t()

Decode a CID string, raising on error.

encode(cid)

@spec encode(t()) :: String.t()

Encode a CID to its string representation using base32.

from_bytes(arg1)

@spec from_bytes(binary()) :: {:ok, t()} | {:error, term()}

Create a CID from raw bytes (without multibase prefix).

Examples

iex> Exosphere.ATProto.CID.from_bytes(<<0x01, 0x71, 0x12, 0x20, hash::binary-32>>)
{:ok, %Exosphere.ATProto.CID{...}}

from_bytes!(bytes)

@spec from_bytes!(binary()) :: t()

Create a CID from raw bytes, raising on error.

raw?(arg1)

@spec raw?(t()) :: boolean()

Check if a CID uses the raw codec.

to_bytes(cid)

@spec to_bytes(t()) :: binary()

Convert a CID to its raw byte representation (without multibase prefix).