Nostr.Tag (Nostr Lib v0.2.1)

View Source

Nostr Event tag.

Tags are metadata attached to events. Common tag types:

  • :e - references another event (event ID)
  • :p - references a pubkey (user mention)
  • :a - references a parameterized replaceable event
  • :d - identifier for parameterized replaceable events
  • :relay - relay URL
  • :challenge - authentication challenge

The wire format is a JSON array: ["type"] or ["type", "data", ...additional_info].

NIP-01 specifies that each tag is an array of one or more strings. Single-element tags (e.g. ["test"]) are valid and represented with data: nil.

Summary

Functions

Create a type-only Nostr tag (no data or info).

Parses a JSON tag array into a Tag struct.

Types

t()

@type t() :: %Nostr.Tag{data: binary() | nil, info: [binary()], type: atom()}

Functions

create(type)

@spec create(type :: atom() | binary()) :: t()

Create a type-only Nostr tag (no data or info).

Example:

iex> Nostr.Tag.create(:test)
%Nostr.Tag{type: :test, data: nil, info: []}

create(type, data, other_data \\ [])

@spec create(type :: atom() | binary(), data :: binary(), other_data :: [binary()]) ::
  t()

Create new Nostr tag

Each tag needs to have type and at least one data field. If tag requires more then one data field supply them as third argument (list of strings)

Example:

iex> Nostr.Tag.create(:e, "event-id", ["wss://relay.example.com"])
%Nostr.Tag{type: :e, data: "event-id", info: ["wss://relay.example.com"]}

iex> Nostr.Tag.create(:p, "pubkey")
%Nostr.Tag{type: :p, data: "pubkey", info: []}

parse(list)

@spec parse(tag :: list()) :: t() | nil

Parses a JSON tag array into a Tag struct.

Accepts arrays with one or more string elements per NIP-01:

  • ["type"]%Tag{type: :type, data: nil, info: []}
  • ["type", "data"]%Tag{type: :type, data: "data", info: []}
  • ["type", "data", ...info]%Tag{type: :type, data: "data", info: info}

Returns nil for empty arrays.