@openclaw/nostr 2026.1.29

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.
@@ -0,0 +1,741 @@
1
+ import {
2
+ SimplePool,
3
+ finalizeEvent,
4
+ getPublicKey,
5
+ verifyEvent,
6
+ nip19,
7
+ type Event,
8
+ } from "nostr-tools";
9
+ import { decrypt, encrypt } from "nostr-tools/nip04";
10
+
11
+ import {
12
+ readNostrBusState,
13
+ writeNostrBusState,
14
+ computeSinceTimestamp,
15
+ readNostrProfileState,
16
+ writeNostrProfileState,
17
+ } from "./nostr-state-store.js";
18
+ import {
19
+ publishProfile as publishProfileFn,
20
+ type ProfilePublishResult,
21
+ } from "./nostr-profile.js";
22
+ import type { NostrProfile } from "./config-schema.js";
23
+ import { createSeenTracker, type SeenTracker } from "./seen-tracker.js";
24
+ import {
25
+ createMetrics,
26
+ createNoopMetrics,
27
+ type NostrMetrics,
28
+ type MetricsSnapshot,
29
+ type MetricEvent,
30
+ } from "./metrics.js";
31
+
32
+ export const DEFAULT_RELAYS = ["wss://relay.damus.io", "wss://nos.lol"];
33
+
34
+ // ============================================================================
35
+ // Constants
36
+ // ============================================================================
37
+
38
+ const STARTUP_LOOKBACK_SEC = 120; // tolerate relay lag / clock skew
39
+ const MAX_PERSISTED_EVENT_IDS = 5000;
40
+ const STATE_PERSIST_DEBOUNCE_MS = 5000; // Debounce state writes
41
+
42
+ // Reconnect configuration (exponential backoff with jitter)
43
+ const RECONNECT_BASE_MS = 1000; // 1 second base
44
+ const RECONNECT_MAX_MS = 60000; // 60 seconds max
45
+ const RECONNECT_JITTER = 0.3; // ±30% jitter
46
+
47
+ // Circuit breaker configuration
48
+ const CIRCUIT_BREAKER_THRESHOLD = 5; // failures before opening
49
+ const CIRCUIT_BREAKER_RESET_MS = 30000; // 30 seconds before half-open
50
+
51
+ // Health tracker configuration
52
+ const HEALTH_WINDOW_MS = 60000; // 1 minute window for health stats
53
+
54
+ // ============================================================================
55
+ // Types
56
+ // ============================================================================
57
+
58
+ export interface NostrBusOptions {
59
+ /** Private key in hex or nsec format */
60
+ privateKey: string;
61
+ /** WebSocket relay URLs (defaults to damus + nos.lol) */
62
+ relays?: string[];
63
+ /** Account ID for state persistence (optional, defaults to pubkey prefix) */
64
+ accountId?: string;
65
+ /** Called when a DM is received */
66
+ onMessage: (
67
+ pubkey: string,
68
+ text: string,
69
+ reply: (text: string) => Promise<void>
70
+ ) => Promise<void>;
71
+ /** Called on errors (optional) */
72
+ onError?: (error: Error, context: string) => void;
73
+ /** Called on connection status changes (optional) */
74
+ onConnect?: (relay: string) => void;
75
+ /** Called on disconnection (optional) */
76
+ onDisconnect?: (relay: string) => void;
77
+ /** Called on EOSE (end of stored events) for initial sync (optional) */
78
+ onEose?: (relay: string) => void;
79
+ /** Called on each metric event (optional) */
80
+ onMetric?: (event: MetricEvent) => void;
81
+ /** Maximum entries in seen tracker (default: 100,000) */
82
+ maxSeenEntries?: number;
83
+ /** Seen tracker TTL in ms (default: 1 hour) */
84
+ seenTtlMs?: number;
85
+ }
86
+
87
+ export interface NostrBusHandle {
88
+ /** Stop the bus and close connections */
89
+ close: () => void;
90
+ /** Get the bot's public key */
91
+ publicKey: string;
92
+ /** Send a DM to a pubkey */
93
+ sendDm: (toPubkey: string, text: string) => Promise<void>;
94
+ /** Get current metrics snapshot */
95
+ getMetrics: () => MetricsSnapshot;
96
+ /** Publish a profile (kind:0) to all relays */
97
+ publishProfile: (profile: NostrProfile) => Promise<ProfilePublishResult>;
98
+ /** Get the last profile publish state */
99
+ getProfileState: () => Promise<{
100
+ lastPublishedAt: number | null;
101
+ lastPublishedEventId: string | null;
102
+ lastPublishResults: Record<string, "ok" | "failed" | "timeout"> | null;
103
+ }>;
104
+ }
105
+
106
+ // ============================================================================
107
+ // Circuit Breaker
108
+ // ============================================================================
109
+
110
+ interface CircuitBreakerState {
111
+ state: "closed" | "open" | "half_open";
112
+ failures: number;
113
+ lastFailure: number;
114
+ lastSuccess: number;
115
+ }
116
+
117
+ interface CircuitBreaker {
118
+ /** Check if requests should be allowed */
119
+ canAttempt: () => boolean;
120
+ /** Record a success */
121
+ recordSuccess: () => void;
122
+ /** Record a failure */
123
+ recordFailure: () => void;
124
+ /** Get current state */
125
+ getState: () => CircuitBreakerState["state"];
126
+ }
127
+
128
+ function createCircuitBreaker(
129
+ relay: string,
130
+ metrics: NostrMetrics,
131
+ threshold: number = CIRCUIT_BREAKER_THRESHOLD,
132
+ resetMs: number = CIRCUIT_BREAKER_RESET_MS
133
+ ): CircuitBreaker {
134
+ const state: CircuitBreakerState = {
135
+ state: "closed",
136
+ failures: 0,
137
+ lastFailure: 0,
138
+ lastSuccess: Date.now(),
139
+ };
140
+
141
+ return {
142
+ canAttempt(): boolean {
143
+ if (state.state === "closed") return true;
144
+
145
+ if (state.state === "open") {
146
+ // Check if enough time has passed to try half-open
147
+ if (Date.now() - state.lastFailure >= resetMs) {
148
+ state.state = "half_open";
149
+ metrics.emit("relay.circuit_breaker.half_open", 1, { relay });
150
+ return true;
151
+ }
152
+ return false;
153
+ }
154
+
155
+ // half_open: allow one attempt
156
+ return true;
157
+ },
158
+
159
+ recordSuccess(): void {
160
+ if (state.state === "half_open") {
161
+ state.state = "closed";
162
+ state.failures = 0;
163
+ metrics.emit("relay.circuit_breaker.close", 1, { relay });
164
+ } else if (state.state === "closed") {
165
+ state.failures = 0;
166
+ }
167
+ state.lastSuccess = Date.now();
168
+ },
169
+
170
+ recordFailure(): void {
171
+ state.failures++;
172
+ state.lastFailure = Date.now();
173
+
174
+ if (state.state === "half_open") {
175
+ state.state = "open";
176
+ metrics.emit("relay.circuit_breaker.open", 1, { relay });
177
+ } else if (state.state === "closed" && state.failures >= threshold) {
178
+ state.state = "open";
179
+ metrics.emit("relay.circuit_breaker.open", 1, { relay });
180
+ }
181
+ },
182
+
183
+ getState(): CircuitBreakerState["state"] {
184
+ return state.state;
185
+ },
186
+ };
187
+ }
188
+
189
+ // ============================================================================
190
+ // Relay Health Tracker
191
+ // ============================================================================
192
+
193
+ interface RelayHealthStats {
194
+ successCount: number;
195
+ failureCount: number;
196
+ latencySum: number;
197
+ latencyCount: number;
198
+ lastSuccess: number;
199
+ lastFailure: number;
200
+ }
201
+
202
+ interface RelayHealthTracker {
203
+ /** Record a successful operation */
204
+ recordSuccess: (relay: string, latencyMs: number) => void;
205
+ /** Record a failed operation */
206
+ recordFailure: (relay: string) => void;
207
+ /** Get health score (0-1, higher is better) */
208
+ getScore: (relay: string) => number;
209
+ /** Get relays sorted by health (best first) */
210
+ getSortedRelays: (relays: string[]) => string[];
211
+ }
212
+
213
+ function createRelayHealthTracker(): RelayHealthTracker {
214
+ const stats = new Map<string, RelayHealthStats>();
215
+
216
+ function getOrCreate(relay: string): RelayHealthStats {
217
+ let s = stats.get(relay);
218
+ if (!s) {
219
+ s = {
220
+ successCount: 0,
221
+ failureCount: 0,
222
+ latencySum: 0,
223
+ latencyCount: 0,
224
+ lastSuccess: 0,
225
+ lastFailure: 0,
226
+ };
227
+ stats.set(relay, s);
228
+ }
229
+ return s;
230
+ }
231
+
232
+ return {
233
+ recordSuccess(relay: string, latencyMs: number): void {
234
+ const s = getOrCreate(relay);
235
+ s.successCount++;
236
+ s.latencySum += latencyMs;
237
+ s.latencyCount++;
238
+ s.lastSuccess = Date.now();
239
+ },
240
+
241
+ recordFailure(relay: string): void {
242
+ const s = getOrCreate(relay);
243
+ s.failureCount++;
244
+ s.lastFailure = Date.now();
245
+ },
246
+
247
+ getScore(relay: string): number {
248
+ const s = stats.get(relay);
249
+ if (!s) return 0.5; // Unknown relay gets neutral score
250
+
251
+ const total = s.successCount + s.failureCount;
252
+ if (total === 0) return 0.5;
253
+
254
+ // Success rate (0-1)
255
+ const successRate = s.successCount / total;
256
+
257
+ // Recency bonus (prefer recently successful relays)
258
+ const now = Date.now();
259
+ const recencyBonus =
260
+ s.lastSuccess > s.lastFailure
261
+ ? Math.max(0, 1 - (now - s.lastSuccess) / HEALTH_WINDOW_MS) * 0.2
262
+ : 0;
263
+
264
+ // Latency penalty (lower is better)
265
+ const avgLatency =
266
+ s.latencyCount > 0 ? s.latencySum / s.latencyCount : 1000;
267
+ const latencyPenalty = Math.min(0.2, avgLatency / 10000);
268
+
269
+ return Math.max(0, Math.min(1, successRate + recencyBonus - latencyPenalty));
270
+ },
271
+
272
+ getSortedRelays(relays: string[]): string[] {
273
+ return [...relays].sort((a, b) => this.getScore(b) - this.getScore(a));
274
+ },
275
+ };
276
+ }
277
+
278
+ // ============================================================================
279
+ // Reconnect with Exponential Backoff + Jitter
280
+ // ============================================================================
281
+
282
+ function computeReconnectDelay(attempt: number): number {
283
+ // Exponential backoff: base * 2^attempt
284
+ const exponential = RECONNECT_BASE_MS * Math.pow(2, attempt);
285
+ const capped = Math.min(exponential, RECONNECT_MAX_MS);
286
+
287
+ // Add jitter: ±JITTER%
288
+ const jitter = capped * RECONNECT_JITTER * (Math.random() * 2 - 1);
289
+ return Math.max(RECONNECT_BASE_MS, capped + jitter);
290
+ }
291
+
292
+ // ============================================================================
293
+ // Key Validation
294
+ // ============================================================================
295
+
296
+ /**
297
+ * Validate and normalize a private key (accepts hex or nsec format)
298
+ */
299
+ export function validatePrivateKey(key: string): Uint8Array {
300
+ const trimmed = key.trim();
301
+
302
+ // Handle nsec (bech32) format
303
+ if (trimmed.startsWith("nsec1")) {
304
+ const decoded = nip19.decode(trimmed);
305
+ if (decoded.type !== "nsec") {
306
+ throw new Error("Invalid nsec key: wrong type");
307
+ }
308
+ return decoded.data;
309
+ }
310
+
311
+ // Handle hex format
312
+ if (!/^[0-9a-fA-F]{64}$/.test(trimmed)) {
313
+ throw new Error(
314
+ "Private key must be 64 hex characters or nsec bech32 format"
315
+ );
316
+ }
317
+
318
+ // Convert hex string to Uint8Array
319
+ const bytes = new Uint8Array(32);
320
+ for (let i = 0; i < 32; i++) {
321
+ bytes[i] = parseInt(trimmed.slice(i * 2, i * 2 + 2), 16);
322
+ }
323
+ return bytes;
324
+ }
325
+
326
+ /**
327
+ * Get public key from private key (hex or nsec format)
328
+ */
329
+ export function getPublicKeyFromPrivate(privateKey: string): string {
330
+ const sk = validatePrivateKey(privateKey);
331
+ return getPublicKey(sk);
332
+ }
333
+
334
+ // ============================================================================
335
+ // Main Bus
336
+ // ============================================================================
337
+
338
+ /**
339
+ * Start the Nostr DM bus - subscribes to NIP-04 encrypted DMs
340
+ */
341
+ export async function startNostrBus(
342
+ options: NostrBusOptions
343
+ ): Promise<NostrBusHandle> {
344
+ const {
345
+ privateKey,
346
+ relays = DEFAULT_RELAYS,
347
+ onMessage,
348
+ onError,
349
+ onEose,
350
+ onMetric,
351
+ maxSeenEntries = 100_000,
352
+ seenTtlMs = 60 * 60 * 1000,
353
+ } = options;
354
+
355
+ const sk = validatePrivateKey(privateKey);
356
+ const pk = getPublicKey(sk);
357
+ const pool = new SimplePool();
358
+ const accountId = options.accountId ?? pk.slice(0, 16);
359
+ const gatewayStartedAt = Math.floor(Date.now() / 1000);
360
+
361
+ // Initialize metrics
362
+ const metrics = onMetric ? createMetrics(onMetric) : createNoopMetrics();
363
+
364
+ // Initialize seen tracker with LRU
365
+ const seen: SeenTracker = createSeenTracker({
366
+ maxEntries: maxSeenEntries,
367
+ ttlMs: seenTtlMs,
368
+ });
369
+
370
+ // Initialize circuit breakers and health tracker
371
+ const circuitBreakers = new Map<string, CircuitBreaker>();
372
+ const healthTracker = createRelayHealthTracker();
373
+
374
+ for (const relay of relays) {
375
+ circuitBreakers.set(relay, createCircuitBreaker(relay, metrics));
376
+ }
377
+
378
+ // Read persisted state and compute `since` timestamp (with small overlap)
379
+ const state = await readNostrBusState({ accountId });
380
+ const baseSince = computeSinceTimestamp(state, gatewayStartedAt);
381
+ const since = Math.max(0, baseSince - STARTUP_LOOKBACK_SEC);
382
+
383
+ // Seed in-memory dedupe with recent IDs from disk (prevents restart replay)
384
+ if (state?.recentEventIds?.length) {
385
+ seen.seed(state.recentEventIds);
386
+ }
387
+
388
+ // Persist startup timestamp
389
+ await writeNostrBusState({
390
+ accountId,
391
+ lastProcessedAt: state?.lastProcessedAt ?? gatewayStartedAt,
392
+ gatewayStartedAt,
393
+ recentEventIds: state?.recentEventIds ?? [],
394
+ });
395
+
396
+ // Debounced state persistence
397
+ let pendingWrite: ReturnType<typeof setTimeout> | undefined;
398
+ let lastProcessedAt = state?.lastProcessedAt ?? gatewayStartedAt;
399
+ let recentEventIds = (state?.recentEventIds ?? []).slice(
400
+ -MAX_PERSISTED_EVENT_IDS
401
+ );
402
+
403
+ function scheduleStatePersist(eventCreatedAt: number, eventId: string): void {
404
+ lastProcessedAt = Math.max(lastProcessedAt, eventCreatedAt);
405
+ recentEventIds.push(eventId);
406
+ if (recentEventIds.length > MAX_PERSISTED_EVENT_IDS) {
407
+ recentEventIds = recentEventIds.slice(-MAX_PERSISTED_EVENT_IDS);
408
+ }
409
+
410
+ if (pendingWrite) clearTimeout(pendingWrite);
411
+ pendingWrite = setTimeout(() => {
412
+ writeNostrBusState({
413
+ accountId,
414
+ lastProcessedAt,
415
+ gatewayStartedAt,
416
+ recentEventIds,
417
+ }).catch((err) => onError?.(err as Error, "persist state"));
418
+ }, STATE_PERSIST_DEBOUNCE_MS);
419
+ }
420
+
421
+ const inflight = new Set<string>();
422
+
423
+ // Event handler
424
+ async function handleEvent(event: Event): Promise<void> {
425
+ try {
426
+ metrics.emit("event.received");
427
+
428
+ // Fast dedupe check (handles relay reconnections)
429
+ if (seen.peek(event.id) || inflight.has(event.id)) {
430
+ metrics.emit("event.duplicate");
431
+ return;
432
+ }
433
+ inflight.add(event.id);
434
+
435
+ // Self-message loop prevention: skip our own messages
436
+ if (event.pubkey === pk) {
437
+ metrics.emit("event.rejected.self_message");
438
+ return;
439
+ }
440
+
441
+ // Skip events older than our `since` (relay may ignore filter)
442
+ if (event.created_at < since) {
443
+ metrics.emit("event.rejected.stale");
444
+ return;
445
+ }
446
+
447
+ // Fast p-tag check BEFORE crypto (no allocation, cheaper)
448
+ let targetsUs = false;
449
+ for (const t of event.tags) {
450
+ if (t[0] === "p" && t[1] === pk) {
451
+ targetsUs = true;
452
+ break;
453
+ }
454
+ }
455
+ if (!targetsUs) {
456
+ metrics.emit("event.rejected.wrong_kind");
457
+ return;
458
+ }
459
+
460
+ // Verify signature (must pass before we trust the event)
461
+ if (!verifyEvent(event)) {
462
+ metrics.emit("event.rejected.invalid_signature");
463
+ onError?.(new Error("Invalid signature"), `event ${event.id}`);
464
+ return;
465
+ }
466
+
467
+ // Mark seen AFTER verify (don't cache invalid IDs)
468
+ seen.add(event.id);
469
+ metrics.emit("memory.seen_tracker_size", seen.size());
470
+
471
+ // Decrypt the message
472
+ let plaintext: string;
473
+ try {
474
+ plaintext = await decrypt(sk, event.pubkey, event.content);
475
+ metrics.emit("decrypt.success");
476
+ } catch (err) {
477
+ metrics.emit("decrypt.failure");
478
+ metrics.emit("event.rejected.decrypt_failed");
479
+ onError?.(err as Error, `decrypt from ${event.pubkey}`);
480
+ return;
481
+ }
482
+
483
+ // Create reply function (try relays by health score)
484
+ const replyTo = async (text: string): Promise<void> => {
485
+ await sendEncryptedDm(
486
+ pool,
487
+ sk,
488
+ event.pubkey,
489
+ text,
490
+ relays,
491
+ metrics,
492
+ circuitBreakers,
493
+ healthTracker,
494
+ onError
495
+ );
496
+ };
497
+
498
+ // Call the message handler
499
+ await onMessage(event.pubkey, plaintext, replyTo);
500
+
501
+ // Mark as processed
502
+ metrics.emit("event.processed");
503
+
504
+ // Persist progress (debounced)
505
+ scheduleStatePersist(event.created_at, event.id);
506
+ } catch (err) {
507
+ onError?.(err as Error, `event ${event.id}`);
508
+ } finally {
509
+ inflight.delete(event.id);
510
+ }
511
+ }
512
+
513
+ const sub = pool.subscribeMany(
514
+ relays,
515
+ [{ kinds: [4], "#p": [pk], since }],
516
+ {
517
+ onevent: handleEvent,
518
+ oneose: () => {
519
+ // EOSE handler - called when all stored events have been received
520
+ for (const relay of relays) {
521
+ metrics.emit("relay.message.eose", 1, { relay });
522
+ }
523
+ onEose?.(relays.join(", "));
524
+ },
525
+ onclose: (reason) => {
526
+ // Handle subscription close
527
+ for (const relay of relays) {
528
+ metrics.emit("relay.message.closed", 1, { relay });
529
+ options.onDisconnect?.(relay);
530
+ }
531
+ onError?.(
532
+ new Error(`Subscription closed: ${reason}`),
533
+ "subscription"
534
+ );
535
+ },
536
+ }
537
+ );
538
+
539
+ // Public sendDm function
540
+ const sendDm = async (toPubkey: string, text: string): Promise<void> => {
541
+ await sendEncryptedDm(
542
+ pool,
543
+ sk,
544
+ toPubkey,
545
+ text,
546
+ relays,
547
+ metrics,
548
+ circuitBreakers,
549
+ healthTracker,
550
+ onError
551
+ );
552
+ };
553
+
554
+ // Profile publishing function
555
+ const publishProfile = async (profile: NostrProfile): Promise<ProfilePublishResult> => {
556
+ // Read last published timestamp for monotonic ordering
557
+ const profileState = await readNostrProfileState({ accountId });
558
+ const lastPublishedAt = profileState?.lastPublishedAt ?? undefined;
559
+
560
+ // Publish the profile
561
+ const result = await publishProfileFn(pool, sk, relays, profile, lastPublishedAt);
562
+
563
+ // Convert results to state format
564
+ const publishResults: Record<string, "ok" | "failed" | "timeout"> = {};
565
+ for (const relay of result.successes) {
566
+ publishResults[relay] = "ok";
567
+ }
568
+ for (const { relay, error } of result.failures) {
569
+ publishResults[relay] = error === "timeout" ? "timeout" : "failed";
570
+ }
571
+
572
+ // Persist the publish state
573
+ await writeNostrProfileState({
574
+ accountId,
575
+ lastPublishedAt: result.createdAt,
576
+ lastPublishedEventId: result.eventId,
577
+ lastPublishResults: publishResults,
578
+ });
579
+
580
+ return result;
581
+ };
582
+
583
+ // Get profile state function
584
+ const getProfileState = async () => {
585
+ const state = await readNostrProfileState({ accountId });
586
+ return {
587
+ lastPublishedAt: state?.lastPublishedAt ?? null,
588
+ lastPublishedEventId: state?.lastPublishedEventId ?? null,
589
+ lastPublishResults: state?.lastPublishResults ?? null,
590
+ };
591
+ };
592
+
593
+ return {
594
+ close: () => {
595
+ sub.close();
596
+ seen.stop();
597
+ // Flush pending state write synchronously on close
598
+ if (pendingWrite) {
599
+ clearTimeout(pendingWrite);
600
+ writeNostrBusState({
601
+ accountId,
602
+ lastProcessedAt,
603
+ gatewayStartedAt,
604
+ recentEventIds,
605
+ }).catch((err) => onError?.(err as Error, "persist state on close"));
606
+ }
607
+ },
608
+ publicKey: pk,
609
+ sendDm,
610
+ getMetrics: () => metrics.getSnapshot(),
611
+ publishProfile,
612
+ getProfileState,
613
+ };
614
+ }
615
+
616
+ // ============================================================================
617
+ // Send DM with Circuit Breaker + Health Scoring
618
+ // ============================================================================
619
+
620
+ /**
621
+ * Send an encrypted DM to a pubkey
622
+ */
623
+ async function sendEncryptedDm(
624
+ pool: SimplePool,
625
+ sk: Uint8Array,
626
+ toPubkey: string,
627
+ text: string,
628
+ relays: string[],
629
+ metrics: NostrMetrics,
630
+ circuitBreakers: Map<string, CircuitBreaker>,
631
+ healthTracker: RelayHealthTracker,
632
+ onError?: (error: Error, context: string) => void
633
+ ): Promise<void> {
634
+ const ciphertext = await encrypt(sk, toPubkey, text);
635
+ const reply = finalizeEvent(
636
+ {
637
+ kind: 4,
638
+ content: ciphertext,
639
+ tags: [["p", toPubkey]],
640
+ created_at: Math.floor(Date.now() / 1000),
641
+ },
642
+ sk
643
+ );
644
+
645
+ // Sort relays by health score (best first)
646
+ const sortedRelays = healthTracker.getSortedRelays(relays);
647
+
648
+ // Try relays in order of health, respecting circuit breakers
649
+ let lastError: Error | undefined;
650
+ for (const relay of sortedRelays) {
651
+ const cb = circuitBreakers.get(relay);
652
+
653
+ // Skip if circuit breaker is open
654
+ if (cb && !cb.canAttempt()) {
655
+ continue;
656
+ }
657
+
658
+ const startTime = Date.now();
659
+ try {
660
+ await pool.publish([relay], reply);
661
+ const latency = Date.now() - startTime;
662
+
663
+ // Record success
664
+ cb?.recordSuccess();
665
+ healthTracker.recordSuccess(relay, latency);
666
+
667
+ return; // Success - exit early
668
+ } catch (err) {
669
+ lastError = err as Error;
670
+ const latency = Date.now() - startTime;
671
+
672
+ // Record failure
673
+ cb?.recordFailure();
674
+ healthTracker.recordFailure(relay);
675
+ metrics.emit("relay.error", 1, { relay, latency });
676
+
677
+ onError?.(lastError, `publish to ${relay}`);
678
+ }
679
+ }
680
+
681
+ throw new Error(`Failed to publish to any relay: ${lastError?.message}`);
682
+ }
683
+
684
+ // ============================================================================
685
+ // Pubkey Utilities
686
+ // ============================================================================
687
+
688
+ /**
689
+ * Check if a string looks like a valid Nostr pubkey (hex or npub)
690
+ */
691
+ export function isValidPubkey(input: string): boolean {
692
+ if (typeof input !== "string") return false;
693
+ const trimmed = input.trim();
694
+
695
+ // npub format
696
+ if (trimmed.startsWith("npub1")) {
697
+ try {
698
+ const decoded = nip19.decode(trimmed);
699
+ return decoded.type === "npub";
700
+ } catch {
701
+ return false;
702
+ }
703
+ }
704
+
705
+ // Hex format
706
+ return /^[0-9a-fA-F]{64}$/.test(trimmed);
707
+ }
708
+
709
+ /**
710
+ * Normalize a pubkey to hex format (accepts npub or hex)
711
+ */
712
+ export function normalizePubkey(input: string): string {
713
+ const trimmed = input.trim();
714
+
715
+ // npub format - decode to hex
716
+ if (trimmed.startsWith("npub1")) {
717
+ const decoded = nip19.decode(trimmed);
718
+ if (decoded.type !== "npub") {
719
+ throw new Error("Invalid npub key");
720
+ }
721
+ // Convert Uint8Array to hex string
722
+ return Array.from(decoded.data)
723
+ .map((b) => b.toString(16).padStart(2, "0"))
724
+ .join("");
725
+ }
726
+
727
+ // Already hex - validate and return lowercase
728
+ if (!/^[0-9a-fA-F]{64}$/.test(trimmed)) {
729
+ throw new Error("Pubkey must be 64 hex characters or npub format");
730
+ }
731
+ return trimmed.toLowerCase();
732
+ }
733
+
734
+ /**
735
+ * Convert a hex pubkey to npub format
736
+ */
737
+ export function pubkeyToNpub(hexPubkey: string): string {
738
+ const normalized = normalizePubkey(hexPubkey);
739
+ // npubEncode expects a hex string, not Uint8Array
740
+ return nip19.npubEncode(normalized);
741
+ }