// Package relayinfo reads a relay's NIP-11 document: what it is called, who runs it, and which // back-end keys it authorises. // // That last field is the contact-free part of joining a relay. A relay publishes the gateways it // admits, so a gateway can ask a candidate relay "do you already list me" without an operator // exchange, and an operator can see which gateways a relay would accept before deciding to add // one. The list is public by construction: these are the keys the relay will accept publishes // from, and the listings they sign are public too. package relayinfo import ( "context" "encoding/json" "fmt" "io" "net/http" "net/url" "strings" "time" ) // Document is the part of NIP-11 this protocol reads. Unknown fields are ignored, which is what // lets the relay advertise more later without breaking older gateways. type Document struct { Name string `json:"name"` PubKey string `json:"pubkey,omitempty"` Contact string `json:"contact"` Software string `json:"software"` Version string `json:"version"` // Gateways is the back-end keys the relay authorises, as hex. Empty means the relay // advertises none, which is not the same as refusing every gateway: an older relay has no // field at all. Gateways []string `json:"gateways,omitempty"` } // HTTPURL turns a relay WebSocket URL into the HTTP URL that serves its NIP-11 document. func HTTPURL(relayURL string) (string, error) { parsed, err := url.Parse(relayURL) if err != nil { return "", fmt.Errorf("relayinfo: %w", err) } switch parsed.Scheme { case "wss": parsed.Scheme = "https" case "ws": parsed.Scheme = "http" case "https", "http": default: return "", fmt.Errorf("relayinfo: unsupported relay scheme %q", parsed.Scheme) } parsed.Path = "/" parsed.RawQuery = "" return parsed.String(), nil } // Fetch reads and decodes the document. A relay that serves something else, or nothing, is // reported as an error rather than as a relay with no gateways: the two mean different things. func Fetch(ctx context.Context, client *http.Client, relayURL string) (Document, error) { target, err := HTTPURL(relayURL) if err != nil { return Document{}, err } if client == nil { client = &http.Client{Timeout: 10 * time.Second} } request, err := http.NewRequestWithContext(ctx, http.MethodGet, target, nil) if err != nil { return Document{}, fmt.Errorf("relayinfo: %w", err) } request.Header.Set("Accept", "application/nostr+json") response, err := client.Do(request) if err != nil { return Document{}, fmt.Errorf("relayinfo: %s: %w", target, err) } defer response.Body.Close() if response.StatusCode != http.StatusOK { return Document{}, fmt.Errorf("relayinfo: %s: HTTP %d", target, response.StatusCode) } body, err := io.ReadAll(io.LimitReader(response.Body, 1<<20)) if err != nil { return Document{}, fmt.Errorf("relayinfo: %s: %w", target, err) } var doc Document if err := json.Unmarshal(body, &doc); err != nil { return Document{}, fmt.Errorf("relayinfo: %s does not serve NIP-11 JSON: %w", target, err) } return doc, nil } // Authorizes reports whether this relay's advertised list contains key, case-insensitively. func (d Document) Authorizes(key string) bool { want := strings.ToLower(strings.TrimSpace(key)) for _, gateway := range d.Gateways { if strings.ToLower(strings.TrimSpace(gateway)) == want { return true } } return false }