NostrElixir.Nip02 (nostr_elixir v0.1.0)

View Source

NIP-02: Follow List

Functions to create and extract follow list events (kind 3).

Examples

iex> follows = [
...>   {"npub1...", "wss://relay.example.com", "Alice"},
...>   {"npub2...", nil, nil}
...> ]
iex> pubkey = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
iex> event_json = NostrElixir.Nip02.create_follow_list_event(follows, pubkey)
iex> is_binary(event_json)
true
iex> NostrElixir.Nip02.extract_follows(event_json)
[
  {"npub1...", "wss://relay.example.com", "Alice"},
  {"npub2...", nil, nil}
]

The term 'contact' is used in some libraries, but the protocol and this module use 'follow' to match NIP-02.

Summary

Functions

Create a follow list event from a list of {pubkey, relay_url, alias} tuples and a public key. Returns the event as a JSON string.

Extract the follow list from an event JSON string. Returns a list of {pubkey, relay_url, alias} tuples.

Pretty-print a follow list (list of tuples or structs).

Convert a %Follow{} struct to a {pubkey, relay_url, alias} tuple.

Convert a {pubkey, relay_url, alias} tuple to a %Follow{} struct.

Validate a public key (hex or bech32, 64+ chars).

Validate a relay URL (must start with ws:// or wss://).

Functions

create_follow_list_event(follows, pubkey)

@spec create_follow_list_event(
  [{String.t(), String.t() | nil, String.t() | nil}],
  String.t()
) ::
  String.t()

Create a follow list event from a list of {pubkey, relay_url, alias} tuples and a public key. Returns the event as a JSON string.

extract_follows(event_json)

@spec extract_follows(String.t()) :: [
  {String.t(), String.t() | nil, String.t() | nil}
]

Extract the follow list from an event JSON string. Returns a list of {pubkey, relay_url, alias} tuples.

pretty_print(list)

@spec pretty_print(
  [NostrElixir.Nip02.Follow.t()]
  | [{String.t(), String.t() | nil, String.t() | nil}]
) ::
  String.t()

Pretty-print a follow list (list of tuples or structs).

struct_to_tuple(follow)

@spec struct_to_tuple(NostrElixir.Nip02.Follow.t()) ::
  {String.t(), String.t() | nil, String.t() | nil}

Convert a %Follow{} struct to a {pubkey, relay_url, alias} tuple.

tuple_to_struct(arg)

@spec tuple_to_struct({String.t(), String.t() | nil, String.t() | nil}) ::
  NostrElixir.Nip02.Follow.t()

Convert a {pubkey, relay_url, alias} tuple to a %Follow{} struct.

valid_pubkey?(pk)

@spec valid_pubkey?(String.t()) :: boolean()

Validate a public key (hex or bech32, 64+ chars).

valid_relay_url?(url)

@spec valid_relay_url?(String.t() | nil) :: boolean()

Validate a relay URL (must start with ws:// or wss://).