@kodelyth/nostr 2026.5.39 → 2026.5.42

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.
Files changed (69) hide show
  1. package/README.md +142 -0
  2. package/api.ts +10 -0
  3. package/channel-plugin-api.ts +1 -0
  4. package/dist/api.js +522 -0
  5. package/dist/channel-CnPQxTzj.js +1467 -0
  6. package/dist/channel-plugin-api.js +2 -0
  7. package/dist/config-schema-KoL8Et_9.js +63 -0
  8. package/dist/default-relays-DLwdWOTu.js +4 -0
  9. package/dist/inbound-direct-dm-runtime-CeYGU_Fo.js +2 -0
  10. package/dist/index.js +81 -0
  11. package/dist/runtime-api.js +2 -0
  12. package/dist/setup-api.js +2 -0
  13. package/dist/setup-entry.js +11 -0
  14. package/dist/setup-plugin-api.js +166 -0
  15. package/dist/setup-surface-DFlfVW6j.js +337 -0
  16. package/dist/test-api.js +2 -0
  17. package/index.ts +95 -0
  18. package/klaw.plugin.json +2 -185
  19. package/package.json +4 -4
  20. package/runtime-api.ts +6 -0
  21. package/setup-api.ts +1 -0
  22. package/setup-entry.ts +9 -0
  23. package/setup-plugin-api.ts +3 -0
  24. package/src/channel-api.ts +11 -0
  25. package/src/channel.inbound.test.ts +187 -0
  26. package/src/channel.outbound.test.ts +163 -0
  27. package/src/channel.setup.ts +234 -0
  28. package/src/channel.test.ts +526 -0
  29. package/src/channel.ts +215 -0
  30. package/src/config-schema.ts +98 -0
  31. package/src/default-relays.ts +1 -0
  32. package/src/gateway.ts +321 -0
  33. package/src/inbound-direct-dm-runtime.ts +1 -0
  34. package/src/metrics.ts +458 -0
  35. package/src/nostr-bus.fuzz.test.ts +382 -0
  36. package/src/nostr-bus.inbound.test.ts +526 -0
  37. package/src/nostr-bus.integration.test.ts +477 -0
  38. package/src/nostr-bus.test.ts +231 -0
  39. package/src/nostr-bus.ts +789 -0
  40. package/src/nostr-key-utils.ts +94 -0
  41. package/src/nostr-profile-core.ts +134 -0
  42. package/src/nostr-profile-http-runtime.ts +6 -0
  43. package/src/nostr-profile-http.test.ts +632 -0
  44. package/src/nostr-profile-http.ts +583 -0
  45. package/src/nostr-profile-import.test.ts +119 -0
  46. package/src/nostr-profile-import.ts +262 -0
  47. package/src/nostr-profile-url-safety.ts +21 -0
  48. package/src/nostr-profile.fuzz.test.ts +430 -0
  49. package/src/nostr-profile.test.ts +415 -0
  50. package/src/nostr-profile.ts +144 -0
  51. package/src/nostr-state-store.test.ts +237 -0
  52. package/src/nostr-state-store.ts +206 -0
  53. package/src/runtime.ts +9 -0
  54. package/src/seen-tracker.ts +289 -0
  55. package/src/session-route.ts +25 -0
  56. package/src/setup-surface.ts +264 -0
  57. package/src/test-fixtures.ts +45 -0
  58. package/src/types.ts +117 -0
  59. package/test/setup.ts +5 -0
  60. package/test-api.ts +1 -0
  61. package/tsconfig.json +16 -0
  62. package/api.js +0 -7
  63. package/channel-plugin-api.js +0 -7
  64. package/index.js +0 -7
  65. package/runtime-api.js +0 -7
  66. package/setup-api.js +0 -7
  67. package/setup-entry.js +0 -7
  68. package/setup-plugin-api.js +0 -7
  69. package/test-api.js +0 -7
@@ -0,0 +1,289 @@
1
+ /**
2
+ * LRU-based seen event tracker with TTL support.
3
+ * Prevents unbounded memory growth under high load or abuse.
4
+ */
5
+
6
+ interface SeenTrackerOptions {
7
+ /** Maximum number of entries to track (default: 100,000) */
8
+ maxEntries?: number;
9
+ /** TTL in milliseconds (default: 1 hour) */
10
+ ttlMs?: number;
11
+ /** Prune interval in milliseconds (default: 10 minutes) */
12
+ pruneIntervalMs?: number;
13
+ }
14
+
15
+ export interface SeenTracker {
16
+ /** Check if an ID has been seen (also marks it as seen if not) */
17
+ has: (id: string) => boolean;
18
+ /** Mark an ID as seen */
19
+ add: (id: string) => void;
20
+ /** Check if ID exists without marking */
21
+ peek: (id: string) => boolean;
22
+ /** Delete an ID */
23
+ delete: (id: string) => void;
24
+ /** Clear all entries */
25
+ clear: () => void;
26
+ /** Get current size */
27
+ size: () => number;
28
+ /** Stop the pruning timer */
29
+ stop: () => void;
30
+ /** Pre-seed with IDs (useful for restart recovery) */
31
+ seed: (ids: string[]) => void;
32
+ }
33
+
34
+ interface Entry {
35
+ seenAt: number;
36
+ // For LRU: track order via doubly-linked list
37
+ prev: string | null;
38
+ next: string | null;
39
+ }
40
+
41
+ /**
42
+ * Create a new seen tracker with LRU eviction and TTL expiration.
43
+ */
44
+ export function createSeenTracker(options?: SeenTrackerOptions): SeenTracker {
45
+ const maxEntries = options?.maxEntries ?? 100_000;
46
+ const ttlMs = options?.ttlMs ?? 60 * 60 * 1000; // 1 hour
47
+ const pruneIntervalMs = options?.pruneIntervalMs ?? 10 * 60 * 1000; // 10 minutes
48
+
49
+ // Main storage
50
+ const entries = new Map<string, Entry>();
51
+
52
+ // LRU tracking: head = most recent, tail = least recent
53
+ let head: string | null = null;
54
+ let tail: string | null = null;
55
+
56
+ // Move an entry to the front (most recently used)
57
+ function moveToFront(id: string): void {
58
+ const entry = entries.get(id);
59
+ if (!entry) {
60
+ return;
61
+ }
62
+
63
+ // Already at front
64
+ if (head === id) {
65
+ return;
66
+ }
67
+
68
+ // Remove from current position
69
+ if (entry.prev) {
70
+ const prevEntry = entries.get(entry.prev);
71
+ if (prevEntry) {
72
+ prevEntry.next = entry.next;
73
+ }
74
+ }
75
+ if (entry.next) {
76
+ const nextEntry = entries.get(entry.next);
77
+ if (nextEntry) {
78
+ nextEntry.prev = entry.prev;
79
+ }
80
+ }
81
+
82
+ // Update tail if this was the tail
83
+ if (tail === id) {
84
+ tail = entry.prev;
85
+ }
86
+
87
+ // Move to front
88
+ entry.prev = null;
89
+ entry.next = head;
90
+ if (head) {
91
+ const headEntry = entries.get(head);
92
+ if (headEntry) {
93
+ headEntry.prev = id;
94
+ }
95
+ }
96
+ head = id;
97
+
98
+ // If no tail, this is also the tail
99
+ if (!tail) {
100
+ tail = id;
101
+ }
102
+ }
103
+
104
+ // Remove an entry from the linked list
105
+ function removeFromList(id: string): void {
106
+ const entry = entries.get(id);
107
+ if (!entry) {
108
+ return;
109
+ }
110
+
111
+ if (entry.prev) {
112
+ const prevEntry = entries.get(entry.prev);
113
+ if (prevEntry) {
114
+ prevEntry.next = entry.next;
115
+ }
116
+ } else {
117
+ head = entry.next;
118
+ }
119
+
120
+ if (entry.next) {
121
+ const nextEntry = entries.get(entry.next);
122
+ if (nextEntry) {
123
+ nextEntry.prev = entry.prev;
124
+ }
125
+ } else {
126
+ tail = entry.prev;
127
+ }
128
+ }
129
+
130
+ // Evict the least recently used entry
131
+ function evictLRU(): void {
132
+ if (!tail) {
133
+ return;
134
+ }
135
+ const idToEvict = tail;
136
+ removeFromList(idToEvict);
137
+ entries.delete(idToEvict);
138
+ }
139
+
140
+ function insertAtFront(id: string, seenAt: number): void {
141
+ const newEntry: Entry = {
142
+ seenAt,
143
+ prev: null,
144
+ next: head,
145
+ };
146
+
147
+ if (head) {
148
+ const headEntry = entries.get(head);
149
+ if (headEntry) {
150
+ headEntry.prev = id;
151
+ }
152
+ }
153
+
154
+ entries.set(id, newEntry);
155
+ head = id;
156
+ if (!tail) {
157
+ tail = id;
158
+ }
159
+ }
160
+
161
+ // Prune expired entries
162
+ function pruneExpired(): void {
163
+ const now = Date.now();
164
+ const toDelete: string[] = [];
165
+
166
+ for (const [id, entry] of entries) {
167
+ if (now - entry.seenAt > ttlMs) {
168
+ toDelete.push(id);
169
+ }
170
+ }
171
+
172
+ for (const id of toDelete) {
173
+ removeFromList(id);
174
+ entries.delete(id);
175
+ }
176
+ }
177
+
178
+ // Start pruning timer
179
+ let pruneTimer: ReturnType<typeof setInterval> | undefined;
180
+ if (pruneIntervalMs > 0) {
181
+ pruneTimer = setInterval(pruneExpired, pruneIntervalMs);
182
+ // Don't keep process alive just for pruning
183
+ if (pruneTimer.unref) {
184
+ pruneTimer.unref();
185
+ }
186
+ }
187
+
188
+ function add(id: string): void {
189
+ const now = Date.now();
190
+
191
+ // If already exists, update and move to front
192
+ const existing = entries.get(id);
193
+ if (existing) {
194
+ existing.seenAt = now;
195
+ moveToFront(id);
196
+ return;
197
+ }
198
+
199
+ // Evict if at capacity
200
+ while (entries.size >= maxEntries) {
201
+ evictLRU();
202
+ }
203
+
204
+ insertAtFront(id, now);
205
+ }
206
+
207
+ function has(id: string): boolean {
208
+ const entry = entries.get(id);
209
+ if (!entry) {
210
+ add(id);
211
+ return false;
212
+ }
213
+
214
+ // Check if expired
215
+ if (Date.now() - entry.seenAt > ttlMs) {
216
+ removeFromList(id);
217
+ entries.delete(id);
218
+ add(id);
219
+ return false;
220
+ }
221
+
222
+ // Mark as recently used
223
+ entry.seenAt = Date.now();
224
+ moveToFront(id);
225
+ return true;
226
+ }
227
+
228
+ function peek(id: string): boolean {
229
+ const entry = entries.get(id);
230
+ if (!entry) {
231
+ return false;
232
+ }
233
+
234
+ // Check if expired
235
+ if (Date.now() - entry.seenAt > ttlMs) {
236
+ removeFromList(id);
237
+ entries.delete(id);
238
+ return false;
239
+ }
240
+
241
+ return true;
242
+ }
243
+
244
+ function deleteEntry(id: string): void {
245
+ if (entries.has(id)) {
246
+ removeFromList(id);
247
+ entries.delete(id);
248
+ }
249
+ }
250
+
251
+ function clear(): void {
252
+ entries.clear();
253
+ head = null;
254
+ tail = null;
255
+ }
256
+
257
+ function size(): number {
258
+ return entries.size;
259
+ }
260
+
261
+ function stop(): void {
262
+ if (pruneTimer) {
263
+ clearInterval(pruneTimer);
264
+ pruneTimer = undefined;
265
+ }
266
+ }
267
+
268
+ function seed(ids: string[]): void {
269
+ const now = Date.now();
270
+ // Seed in reverse order so first IDs end up at front
271
+ for (let i = ids.length - 1; i >= 0; i--) {
272
+ const id = ids[i];
273
+ if (!entries.has(id) && entries.size < maxEntries) {
274
+ insertAtFront(id, now);
275
+ }
276
+ }
277
+ }
278
+
279
+ return {
280
+ has,
281
+ add,
282
+ peek,
283
+ delete: deleteEntry,
284
+ clear,
285
+ size,
286
+ stop,
287
+ seed,
288
+ };
289
+ }
@@ -0,0 +1,25 @@
1
+ import {
2
+ buildChannelOutboundSessionRoute,
3
+ stripChannelTargetPrefix,
4
+ type ChannelOutboundSessionRouteParams,
5
+ } from "klaw/plugin-sdk/core";
6
+
7
+ export function resolveNostrOutboundSessionRoute(params: ChannelOutboundSessionRouteParams) {
8
+ const target = stripChannelTargetPrefix(params.target, "nostr");
9
+ if (!target) {
10
+ return null;
11
+ }
12
+ return buildChannelOutboundSessionRoute({
13
+ cfg: params.cfg,
14
+ agentId: params.agentId,
15
+ channel: "nostr",
16
+ accountId: params.accountId,
17
+ peer: {
18
+ kind: "direct",
19
+ id: target,
20
+ },
21
+ chatType: "direct",
22
+ from: `nostr:${target}`,
23
+ to: `nostr:${target}`,
24
+ });
25
+ }
@@ -0,0 +1,264 @@
1
+ import type { ChannelSetupAdapter } from "klaw/plugin-sdk/channel-setup";
2
+ import { DEFAULT_ACCOUNT_ID } from "klaw/plugin-sdk/routing";
3
+ import { hasConfiguredSecretInput, normalizeSecretInputString } from "klaw/plugin-sdk/secret-input";
4
+ import type { ChannelSetupDmPolicy, ChannelSetupWizard, DmPolicy } from "klaw/plugin-sdk/setup";
5
+ import {
6
+ createSetupTranslator,
7
+ createStandardChannelSetupStatus,
8
+ createTopLevelChannelDmPolicy,
9
+ createTopLevelChannelParsedAllowFromPrompt,
10
+ formatDocsLink,
11
+ mergeAllowFromEntries,
12
+ parseSetupEntriesWithParser,
13
+ patchTopLevelChannelConfigSection,
14
+ splitSetupEntries,
15
+ } from "klaw/plugin-sdk/setup";
16
+ import { DEFAULT_RELAYS } from "./default-relays.js";
17
+ import { getPublicKeyFromPrivate, normalizePubkey } from "./nostr-key-utils.js";
18
+ import { resolveDefaultNostrAccountId, resolveNostrAccount } from "./types.js";
19
+
20
+ const t = createSetupTranslator();
21
+
22
+ const channel = "nostr" as const;
23
+ const NOSTR_SETUP_HELP_LINES = [
24
+ t("wizard.nostr.helpPrivateKeyFormat"),
25
+ t("wizard.nostr.helpRelaysOptional"),
26
+ t("wizard.nostr.helpEnvVars"),
27
+ `Docs: ${formatDocsLink("/channels/nostr", "channels/nostr")}`,
28
+ ];
29
+
30
+ const NOSTR_ALLOW_FROM_HELP_LINES = [
31
+ t("wizard.nostr.allowlistIntro"),
32
+ t("wizard.nostr.examples"),
33
+ "- npub1...",
34
+ "- nostr:npub1...",
35
+ "- 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
36
+ t("wizard.nostr.multipleEntries"),
37
+ `Docs: ${formatDocsLink("/channels/nostr", "channels/nostr")}`,
38
+ ];
39
+
40
+ function buildNostrSetupPatch(accountId: string, patch: Record<string, unknown>) {
41
+ return {
42
+ ...(accountId !== DEFAULT_ACCOUNT_ID ? { defaultAccount: accountId } : {}),
43
+ ...patch,
44
+ };
45
+ }
46
+
47
+ function parseRelayUrls(raw: string): { relays: string[]; error?: string } {
48
+ const entries = splitSetupEntries(raw);
49
+ const relays: string[] = [];
50
+ for (const entry of entries) {
51
+ try {
52
+ const parsed = new URL(entry);
53
+ if (parsed.protocol !== "ws:" && parsed.protocol !== "wss:") {
54
+ return { relays: [], error: `Relay must use ws:// or wss:// (${entry})` };
55
+ }
56
+ } catch {
57
+ return { relays: [], error: `Invalid relay URL: ${entry}` };
58
+ }
59
+ relays.push(entry);
60
+ }
61
+ return { relays: [...new Set(relays)] };
62
+ }
63
+
64
+ function parseNostrAllowFrom(raw: string): { entries: string[]; error?: string } {
65
+ return parseSetupEntriesWithParser(raw, (entry) => {
66
+ const cleaned = entry.replace(/^nostr:/i, "").trim();
67
+ try {
68
+ return { value: normalizePubkey(cleaned) };
69
+ } catch {
70
+ return { error: `Invalid Nostr pubkey: ${entry}` };
71
+ }
72
+ });
73
+ }
74
+
75
+ const promptNostrAllowFrom = createTopLevelChannelParsedAllowFromPrompt({
76
+ channel,
77
+ defaultAccountId: resolveDefaultNostrAccountId,
78
+ noteTitle: t("wizard.nostr.allowlistTitle"),
79
+ noteLines: NOSTR_ALLOW_FROM_HELP_LINES,
80
+ message: t("wizard.nostr.allowFromPrompt"),
81
+ placeholder: "npub1..., 0123abcd...",
82
+ parseEntries: parseNostrAllowFrom,
83
+ mergeEntries: ({ existing, parsed }) => mergeAllowFromEntries(existing, parsed),
84
+ });
85
+
86
+ const nostrDmPolicy: ChannelSetupDmPolicy = createTopLevelChannelDmPolicy({
87
+ label: "Nostr",
88
+ channel,
89
+ policyKey: "channels.nostr.dmPolicy",
90
+ allowFromKey: "channels.nostr.allowFrom",
91
+ getCurrent: (cfg) => (cfg.channels?.nostr?.dmPolicy as DmPolicy | undefined) ?? "pairing",
92
+ promptAllowFrom: promptNostrAllowFrom,
93
+ });
94
+
95
+ export const nostrSetupAdapter: ChannelSetupAdapter = {
96
+ resolveAccountId: ({ cfg, accountId }) => accountId?.trim() || resolveDefaultNostrAccountId(cfg),
97
+ applyAccountName: ({ cfg, accountId, name }) =>
98
+ patchTopLevelChannelConfigSection({
99
+ cfg,
100
+ channel,
101
+ patch: buildNostrSetupPatch(accountId, name?.trim() ? { name: name.trim() } : {}),
102
+ }),
103
+ validateInput: ({ input }) => {
104
+ const typedInput = input as {
105
+ useEnv?: boolean;
106
+ privateKey?: string;
107
+ relayUrls?: string;
108
+ };
109
+ if (!typedInput.useEnv) {
110
+ const privateKey = typedInput.privateKey?.trim();
111
+ if (!privateKey) {
112
+ return "Nostr requires --private-key or --use-env.";
113
+ }
114
+ try {
115
+ getPublicKeyFromPrivate(privateKey);
116
+ } catch {
117
+ return "Nostr private key must be valid nsec or 64-character hex.";
118
+ }
119
+ }
120
+ if (typedInput.relayUrls?.trim()) {
121
+ return parseRelayUrls(typedInput.relayUrls).error ?? null;
122
+ }
123
+ return null;
124
+ },
125
+ applyAccountConfig: ({ cfg, accountId, input }) => {
126
+ const typedInput = input as {
127
+ useEnv?: boolean;
128
+ privateKey?: string;
129
+ relayUrls?: string;
130
+ };
131
+ const relayResult = typedInput.relayUrls?.trim()
132
+ ? parseRelayUrls(typedInput.relayUrls)
133
+ : { relays: [] };
134
+ return patchTopLevelChannelConfigSection({
135
+ cfg,
136
+ channel,
137
+ enabled: true,
138
+ clearFields: typedInput.useEnv ? ["privateKey"] : undefined,
139
+ patch: buildNostrSetupPatch(accountId, {
140
+ ...(typedInput.useEnv ? {} : { privateKey: typedInput.privateKey?.trim() }),
141
+ ...(relayResult.relays.length > 0 ? { relays: relayResult.relays } : {}),
142
+ }),
143
+ });
144
+ },
145
+ };
146
+
147
+ export const nostrSetupWizard: ChannelSetupWizard = {
148
+ channel,
149
+ resolveAccountIdForConfigure: ({ accountOverride, defaultAccountId }) =>
150
+ accountOverride?.trim() || defaultAccountId,
151
+ resolveShouldPromptAccountIds: () => false,
152
+ status: createStandardChannelSetupStatus({
153
+ channelLabel: "Nostr",
154
+ configuredLabel: t("wizard.channels.statusConfigured"),
155
+ unconfiguredLabel: t("wizard.channels.statusNeedsPrivateKey"),
156
+ configuredHint: t("wizard.channels.statusConfigured"),
157
+ unconfiguredHint: t("wizard.channels.statusNeedsPrivateKey"),
158
+ configuredScore: 1,
159
+ unconfiguredScore: 0,
160
+ includeStatusLine: true,
161
+ resolveConfigured: ({ cfg }) => resolveNostrAccount({ cfg }).configured,
162
+ resolveExtraStatusLines: ({ cfg }) => {
163
+ const account = resolveNostrAccount({ cfg });
164
+ return [`Relays: ${account.relays.length || DEFAULT_RELAYS.length}`];
165
+ },
166
+ }),
167
+ introNote: {
168
+ title: t("wizard.nostr.setupTitle"),
169
+ lines: NOSTR_SETUP_HELP_LINES,
170
+ },
171
+ envShortcut: {
172
+ prompt: t("wizard.nostr.privateKeyEnvPrompt"),
173
+ preferredEnvVar: "NOSTR_PRIVATE_KEY",
174
+ isAvailable: ({ cfg, accountId }) =>
175
+ accountId === DEFAULT_ACCOUNT_ID &&
176
+ Boolean(process.env.NOSTR_PRIVATE_KEY?.trim()) &&
177
+ !hasConfiguredSecretInput(resolveNostrAccount({ cfg, accountId }).config.privateKey),
178
+ apply: async ({ cfg, accountId }) =>
179
+ patchTopLevelChannelConfigSection({
180
+ cfg,
181
+ channel,
182
+ enabled: true,
183
+ clearFields: ["privateKey"],
184
+ patch: buildNostrSetupPatch(accountId, {}),
185
+ }),
186
+ },
187
+ credentials: [
188
+ {
189
+ inputKey: "privateKey",
190
+ providerHint: channel,
191
+ credentialLabel: "private key",
192
+ preferredEnvVar: "NOSTR_PRIVATE_KEY",
193
+ helpTitle: t("wizard.nostr.privateKeyTitle"),
194
+ helpLines: NOSTR_SETUP_HELP_LINES,
195
+ envPrompt: t("wizard.nostr.privateKeyEnvPrompt"),
196
+ keepPrompt: t("wizard.nostr.privateKeyKeep"),
197
+ inputPrompt: t("wizard.nostr.privateKeyInput"),
198
+ allowEnv: ({ accountId }) => accountId === DEFAULT_ACCOUNT_ID,
199
+ inspect: ({ cfg, accountId }) => {
200
+ const account = resolveNostrAccount({ cfg, accountId });
201
+ return {
202
+ accountConfigured: account.configured,
203
+ hasConfiguredValue: hasConfiguredSecretInput(account.config.privateKey),
204
+ resolvedValue: normalizeSecretInputString(account.config.privateKey),
205
+ envValue: process.env.NOSTR_PRIVATE_KEY?.trim(),
206
+ };
207
+ },
208
+ applyUseEnv: async ({ cfg, accountId }) =>
209
+ patchTopLevelChannelConfigSection({
210
+ cfg,
211
+ channel,
212
+ enabled: true,
213
+ clearFields: ["privateKey"],
214
+ patch: buildNostrSetupPatch(accountId, {}),
215
+ }),
216
+ applySet: async ({ cfg, accountId, resolvedValue }) =>
217
+ patchTopLevelChannelConfigSection({
218
+ cfg,
219
+ channel,
220
+ enabled: true,
221
+ patch: buildNostrSetupPatch(accountId, { privateKey: resolvedValue }),
222
+ }),
223
+ },
224
+ ],
225
+ textInputs: [
226
+ {
227
+ inputKey: "relayUrls",
228
+ message: t("wizard.nostr.relayUrlsPrompt"),
229
+ placeholder: DEFAULT_RELAYS.join(", "),
230
+ required: false,
231
+ applyEmptyValue: true,
232
+ helpTitle: t("wizard.nostr.relaysTitle"),
233
+ helpLines: [t("wizard.nostr.relaysWsOnly"), t("wizard.nostr.helpRelaysOptional")],
234
+ currentValue: ({ cfg, accountId }) => {
235
+ const account = resolveNostrAccount({ cfg, accountId });
236
+ const configuredRelays = cfg.channels?.nostr?.relays as string[] | undefined;
237
+ const relays = configuredRelays && configuredRelays.length > 0 ? account.relays : [];
238
+ return relays.join(", ");
239
+ },
240
+ keepPrompt: (value) => t("wizard.nostr.relayUrlsKeep", { value }),
241
+ validate: ({ value }) => parseRelayUrls(value).error,
242
+ applySet: async ({ cfg, accountId, value }) => {
243
+ const relayResult = parseRelayUrls(value);
244
+ return patchTopLevelChannelConfigSection({
245
+ cfg,
246
+ channel,
247
+ enabled: true,
248
+ clearFields: relayResult.relays.length > 0 ? undefined : ["relays"],
249
+ patch: buildNostrSetupPatch(
250
+ accountId,
251
+ relayResult.relays.length > 0 ? { relays: relayResult.relays } : {},
252
+ ),
253
+ });
254
+ },
255
+ },
256
+ ],
257
+ dmPolicy: nostrDmPolicy,
258
+ disable: (cfg) =>
259
+ patchTopLevelChannelConfigSection({
260
+ cfg,
261
+ channel,
262
+ patch: { enabled: false },
263
+ }),
264
+ };
@@ -0,0 +1,45 @@
1
+ import type { ResolvedNostrAccount } from "./types.js";
2
+
3
+ export const TEST_HEX_PRIVATE_KEY =
4
+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
5
+
6
+ export const TEST_HEX_PUBLIC_KEY =
7
+ "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789";
8
+
9
+ export const TEST_NSEC = "nsec1qypqxpq9qtpqscx7peytzfwtdjmcv0mrz5rjpej8vjppfkqfqy8skqfv3l";
10
+
11
+ export const TEST_RELAY_URL = "wss://relay.example.com";
12
+ export const TEST_SETUP_RELAY_URLS = ["wss://relay.damus.io", "wss://relay.primal.net"];
13
+ export const TEST_RESOLVED_PRIVATE_KEY = "resolved-nostr-private-key";
14
+
15
+ export const TEST_HEX_PRIVATE_KEY_BYTES = new Uint8Array(
16
+ TEST_HEX_PRIVATE_KEY.match(/.{2}/g)!.map((byte) => Number.parseInt(byte, 16)),
17
+ );
18
+
19
+ export function createConfiguredNostrCfg(overrides: Record<string, unknown> = {}): {
20
+ channels: { nostr: Record<string, unknown> };
21
+ } {
22
+ return {
23
+ channels: {
24
+ nostr: {
25
+ privateKey: TEST_HEX_PRIVATE_KEY,
26
+ ...overrides,
27
+ },
28
+ },
29
+ };
30
+ }
31
+
32
+ export function buildResolvedNostrAccount(
33
+ overrides: Partial<ResolvedNostrAccount> = {},
34
+ ): ResolvedNostrAccount {
35
+ return {
36
+ accountId: "default",
37
+ enabled: true,
38
+ configured: true,
39
+ privateKey: TEST_HEX_PRIVATE_KEY,
40
+ publicKey: TEST_HEX_PUBLIC_KEY,
41
+ relays: [TEST_RELAY_URL],
42
+ config: {},
43
+ ...overrides,
44
+ };
45
+ }