NostrElixir.Nip02 (nostr_elixir v0.3.0)

View Source

NIP-02: Follow List

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

Examples

keys = NostrElixir.Keys.generate_keypair()
follows = [
  {followed_pubkey, "wss://relay.example.com", "Alice"},
  {other_pubkey, nil, nil}
]
event_json = NostrElixir.Nip02.create_follow_list_event(follows, keys.secret_key)
NostrElixir.Nip02.extract_follows(event_json)

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

Summary

Functions

Create a signed follow list event from a list of {pubkey, relay_url, alias} tuples.

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, secret_key)

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

Create a signed follow list event from a list of {pubkey, relay_url, alias} tuples.

The second argument is the author's secret key (hex or bech32) or a keys map with a :secret_key field. The event pubkey is derived from that 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://).