@kodelyth/nostr 2026.5.42 → 2026.6.2

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 (47) hide show
  1. package/klaw.plugin.json +185 -2
  2. package/package.json +19 -6
  3. package/api.ts +0 -10
  4. package/channel-plugin-api.ts +0 -1
  5. package/index.ts +0 -95
  6. package/runtime-api.ts +0 -6
  7. package/setup-api.ts +0 -1
  8. package/setup-entry.ts +0 -9
  9. package/setup-plugin-api.ts +0 -3
  10. package/src/channel-api.ts +0 -11
  11. package/src/channel.inbound.test.ts +0 -187
  12. package/src/channel.outbound.test.ts +0 -163
  13. package/src/channel.setup.ts +0 -234
  14. package/src/channel.test.ts +0 -526
  15. package/src/channel.ts +0 -215
  16. package/src/config-schema.ts +0 -98
  17. package/src/default-relays.ts +0 -1
  18. package/src/gateway.ts +0 -321
  19. package/src/inbound-direct-dm-runtime.ts +0 -1
  20. package/src/metrics.ts +0 -458
  21. package/src/nostr-bus.fuzz.test.ts +0 -382
  22. package/src/nostr-bus.inbound.test.ts +0 -526
  23. package/src/nostr-bus.integration.test.ts +0 -477
  24. package/src/nostr-bus.test.ts +0 -231
  25. package/src/nostr-bus.ts +0 -789
  26. package/src/nostr-key-utils.ts +0 -94
  27. package/src/nostr-profile-core.ts +0 -134
  28. package/src/nostr-profile-http-runtime.ts +0 -6
  29. package/src/nostr-profile-http.test.ts +0 -632
  30. package/src/nostr-profile-http.ts +0 -583
  31. package/src/nostr-profile-import.test.ts +0 -119
  32. package/src/nostr-profile-import.ts +0 -262
  33. package/src/nostr-profile-url-safety.ts +0 -21
  34. package/src/nostr-profile.fuzz.test.ts +0 -430
  35. package/src/nostr-profile.test.ts +0 -415
  36. package/src/nostr-profile.ts +0 -144
  37. package/src/nostr-state-store.test.ts +0 -237
  38. package/src/nostr-state-store.ts +0 -206
  39. package/src/runtime.ts +0 -9
  40. package/src/seen-tracker.ts +0 -289
  41. package/src/session-route.ts +0 -25
  42. package/src/setup-surface.ts +0 -264
  43. package/src/test-fixtures.ts +0 -45
  44. package/src/types.ts +0 -117
  45. package/test/setup.ts +0 -5
  46. package/test-api.ts +0 -1
  47. package/tsconfig.json +0 -16
@@ -1,526 +0,0 @@
1
- import {
2
- createPluginSetupWizardConfigure,
3
- createTestWizardPrompter,
4
- runSetupWizardConfigure,
5
- } from "klaw/plugin-sdk/plugin-test-runtime";
6
- import type { WizardPrompter } from "klaw/plugin-sdk/plugin-test-runtime";
7
- import { describe, expect, it, vi } from "vitest";
8
- import type { KlawConfig } from "../runtime-api.js";
9
- import { nostrSetupWizard } from "./setup-surface.js";
10
- import {
11
- TEST_HEX_PRIVATE_KEY,
12
- TEST_SETUP_RELAY_URLS,
13
- buildResolvedNostrAccount,
14
- createConfiguredNostrCfg,
15
- } from "./test-fixtures.js";
16
- import { listNostrAccountIds, resolveDefaultNostrAccountId, resolveNostrAccount } from "./types.js";
17
-
18
- function normalizeNostrTestEntry(entry: string): string {
19
- return entry
20
- .trim()
21
- .replace(/^nostr:/i, "")
22
- .toLowerCase();
23
- }
24
-
25
- function resolveNostrTestDmPolicy(params: {
26
- cfg: KlawConfig;
27
- account: ReturnType<typeof resolveNostrAccount>;
28
- }) {
29
- return {
30
- cfg: params.cfg,
31
- accountId: params.account.accountId,
32
- policy: params.account.config.dmPolicy ?? "pairing",
33
- allowFrom: params.account.config.allowFrom ?? [],
34
- normalizeEntry: normalizeNostrTestEntry,
35
- };
36
- }
37
-
38
- const nostrTestPlugin = {
39
- id: "nostr",
40
- meta: {
41
- label: "Nostr",
42
- docsPath: "/channels/nostr",
43
- blurb: "Decentralized DMs via Nostr relays (NIP-04)",
44
- },
45
- capabilities: {
46
- chatTypes: ["direct"],
47
- media: false,
48
- },
49
- config: {
50
- listAccountIds: listNostrAccountIds,
51
- resolveAccount: (cfg: KlawConfig, accountId?: string | null) =>
52
- resolveNostrAccount({ cfg, accountId }),
53
- },
54
- messaging: {
55
- normalizeTarget: (target: string) => normalizeNostrTestEntry(target),
56
- targetResolver: {
57
- looksLikeId: (input: string) => {
58
- const trimmed = input.trim();
59
- return trimmed.startsWith("npub1") || /^[0-9a-fA-F]{64}$/.test(trimmed);
60
- },
61
- },
62
- },
63
- outbound: {
64
- deliveryMode: "direct",
65
- textChunkLimit: 4000,
66
- },
67
- pairing: {
68
- idLabel: "nostrPubkey",
69
- normalizeAllowEntry: normalizeNostrTestEntry,
70
- },
71
- security: {
72
- resolveDmPolicy: resolveNostrTestDmPolicy,
73
- },
74
- status: {
75
- defaultRuntime: {
76
- accountId: "default",
77
- running: false,
78
- lastStartAt: null,
79
- lastStopAt: null,
80
- lastError: null,
81
- },
82
- },
83
- setupWizard: nostrSetupWizard,
84
- setup: {
85
- resolveAccountId: ({
86
- cfg,
87
- accountId,
88
- }: {
89
- cfg: KlawConfig;
90
- accountId?: string;
91
- input: unknown;
92
- }) => accountId?.trim() || resolveDefaultNostrAccountId(cfg),
93
- },
94
- };
95
-
96
- const nostrConfigure = createPluginSetupWizardConfigure(nostrTestPlugin);
97
-
98
- function requireNostrLooksLikeId() {
99
- const looksLikeId = nostrTestPlugin.messaging?.targetResolver?.looksLikeId;
100
- if (!looksLikeId) {
101
- throw new Error("nostr messaging.targetResolver.looksLikeId missing");
102
- }
103
- return looksLikeId;
104
- }
105
-
106
- function requireNostrNormalizeTarget() {
107
- const normalize = nostrTestPlugin.messaging?.normalizeTarget;
108
- if (!normalize) {
109
- throw new Error("nostr messaging.normalizeTarget missing");
110
- }
111
- return normalize;
112
- }
113
-
114
- function requireNostrPairingNormalizer() {
115
- const normalize = nostrTestPlugin.pairing?.normalizeAllowEntry;
116
- if (!normalize) {
117
- throw new Error("nostr pairing.normalizeAllowEntry missing");
118
- }
119
- return normalize;
120
- }
121
-
122
- function requireNostrResolveDmPolicy() {
123
- const resolveDmPolicy = nostrTestPlugin.security?.resolveDmPolicy;
124
- if (!resolveDmPolicy) {
125
- throw new Error("nostr security.resolveDmPolicy missing");
126
- }
127
- return resolveDmPolicy;
128
- }
129
-
130
- function createUnresolvedNostrPrivateKeyCfg() {
131
- return {
132
- channels: {
133
- nostr: {
134
- privateKey: {
135
- source: "env" as const,
136
- provider: "default",
137
- id: "NOSTR_PRIVATE_KEY",
138
- },
139
- },
140
- },
141
- };
142
- }
143
-
144
- const unresolvedSecretRefPrivateKeyCases = [
145
- {
146
- name: "listNostrAccountIds",
147
- assert: (cfg: ReturnType<typeof createUnresolvedNostrPrivateKeyCfg>) => {
148
- expect(listNostrAccountIds(cfg)).toStrictEqual([]);
149
- },
150
- },
151
- {
152
- name: "resolveNostrAccount",
153
- assert: (cfg: ReturnType<typeof createUnresolvedNostrPrivateKeyCfg>) => {
154
- const account = resolveNostrAccount({ cfg });
155
-
156
- expect(account.configured).toBe(false);
157
- expect(account.privateKey).toBe("");
158
- expect(account.publicKey).toBe("");
159
- expect(account.config.privateKey).toEqual(cfg.channels.nostr.privateKey);
160
- },
161
- },
162
- ];
163
-
164
- describe("nostrPlugin", () => {
165
- describe("meta", () => {
166
- it("has correct id", () => {
167
- expect(nostrTestPlugin.id).toBe("nostr");
168
- });
169
-
170
- it("has required meta fields", () => {
171
- expect(nostrTestPlugin.meta.label).toBe("Nostr");
172
- expect(nostrTestPlugin.meta.docsPath).toBe("/channels/nostr");
173
- expect(nostrTestPlugin.meta.blurb).toContain("NIP-04");
174
- });
175
- });
176
-
177
- describe("capabilities", () => {
178
- it("supports direct messages", () => {
179
- expect(nostrTestPlugin.capabilities.chatTypes).toContain("direct");
180
- });
181
-
182
- it("does not support groups (MVP)", () => {
183
- expect(nostrTestPlugin.capabilities.chatTypes).not.toContain("group");
184
- });
185
-
186
- it("does not support media (MVP)", () => {
187
- expect(nostrTestPlugin.capabilities.media).toBe(false);
188
- });
189
- });
190
-
191
- describe("config adapter", () => {
192
- it("listAccountIds returns empty array for unconfigured", () => {
193
- const cfg = { channels: {} };
194
- const ids = nostrTestPlugin.config.listAccountIds(cfg);
195
- expect(ids).toStrictEqual([]);
196
- });
197
-
198
- it("listAccountIds returns default for configured", () => {
199
- const cfg = createConfiguredNostrCfg();
200
- const ids = nostrTestPlugin.config.listAccountIds(cfg);
201
- expect(ids).toContain("default");
202
- });
203
- });
204
-
205
- describe("messaging", () => {
206
- it("recognizes npub as valid target", () => {
207
- const looksLikeId = requireNostrLooksLikeId();
208
-
209
- expect(looksLikeId("npub1xyz123")).toBe(true);
210
- });
211
-
212
- it("recognizes hex pubkey as valid target", () => {
213
- const looksLikeId = requireNostrLooksLikeId();
214
-
215
- expect(looksLikeId(TEST_HEX_PRIVATE_KEY)).toBe(true);
216
- });
217
-
218
- it("rejects invalid input", () => {
219
- const looksLikeId = requireNostrLooksLikeId();
220
-
221
- expect(looksLikeId("not-a-pubkey")).toBe(false);
222
- expect(looksLikeId("")).toBe(false);
223
- });
224
-
225
- it("normalizeTarget strips spaced nostr prefixes", () => {
226
- const normalize = requireNostrNormalizeTarget();
227
-
228
- expect(normalize(`nostr:${TEST_HEX_PRIVATE_KEY}`)).toBe(TEST_HEX_PRIVATE_KEY);
229
- expect(normalize(` nostr:${TEST_HEX_PRIVATE_KEY} `)).toBe(TEST_HEX_PRIVATE_KEY);
230
- });
231
- });
232
-
233
- describe("outbound", () => {
234
- it("has correct delivery mode", () => {
235
- expect(nostrTestPlugin.outbound?.deliveryMode).toBe("direct");
236
- });
237
-
238
- it("has reasonable text chunk limit", () => {
239
- expect(nostrTestPlugin.outbound?.textChunkLimit).toBe(4000);
240
- });
241
- });
242
-
243
- describe("pairing", () => {
244
- it("has id label for pairing", () => {
245
- expect(nostrTestPlugin.pairing?.idLabel).toBe("nostrPubkey");
246
- });
247
-
248
- it("normalizes spaced nostr prefixes in allow entries", () => {
249
- const normalize = requireNostrPairingNormalizer();
250
-
251
- expect(normalize(`nostr:${TEST_HEX_PRIVATE_KEY}`)).toBe(TEST_HEX_PRIVATE_KEY);
252
- expect(normalize(` nostr:${TEST_HEX_PRIVATE_KEY} `)).toBe(TEST_HEX_PRIVATE_KEY);
253
- });
254
- });
255
-
256
- describe("security", () => {
257
- it("normalizes dm allowlist entries through the dm policy adapter", () => {
258
- const resolveDmPolicy = requireNostrResolveDmPolicy();
259
-
260
- const cfg = createConfiguredNostrCfg({
261
- dmPolicy: "allowlist",
262
- allowFrom: [` nostr:${TEST_HEX_PRIVATE_KEY} `],
263
- });
264
- const account = buildResolvedNostrAccount({
265
- config: cfg.channels.nostr,
266
- });
267
-
268
- const result = resolveDmPolicy({ cfg, account });
269
- if (!result) {
270
- throw new Error("nostr resolveDmPolicy returned null");
271
- }
272
-
273
- expect(result.policy).toBe("allowlist");
274
- expect(result.allowFrom).toEqual([` nostr:${TEST_HEX_PRIVATE_KEY} `]);
275
- expect(result.normalizeEntry?.(` nostr:${TEST_HEX_PRIVATE_KEY} `)).toBe(
276
- TEST_HEX_PRIVATE_KEY,
277
- );
278
- });
279
- });
280
-
281
- describe("status", () => {
282
- it("has default runtime", () => {
283
- expect(nostrTestPlugin.status?.defaultRuntime).toEqual({
284
- accountId: "default",
285
- running: false,
286
- lastStartAt: null,
287
- lastStopAt: null,
288
- lastError: null,
289
- });
290
- });
291
- });
292
- });
293
-
294
- describe("nostr setup wizard", () => {
295
- it("configures a private key and relay URLs", async () => {
296
- const prompter = createTestWizardPrompter({
297
- text: vi.fn(async ({ message }: { message: string }) => {
298
- if (message === "Nostr private key (nsec... or hex)") {
299
- return TEST_HEX_PRIVATE_KEY;
300
- }
301
- if (message === "Relay URLs (comma-separated, optional)") {
302
- return TEST_SETUP_RELAY_URLS.join(", ");
303
- }
304
- throw new Error(`Unexpected prompt: ${message}`);
305
- }) as WizardPrompter["text"],
306
- });
307
-
308
- const result = await runSetupWizardConfigure({
309
- configure: nostrConfigure,
310
- cfg: {} as KlawConfig,
311
- prompter,
312
- options: {},
313
- });
314
-
315
- expect(result.accountId).toBe("default");
316
- expect(result.cfg.channels?.nostr?.enabled).toBe(true);
317
- expect(result.cfg.channels?.nostr?.privateKey).toBe(TEST_HEX_PRIVATE_KEY);
318
- expect(result.cfg.channels?.nostr?.relays).toEqual(TEST_SETUP_RELAY_URLS);
319
- });
320
-
321
- it("preserves the selected named account label during setup", async () => {
322
- const prompter = createTestWizardPrompter({
323
- text: vi.fn(async ({ message }: { message: string }) => {
324
- if (message === "Nostr private key (nsec... or hex)") {
325
- return TEST_HEX_PRIVATE_KEY;
326
- }
327
- if (message === "Relay URLs (comma-separated, optional)") {
328
- return "";
329
- }
330
- throw new Error(`Unexpected prompt: ${message}`);
331
- }) as WizardPrompter["text"],
332
- });
333
-
334
- const result = await runSetupWizardConfigure({
335
- configure: nostrConfigure,
336
- cfg: {} as KlawConfig,
337
- prompter,
338
- options: {},
339
- accountOverrides: {
340
- nostr: "work",
341
- },
342
- });
343
-
344
- expect(result.accountId).toBe("work");
345
- expect(result.cfg.channels?.nostr?.defaultAccount).toBe("work");
346
- expect(result.cfg.channels?.nostr?.privateKey).toBe(TEST_HEX_PRIVATE_KEY);
347
- });
348
-
349
- it("uses configured defaultAccount when setup accountId is omitted", () => {
350
- expect(
351
- nostrTestPlugin.setup?.resolveAccountId?.({
352
- cfg: createConfiguredNostrCfg({ defaultAccount: "work" }) as KlawConfig,
353
- accountId: undefined,
354
- input: {},
355
- } as never),
356
- ).toBe("work");
357
- });
358
- });
359
-
360
- describe("nostr unresolved SecretRef privateKey", () => {
361
- it.each(unresolvedSecretRefPrivateKeyCases)(
362
- "$name does not treat unresolved SecretRef privateKey as configured",
363
- ({ assert }) => {
364
- assert(createUnresolvedNostrPrivateKeyCfg());
365
- },
366
- );
367
- });
368
-
369
- describe("nostr account helpers", () => {
370
- describe("listNostrAccountIds", () => {
371
- it("returns empty array when not configured", () => {
372
- const cfg = { channels: {} };
373
- expect(listNostrAccountIds(cfg)).toStrictEqual([]);
374
- });
375
-
376
- it("returns empty array when nostr section exists but no privateKey", () => {
377
- const cfg = { channels: { nostr: { enabled: true } } };
378
- expect(listNostrAccountIds(cfg)).toStrictEqual([]);
379
- });
380
-
381
- it("returns default when privateKey is configured", () => {
382
- const cfg = createConfiguredNostrCfg();
383
- expect(listNostrAccountIds(cfg)).toEqual(["default"]);
384
- });
385
-
386
- it("returns configured defaultAccount when privateKey is configured", () => {
387
- const cfg = createConfiguredNostrCfg({ defaultAccount: "work" });
388
- expect(listNostrAccountIds(cfg)).toEqual(["work"]);
389
- });
390
- });
391
-
392
- describe("resolveDefaultNostrAccountId", () => {
393
- it("returns default when configured", () => {
394
- const cfg = createConfiguredNostrCfg();
395
- expect(resolveDefaultNostrAccountId(cfg)).toBe("default");
396
- });
397
-
398
- it("returns default when not configured", () => {
399
- const cfg = { channels: {} };
400
- expect(resolveDefaultNostrAccountId(cfg)).toBe("default");
401
- });
402
-
403
- it("prefers configured defaultAccount when present", () => {
404
- const cfg = createConfiguredNostrCfg({ defaultAccount: "work" });
405
- expect(resolveDefaultNostrAccountId(cfg)).toBe("work");
406
- });
407
- });
408
-
409
- describe("resolveNostrAccount", () => {
410
- it("resolves configured account", () => {
411
- const cfg = createConfiguredNostrCfg({
412
- name: "Test Bot",
413
- relays: ["wss://test.relay"],
414
- dmPolicy: "pairing" as const,
415
- });
416
- const account = resolveNostrAccount({ cfg });
417
-
418
- expect(account.accountId).toBe("default");
419
- expect(account.name).toBe("Test Bot");
420
- expect(account.enabled).toBe(true);
421
- expect(account.configured).toBe(true);
422
- expect(account.privateKey).toBe(TEST_HEX_PRIVATE_KEY);
423
- expect(account.publicKey).toMatch(/^[0-9a-f]{64}$/);
424
- expect(account.relays).toEqual(["wss://test.relay"]);
425
- });
426
-
427
- it("resolves unconfigured account with defaults", () => {
428
- const cfg = { channels: {} };
429
- const account = resolveNostrAccount({ cfg });
430
-
431
- expect(account.accountId).toBe("default");
432
- expect(account.enabled).toBe(true);
433
- expect(account.configured).toBe(false);
434
- expect(account.privateKey).toBe("");
435
- expect(account.publicKey).toBe("");
436
- expect(account.relays).toContain("wss://relay.damus.io");
437
- expect(account.relays).toContain("wss://nos.lol");
438
- });
439
-
440
- it("handles disabled channel", () => {
441
- const cfg = createConfiguredNostrCfg({ enabled: false });
442
- const account = resolveNostrAccount({ cfg });
443
-
444
- expect(account.enabled).toBe(false);
445
- expect(account.configured).toBe(true);
446
- });
447
-
448
- it("handles custom accountId parameter", () => {
449
- const cfg = createConfiguredNostrCfg();
450
- const account = resolveNostrAccount({ cfg, accountId: "custom" });
451
-
452
- expect(account.accountId).toBe("custom");
453
- });
454
-
455
- it("handles allowFrom config", () => {
456
- const cfg = createConfiguredNostrCfg({
457
- allowFrom: ["npub1test", "0123456789abcdef"],
458
- });
459
- const account = resolveNostrAccount({ cfg });
460
-
461
- expect(account.config.allowFrom).toEqual(["npub1test", "0123456789abcdef"]);
462
- });
463
-
464
- it("handles invalid private key gracefully", () => {
465
- const cfg = {
466
- channels: {
467
- nostr: {
468
- privateKey: "invalid-key",
469
- },
470
- },
471
- };
472
- const account = resolveNostrAccount({ cfg });
473
-
474
- expect(account.configured).toBe(true);
475
- expect(account.publicKey).toBe("");
476
- });
477
-
478
- it("preserves all config options", () => {
479
- const cfg = createConfiguredNostrCfg({
480
- name: "Bot",
481
- enabled: true,
482
- relays: ["wss://relay1", "wss://relay2"],
483
- dmPolicy: "allowlist" as const,
484
- allowFrom: ["pubkey1", "pubkey2"],
485
- });
486
- const account = resolveNostrAccount({ cfg });
487
-
488
- expect(account.config).toEqual({
489
- privateKey: TEST_HEX_PRIVATE_KEY,
490
- name: "Bot",
491
- enabled: true,
492
- relays: ["wss://relay1", "wss://relay2"],
493
- dmPolicy: "allowlist",
494
- allowFrom: ["pubkey1", "pubkey2"],
495
- });
496
- });
497
- });
498
-
499
- describe("setup wizard", () => {
500
- it("keeps unresolved SecretRef privateKey visible without marking the account configured", () => {
501
- const secretRef = {
502
- source: "env" as const,
503
- provider: "default",
504
- id: "NOSTR_PRIVATE_KEY",
505
- };
506
- const cfg = {
507
- channels: {
508
- nostr: {
509
- privateKey: secretRef,
510
- },
511
- },
512
- };
513
- const credential = nostrSetupWizard.credentials?.[0];
514
- if (!credential?.inspect) {
515
- throw new Error("nostr setup credential inspect missing");
516
- }
517
-
518
- expect(credential.inspect({ cfg, accountId: "default" })).toEqual({
519
- accountConfigured: false,
520
- hasConfiguredValue: true,
521
- resolvedValue: undefined,
522
- envValue: undefined,
523
- });
524
- });
525
- });
526
- });