diff options
| author | Your Name <you@example.com> | 2026-05-19 02:31:19 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-19 02:32:41 +0530 |
| commit | 81f2dc52dc42d01c89dff45a5407ec40b8863052 (patch) | |
| tree | 15018c2438639ca89dc6d33a5144c10d0b1c2af0 /components/wisp_relay/nip11_relay.c | |
| parent | 75688d55b3c8d13c8c9a50da9668ec408f684cb3 (diff) | |
feat: local Nostr relay with relay selection, sync, and integration tests
Local Nostr relay (NIP-01) on port 4869 with LittleFS 4MB storage.
All events published locally first, then synced to public relays via REQ-diff.
Relay selection via NIP-11 HTTP probing with NIP-77 scoring and auto-failover.
Components:
- wisp_relay: 16-file local relay (ws_server, storage_engine, sub_manager,
broadcaster, relay_validator, router, handlers, rate_limiter, nip11,
deletion, flash_monitor, relay_types)
- esp_littlefs: LittleFS VFS integration (git submodule)
- negentropy: for future NIP-77 binary sync (git submodule)
New source files:
- local_relay.c/h: thin wrapper for relay init/start/publish
- relay_selector.c/h: NIP-11 probe + scoring + auto-failover
- sync_manager.c/h: REQ-diff sync (primary 30min, fallback 6h)
Bug fixes:
- config.c: use-after-free (cJSON_Delete before seed_relays/sync parsing)
- local_relay: moved init to app_main for boot-time start (not gated on STA IP)
Flash layout: 4MB LittleFS partition at 0x500000 for relay_store
Test results (Board B, live hardware):
- Smoke: ping + HTTP 4869 + NIP-11: PASS
- NIP-11 info document: 10/11 PASS
- WS pub/sub (connect, REQ/EOSE, EVENT/OK, CLOSE, concurrent): 6/6 PASS
- Unit tests (relay_validator + relay_selector): 13/13 PASS
Hardware test make targets in physical-router-test-automation/:
- make relay-build, relay-flash-b, relay-test-smoke/nip11/pubsub/sync/full
Diffstat (limited to 'components/wisp_relay/nip11_relay.c')
| -rw-r--r-- | components/wisp_relay/nip11_relay.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/components/wisp_relay/nip11_relay.c b/components/wisp_relay/nip11_relay.c new file mode 100644 index 0000000..4e1df37 --- /dev/null +++ b/components/wisp_relay/nip11_relay.c | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | #include "nip11_relay.h" | ||
| 2 | #include <string.h> | ||
| 3 | |||
| 4 | static const char *NIP11_JSON = | ||
| 5 | "{" | ||
| 6 | "\"name\":\"TollGate Relay\"," | ||
| 7 | "\"description\":\"Local Nostr relay with 21-day TTL and negentropy sync\"," | ||
| 8 | "\"pubkey\":\"\"," | ||
| 9 | "\"contact\":\"\"," | ||
| 10 | "\"supported_nips\":[1,9,11,20,40,77]," | ||
| 11 | "\"software\":\"https://github.com/nicobao/esp32-tollgate\"," | ||
| 12 | "\"version\":\"1.0.0\"," | ||
| 13 | "\"limitation\":{" | ||
| 14 | "\"max_message_length\":65536," | ||
| 15 | "\"max_subscriptions\":8," | ||
| 16 | "\"max_filters\":4," | ||
| 17 | "\"max_limit\":500," | ||
| 18 | "\"max_subid_length\":64," | ||
| 19 | "\"max_event_tags\":100," | ||
| 20 | "\"max_content_length\":32768," | ||
| 21 | "\"min_pow_difficulty\":0," | ||
| 22 | "\"auth_required\":false," | ||
| 23 | "\"payment_required\":false" | ||
| 24 | "}," | ||
| 25 | "\"retention\":[{\"kinds\":[0,1,2,3,4,5,6,7],\"time\":1814400}]," | ||
| 26 | "\"relay_countries\":[\"DE\"]" | ||
| 27 | "}"; | ||
| 28 | |||
| 29 | esp_err_t relay_nip11_handler(httpd_req_t *req) | ||
| 30 | { | ||
| 31 | char accept[64] = ""; | ||
| 32 | httpd_req_get_hdr_value_str(req, "Accept", accept, sizeof(accept)); | ||
| 33 | |||
| 34 | if (strstr(accept, "application/nostr+json")) { | ||
| 35 | httpd_resp_set_type(req, "application/nostr+json"); | ||
| 36 | } else { | ||
| 37 | httpd_resp_set_type(req, "application/json"); | ||
| 38 | } | ||
| 39 | |||
| 40 | httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); | ||
| 41 | httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type, Accept"); | ||
| 42 | httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, OPTIONS"); | ||
| 43 | return httpd_resp_send(req, NIP11_JSON, strlen(NIP11_JSON)); | ||
| 44 | } | ||
| 45 | |||
| 46 | esp_err_t relay_nip11_options_handler(httpd_req_t *req) | ||
| 47 | { | ||
| 48 | httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); | ||
| 49 | httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type, Accept"); | ||
| 50 | httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, OPTIONS"); | ||
| 51 | httpd_resp_set_status(req, "204 No Content"); | ||
| 52 | return httpd_resp_send(req, NULL, 0); | ||
| 53 | } | ||