POLICY.md raw

Write policy

A relay that serves one job rarely wants to accept everything. A nostrino rendezvous relay keeps one stored, gateway-signed presence record and forwards two ephemeral frame kinds; a market relay stores signed notes from anybody; a private relay serves a handful of keys. Those are the same four questions with different answers:

  1. is this kind accepted at all,
  2. must the connection be authenticated (NIP-42),
  3. must the author be one of a registered set,
  4. how many of them per second.

Set TORLY_POLICY_FILE to a JSON document and the relay answers them. Without the variable the relay accepts every kind from every author, exactly as before.

Format

{
  "version": 1,
  "default": "deny",
  "rules": [
    {
      "kinds": [30333],
      "require_auth": true,
      "authors": ["<64 hex>"]
    },
    {
      "kinds": [23196, 23197],
      "rate": { "per_second": 400, "burst": 800 }
    }
  ]
}

that only restricts a few kinds does not silently become an allowlist.

catch-all; a rule with no kinds matches every kind.

author. A signature proves who wrote an event; authentication proves who is speaking, and a restricted kind wants both.

the event's author whether or not authentication is required; combine it with require_auth when the connection's identity matters too.

listed values. It is what separates a protocol's notes from every other note of the same kind, so a gateway-only rule can cover a marketplace's listings while a different rule covers the rest of kind 1.

per_second sustained. burst defaults to per_second, and may be smaller (less than a second of buffering). bytes_per_second/bytes_burst add the same budget measured in bytes, which a count alone cannot express: without it, a connection may push per_second events of the maximum size every second. One client cannot spend another client's budget.

Unknown fields are refused, so a typo is a startup error rather than a rule that quietly does something else. A file that cannot be read or parsed stops the relay: running without the policy would accept what the operator believes is refused.

Refusals

A refused write is answered with OK false and a NIP-01 machine-readable prefix, so a client can tell the cases apart without reading prose:

PrefixMeaning
blocked:the kind is not accepted, or the author is not registered for it
auth-required:the rule requires NIP-42, the connection is not authenticated, or the authenticated key is not the author
rate-limited:this connection spent its budget for the rule
invalid:the event itself is malformed, expired, too large or too far in the future

The policy is evaluated before the signature is verified, so a refused write costs a table lookup rather than a verification slot. AUTH events (kind 22242) are never subject to it: a policy that required authentication for them could not be satisfied.

Examples

deploy/policy/rendezvous.json is the nostrino case:

addressable, so a client that connects later still finds it;

frames are encrypted for the gateway and the client is not known in advance, with a per connection rate;

deploy/torly-rendezvous.env is the matching environment: Blossom off, a 128 KiB event ceiling (the tunnel's 32 KiB frames are NIP-44 ciphertext, about 4/3 of the frame), and the query limits a rendezvous relay needs rather than a general one.

deploy/policy/market.json is the other shape: the standing offers (kind 1 tagged blake-swap-market) are the gateway's alone, while takes, cancels and matches are what participants publish, each at its own rate, and deletions need authentication. Everybody can read the offers, and TORLY_QUERY_PER_SEC stops a client from asking for them in a loop. The market's own expiry rides NIP-40, which the relay already honours (TORLY_EXPIRY_INTERVAL), so an offer disappears when its expiration tag says.

Inbound and outbound

The policy governs inbound writes only. Delivery to subscribers — what a wallet actually downloads — is not rate-limited at all: a relay sends each matching event to every matching subscription as fast as the sockets take it, and a client that cannot keep up loses its connection rather than slowing the relay down. That asymmetry is deliberate, and the rendezvous policy uses it: the gateway's frames (kind 23197, gateway to wallet, the bulk of a sync) are gateway-only and carry no rate, while a wallet's frames (kind 23196, requests and acks) are open but rate- and byte-limited per connection.

Two more limits are direction- and request-aware:

Zero, the default, delivers at whatever the socket takes. A value queues the excess in order and releases it as the budget allows, so a paced stream is slowed, never broken: the nostrino tunnel would otherwise see a gap in its sequence numbers and ask for the frame again. A connection that queues more than 8 MiB is closed as a slow consumer.

REQ or COUNT over the budget is not answered at all, which is what stops a client from polling a marketplace flat; one that keeps asking is closed after TORLY_ABUSE_CLOSE_AFTER requests.

Two limits sit above the policy:

message before the JSON is parsed. It is the line: set it to what the relay can actually serve. A message over the ceiling is dropped with no reply (answering a flood would cost a write per dropped frame), and a connection that keeps exceeding it is closed after TORLY_ABUSE_CLOSE_AFTER messages. This is what stops a flood spread across connections that each stay inside their own budget.

connections there are to share the ceiling.

Interaction with the other settings

policy. The policy is how a relay asks for authentication on some kinds and not others.

TORLY_QUERY_LIMIT bound a single event or subscription. The policy decides what may be published at all; the limits decide how large and how many.

a kind does not keep it forever if its events carry expiration.

Federating: adding this relay's back ends to another relay

The announcements on a relay already name every relay that back end talks to, so a client that discovers a gateway on one relay learns the others and can fail over between them.

Before any of that, the relay has to say where to ask. Set

TORLY_CONTACT_EMAIL=keys@your.relay

and it is published as the NIP-11 contact (`curl -H 'Accept: application/nostr+json' https://your.relay/`), which is where a back-end operator looks. A relay that keeps an allowlist without publishing one gives the other side no way to join. TORLY_CONTACT remains the general contact and is used only when no email is set.

A second relay operator who wants the same back ends adds their public keys to the gateway-only rules. nostrino-discover prints exactly that list from any relay that already holds the announcements:

nostrino-discover -relay wss://swaprelay.smesh.lol -all -authors
"authors": ["<pubkey>", "<pubkey>"]

The back end then adds the new relay to its own -relay flags; its listing, which already names every relay it uses, is published there as well, so the two relays describe the same back ends and clients can discover them from either one. The loop closes with the addresses: the relay publishes its TORLY_CONTACT_EMAIL, the back end writes to it with its public key, and both sides know who to talk to before and after they are connected.