Parrhesia.Protocol (parrhesia v0.12.0)

Copy Markdown

Nostr protocol message decode/encode helpers.

This module is transport-oriented: it turns websocket payloads into structured tuples and back again.

For programmatic API calls inside the application, prefer the Parrhesia.API.* modules. In particular:

Summary

Functions

Decodes a client websocket payload into a structured protocol tuple.

Converts a decode error into the relay notice string that should be sent to a client.

Encodes a relay message tuple into the JSON frame sent to clients.

Validates an event and returns relay-facing error strings.

Types

client_message()

@type client_message() ::
  {:event, event()}
  | {:req, String.t(), [filter()]}
  | {:close, String.t()}
  | {:auth, event()}
  | {:count, String.t(), [filter()], map()}
  | {:neg_open, String.t(), filter(), binary()}
  | {:neg_msg, String.t(), binary()}
  | {:neg_close, String.t()}

decode_error()

@type decode_error() ::
  :invalid_json
  | :invalid_message
  | :invalid_event
  | :invalid_subscription_id
  | :invalid_filters
  | :invalid_auth
  | :invalid_count
  | :invalid_negentropy

event()

@type event() :: map()

filter()

@type filter() :: map()

relay_message()

@type relay_message() ::
  {:notice, String.t()}
  | {:ok, String.t(), boolean(), String.t()}
  | {:closed, String.t(), String.t()}
  | {:eose, String.t()}
  | {:event, String.t(), event()}
  | {:auth, String.t()}
  | {:count, String.t(), map()}
  | {:neg_msg, String.t(), String.t()}
  | {:neg_err, String.t(), String.t()}

Functions

decode_client(payload)

@spec decode_client(binary()) :: {:ok, client_message()} | {:error, decode_error()}

Decodes a client websocket payload into a structured protocol tuple.

decode_error_notice(reason)

@spec decode_error_notice(decode_error()) :: String.t()

Converts a decode error into the relay notice string that should be sent to a client.

encode_relay(message)

@spec encode_relay(relay_message()) :: binary()

Encodes a relay message tuple into the JSON frame sent to clients.

validate_event(event)

@spec validate_event(event()) :: :ok | {:error, String.t()}

Validates an event and returns relay-facing error strings.