@toon-protocol/relay 1.3.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2103 +0,0 @@
1
- // src/types.ts
2
- var DEFAULT_RELAY_CONFIG = {
3
- port: 7e3,
4
- host: "0.0.0.0",
5
- maxConnections: 100,
6
- maxSubscriptionsPerConnection: 20,
7
- maxFiltersPerSubscription: 10,
8
- databasePath: ":memory:"
9
- };
10
-
11
- // src/filters/matchFilter.ts
12
- function matchFilter(event, filter) {
13
- if (Object.keys(filter).length === 0) {
14
- return true;
15
- }
16
- if (filter.ids !== void 0 && filter.ids.length > 0) {
17
- const matches = filter.ids.some((id) => event.id.startsWith(id));
18
- if (!matches) return false;
19
- }
20
- if (filter.authors !== void 0 && filter.authors.length > 0) {
21
- const matches = filter.authors.some(
22
- (author) => event.pubkey.startsWith(author)
23
- );
24
- if (!matches) return false;
25
- }
26
- if (filter.kinds !== void 0 && filter.kinds.length > 0) {
27
- if (!filter.kinds.includes(event.kind)) return false;
28
- }
29
- if (filter.since !== void 0) {
30
- if (event.created_at < filter.since) return false;
31
- }
32
- if (filter.until !== void 0) {
33
- if (event.created_at > filter.until) return false;
34
- }
35
- for (const key of Object.keys(filter)) {
36
- if (key.startsWith("#") && key.length === 2) {
37
- const tagName = key.slice(1);
38
- const filterValues = filter[key];
39
- if (filterValues !== void 0 && filterValues.length > 0) {
40
- const eventTagValues = event.tags.filter((tag) => tag[0] === tagName).map((tag) => tag[1]);
41
- const hasMatch = filterValues.some((v) => eventTagValues.includes(v));
42
- if (!hasMatch) return false;
43
- }
44
- }
45
- }
46
- return true;
47
- }
48
-
49
- // src/storage/InMemoryEventStore.ts
50
- var InMemoryEventStore = class {
51
- events = /* @__PURE__ */ new Map();
52
- store(event) {
53
- this.events.set(event.id, event);
54
- }
55
- get(id) {
56
- return this.events.get(id);
57
- }
58
- query(filters) {
59
- const allEvents = Array.from(this.events.values());
60
- if (filters.length === 0) {
61
- return allEvents.sort((a, b) => b.created_at - a.created_at);
62
- }
63
- const matchingEvents = [];
64
- for (const event of allEvents) {
65
- for (const filter of filters) {
66
- if (matchFilter(event, filter)) {
67
- matchingEvents.push(event);
68
- break;
69
- }
70
- }
71
- }
72
- matchingEvents.sort((a, b) => b.created_at - a.created_at);
73
- const limitFilter = filters.find((f) => f.limit !== void 0);
74
- if (limitFilter?.limit !== void 0) {
75
- return matchingEvents.slice(0, limitFilter.limit);
76
- }
77
- return matchingEvents;
78
- }
79
- /**
80
- * Close the storage backend (no-op for in-memory store).
81
- */
82
- close() {
83
- }
84
- };
85
-
86
- // src/storage/SqliteEventStore.ts
87
- import Database from "better-sqlite3";
88
- var SCHEMA_SQL = `
89
- CREATE TABLE IF NOT EXISTS events (
90
- id TEXT PRIMARY KEY,
91
- pubkey TEXT NOT NULL,
92
- kind INTEGER NOT NULL,
93
- content TEXT NOT NULL,
94
- tags TEXT NOT NULL,
95
- created_at INTEGER NOT NULL,
96
- sig TEXT NOT NULL,
97
- received_at INTEGER NOT NULL
98
- )
99
- `;
100
- var INDEX_SQL = [
101
- "CREATE INDEX IF NOT EXISTS idx_events_pubkey ON events(pubkey)",
102
- "CREATE INDEX IF NOT EXISTS idx_events_kind ON events(kind)",
103
- "CREATE INDEX IF NOT EXISTS idx_events_created_at ON events(created_at)",
104
- "CREATE INDEX IF NOT EXISTS idx_events_pubkey_kind ON events(pubkey, kind)"
105
- ];
106
- function initializeSchema(db) {
107
- db.exec(SCHEMA_SQL);
108
- for (const indexSql of INDEX_SQL) {
109
- db.exec(indexSql);
110
- }
111
- }
112
- var RelayError = class extends Error {
113
- constructor(message, code) {
114
- super(message);
115
- this.code = code;
116
- this.name = "RelayError";
117
- }
118
- };
119
- function isReplaceableKind(kind) {
120
- return kind >= 1e4 && kind <= 19999 && !(kind >= 10032 && kind <= 10099);
121
- }
122
- function isParameterizedReplaceableKind(kind) {
123
- return kind >= 3e4 && kind <= 39999 || kind >= 10032 && kind <= 10099;
124
- }
125
- function getDTagValue(tags) {
126
- const dTag = tags.find((tag) => tag[0] === "d");
127
- return dTag?.[1] ?? "";
128
- }
129
- var SqliteEventStore = class {
130
- db;
131
- insertStmt;
132
- getStmt;
133
- deleteByPubkeyKindStmt;
134
- deleteByPubkeyKindDTagStmt;
135
- getByPubkeyKindStmt;
136
- getByPubkeyKindDTagStmt;
137
- /**
138
- * Create a new SqliteEventStore.
139
- * @param dbPath - Path to the database file. Use ':memory:' for in-memory database.
140
- */
141
- constructor(dbPath = ":memory:") {
142
- try {
143
- this.db = new Database(dbPath);
144
- initializeSchema(this.db);
145
- this.insertStmt = this.db.prepare(`
146
- INSERT OR REPLACE INTO events (id, pubkey, kind, content, tags, created_at, sig, received_at)
147
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
148
- `);
149
- this.getStmt = this.db.prepare("SELECT * FROM events WHERE id = ?");
150
- this.deleteByPubkeyKindStmt = this.db.prepare(
151
- "DELETE FROM events WHERE pubkey = ? AND kind = ?"
152
- );
153
- this.deleteByPubkeyKindDTagStmt = this.db.prepare(
154
- "DELETE FROM events WHERE pubkey = ? AND kind = ? AND json_extract(tags, '$') LIKE ?"
155
- );
156
- this.getByPubkeyKindStmt = this.db.prepare(
157
- "SELECT id, created_at FROM events WHERE pubkey = ? AND kind = ?"
158
- );
159
- this.getByPubkeyKindDTagStmt = this.db.prepare(
160
- "SELECT id, created_at FROM events WHERE pubkey = ? AND kind = ? AND tags LIKE ?"
161
- );
162
- } catch (error) {
163
- throw new RelayError(
164
- `Failed to initialize database: ${error instanceof Error ? error.message : String(error)}`,
165
- "STORAGE_ERROR"
166
- );
167
- }
168
- }
169
- /**
170
- * Store an event in the database.
171
- * Handles replaceable and parameterized replaceable events according to NIP-01.
172
- */
173
- store(event) {
174
- try {
175
- const tagsJson = JSON.stringify(event.tags);
176
- const receivedAt = Math.floor(Date.now() / 1e3);
177
- if (isReplaceableKind(event.kind)) {
178
- this.storeReplaceableEvent(event, tagsJson, receivedAt);
179
- } else if (isParameterizedReplaceableKind(event.kind)) {
180
- this.storeParameterizedReplaceableEvent(event, tagsJson, receivedAt);
181
- } else {
182
- const insertOrIgnore = this.db.prepare(`
183
- INSERT OR IGNORE INTO events (id, pubkey, kind, content, tags, created_at, sig, received_at)
184
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
185
- `);
186
- insertOrIgnore.run(
187
- event.id,
188
- event.pubkey,
189
- event.kind,
190
- event.content,
191
- tagsJson,
192
- event.created_at,
193
- event.sig,
194
- receivedAt
195
- );
196
- }
197
- } catch (error) {
198
- if (error instanceof RelayError) {
199
- throw error;
200
- }
201
- throw new RelayError(
202
- `Failed to store event: ${error instanceof Error ? error.message : String(error)}`,
203
- "STORAGE_ERROR"
204
- );
205
- }
206
- }
207
- /**
208
- * Store a replaceable event (kinds 10000-19999).
209
- * Only keeps the latest event per pubkey+kind.
210
- */
211
- storeReplaceableEvent(event, tagsJson, receivedAt) {
212
- const existing = this.getByPubkeyKindStmt.get(event.pubkey, event.kind);
213
- if (existing) {
214
- if (event.created_at > existing.created_at || event.created_at === existing.created_at && event.id < existing.id) {
215
- const transaction = this.db.transaction(() => {
216
- this.deleteByPubkeyKindStmt.run(event.pubkey, event.kind);
217
- this.insertStmt.run(
218
- event.id,
219
- event.pubkey,
220
- event.kind,
221
- event.content,
222
- tagsJson,
223
- event.created_at,
224
- event.sig,
225
- receivedAt
226
- );
227
- });
228
- transaction();
229
- }
230
- } else {
231
- this.insertStmt.run(
232
- event.id,
233
- event.pubkey,
234
- event.kind,
235
- event.content,
236
- tagsJson,
237
- event.created_at,
238
- event.sig,
239
- receivedAt
240
- );
241
- }
242
- }
243
- /**
244
- * Store a parameterized replaceable event (kinds 30000-39999).
245
- * Only keeps the latest event per pubkey+kind+d-tag.
246
- */
247
- storeParameterizedReplaceableEvent(event, tagsJson, receivedAt) {
248
- const dTagValue = getDTagValue(event.tags);
249
- let existing;
250
- if (dTagValue === "") {
251
- const candidates = this.db.prepare(
252
- "SELECT id, created_at, tags FROM events WHERE pubkey = ? AND kind = ?"
253
- ).all(event.pubkey, event.kind);
254
- for (const candidate of candidates) {
255
- const candidateTags = JSON.parse(candidate.tags);
256
- const candidateDTagValue = getDTagValue(candidateTags);
257
- if (candidateDTagValue === "") {
258
- existing = { id: candidate.id, created_at: candidate.created_at };
259
- break;
260
- }
261
- }
262
- } else {
263
- const dTagPattern = `%["d","${dTagValue}"%`;
264
- existing = this.getByPubkeyKindDTagStmt.get(
265
- event.pubkey,
266
- event.kind,
267
- dTagPattern
268
- );
269
- }
270
- if (existing) {
271
- if (event.created_at > existing.created_at || event.created_at === existing.created_at && event.id < existing.id) {
272
- const transaction = this.db.transaction(() => {
273
- this.db.prepare("DELETE FROM events WHERE id = ?").run(existing.id);
274
- this.insertStmt.run(
275
- event.id,
276
- event.pubkey,
277
- event.kind,
278
- event.content,
279
- tagsJson,
280
- event.created_at,
281
- event.sig,
282
- receivedAt
283
- );
284
- });
285
- transaction();
286
- }
287
- } else {
288
- this.insertStmt.run(
289
- event.id,
290
- event.pubkey,
291
- event.kind,
292
- event.content,
293
- tagsJson,
294
- event.created_at,
295
- event.sig,
296
- receivedAt
297
- );
298
- }
299
- }
300
- /**
301
- * Retrieve an event by its ID.
302
- */
303
- get(id) {
304
- try {
305
- const row = this.getStmt.get(id);
306
- if (!row) {
307
- return void 0;
308
- }
309
- return {
310
- id: row.id,
311
- pubkey: row.pubkey,
312
- kind: row.kind,
313
- content: row.content,
314
- tags: JSON.parse(row.tags),
315
- created_at: row.created_at,
316
- sig: row.sig
317
- };
318
- } catch (error) {
319
- throw new RelayError(
320
- `Failed to get event: ${error instanceof Error ? error.message : String(error)}`,
321
- "STORAGE_ERROR"
322
- );
323
- }
324
- }
325
- /**
326
- * Query events matching any of the provided filters.
327
- */
328
- query(filters) {
329
- try {
330
- const { sql, params } = this.buildQuerySql(filters);
331
- const stmt = this.db.prepare(sql);
332
- const rows = stmt.all(...params);
333
- return rows.map((row) => ({
334
- id: row.id,
335
- pubkey: row.pubkey,
336
- kind: row.kind,
337
- content: row.content,
338
- tags: JSON.parse(row.tags),
339
- created_at: row.created_at,
340
- sig: row.sig
341
- }));
342
- } catch (error) {
343
- throw new RelayError(
344
- `Failed to query events: ${error instanceof Error ? error.message : String(error)}`,
345
- "STORAGE_ERROR"
346
- );
347
- }
348
- }
349
- /**
350
- * Build SQL query from filters.
351
- */
352
- buildQuerySql(filters) {
353
- if (filters.length === 0) {
354
- return {
355
- sql: "SELECT * FROM events ORDER BY created_at DESC",
356
- params: []
357
- };
358
- }
359
- const conditions = [];
360
- const params = [];
361
- for (const filter of filters) {
362
- const filterConditions = [];
363
- if (filter.ids?.length) {
364
- const idConditions = filter.ids.map(() => "id LIKE ?");
365
- filterConditions.push(`(${idConditions.join(" OR ")})`);
366
- params.push(...filter.ids.map((id) => `${id}%`));
367
- }
368
- if (filter.authors?.length) {
369
- const authorConditions = filter.authors.map(() => "pubkey LIKE ?");
370
- filterConditions.push(`(${authorConditions.join(" OR ")})`);
371
- params.push(...filter.authors.map((a) => `${a}%`));
372
- }
373
- if (filter.kinds?.length) {
374
- filterConditions.push(
375
- `kind IN (${filter.kinds.map(() => "?").join(", ")})`
376
- );
377
- params.push(...filter.kinds);
378
- }
379
- if (filter.since !== void 0) {
380
- filterConditions.push("created_at >= ?");
381
- params.push(filter.since);
382
- }
383
- if (filter.until !== void 0) {
384
- filterConditions.push("created_at <= ?");
385
- params.push(filter.until);
386
- }
387
- for (const [key, values] of Object.entries(filter)) {
388
- if (key.startsWith("#") && Array.isArray(values) && values.length > 0) {
389
- const tagName = key.slice(1);
390
- const tagConditions = values.map(() => `tags LIKE ?`);
391
- filterConditions.push(`(${tagConditions.join(" OR ")})`);
392
- params.push(...values.map((v) => `%["${tagName}","${v}"%`));
393
- }
394
- }
395
- if (filterConditions.length > 0) {
396
- conditions.push(`(${filterConditions.join(" AND ")})`);
397
- }
398
- }
399
- let sql = "SELECT * FROM events";
400
- if (conditions.length > 0) {
401
- sql += ` WHERE ${conditions.join(" OR ")}`;
402
- }
403
- sql += " ORDER BY created_at DESC";
404
- const limitFilter = filters.find((f) => f.limit !== void 0);
405
- if (limitFilter?.limit !== void 0) {
406
- sql += " LIMIT ?";
407
- params.push(limitFilter.limit);
408
- }
409
- return { sql, params };
410
- }
411
- /**
412
- * Close the database connection.
413
- */
414
- close() {
415
- this.db.close();
416
- }
417
- };
418
-
419
- // src/toon/index.ts
420
- import {
421
- encodeEventToToon,
422
- encodeEventToToonString,
423
- ToonEncodeError
424
- } from "@toon-protocol/core";
425
- import { decodeEventFromToon, ToonDecodeError } from "@toon-protocol/core";
426
-
427
- // src/websocket/ConnectionHandler.ts
428
- var ConnectionHandler = class {
429
- constructor(ws, eventStore, config = {}) {
430
- this.ws = ws;
431
- this.eventStore = eventStore;
432
- this.config = { ...DEFAULT_RELAY_CONFIG, ...config };
433
- }
434
- subscriptions = /* @__PURE__ */ new Map();
435
- config;
436
- /**
437
- * Handle an incoming message from the WebSocket.
438
- */
439
- handleMessage(data) {
440
- console.log(`[ConnectionHandler] Received message:`, data.slice(0, 150));
441
- let message;
442
- try {
443
- const parsed = JSON.parse(data);
444
- if (!Array.isArray(parsed)) {
445
- this.sendNotice("error: invalid message format, expected JSON array");
446
- return;
447
- }
448
- message = parsed;
449
- } catch {
450
- this.sendNotice("error: invalid JSON");
451
- return;
452
- }
453
- const messageType = message[0];
454
- console.log(`[ConnectionHandler] Message type: ${messageType}`);
455
- if (messageType === "REQ") {
456
- const subscriptionId = message[1];
457
- const filters = message.slice(2);
458
- this.handleReq(subscriptionId, filters);
459
- } else if (messageType === "EVENT") {
460
- const event = message[1];
461
- this.handleEvent(event);
462
- } else if (messageType === "CLOSE") {
463
- const subscriptionId = message[1];
464
- this.handleClose(subscriptionId);
465
- } else {
466
- this.sendNotice(`error: unknown message type: ${messageType}`);
467
- }
468
- }
469
- /**
470
- * Handle a REQ message to create/update a subscription.
471
- */
472
- handleReq(subscriptionId, filters) {
473
- if (typeof subscriptionId !== "string" || subscriptionId.length === 0) {
474
- this.sendNotice("error: invalid subscription id");
475
- return;
476
- }
477
- if (!this.subscriptions.has(subscriptionId)) {
478
- if (this.subscriptions.size >= this.config.maxSubscriptionsPerConnection) {
479
- this.sendNotice("error: too many subscriptions");
480
- return;
481
- }
482
- }
483
- if (filters.length > this.config.maxFiltersPerSubscription) {
484
- this.sendNotice("error: too many filters");
485
- return;
486
- }
487
- this.subscriptions.set(subscriptionId, {
488
- id: subscriptionId,
489
- filters
490
- });
491
- console.log(
492
- `[ConnectionHandler] REQ: ${subscriptionId}, filters:`,
493
- JSON.stringify(filters).slice(0, 100)
494
- );
495
- const events = this.eventStore.query(filters);
496
- console.log(
497
- `[ConnectionHandler] Query returned ${events.length} events for ${subscriptionId}`
498
- );
499
- for (const event of events) {
500
- console.log(
501
- `[ConnectionHandler] Sending event ${event.id.slice(0, 16)}... to ${subscriptionId}`
502
- );
503
- this.sendEvent(subscriptionId, event);
504
- }
505
- console.log(`[ConnectionHandler] Sending EOSE for ${subscriptionId}`);
506
- this.sendEose(subscriptionId);
507
- }
508
- /**
509
- * Handle an EVENT message from a WebSocket client.
510
- *
511
- * Rejects all external writes — the relay is ILP-gated (pay to write).
512
- * Events are only stored through the ILP packet handler which calls
513
- * eventStore.store() directly and then broadcastEvent() to notify subscribers.
514
- */
515
- handleEvent(event) {
516
- this.sendOk(event.id, false, "restricted: writes require ILP payment");
517
- }
518
- /**
519
- * Handle a CLOSE message to terminate a subscription.
520
- */
521
- handleClose(subscriptionId) {
522
- this.subscriptions.delete(subscriptionId);
523
- }
524
- /**
525
- * Push a new event to all matching subscriptions on this connection.
526
- * Used when events are stored outside the WebSocket flow (e.g., via ILP).
527
- */
528
- notifyNewEvent(event) {
529
- for (const sub of this.subscriptions.values()) {
530
- const matches = sub.filters.some((f) => matchFilter(event, f));
531
- if (matches) {
532
- this.sendEvent(sub.id, event);
533
- }
534
- }
535
- }
536
- /**
537
- * Clean up all subscriptions for this connection.
538
- */
539
- cleanup() {
540
- this.subscriptions.clear();
541
- }
542
- /**
543
- * Get the number of active subscriptions.
544
- */
545
- getSubscriptionCount() {
546
- return this.subscriptions.size;
547
- }
548
- sendEvent(subscriptionId, event) {
549
- this.send(["EVENT", subscriptionId, encodeEventToToonString(event)]);
550
- }
551
- sendEose(subscriptionId) {
552
- this.send(["EOSE", subscriptionId]);
553
- }
554
- sendOk(eventId, success, message) {
555
- this.send(["OK", eventId, success, message]);
556
- }
557
- sendNotice(message) {
558
- this.send(["NOTICE", message]);
559
- }
560
- send(message) {
561
- if (this.ws.readyState === 1) {
562
- this.ws.send(JSON.stringify(message));
563
- }
564
- }
565
- };
566
-
567
- // src/websocket/NostrRelayServer.ts
568
- import { WebSocketServer } from "ws";
569
- var NostrRelayServer = class {
570
- constructor(config = {}, eventStore) {
571
- this.eventStore = eventStore;
572
- this.config = { ...DEFAULT_RELAY_CONFIG, ...config };
573
- }
574
- wss = null;
575
- handlers = /* @__PURE__ */ new Map();
576
- config;
577
- /**
578
- * Start the WebSocket server.
579
- */
580
- async start() {
581
- return new Promise((resolve, reject) => {
582
- try {
583
- this.wss = new WebSocketServer({
584
- port: this.config.port,
585
- host: this.config.host
586
- });
587
- this.wss.on("connection", (ws) => {
588
- this.handleConnection(ws);
589
- });
590
- this.wss.on("error", (error) => {
591
- console.error("[NostrRelayServer] Server error:", error.message);
592
- });
593
- this.wss.on("listening", () => {
594
- const address = this.wss?.address();
595
- if (address && typeof address === "object") {
596
- console.log(`[NostrRelayServer] Listening on port ${address.port}`);
597
- }
598
- resolve();
599
- });
600
- } catch (error) {
601
- reject(error);
602
- }
603
- });
604
- }
605
- /**
606
- * Stop the WebSocket server and close all connections.
607
- */
608
- async stop() {
609
- return new Promise((resolve) => {
610
- if (!this.wss) {
611
- resolve();
612
- return;
613
- }
614
- for (const [ws, handler] of this.handlers) {
615
- handler.cleanup();
616
- ws.close();
617
- }
618
- this.handlers.clear();
619
- this.wss.close(() => {
620
- this.wss = null;
621
- resolve();
622
- });
623
- });
624
- }
625
- /**
626
- * Get the port the server is listening on.
627
- * Returns 0 if the server is not started.
628
- */
629
- getPort() {
630
- if (!this.wss) return 0;
631
- const address = this.wss.address();
632
- if (address && typeof address === "object") {
633
- return address.port;
634
- }
635
- return 0;
636
- }
637
- /**
638
- * Get the number of connected clients.
639
- */
640
- getClientCount() {
641
- return this.handlers.size;
642
- }
643
- /**
644
- * Broadcast an event to all connected clients with matching subscriptions.
645
- * Call this after storing an event outside the WebSocket flow (e.g., via ILP)
646
- * so that discovery subscribers are notified.
647
- */
648
- broadcastEvent(event) {
649
- for (const handler of this.handlers.values()) {
650
- handler.notifyNewEvent(event);
651
- }
652
- }
653
- handleConnection(ws) {
654
- if (this.handlers.size >= this.config.maxConnections) {
655
- ws.close(1013, "max connections reached");
656
- return;
657
- }
658
- console.log("[NostrRelayServer] Client connected");
659
- const handler = new ConnectionHandler(ws, this.eventStore, this.config);
660
- this.handlers.set(ws, handler);
661
- ws.on("message", (data) => {
662
- const message = typeof data === "string" ? data : data.toString();
663
- handler.handleMessage(message);
664
- });
665
- ws.on("close", () => {
666
- console.log("[NostrRelayServer] Client disconnected");
667
- handler.cleanup();
668
- this.handlers.delete(ws);
669
- });
670
- ws.on("error", (error) => {
671
- console.error("[NostrRelayServer] Client error:", error.message);
672
- handler.cleanup();
673
- this.handlers.delete(ws);
674
- });
675
- }
676
- };
677
-
678
- // src/subscriber/RelaySubscriber.ts
679
- import { SimplePool } from "nostr-tools/pool";
680
- import { verifyEvent } from "nostr-tools/pure";
681
- var RelaySubscriber = class {
682
- config;
683
- eventStore;
684
- pool;
685
- started = false;
686
- /**
687
- * @param config - Subscriber configuration
688
- * @param eventStore - Storage backend to write events into
689
- * @param pool - Optional SimplePool instance (creates new one if not provided)
690
- */
691
- constructor(config, eventStore, pool) {
692
- this.config = config;
693
- this.eventStore = eventStore;
694
- this.pool = pool ?? new SimplePool();
695
- }
696
- /**
697
- * Start subscribing to the configured upstream relays.
698
- *
699
- * @returns Handle with unsubscribe() to stop the subscription
700
- * @throws Error if already started
701
- */
702
- start() {
703
- if (this.started) {
704
- throw new Error("RelaySubscriber already started");
705
- }
706
- this.started = true;
707
- const shouldVerify = this.config.verifySignatures !== false;
708
- let isUnsubscribed = false;
709
- const subCloser = this.pool.subscribeMany(
710
- this.config.relayUrls,
711
- this.config.filter,
712
- {
713
- onevent: (event) => {
714
- if (isUnsubscribed) return;
715
- if (shouldVerify && !verifyEvent(event)) {
716
- return;
717
- }
718
- try {
719
- this.eventStore.store(event);
720
- } catch (error) {
721
- console.warn(
722
- "[RelaySubscriber] Failed to store event:",
723
- error instanceof Error ? error.message : "Unknown error"
724
- );
725
- }
726
- }
727
- }
728
- );
729
- return {
730
- unsubscribe: () => {
731
- if (!isUnsubscribed) {
732
- isUnsubscribed = true;
733
- subCloser.close();
734
- this.started = false;
735
- }
736
- }
737
- };
738
- }
739
- };
740
-
741
- // src/launcher/handlers/event-storage-handler.ts
742
- function createEventStorageHandler(config) {
743
- const { eventStore } = config;
744
- return async (ctx) => {
745
- const event = ctx.decode();
746
- eventStore.store(event);
747
- return ctx.accept({ eventId: event.id, storedAt: Date.now() });
748
- };
749
- }
750
-
751
- // src/launcher/handlers/x402-pricing.ts
752
- function calculateX402Price(config, toonLength) {
753
- const clampedBuffer = Math.max(0, Math.min(200, config.routingBufferPercent));
754
- const basePrice = config.basePricePerByte * BigInt(toonLength);
755
- const buffer = basePrice * BigInt(clampedBuffer) / 100n;
756
- return basePrice + buffer;
757
- }
758
-
759
- // src/launcher/handlers/x402-types.ts
760
- var EIP_3009_TYPES = {
761
- TransferWithAuthorization: [
762
- { name: "from", type: "address" },
763
- { name: "to", type: "address" },
764
- { name: "value", type: "uint256" },
765
- { name: "validAfter", type: "uint256" },
766
- { name: "validBefore", type: "uint256" },
767
- { name: "nonce", type: "bytes32" }
768
- ]
769
- };
770
- var USDC_EIP712_DOMAIN = {
771
- name: "USD Coin",
772
- version: "2"
773
- };
774
- var USDC_ABI = [
775
- {
776
- name: "balanceOf",
777
- type: "function",
778
- stateMutability: "view",
779
- inputs: [{ name: "account", type: "address" }],
780
- outputs: [{ name: "", type: "uint256" }]
781
- },
782
- {
783
- name: "authorizationState",
784
- type: "function",
785
- stateMutability: "view",
786
- inputs: [
787
- { name: "authorizer", type: "address" },
788
- { name: "nonce", type: "bytes32" }
789
- ],
790
- outputs: [{ name: "", type: "bool" }]
791
- },
792
- {
793
- name: "transferWithAuthorization",
794
- type: "function",
795
- stateMutability: "nonpayable",
796
- inputs: [
797
- { name: "from", type: "address" },
798
- { name: "to", type: "address" },
799
- { name: "value", type: "uint256" },
800
- { name: "validAfter", type: "uint256" },
801
- { name: "validBefore", type: "uint256" },
802
- { name: "nonce", type: "bytes32" },
803
- { name: "v", type: "uint8" },
804
- { name: "r", type: "bytes32" },
805
- { name: "s", type: "bytes32" }
806
- ],
807
- outputs: []
808
- }
809
- ];
810
-
811
- // src/launcher/handlers/x402-preflight.ts
812
- import { verifyTypedData } from "viem";
813
- import { shallowParseToon } from "@toon-protocol/core/toon";
814
- async function runPreflight(authorization, toonData, destination, config) {
815
- const checksPerformed = [];
816
- checksPerformed.push("eip3009-signature");
817
- try {
818
- const domain = {
819
- ...USDC_EIP712_DOMAIN,
820
- chainId: config.chainConfig.chainId,
821
- verifyingContract: config.chainConfig.usdcAddress
822
- };
823
- const valid = await verifyTypedData({
824
- address: authorization.from,
825
- domain,
826
- types: EIP_3009_TYPES,
827
- primaryType: "TransferWithAuthorization",
828
- message: {
829
- from: authorization.from,
830
- to: authorization.to,
831
- value: authorization.value,
832
- validAfter: BigInt(authorization.validAfter),
833
- validBefore: BigInt(authorization.validBefore),
834
- nonce: authorization.nonce
835
- },
836
- signature: encodeSignature(authorization)
837
- });
838
- if (!valid) {
839
- return {
840
- passed: false,
841
- failedCheck: "eip3009-signature",
842
- checksPerformed
843
- };
844
- }
845
- } catch {
846
- return { passed: false, failedCheck: "eip3009-signature", checksPerformed };
847
- }
848
- checksPerformed.push("usdc-balance");
849
- if (config.publicClient) {
850
- try {
851
- const balance = await config.publicClient.readContract({
852
- address: config.chainConfig.usdcAddress,
853
- abi: USDC_ABI,
854
- functionName: "balanceOf",
855
- args: [authorization.from]
856
- });
857
- if (balance < authorization.value) {
858
- return { passed: false, failedCheck: "usdc-balance", checksPerformed };
859
- }
860
- } catch {
861
- return { passed: false, failedCheck: "usdc-balance", checksPerformed };
862
- }
863
- }
864
- checksPerformed.push("nonce-freshness");
865
- if (config.publicClient) {
866
- try {
867
- const used = await config.publicClient.readContract({
868
- address: config.chainConfig.usdcAddress,
869
- abi: USDC_ABI,
870
- functionName: "authorizationState",
871
- args: [
872
- authorization.from,
873
- authorization.nonce
874
- ]
875
- });
876
- if (used) {
877
- return {
878
- passed: false,
879
- failedCheck: "nonce-freshness",
880
- checksPerformed
881
- };
882
- }
883
- } catch {
884
- return { passed: false, failedCheck: "nonce-freshness", checksPerformed };
885
- }
886
- }
887
- checksPerformed.push("toon-shallow-parse");
888
- let toonMeta;
889
- try {
890
- const toonBytes = Buffer.from(toonData, "base64");
891
- toonMeta = shallowParseToon(toonBytes);
892
- } catch {
893
- return {
894
- passed: false,
895
- failedCheck: "toon-shallow-parse",
896
- checksPerformed
897
- };
898
- }
899
- checksPerformed.push("schnorr-signature");
900
- if (!config.devMode && config.schnorrVerify) {
901
- try {
902
- const valid = await config.schnorrVerify(toonMeta);
903
- if (!valid) {
904
- return {
905
- passed: false,
906
- failedCheck: "schnorr-signature",
907
- checksPerformed
908
- };
909
- }
910
- } catch {
911
- return {
912
- passed: false,
913
- failedCheck: "schnorr-signature",
914
- checksPerformed
915
- };
916
- }
917
- }
918
- checksPerformed.push("destination-reachability");
919
- if (config.eventStore) {
920
- try {
921
- const events = config.eventStore.query([{ kinds: [10032] }]);
922
- if (events.length === 0) {
923
- return {
924
- passed: false,
925
- failedCheck: "destination-reachability",
926
- checksPerformed
927
- };
928
- }
929
- } catch {
930
- return {
931
- passed: false,
932
- failedCheck: "destination-reachability",
933
- checksPerformed
934
- };
935
- }
936
- }
937
- return { passed: true, checksPerformed };
938
- }
939
- function encodeSignature(auth) {
940
- const r = auth.r.startsWith("0x") ? auth.r.slice(2) : auth.r;
941
- const s = auth.s.startsWith("0x") ? auth.s.slice(2) : auth.s;
942
- const v = auth.v.toString(16).padStart(2, "0");
943
- return `0x${r}${s}${v}`;
944
- }
945
-
946
- // src/launcher/handlers/x402-settlement.ts
947
- async function settleEip3009(authorization, config) {
948
- try {
949
- const hash = await config.walletClient.writeContract({
950
- address: config.chainConfig.usdcAddress,
951
- abi: USDC_ABI,
952
- functionName: "transferWithAuthorization",
953
- args: [
954
- authorization.from,
955
- authorization.to,
956
- authorization.value,
957
- BigInt(authorization.validAfter),
958
- BigInt(authorization.validBefore),
959
- authorization.nonce,
960
- authorization.v,
961
- authorization.r,
962
- authorization.s
963
- ],
964
- chain: null,
965
- // Use the wallet client's configured chain
966
- account: config.walletClient.account ?? null
967
- });
968
- if (config.publicClient) {
969
- const receipt = await config.publicClient.waitForTransactionReceipt({
970
- hash
971
- });
972
- if (receipt.status === "reverted") {
973
- return {
974
- success: false,
975
- error: "Transaction reverted on-chain"
976
- };
977
- }
978
- }
979
- return {
980
- success: true,
981
- txHash: hash
982
- };
983
- } catch (error) {
984
- const message = error instanceof Error ? error.message : "Unknown settlement error";
985
- return {
986
- success: false,
987
- error: message
988
- };
989
- }
990
- }
991
-
992
- // src/launcher/handlers/x402-publish-handler.ts
993
- import { buildIlpPrepare, encodeEventToToon as encodeEventToToon2 } from "@toon-protocol/core";
994
- function createX402Handler(config) {
995
- const encoder = config.toonEncoder ?? encodeEventToToon2;
996
- if (config.x402Enabled && (!config.facilitatorAddress || !/^0x[0-9a-fA-F]{40}$/.test(config.facilitatorAddress))) {
997
- throw new Error(
998
- "x402 enabled but facilitatorAddress is not a valid EVM address"
999
- );
1000
- }
1001
- return {
1002
- async handlePublish(c) {
1003
- if (!config.x402Enabled) {
1004
- return c.json({ error: "x402 not enabled" }, 404);
1005
- }
1006
- let body;
1007
- try {
1008
- body = await c.req.json();
1009
- } catch {
1010
- return c.json({ error: "Invalid request body" }, 400);
1011
- }
1012
- if (!body.event || !body.destination) {
1013
- return c.json(
1014
- { error: "Missing required fields: event, destination" },
1015
- 400
1016
- );
1017
- }
1018
- if (typeof body.destination !== "string" || !body.destination.startsWith("g.")) {
1019
- return c.json(
1020
- { error: "Invalid destination: must be a global ILP address (g.*)" },
1021
- 400
1022
- );
1023
- }
1024
- let toonBytes;
1025
- try {
1026
- toonBytes = encoder(body.event);
1027
- } catch {
1028
- return c.json({ error: "Failed to TOON-encode event" }, 400);
1029
- }
1030
- const toonBase64 = Buffer.from(toonBytes).toString("base64");
1031
- const paymentHeader = c.req.header("X-PAYMENT");
1032
- if (!paymentHeader) {
1033
- const price = calculateX402Price(
1034
- {
1035
- basePricePerByte: config.basePricePerByte,
1036
- routingBufferPercent: config.routingBufferPercent
1037
- },
1038
- toonBytes.length
1039
- );
1040
- const pricing = {
1041
- amount: String(price),
1042
- facilitatorAddress: config.facilitatorAddress,
1043
- paymentNetwork: "eip-3009",
1044
- chainId: config.chainConfig.chainId,
1045
- usdcAddress: config.chainConfig.usdcAddress
1046
- };
1047
- return c.json(pricing, 402);
1048
- }
1049
- let authorization;
1050
- try {
1051
- const parsed = JSON.parse(paymentHeader);
1052
- authorization = parseAuthorization(parsed);
1053
- } catch {
1054
- return c.json({ error: "Invalid X-PAYMENT header" }, 400);
1055
- }
1056
- const preflightConfig = {
1057
- chainConfig: config.chainConfig,
1058
- basePricePerByte: config.basePricePerByte,
1059
- ownPubkey: config.ownPubkey,
1060
- devMode: config.devMode,
1061
- publicClient: config.publicClient,
1062
- eventStore: config.eventStore
1063
- };
1064
- try {
1065
- const preflightFn = config.runPreflightFn ?? runPreflight;
1066
- const preflightResult = await preflightFn(
1067
- authorization,
1068
- toonBase64,
1069
- body.destination,
1070
- preflightConfig
1071
- );
1072
- if (!preflightResult.passed) {
1073
- return c.json(
1074
- {
1075
- error: `Pre-flight check failed: ${preflightResult.failedCheck}`,
1076
- failedCheck: preflightResult.failedCheck
1077
- },
1078
- 400
1079
- );
1080
- }
1081
- } catch {
1082
- console.error("[x402] Pre-flight error");
1083
- return c.json({ error: "Internal server error" }, 500);
1084
- }
1085
- let settlementResult;
1086
- try {
1087
- const settleFn = config.settle ?? settleEip3009;
1088
- if (!config.settle && !config.walletClient) {
1089
- console.error("[x402] Settlement error: walletClient not configured");
1090
- return c.json({ error: "Internal server error" }, 500);
1091
- }
1092
- const settlementConfig = {
1093
- chainConfig: config.chainConfig,
1094
- walletClient: config.walletClient,
1095
- publicClient: config.publicClient
1096
- };
1097
- settlementResult = await settleFn(authorization, settlementConfig);
1098
- } catch {
1099
- console.error("[x402] Settlement error");
1100
- return c.json({ error: "Internal server error" }, 500);
1101
- }
1102
- if (!settlementResult.success) {
1103
- console.error(
1104
- "[x402] Settlement failed:",
1105
- settlementResult.error ?? "unknown"
1106
- );
1107
- return c.json({ error: "Settlement failed" }, 400);
1108
- }
1109
- const amount = config.basePricePerByte * BigInt(toonBytes.length);
1110
- const prepareParams = {
1111
- destination: body.destination,
1112
- amount,
1113
- data: toonBytes
1114
- };
1115
- const prepare = buildIlpPrepare(prepareParams);
1116
- let deliveryStatus = "rejected";
1117
- if (config.ilpClient) {
1118
- try {
1119
- const ilpResult = await config.ilpClient.sendIlpPacket(prepare);
1120
- deliveryStatus = ilpResult.accepted ? "fulfilled" : "rejected";
1121
- } catch {
1122
- deliveryStatus = "rejected";
1123
- }
1124
- }
1125
- const response = {
1126
- eventId: body.event.id,
1127
- settlementTxHash: settlementResult.txHash ?? "",
1128
- deliveryStatus,
1129
- refundInitiated: false
1130
- };
1131
- return c.json(response, 200);
1132
- }
1133
- };
1134
- }
1135
- function isValidHex(value, expectedLength) {
1136
- if (value.length !== expectedLength) return false;
1137
- if (!value.startsWith("0x")) return false;
1138
- return /^0x[0-9a-fA-F]+$/.test(value);
1139
- }
1140
- function parseAuthorization(parsed) {
1141
- if (typeof parsed !== "object" || parsed === null) {
1142
- throw new Error("Authorization must be an object");
1143
- }
1144
- const obj = parsed;
1145
- const from = obj["from"];
1146
- const to = obj["to"];
1147
- const value = obj["value"];
1148
- const validAfter = obj["validAfter"];
1149
- const validBefore = obj["validBefore"];
1150
- const nonce = obj["nonce"];
1151
- const v = obj["v"];
1152
- const r = obj["r"];
1153
- const s = obj["s"];
1154
- if (typeof from !== "string" || !isValidHex(from, 42)) {
1155
- throw new Error("Invalid from address");
1156
- }
1157
- if (typeof to !== "string" || !isValidHex(to, 42)) {
1158
- throw new Error("Invalid to address");
1159
- }
1160
- if (typeof nonce !== "string" || !isValidHex(nonce, 66)) {
1161
- throw new Error("Invalid nonce");
1162
- }
1163
- if (typeof r !== "string" || !isValidHex(r, 66)) {
1164
- throw new Error("Invalid r");
1165
- }
1166
- if (typeof s !== "string" || !isValidHex(s, 66)) {
1167
- throw new Error("Invalid s");
1168
- }
1169
- if (typeof v !== "number" || v !== 27 && v !== 28) {
1170
- throw new Error("Invalid v");
1171
- }
1172
- const parsedValidAfter = Number(validAfter);
1173
- const parsedValidBefore = Number(validBefore);
1174
- if (Number.isNaN(parsedValidAfter) || parsedValidAfter < 0) {
1175
- throw new Error("Invalid validAfter");
1176
- }
1177
- if (Number.isNaN(parsedValidBefore) || parsedValidBefore < 0) {
1178
- throw new Error("Invalid validBefore");
1179
- }
1180
- const valueStr = String(value);
1181
- let valueBigInt;
1182
- try {
1183
- valueBigInt = BigInt(valueStr);
1184
- } catch {
1185
- throw new Error("Invalid value");
1186
- }
1187
- if (valueBigInt < 0n) {
1188
- throw new Error("Invalid value: must be non-negative");
1189
- }
1190
- return {
1191
- from,
1192
- to,
1193
- value: valueBigInt,
1194
- validAfter: parsedValidAfter,
1195
- validBefore: parsedValidBefore,
1196
- nonce,
1197
- v,
1198
- r,
1199
- s
1200
- };
1201
- }
1202
-
1203
- // src/launcher/health.ts
1204
- import { VERSION } from "@toon-protocol/core";
1205
- function createHealthResponse(config) {
1206
- const response = {
1207
- status: "healthy",
1208
- phase: config.phase,
1209
- pubkey: config.pubkey,
1210
- ilpAddress: config.ilpAddress,
1211
- peerCount: config.peerCount,
1212
- discoveredPeerCount: config.discoveredPeerCount,
1213
- channelCount: config.channelCount,
1214
- pricing: {
1215
- basePricePerByte: Number(config.basePricePerByte),
1216
- currency: "USDC"
1217
- },
1218
- capabilities: config.x402Enabled ? ["relay", "x402"] : ["relay"],
1219
- chain: config.chain,
1220
- version: VERSION,
1221
- sdk: true,
1222
- timestamp: Date.now()
1223
- };
1224
- if (config.x402Enabled) {
1225
- response.x402 = {
1226
- enabled: true,
1227
- endpoint: "/publish"
1228
- };
1229
- }
1230
- if (config.tee) {
1231
- response.tee = config.tee;
1232
- }
1233
- return response;
1234
- }
1235
-
1236
- // src/launcher/town.ts
1237
- import { mkdirSync } from "fs";
1238
- import { join } from "path";
1239
- import { serve } from "@hono/node-server";
1240
- import { Hono } from "hono";
1241
- import {
1242
- HandlerRegistry,
1243
- createVerificationPipeline,
1244
- createPricingValidator,
1245
- createHandlerContext,
1246
- fromMnemonic,
1247
- fromSecretKey
1248
- } from "@toon-protocol/sdk";
1249
-
1250
- // src/launcher/handlers/oblivious-write-handler.ts
1251
- import { verifyEvent as verifyEvent2 } from "nostr-tools/pure";
1252
- function createObliviousWriteHandler(config) {
1253
- return {
1254
- async handleWrite(c) {
1255
- let body;
1256
- try {
1257
- body = await c.req.json();
1258
- } catch {
1259
- return c.json({ error: "Invalid request body" }, 400);
1260
- }
1261
- if (!body.event) {
1262
- return c.json({ error: "Missing required field: event" }, 400);
1263
- }
1264
- const event = body.event;
1265
- const payer = c.req.header("X-TOON-Payer");
1266
- const amount = c.req.header("X-TOON-Amount");
1267
- const chain = c.req.header("X-TOON-Chain");
1268
- console.log(
1269
- `[oblivious-write] event=${event.id} payer=${payer ?? "-"} amount=${amount ?? "-"} chain=${chain ?? "-"}`
1270
- );
1271
- if (!config.devMode && !verifyEvent2(event)) {
1272
- return c.json({ error: "Invalid event signature" }, 422);
1273
- }
1274
- config.eventStore.store(event);
1275
- config.onStored?.(event);
1276
- return c.json(
1277
- {
1278
- eventId: event.id,
1279
- storedAt: Math.floor(Date.now() / 1e3),
1280
- payer,
1281
- amount,
1282
- chain
1283
- },
1284
- 200
1285
- );
1286
- }
1287
- };
1288
- }
1289
-
1290
- // src/launcher/town.ts
1291
- import {
1292
- BootstrapService,
1293
- createDiscoveryTracker,
1294
- ILP_PEER_INFO_KIND,
1295
- createDirectIlpClient,
1296
- createDirectConnectorAdmin,
1297
- createDirectChannelClient,
1298
- SocialPeerDiscovery,
1299
- buildIlpPeerInfoEvent,
1300
- resolveChainConfig,
1301
- SeedRelayDiscovery,
1302
- publishSeedRelayEntry,
1303
- buildServiceDiscoveryEvent,
1304
- VERSION as VERSION2
1305
- } from "@toon-protocol/core";
1306
- import {
1307
- shallowParseToon as shallowParseToon2,
1308
- decodeEventFromToon as decodeEventFromToon2,
1309
- encodeEventToToon as encodeEventToToon3
1310
- } from "@toon-protocol/core/toon";
1311
- import {
1312
- ConnectorNode,
1313
- createLogger as createConnectorLogger
1314
- } from "@toon-protocol/connector";
1315
- import {
1316
- createPublicClient,
1317
- createWalletClient,
1318
- defineChain,
1319
- http
1320
- } from "viem";
1321
- import { privateKeyToAccount } from "viem/accounts";
1322
- var MAX_PAYLOAD_BASE64_LENGTH = 1048576;
1323
- function createSubscription(relayUrl, filter, eventStore, activeSubscriptions) {
1324
- if (!relayUrl.startsWith("ws://") && !relayUrl.startsWith("wss://")) {
1325
- throw new Error(
1326
- "Invalid relay URL -- must use WebSocket scheme (ws or wss)"
1327
- );
1328
- }
1329
- const subscriber = new RelaySubscriber(
1330
- { relayUrls: [relayUrl], filter },
1331
- eventStore
1332
- );
1333
- const handle = subscriber.start();
1334
- let active = true;
1335
- let _lastSeenTimestamp = 0;
1336
- void _lastSeenTimestamp;
1337
- const subscription = {
1338
- close() {
1339
- if (!active) return;
1340
- active = false;
1341
- handle.unsubscribe();
1342
- activeSubscriptions.delete(subscription);
1343
- },
1344
- relayUrl,
1345
- isActive() {
1346
- return active;
1347
- }
1348
- };
1349
- activeSubscriptions.add(subscription);
1350
- return subscription;
1351
- }
1352
- async function startRelay(config) {
1353
- const hasMnemonic = config.mnemonic !== void 0;
1354
- const hasSecretKey = config.secretKey !== void 0;
1355
- if (hasMnemonic && hasSecretKey) {
1356
- throw new Error(
1357
- "RelayConfig: provide either mnemonic or secretKey, not both"
1358
- );
1359
- }
1360
- if (!hasMnemonic && !hasSecretKey) {
1361
- throw new Error("RelayConfig: one of mnemonic or secretKey is required");
1362
- }
1363
- const hasConnector = config.connector !== void 0;
1364
- const hasConnectorUrl = config.connectorUrl !== void 0;
1365
- if (hasConnector && hasConnectorUrl) {
1366
- throw new Error(
1367
- "RelayConfig: provide either connector or connectorUrl, not both"
1368
- );
1369
- }
1370
- const obliviousMode = config.obliviousMode ?? process.env["TOON_OBLIVIOUS_MODE"] === "true";
1371
- if (obliviousMode && (hasConnector || hasConnectorUrl)) {
1372
- throw new Error(
1373
- "RelayConfig: obliviousMode is mutually exclusive with connector/connectorUrl (an oblivious relay runs no embedded connector)"
1374
- );
1375
- }
1376
- if (hasConnectorUrl && config.ilpAddress === void 0) {
1377
- throw new Error(
1378
- "RelayConfig: ilpAddress is required when connectorUrl is set (must fall under the parent connector prefix, e.g. g.townhouse.<self>)"
1379
- );
1380
- }
1381
- const identity = hasMnemonic ? fromMnemonic(config.mnemonic) : fromSecretKey(config.secretKey);
1382
- const relayPort = config.relayPort ?? 7100;
1383
- const blsPort = config.blsPort ?? 3100;
1384
- const pubkeyShort = identity.pubkey.slice(0, 16);
1385
- const ilpAddress = config.ilpAddress ?? `g.toon.${pubkeyShort}`;
1386
- const btpEndpoint = config.btpEndpoint ?? "";
1387
- const nodeId = config.nodeId ?? `toon-${pubkeyShort}`;
1388
- const parentPeerId = config.parentPeerId ?? "apex";
1389
- const parentAuthToken = config.parentAuthToken ?? "";
1390
- const connectorUrl = config.connectorUrl;
1391
- const basePricePerByte = config.feePerEvent !== void 0 ? BigInt(config.feePerEvent) : config.basePricePerByte ?? 10n;
1392
- const routingBufferPercent = config.routingBufferPercent ?? 10;
1393
- const x402Enabled = obliviousMode ? false : config.x402Enabled ?? false;
1394
- const knownPeers = [...config.knownPeers ?? []];
1395
- const dataDir = config.dataDir ?? "./data";
1396
- const devMode = config.devMode ?? false;
1397
- const ardriveEnabled = config.ardriveEnabled ?? false;
1398
- const relayUrls = config.relayUrls ?? [`ws://localhost:${relayPort}`];
1399
- const assetCode = config.assetCode ?? "USD";
1400
- const assetScale = config.assetScale ?? 6;
1401
- const discovery = config.discovery ?? "genesis";
1402
- const announcementTtlSeconds = (() => {
1403
- const fromEnv = process.env["TOON_ANNOUNCEMENT_TTL_SECONDS"];
1404
- const raw = fromEnv !== void 0 && fromEnv !== "" ? Number(fromEnv) : config.announcementTtlSeconds;
1405
- if (raw === void 0) return 3600;
1406
- if (!Number.isFinite(raw) || raw < 0) return 3600;
1407
- return Math.floor(raw);
1408
- })();
1409
- const seedRelays = config.seedRelays ?? [];
1410
- const publishSeedEntryFlag = config.publishSeedEntry ?? false;
1411
- const externalRelayUrl = config.externalRelayUrl ?? (config.ator?.enabled && config.ator.anonAddress ? config.ator.anonAddress : void 0);
1412
- const requestedChain = process.env["TOON_CHAIN"] || config.chain;
1413
- const relayOnly = requestedChain === "none" || obliviousMode;
1414
- if (relayOnly) {
1415
- console.log("[Town] connector.relay_only", {
1416
- reason: "no settlement chain configured (chain=none)"
1417
- });
1418
- }
1419
- const chainConfig = relayOnly ? {
1420
- name: "none",
1421
- chainId: 0,
1422
- rpcUrl: "",
1423
- usdcAddress: "",
1424
- tokenNetworkAddress: "",
1425
- registryAddress: ""
1426
- } : resolveChainConfig(config.chain);
1427
- const chainKey = `evm:base:${chainConfig.chainId}`;
1428
- const resolvedConfig = {
1429
- relayPort,
1430
- blsPort,
1431
- ilpAddress,
1432
- btpEndpoint,
1433
- nodeId,
1434
- ...connectorUrl && { connectorUrl, parentPeerId },
1435
- basePricePerByte,
1436
- routingBufferPercent,
1437
- x402Enabled,
1438
- knownPeers,
1439
- dataDir,
1440
- devMode,
1441
- ardriveEnabled,
1442
- relayUrls,
1443
- assetCode,
1444
- assetScale,
1445
- discovery,
1446
- seedRelays,
1447
- publishSeedEntry: publishSeedEntryFlag,
1448
- ...externalRelayUrl && { externalRelayUrl },
1449
- chain: chainConfig.name,
1450
- obliviousMode
1451
- };
1452
- let autoCreatedConnector = null;
1453
- if (!hasConnector && !obliviousMode) {
1454
- const btpServerPort = config.btpServerPort ?? 3e3;
1455
- const connectorLogger = createConnectorLogger(
1456
- nodeId,
1457
- process.env["TOON_CONNECTOR_LOG_LEVEL"] ?? "warn"
1458
- );
1459
- const routes = [{ prefix: ilpAddress, nextHop: nodeId, priority: 100 }];
1460
- const peers = [];
1461
- if (hasConnectorUrl) {
1462
- peers.push({
1463
- id: parentPeerId,
1464
- url: connectorUrl,
1465
- authToken: parentAuthToken,
1466
- // Tag the upstream as our PARENT so the embedded connector's
1467
- // relation-aware logic applies (toon-protocol/connector#78): a child
1468
- // skips the inbound per-packet-claim requirement for PREPAREs forwarded
1469
- // by its parent (the parent settles in aggregate and attaches no
1470
- // per-packet claim to a child). Without this the peer defaults to
1471
- // 'peer' and the child F06-rejects every parent-forwarded paid packet.
1472
- // NOTE: `parentPeerId` MUST equal the parent connector's nodeId (its BTP
1473
- // auth identity), since the connector keys peerRelations by the
1474
- // auth-declared peerId of the inbound session — not a local alias.
1475
- relation: "parent",
1476
- // When the operator publishes their EVM treasury address to the
1477
- // parent, the apex can open a settlement channel toward this child
1478
- // without needing to discover the address via kind:10032. The
1479
- // connector schema treats this field as optional metadata.
1480
- ...config.parentEvmAddress && { evmAddress: config.parentEvmAddress }
1481
- });
1482
- routes.push({ prefix: "g", nextHop: parentPeerId, priority: 0 });
1483
- }
1484
- const hasSettlementAddresses = !!chainConfig.rpcUrl && !!chainConfig.registryAddress && !!chainConfig.tokenNetworkAddress && !!chainConfig.usdcAddress;
1485
- let chainProvidersEntry = null;
1486
- if (hasSettlementAddresses) {
1487
- const keyHex = config.settlementPrivateKey ?? `0x${Buffer.from(identity.secretKey).toString("hex")}`;
1488
- if (!/^0x[0-9a-fA-F]{64}$/.test(keyHex)) {
1489
- throw new Error(
1490
- `RelayConfig.settlementPrivateKey must be a 0x-prefixed 32-byte hex string (got length ${keyHex.length}); cannot wire chainProviders for ${chainConfig.name}`
1491
- );
1492
- }
1493
- chainProvidersEntry = {
1494
- chainType: "evm",
1495
- chainId: `evm:${chainConfig.chainId}`,
1496
- rpcUrl: chainConfig.rpcUrl,
1497
- registryAddress: chainConfig.registryAddress,
1498
- tokenAddress: chainConfig.usdcAddress,
1499
- keyId: keyHex
1500
- };
1501
- } else {
1502
- console.warn("[Town] connector.chain_providers_skipped", {
1503
- chain: chainConfig.name,
1504
- reason: "missing settlement addresses"
1505
- });
1506
- }
1507
- const connectorConfig = {
1508
- nodeId,
1509
- btpServerPort,
1510
- environment: "development",
1511
- deploymentMode: "embedded",
1512
- peers,
1513
- routes,
1514
- localDelivery: { enabled: false },
1515
- // Children don't expose an admin API — the apex parent is the
1516
- // operator-facing surface. Disabling avoids a hard runtime dep on
1517
- // express in the town docker bundle.
1518
- adminApi: { enabled: false },
1519
- // Belt-and-braces: zero connector forwarding fee. Combined with the
1520
- // packet-handler's automatic skip for local-delivery hops this keeps the
1521
- // child fee surface flat regardless of peering topology.
1522
- settlement: {
1523
- connectorFeePercentage: 0
1524
- },
1525
- ...chainProvidersEntry && { chainProviders: [chainProvidersEntry] }
1526
- };
1527
- if (config.ator?.enabled && config.ator.anonAddress) {
1528
- connectorConfig.transport = {
1529
- type: "socks5",
1530
- socksProxy: config.ator.socksProxy ?? "socks5h://127.0.0.1:9050",
1531
- externalUrl: config.ator.anonAddress,
1532
- managed: false
1533
- };
1534
- }
1535
- autoCreatedConnector = new ConnectorNode(connectorConfig, connectorLogger);
1536
- }
1537
- const effectiveConnector = config.connector ?? autoCreatedConnector;
1538
- mkdirSync(dataDir, { recursive: true });
1539
- const dbPath = join(dataDir, "events.db");
1540
- const eventStore = config.eventStore ?? new SqliteEventStore(dbPath);
1541
- const effectiveChainRpcUrls = config.chainRpcUrls ?? (relayOnly ? void 0 : { [chainKey]: chainConfig.rpcUrl });
1542
- const effectivePreferredTokens = config.preferredTokens ?? (relayOnly ? void 0 : { [chainKey]: chainConfig.usdcAddress });
1543
- const effectiveTokenNetworks = config.tokenNetworks ?? (chainConfig.tokenNetworkAddress ? { [chainKey]: chainConfig.tokenNetworkAddress } : void 0);
1544
- let channelClient;
1545
- let settlementInfo;
1546
- const hasSettlement = effectiveChainRpcUrls || effectiveTokenNetworks || effectivePreferredTokens || config.settlementAddresses;
1547
- if (hasSettlement) {
1548
- const supportedChains = Array.from(
1549
- /* @__PURE__ */ new Set([
1550
- ...Object.keys(effectiveChainRpcUrls ?? {}),
1551
- ...Object.keys(effectiveTokenNetworks ?? {}),
1552
- ...Object.keys(effectivePreferredTokens ?? {}),
1553
- ...Object.keys(config.settlementAddresses ?? {})
1554
- ])
1555
- );
1556
- const settlementAddresses = {};
1557
- for (const chain of supportedChains) {
1558
- settlementAddresses[chain] = config.settlementAddresses?.[chain] ?? identity.evmAddress;
1559
- }
1560
- settlementInfo = {
1561
- supportedChains,
1562
- settlementAddresses,
1563
- preferredTokens: effectivePreferredTokens,
1564
- tokenNetworks: effectiveTokenNetworks
1565
- };
1566
- if (effectiveConnector?.openChannel && effectiveConnector.getChannelState) {
1567
- channelClient = createDirectChannelClient(
1568
- effectiveConnector
1569
- );
1570
- }
1571
- }
1572
- const adminClient = effectiveConnector ? createDirectConnectorAdmin(effectiveConnector) : void 0;
1573
- const verifier = createVerificationPipeline({ devMode });
1574
- const pricer = createPricingValidator({
1575
- basePricePerByte,
1576
- ownPubkey: identity.pubkey
1577
- });
1578
- const registry = new HandlerRegistry();
1579
- registry.onDefault(createEventStorageHandler({ eventStore }));
1580
- const toonDecoder = (toon) => {
1581
- const bytes = Buffer.from(toon, "base64");
1582
- return decodeEventFromToon2(bytes);
1583
- };
1584
- const handlePacket = async (request) => {
1585
- if (request.data.length > MAX_PAYLOAD_BASE64_LENGTH) {
1586
- return { accept: false, code: "F08", message: "Payload too large" };
1587
- }
1588
- const toonBytes = Buffer.from(request.data, "base64");
1589
- let meta;
1590
- try {
1591
- meta = shallowParseToon2(toonBytes);
1592
- } catch {
1593
- return { accept: false, code: "F06", message: "Invalid TOON payload" };
1594
- }
1595
- const verifyResult = await verifier.verify(meta, request.data);
1596
- if (!verifyResult.verified) {
1597
- if (verifyResult.rejection) {
1598
- return verifyResult.rejection;
1599
- }
1600
- return { accept: false, code: "F06", message: "Verification failed" };
1601
- }
1602
- let amount;
1603
- try {
1604
- amount = BigInt(request.amount);
1605
- } catch {
1606
- return {
1607
- accept: false,
1608
- code: "T00",
1609
- message: "Invalid payment amount"
1610
- };
1611
- }
1612
- const priceResult = pricer.validate(meta, amount);
1613
- if (!priceResult.accepted) {
1614
- if (priceResult.rejection) {
1615
- return priceResult.rejection;
1616
- }
1617
- return {
1618
- accept: false,
1619
- code: "F04",
1620
- message: "Pricing validation failed"
1621
- };
1622
- }
1623
- const ctx = createHandlerContext({
1624
- toon: request.data,
1625
- meta,
1626
- amount,
1627
- destination: request.destination,
1628
- toonDecoder
1629
- });
1630
- try {
1631
- const result = await registry.dispatch(ctx);
1632
- if (result.accept) {
1633
- try {
1634
- const event = decodeEventFromToon2(toonBytes);
1635
- wsRelayRef.current?.broadcastEvent(event);
1636
- } catch {
1637
- }
1638
- }
1639
- return result;
1640
- } catch (err) {
1641
- const errMsg = err instanceof Error ? err.message : "Unknown error";
1642
- console.error("[Town] Handler dispatch failed:", errMsg);
1643
- return { accept: false, code: "T00", message: "Internal error" };
1644
- }
1645
- };
1646
- const bootstrapService = new BootstrapService(
1647
- {
1648
- knownPeers,
1649
- ardriveEnabled,
1650
- defaultRelayUrl: `ws://localhost:${relayPort}`,
1651
- ...settlementInfo && { settlementInfo },
1652
- ownIlpAddress: ilpAddress,
1653
- toonEncoder: encodeEventToToon3,
1654
- toonDecoder: decodeEventFromToon2,
1655
- basePricePerByte
1656
- },
1657
- identity.secretKey,
1658
- {
1659
- ilpAddress,
1660
- btpEndpoint,
1661
- assetCode,
1662
- assetScale
1663
- }
1664
- );
1665
- let peerCount = 0;
1666
- let channelCount = 0;
1667
- const discoveryTrackerRef = {};
1668
- const wsRelayRef = {};
1669
- const app = new Hono();
1670
- app.get("/health", (c) => {
1671
- const bootstrapPhase = bootstrapService.getPhase();
1672
- const dt = discoveryTrackerRef.current;
1673
- return c.json(
1674
- createHealthResponse({
1675
- phase: bootstrapPhase,
1676
- pubkey: identity.pubkey,
1677
- ilpAddress,
1678
- peerCount: (dt ? dt.getPeerCount() : 0) + peerCount,
1679
- discoveredPeerCount: dt ? dt.getDiscoveredCount() : 0,
1680
- channelCount,
1681
- basePricePerByte,
1682
- x402Enabled,
1683
- chain: chainConfig.name
1684
- })
1685
- );
1686
- });
1687
- if (!obliviousMode) {
1688
- app.post("/handle-packet", async (c) => {
1689
- try {
1690
- const body = await c.req.json();
1691
- if (body.amount === void 0 || body.amount === null || body.destination === void 0 || body.destination === null || body.data === void 0 || body.data === null) {
1692
- return c.json(
1693
- { accept: false, code: "F00", message: "Missing required fields" },
1694
- 400
1695
- );
1696
- }
1697
- const result = await handlePacket(body);
1698
- if (result.accept) {
1699
- try {
1700
- const toonBytes = Buffer.from(body.data, "base64");
1701
- const decoded = decodeEventFromToon2(toonBytes);
1702
- if (decoded && decoded.kind === ILP_PEER_INFO_KIND) {
1703
- discoveryTrackerRef.current?.processEvent(decoded);
1704
- }
1705
- } catch {
1706
- }
1707
- }
1708
- return c.json(result, result.accept ? 200 : 400);
1709
- } catch (error) {
1710
- console.error("[Town] handle-packet error:", error);
1711
- return c.json(
1712
- { accept: false, code: "T00", message: "Internal server error" },
1713
- 500
1714
- );
1715
- }
1716
- });
1717
- }
1718
- const ilpClient = effectiveConnector ? createDirectIlpClient(effectiveConnector, {
1719
- toonDecoder: (bytes) => decodeEventFromToon2(bytes)
1720
- }) : void 0;
1721
- let x402WalletClient;
1722
- let x402PublicClient;
1723
- if (x402Enabled) {
1724
- let keyBuffer;
1725
- try {
1726
- keyBuffer = Buffer.from(identity.secretKey);
1727
- const privateKeyHex = `0x${keyBuffer.toString("hex")}`;
1728
- const account = privateKeyToAccount(privateKeyHex);
1729
- const viemChain = defineChain({
1730
- id: chainConfig.chainId,
1731
- name: chainConfig.name,
1732
- nativeCurrency: { name: "ETH", symbol: "ETH", decimals: 18 },
1733
- rpcUrls: { default: { http: [] } }
1734
- });
1735
- x402PublicClient = createPublicClient({
1736
- chain: viemChain,
1737
- transport: http(chainConfig.rpcUrl)
1738
- });
1739
- x402WalletClient = createWalletClient({
1740
- account,
1741
- chain: viemChain,
1742
- transport: http(chainConfig.rpcUrl)
1743
- });
1744
- } catch (error) {
1745
- throw new Error(
1746
- `x402 initialization failed: could not derive EVM account from identity key: ${error instanceof Error ? error.message : String(error)}`
1747
- );
1748
- } finally {
1749
- if (keyBuffer) {
1750
- keyBuffer.fill(0);
1751
- }
1752
- }
1753
- }
1754
- if (obliviousMode) {
1755
- const obliviousHandler = createObliviousWriteHandler({
1756
- eventStore,
1757
- devMode,
1758
- onStored: (event) => {
1759
- try {
1760
- wsRelayRef.current?.broadcastEvent(event);
1761
- } catch {
1762
- }
1763
- if (event.kind === ILP_PEER_INFO_KIND) {
1764
- discoveryTrackerRef.current?.processEvent(event);
1765
- }
1766
- }
1767
- });
1768
- app.post("/write", (c) => obliviousHandler.handleWrite(c));
1769
- } else {
1770
- const x402Handler = createX402Handler({
1771
- x402Enabled,
1772
- chainConfig,
1773
- basePricePerByte,
1774
- routingBufferPercent,
1775
- facilitatorAddress: config.facilitatorAddress ?? identity.evmAddress,
1776
- ownPubkey: identity.pubkey,
1777
- devMode,
1778
- eventStore,
1779
- ilpClient,
1780
- walletClient: x402WalletClient,
1781
- publicClient: x402PublicClient
1782
- });
1783
- app.get("/publish", (c) => x402Handler.handlePublish(c));
1784
- app.post("/publish", (c) => x402Handler.handlePublish(c));
1785
- }
1786
- const blsServer = serve({
1787
- fetch: app.fetch,
1788
- port: blsPort
1789
- });
1790
- const relayHost = config.ator?.enabled ? "127.0.0.1" : void 0;
1791
- const wsRelay = new NostrRelayServer(
1792
- { port: relayPort, host: relayHost },
1793
- eventStore
1794
- );
1795
- wsRelayRef.current = wsRelay;
1796
- await wsRelay.start();
1797
- await new Promise((resolve) => setTimeout(resolve, 500));
1798
- let running = true;
1799
- if (adminClient) {
1800
- bootstrapService.setConnectorAdmin(adminClient);
1801
- }
1802
- if (channelClient) {
1803
- bootstrapService.setChannelClient(channelClient);
1804
- }
1805
- if (ilpClient) {
1806
- bootstrapService.setIlpClient(ilpClient);
1807
- }
1808
- bootstrapService.on((event) => {
1809
- switch (event.type) {
1810
- case "bootstrap:peer-registered":
1811
- peerCount++;
1812
- break;
1813
- case "bootstrap:channel-opened":
1814
- channelCount++;
1815
- break;
1816
- case "bootstrap:ready":
1817
- break;
1818
- }
1819
- });
1820
- if (effectiveConnector?.setPacketHandler) {
1821
- effectiveConnector.setPacketHandler(async (request) => {
1822
- const result = await handlePacket(request);
1823
- if (result.accept && discoveryTrackerRef.current) {
1824
- try {
1825
- const toonBytes = Buffer.from(
1826
- request.data,
1827
- "base64"
1828
- );
1829
- const decoded = decodeEventFromToon2(toonBytes);
1830
- if (decoded && decoded.kind === ILP_PEER_INFO_KIND) {
1831
- discoveryTrackerRef.current.processEvent(decoded);
1832
- }
1833
- } catch {
1834
- }
1835
- }
1836
- return result;
1837
- });
1838
- }
1839
- if (autoCreatedConnector) {
1840
- await autoCreatedConnector.start();
1841
- }
1842
- const discoveryTracker = createDiscoveryTracker({
1843
- secretKey: identity.secretKey,
1844
- settlementInfo
1845
- });
1846
- if (adminClient) {
1847
- discoveryTracker.setConnectorAdmin(adminClient);
1848
- }
1849
- if (channelClient) {
1850
- discoveryTracker.setChannelClient(channelClient);
1851
- }
1852
- discoveryTrackerRef.current = discoveryTracker;
1853
- let seedRelayDiscovery;
1854
- if (discovery === "seed-list" && seedRelays.length > 0) {
1855
- seedRelayDiscovery = new SeedRelayDiscovery({
1856
- publicRelays: seedRelays
1857
- });
1858
- try {
1859
- const seedResult = await seedRelayDiscovery.discover();
1860
- const seedPeers = seedResult.discoveredPeers.filter((info) => info.pubkey).map((info) => ({
1861
- pubkey: info.pubkey,
1862
- relayUrl: seedResult.connectedUrls[0] ?? `ws://localhost:${relayPort}`,
1863
- btpEndpoint: info.btpEndpoint
1864
- }));
1865
- const existingPubkeys = new Set(knownPeers.map((p) => p.pubkey));
1866
- for (const seedPeer of seedPeers) {
1867
- if (!existingPubkeys.has(seedPeer.pubkey)) {
1868
- knownPeers.push(seedPeer);
1869
- }
1870
- }
1871
- console.log(
1872
- `[Town] Seed relay discovery: found ${seedPeers.length} peers from ${seedResult.connectedUrls.length} seed relay(s)`
1873
- );
1874
- } catch (seedError) {
1875
- const msg = seedError instanceof Error ? seedError.message : "Unknown error";
1876
- console.warn(`[Town] Seed relay discovery failed: ${msg}`);
1877
- }
1878
- }
1879
- let announcementHeartbeat;
1880
- try {
1881
- const results = await bootstrapService.bootstrap();
1882
- const ownIlpInfo = {
1883
- ilpAddress,
1884
- btpEndpoint,
1885
- assetCode,
1886
- assetScale,
1887
- // Advertise the publish price (per byte, in ILP base units) so clients can
1888
- // compute the amount to attach before sending — derived from feePerEvent /
1889
- // basePricePerByte. Previously omitted, leaving peers to assume free.
1890
- feePerByte: String(basePricePerByte),
1891
- // Public Nostr relay URL for FREE reads, so clients discover where to
1892
- // subscribe (separate from btpEndpoint, which is the pay-to-write path).
1893
- // Set when the operator exposes the relay publicly (HS .anyone or direct).
1894
- ...externalRelayUrl && { relayUrl: externalRelayUrl },
1895
- ...settlementInfo?.supportedChains && {
1896
- supportedChains: settlementInfo.supportedChains
1897
- },
1898
- ...settlementInfo?.settlementAddresses && {
1899
- settlementAddresses: settlementInfo.settlementAddresses
1900
- },
1901
- ...settlementInfo?.preferredTokens && {
1902
- preferredTokens: settlementInfo.preferredTokens
1903
- },
1904
- ...settlementInfo?.tokenNetworks && {
1905
- tokenNetworks: settlementInfo.tokenNetworks
1906
- }
1907
- };
1908
- const publishOwnAnnouncement = () => {
1909
- try {
1910
- const ilpInfoEvent = buildIlpPeerInfoEvent(
1911
- ownIlpInfo,
1912
- identity.secretKey,
1913
- announcementTtlSeconds > 0 ? { ttlSeconds: announcementTtlSeconds } : {}
1914
- );
1915
- eventStore.store(ilpInfoEvent);
1916
- const firstPeer = knownPeers[0];
1917
- const genesisResult = results[0];
1918
- if (ilpClient && firstPeer && genesisResult) {
1919
- const genesisIlpAddress = genesisResult.peerInfo.ilpAddress;
1920
- const toonBytes = encodeEventToToon3(ilpInfoEvent);
1921
- const base64Toon = Buffer.from(toonBytes).toString("base64");
1922
- const ilpAmount = String(BigInt(toonBytes.length) * basePricePerByte);
1923
- ilpClient.sendIlpPacket({
1924
- destination: genesisIlpAddress,
1925
- amount: ilpAmount,
1926
- data: base64Toon
1927
- }).catch((err) => {
1928
- const msg = err instanceof Error ? err.message : "Unknown";
1929
- console.warn("[Town] Failed to publish via ILP:", msg);
1930
- });
1931
- }
1932
- } catch (error) {
1933
- console.warn("[Town] Failed to publish ILP info:", error);
1934
- }
1935
- };
1936
- publishOwnAnnouncement();
1937
- if (announcementTtlSeconds > 0) {
1938
- const heartbeatMs = Math.max(
1939
- 1,
1940
- Math.floor(announcementTtlSeconds * 1e3 / 2)
1941
- );
1942
- announcementHeartbeat = setInterval(publishOwnAnnouncement, heartbeatMs);
1943
- announcementHeartbeat.unref?.();
1944
- }
1945
- try {
1946
- const serviceDiscoveryContent = {
1947
- serviceType: "relay",
1948
- ilpAddress,
1949
- pricing: {
1950
- basePricePerByte: Number(basePricePerByte),
1951
- currency: "USDC"
1952
- },
1953
- supportedKinds: [1, 10032, 10035, 10036],
1954
- capabilities: x402Enabled ? ["relay", "x402"] : ["relay"],
1955
- chain: chainConfig.name,
1956
- version: VERSION2
1957
- };
1958
- if (x402Enabled) {
1959
- serviceDiscoveryContent.x402 = {
1960
- enabled: true,
1961
- endpoint: "/publish"
1962
- };
1963
- }
1964
- if (config.skill) {
1965
- serviceDiscoveryContent.skill = config.skill;
1966
- }
1967
- const serviceDiscoveryEvent = buildServiceDiscoveryEvent(
1968
- serviceDiscoveryContent,
1969
- identity.secretKey
1970
- );
1971
- eventStore.store(serviceDiscoveryEvent);
1972
- const firstPeer = knownPeers[0];
1973
- const genesisResult = results[0];
1974
- if (ilpClient && firstPeer && genesisResult) {
1975
- const genesisIlpAddress = genesisResult.peerInfo.ilpAddress;
1976
- const sdToonBytes = encodeEventToToon3(serviceDiscoveryEvent);
1977
- const sdBase64Toon = Buffer.from(sdToonBytes).toString("base64");
1978
- const sdIlpAmount = String(
1979
- BigInt(sdToonBytes.length) * basePricePerByte
1980
- );
1981
- ilpClient.sendIlpPacket({
1982
- destination: genesisIlpAddress,
1983
- amount: sdIlpAmount,
1984
- data: sdBase64Toon
1985
- }).catch((err) => {
1986
- const msg = err instanceof Error ? err.message : "Unknown";
1987
- console.warn(
1988
- "[Town] Failed to publish service discovery via ILP:",
1989
- msg
1990
- );
1991
- });
1992
- }
1993
- } catch (error) {
1994
- console.warn("[Town] Failed to publish service discovery:", error);
1995
- }
1996
- const bootstrapPeerPubkeys = results.map((r) => r.knownPeer.pubkey);
1997
- discoveryTracker.addExcludedPubkeys(bootstrapPeerPubkeys);
1998
- } catch (error) {
1999
- console.error("[Town] Bootstrap failed:", error);
2000
- }
2001
- if (publishSeedEntryFlag && !externalRelayUrl) {
2002
- console.warn(
2003
- "[Town] publishSeedEntry is true but externalRelayUrl is not set -- skipping seed relay entry publication"
2004
- );
2005
- }
2006
- if (publishSeedEntryFlag && externalRelayUrl && seedRelays.length > 0) {
2007
- publishSeedRelayEntry({
2008
- secretKey: identity.secretKey,
2009
- relayUrl: externalRelayUrl,
2010
- publicRelays: seedRelays
2011
- }).then(({ publishedTo, eventId }) => {
2012
- console.log(
2013
- `[Town] Published seed relay entry to ${publishedTo} relay(s), eventId: ${eventId}`
2014
- );
2015
- }).catch((err) => {
2016
- const msg = err instanceof Error ? err.message : "Unknown error";
2017
- console.warn(`[Town] Failed to publish seed relay entry: ${msg}`);
2018
- });
2019
- }
2020
- const socialDiscovery = new SocialPeerDiscovery(
2021
- { relayUrls },
2022
- identity.secretKey
2023
- );
2024
- const socialSubscription = socialDiscovery.start();
2025
- const activeSubscriptions = /* @__PURE__ */ new Set();
2026
- const instance = {
2027
- isRunning() {
2028
- return running;
2029
- },
2030
- subscribe(subscribeRelayUrl, filter) {
2031
- if (!running) {
2032
- throw new Error("Cannot subscribe: town is not running");
2033
- }
2034
- return createSubscription(
2035
- subscribeRelayUrl,
2036
- filter,
2037
- eventStore,
2038
- activeSubscriptions
2039
- );
2040
- },
2041
- async stop() {
2042
- if (!running) return;
2043
- running = false;
2044
- if (announcementHeartbeat) {
2045
- clearInterval(announcementHeartbeat);
2046
- announcementHeartbeat = void 0;
2047
- }
2048
- for (const sub of activeSubscriptions) {
2049
- sub.close();
2050
- }
2051
- activeSubscriptions.clear();
2052
- if (socialSubscription) {
2053
- socialSubscription.unsubscribe();
2054
- }
2055
- if (seedRelayDiscovery) {
2056
- await seedRelayDiscovery.close();
2057
- }
2058
- await wsRelay.stop();
2059
- blsServer.close();
2060
- if (autoCreatedConnector) {
2061
- await autoCreatedConnector.stop();
2062
- }
2063
- eventStore.close?.();
2064
- },
2065
- pubkey: identity.pubkey,
2066
- evmAddress: identity.evmAddress,
2067
- config: resolvedConfig,
2068
- bootstrapResult: {
2069
- peerCount,
2070
- channelCount
2071
- },
2072
- discoveryMode: discovery
2073
- };
2074
- return instance;
2075
- }
2076
- var startTown = startRelay;
2077
-
2078
- export {
2079
- DEFAULT_RELAY_CONFIG,
2080
- matchFilter,
2081
- InMemoryEventStore,
2082
- RelayError,
2083
- SqliteEventStore,
2084
- encodeEventToToon,
2085
- ToonEncodeError,
2086
- decodeEventFromToon,
2087
- ToonDecodeError,
2088
- ConnectionHandler,
2089
- NostrRelayServer,
2090
- RelaySubscriber,
2091
- createEventStorageHandler,
2092
- calculateX402Price,
2093
- EIP_3009_TYPES,
2094
- USDC_EIP712_DOMAIN,
2095
- USDC_ABI,
2096
- runPreflight,
2097
- settleEip3009,
2098
- createX402Handler,
2099
- createHealthResponse,
2100
- startRelay,
2101
- startTown
2102
- };
2103
- //# sourceMappingURL=chunk-NYKVCNJL.js.map