NostrElixir.Nip65 (nostr_elixir v0.3.0)

View Source

NIP-65: Relay List Metadata

This module provides functions to create and extract relay list metadata events as specified in NIP-65.

Examples

keys = NostrElixir.Keys.generate_keypair()
relays = [
  {"wss://relay1.example.com", "read"},
  {"wss://relay2.example.com", "write"},
  {"wss://relay3.example.com", nil}
]
event_json = NostrElixir.Nip65.create_relay_list_event(relays, keys.secret_key)
NostrElixir.Nip65.extract_relay_list(event_json)

The relay list is a list of {relay_url, metadata} tuples, where metadata is either "read", "write", or nil. Events are signed with the author's secret key (hex, bech32, or a keys map).

Summary

Functions

Create a signed relay list event from a list of {relay_url, metadata} tuples.

Extract the relay list from an event JSON string.

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

Convert a %Relay{} struct to a {url, metadata} tuple.

Convert a {url, metadata} tuple to a %Relay{} struct.

Validate relay metadata (must be "read", "write", or nil). Returns true if valid, false otherwise.

Validate a relay URL (must start with ws:// or wss://). Returns true if valid, false otherwise.

Functions

create_relay_list_event(relays, secret_key)

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

Create a signed relay list event from a list of {relay_url, metadata} tuples.

  • relays: List of {relay_url, metadata} tuples. metadata can be "read", "write", or nil.
  • secret_key: Author's secret key (hex or bech32), or a keys map with :secret_key.

Returns the event as a JSON string.

extract_relay_list(event_json)

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

Extract the relay list from an event JSON string.

Returns a list of {relay_url, metadata} tuples.

pretty_print(list)

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

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

struct_to_tuple(relay)

@spec struct_to_tuple(NostrElixir.Nip65.Relay.t()) :: {String.t(), String.t() | nil}

Convert a %Relay{} struct to a {url, metadata} tuple.

tuple_to_struct(arg)

@spec tuple_to_struct({String.t(), String.t() | nil}) :: NostrElixir.Nip65.Relay.t()

Convert a {url, metadata} tuple to a %Relay{} struct.

valid_metadata?(meta)

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

Validate relay metadata (must be "read", "write", or nil). Returns true if valid, false otherwise.

valid_relay_url?(url)

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

Validate a relay URL (must start with ws:// or wss://). Returns true if valid, false otherwise.