Keep auth-required and restricted prefixes when COUNT fails so clients can respond to authentication challenges. Add a wire-level regression for anonymous denial and authenticated counts.
- commit
- f90315b7cbeaee261066160278feb9bea528c041
- parent
- ee5b53dfaa40c7343483de0e7e48c97c51ef0856
- author
- Dami <dami.etoile@gmail.com>
| M | internal/ | 2 | +- |
| M | tinyrelay/ | 32 | ++++++++++++++++++++++++++++++ |
@@ -629,7 +629,7 @@ func (c *client) handleCount(id string, filters []event.Filter) {
629629 value, err := c.r.backend.Count(c.ctx, filters, c.snapshot())630630 c.r.mu.RUnlock()631631 if err != nil {632- c.closedSub(id, "error: count failed: "+err.Error())632+ c.closedSub(id, queryErrorReason(err))633633 return634634 }635635 _ = c.enqueue(fmt.Sprintf(`["COUNT",%q,%s]`, id, mustJSON(value)))@@ -436,6 +436,38 @@ func TestStandaloneRelayNIP42AndNIP77(t *testing.T) {
436436 }437437 }438438 439+func TestStandaloneRelayCountAuthRequiredWire(t *testing.T) {440+ _, httpServer, anonymous := openRelay(t, true)441+ anonymous.send("COUNT", "anonymous-count", map[string]any{})442+ denied := anonymous.expect("CLOSED", 3)443+ var reason string444+ if err := json.Unmarshal(denied[2], &reason); err != nil {445+ t.Fatalf("decode COUNT denial reason: %v", err)446+ }447+ if reason != "auth-required: this relay requires AUTH" {448+ t.Fatalf("anonymous COUNT reason = %q", reason)449+ }450+451+ secret, err := nostr.GenerateKey()452+ if err != nil {453+ t.Fatal(err)454+ }455+ authenticated := connectAuthClient(t, httpServer.URL)456+ authenticated.send("AUTH", signedEvent(t, secret, 22242, time.Now().Unix(), [][]string{{"relay", "ws://relay.example"}, {"challenge", authenticated.challenge}}, ""))457+ assertAccepted(t, authenticated.expect("OK", 4), true)458+ authenticated.send("COUNT", "authenticated-count", map[string]any{})459+ count := authenticated.expect("COUNT", 3)460+ var value struct {461+ Count int `json:"count"`462+ }463+ if err := json.Unmarshal(count[2], &value); err != nil {464+ t.Fatalf("decode authenticated COUNT: %v", err)465+ }466+ if value.Count != 0 {467+ t.Fatalf("authenticated COUNT = %d, want 0", value.Count)468+ }469+}470+439471 func TestStandaloneRelayGenericInboxAndRegistrationKinds(t *testing.T) {440472 _, httpServer, publisher := openRelay(t, false)441473 recipientSecret, err := nostr.GenerateKey()