"""Kind 10050: where a peer wants its gift wraps delivered (NIP-17).
A node that must reach a peer that is not listening publishes a kind 1059
to relays the peer will read later. Which relays those are is the peer's
to say, and NIP-17 is where it says it: a replaceable kind 10050 with one
`relay` tag per URL. This module reads that event and nothing else; the
node decides what to do when a peer has none (the node's own relays, as
before this module existed).
Reused, not invented: the shape is NIP-17's. What fleetmesh adds is the
rule in docs/implementing.md that a stored wrap goes to these relays when
the peer has published them - and that every node publishes one. The relay
rung is the floor of the transport ladder, the one way a node can always be
reached; a kind 10050 is where that floor is stated, in the place any Nostr
client already looks. `build` is the writing half: the tags a node
publishes, which `dm_relays` reads back unchanged.
"""
from __future__ import annotations
from typing import Any, Mapping, Sequence
from ..wire.interfaces import EventLike
KIND: int = 10050
MAX_RELAYS: int = 8
[docs]def is_relay_url(v: object) -> bool:
return isinstance(v, str) and v.startswith(('ws://', 'wss://')) and len(v) > 5 and ' ' not in v
[docs]def dm_relays(ev: EventLike) -> list[str]:
"""The relay URLs a kind 10050 names, in the order given, deduplicated,
anything that is not a ws(s) URL dropped, capped at MAX_RELAYS. An
event of another kind yields nothing: the caller asked the wrong
question and gets no relays rather than a wrong answer."""
if not isinstance(ev, dict) or isinstance(ev.get('kind'), bool) or not isinstance(ev.get('kind'), int) or ev['kind'] != KIND:
return []
out: list[str] = []
tags = ev.get('tags')
for t in tags if isinstance(tags, list) else []:
if (isinstance(t, list) and len(t) >= 2 and t[0] == 'relay'
and is_relay_url(t[1]) and t[1] not in out):
out.append(t[1])
if len(out) >= MAX_RELAYS:
break
return out
[docs]def build(relays: Sequence[str]) -> list[list[str]]:
"""The tags of the kind 10050 a node publishes for its own inbox: one
`relay` tag per URL, in the order given, deduplicated, anything that is
not a ws(s) URL refused rather than dropped (a node that would publish a
non-relay as its inbox has a config error, not a wire condition), capped
at MAX_RELAYS. dm_relays(build(x)) == x for any valid x."""
out: list[list[str]] = []
seen: set[str] = set()
for r in relays:
if not is_relay_url(r):
raise ValueError('not a relay url: %r' % (r,))
if r in seen:
continue
seen.add(r)
out.append(['relay', r])
if len(out) >= MAX_RELAYS:
break
if not out:
raise ValueError('an inbox relay list names at least one relay')
return out
[docs]def filter_for(pubkey: str) -> Mapping[str, Any]:
"""The NIP-01 filter that fetches a peer's kind 10050: replaceable, so
one event per author suffices."""
return {'kinds': [KIND], 'authors': [pubkey], 'limit': 1}