NostrElixir.Nip65 (nostr_elixir v0.1.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

iex> relays = [
...>   {"wss://relay1.example.com", "read"},
...>   {"wss://relay2.example.com", "write"},
...>   {"wss://relay3.example.com", nil}
...> ]
iex> pubkey = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
iex> event_json = NostrElixir.Nip65.create_relay_list_event(relays, pubkey)
iex> is_binary(event_json)
true
iex> NostrElixir.Nip65.extract_relay_list(event_json)
[
  {"wss://relay1.example.com", "read"},
  {"wss://relay2.example.com", "write"},
  {"wss://relay3.example.com", nil}
]

The relay list is a list of {relay_url, metadata} tuples, where metadata is either "read", "write", or nil. The public key must be a 64-character hex string.

Summary

Functions

Create a relay list event from a list of {relay_url, metadata} tuples and a public key.

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

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

Create a relay list event from a list of {relay_url, metadata} tuples and a public key.

  • relays: List of {relay_url, metadata} tuples. metadata can be "read", "write", or nil.
  • pubkey: 64-character hex string public key.

Returns the event as a JSON string.

Example

iex> relays = [{"wss://relay.example.com", "read"}]
iex> pubkey = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
iex> event_json = NostrElixir.Nip65.create_relay_list_event(relays, pubkey)
iex> is_binary(event_json)
true

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.

Example

iex> event_json = NostrElixir.Nip65.create_relay_list_event([
...>   {"wss://relay.example.com", "read"}
...> ], "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
iex> NostrElixir.Nip65.extract_relay_list(event_json)
[{"wss://relay.example.com", "read"}]

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.