NostrElixir.Nip65 (nostr_elixir v0.1.0)
View SourceNIP-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 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", ornil
.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 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"}]
@spec pretty_print([NostrElixir.Nip65.Relay.t()] | [{String.t(), String.t() | nil}]) :: String.t()
Pretty-print a relay list (list of tuples or structs).
@spec struct_to_tuple(NostrElixir.Nip65.Relay.t()) :: {String.t(), String.t() | nil}
Convert a %Relay{} struct to a {url, metadata}
tuple.
@spec tuple_to_struct({String.t(), String.t() | nil}) :: NostrElixir.Nip65.Relay.t()
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.