@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,163 +0,0 @@
1
- import { verifyChannelMessageAdapterCapabilityProofs } from "klaw/plugin-sdk/channel-message";
2
- import { createStartAccountContext } from "klaw/plugin-sdk/channel-test-helpers";
3
- import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
4
- import { afterEach, describe, expect, it, vi } from "vitest";
5
- import type { PluginRuntime } from "../runtime-api.js";
6
- import { nostrPlugin } from "./channel.js";
7
- import { nostrOutboundAdapter, startNostrGatewayAccount } from "./gateway.js";
8
- import { setNostrRuntime } from "./runtime.js";
9
- import { TEST_RESOLVED_PRIVATE_KEY, buildResolvedNostrAccount } from "./test-fixtures.js";
10
-
11
- const mocks = vi.hoisted(() => ({
12
- normalizePubkey: vi.fn((value: string) => `normalized-${value.toLowerCase()}`),
13
- startNostrBus: vi.fn(),
14
- }));
15
-
16
- vi.mock("./nostr-bus.js", () => ({
17
- DEFAULT_RELAYS: ["wss://relay.example.com"],
18
- startNostrBus: mocks.startNostrBus,
19
- }));
20
-
21
- vi.mock("./nostr-key-utils.js", () => ({
22
- getPublicKeyFromPrivate: vi.fn(() => "pubkey"),
23
- normalizePubkey: mocks.normalizePubkey,
24
- }));
25
-
26
- function createCfg() {
27
- return {
28
- channels: {
29
- nostr: {
30
- privateKey: TEST_RESOLVED_PRIVATE_KEY, // pragma: allowlist secret
31
- },
32
- },
33
- };
34
- }
35
-
36
- function installOutboundRuntime(convertMarkdownTables = vi.fn((text: string) => text)) {
37
- const resolveMarkdownTableMode = vi.fn(() => "off");
38
- setNostrRuntime({
39
- channel: {
40
- text: {
41
- resolveMarkdownTableMode,
42
- convertMarkdownTables,
43
- },
44
- },
45
- reply: {},
46
- } as unknown as PluginRuntime);
47
- return { resolveMarkdownTableMode, convertMarkdownTables };
48
- }
49
-
50
- async function startOutboundAccount(accountId?: string) {
51
- const sendDm = vi.fn(async () => {});
52
- const bus = {
53
- sendDm,
54
- close: vi.fn(),
55
- getMetrics: vi.fn(() => ({ counters: {} })),
56
- publishProfile: vi.fn(),
57
- getProfileState: vi.fn(async () => null),
58
- };
59
- mocks.startNostrBus.mockResolvedValueOnce(bus as unknown);
60
-
61
- const cleanup = (await startNostrGatewayAccount(
62
- createStartAccountContext({
63
- account: buildResolvedNostrAccount(accountId ? { accountId } : undefined),
64
- }),
65
- )) as { stop: () => void };
66
-
67
- return { cleanup, sendDm };
68
- }
69
-
70
- describe("nostr outbound cfg threading", () => {
71
- afterEach(() => {
72
- mocks.normalizePubkey.mockClear();
73
- mocks.startNostrBus.mockReset();
74
- });
75
-
76
- it("uses resolved cfg when converting markdown tables before send", async () => {
77
- const { resolveMarkdownTableMode, convertMarkdownTables } = installOutboundRuntime(
78
- vi.fn((text: string) => `converted:${text}`),
79
- );
80
- const { cleanup, sendDm } = await startOutboundAccount();
81
-
82
- const cfg = createCfg();
83
- await nostrOutboundAdapter.sendText({
84
- cfg: cfg as KlawConfig,
85
- to: "NPUB123",
86
- text: "|a|b|",
87
- accountId: "default",
88
- });
89
-
90
- expect(resolveMarkdownTableMode).toHaveBeenCalledWith({
91
- cfg,
92
- channel: "nostr",
93
- accountId: "default",
94
- });
95
- expect(convertMarkdownTables).toHaveBeenCalledWith("|a|b|", "off");
96
- expect(mocks.normalizePubkey).toHaveBeenCalledWith("NPUB123");
97
- expect(sendDm).toHaveBeenCalledWith("normalized-npub123", "converted:|a|b|");
98
-
99
- cleanup.stop();
100
- });
101
-
102
- it("uses the configured defaultAccount when accountId is omitted", async () => {
103
- const { resolveMarkdownTableMode } = installOutboundRuntime();
104
- const { cleanup, sendDm } = await startOutboundAccount("work");
105
-
106
- const cfg = {
107
- channels: {
108
- nostr: {
109
- privateKey: TEST_RESOLVED_PRIVATE_KEY, // pragma: allowlist secret
110
- defaultAccount: "work",
111
- },
112
- },
113
- };
114
-
115
- await nostrOutboundAdapter.sendText({
116
- cfg: cfg as KlawConfig,
117
- to: "NPUB123",
118
- text: "hello",
119
- });
120
-
121
- expect(resolveMarkdownTableMode).toHaveBeenCalledWith({
122
- cfg,
123
- channel: "nostr",
124
- accountId: "work",
125
- });
126
- expect(sendDm).toHaveBeenCalledWith("normalized-npub123", "hello");
127
-
128
- cleanup.stop();
129
- });
130
-
131
- it("backs declared message adapter capabilities with outbound sends", async () => {
132
- installOutboundRuntime();
133
- const { cleanup, sendDm } = await startOutboundAccount();
134
- const adapter = nostrPlugin.message;
135
- if (!adapter?.send?.text) {
136
- throw new Error("expected Nostr message adapter with text sender");
137
- }
138
- const sendText = adapter.send.text;
139
- expect(adapter.send.media).toBeUndefined();
140
-
141
- await verifyChannelMessageAdapterCapabilityProofs({
142
- adapterName: "nostrMessageAdapter",
143
- adapter,
144
- proofs: {
145
- text: async () => {
146
- const result = await sendText({
147
- cfg: createCfg() as KlawConfig,
148
- to: "NPUB123",
149
- text: "hello",
150
- accountId: "default",
151
- });
152
- expect(sendDm).toHaveBeenCalledWith("normalized-npub123", "hello");
153
- expect(result.receipt.parts[0]?.kind).toBe("text");
154
- },
155
- messageSendingHooks: () => {
156
- expect(sendText).toBeTypeOf("function");
157
- },
158
- },
159
- });
160
-
161
- cleanup.stop();
162
- });
163
- });
@@ -1,234 +0,0 @@
1
- import { describeAccountSnapshot } from "klaw/plugin-sdk/account-helpers";
2
- import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
3
- import { patchTopLevelChannelConfigSection } from "klaw/plugin-sdk/setup";
4
- import {
5
- createDelegatedSetupWizardProxy,
6
- createStandardChannelSetupStatus,
7
- DEFAULT_ACCOUNT_ID,
8
- createSetupTranslator,
9
- type ChannelSetupAdapter,
10
- } from "klaw/plugin-sdk/setup-runtime";
11
- import { buildChannelConfigSchema, type ChannelPlugin } from "./channel-api.js";
12
- import { NostrConfigSchema } from "./config-schema.js";
13
- import { DEFAULT_RELAYS } from "./default-relays.js";
14
-
15
- const t = createSetupTranslator();
16
-
17
- const channel = "nostr" as const;
18
-
19
- type NostrAccountConfig = {
20
- enabled?: boolean;
21
- name?: string;
22
- defaultAccount?: string;
23
- privateKey?: unknown;
24
- relays?: string[];
25
- dmPolicy?: "pairing" | "allowlist" | "open" | "disabled";
26
- allowFrom?: Array<string | number>;
27
- profile?: unknown;
28
- };
29
-
30
- type ResolvedNostrSetupAccount = {
31
- accountId: string;
32
- name?: string;
33
- enabled: boolean;
34
- configured: boolean;
35
- privateKey: string;
36
- publicKey: string;
37
- relays: string[];
38
- profile?: unknown;
39
- config: NostrAccountConfig;
40
- };
41
-
42
- function getNostrConfig(cfg: KlawConfig): NostrAccountConfig | undefined {
43
- return (cfg.channels as Record<string, unknown> | undefined)?.nostr as
44
- | NostrAccountConfig
45
- | undefined;
46
- }
47
-
48
- function listSetupNostrAccountIds(cfg: KlawConfig): string[] {
49
- const nostrCfg = getNostrConfig(cfg);
50
- const privateKey = typeof nostrCfg?.privateKey === "string" ? nostrCfg.privateKey.trim() : "";
51
- if (!privateKey) {
52
- return [];
53
- }
54
- return [resolveDefaultSetupNostrAccountId(cfg)];
55
- }
56
-
57
- function resolveDefaultSetupNostrAccountId(cfg: KlawConfig): string {
58
- const configured = getNostrConfig(cfg)?.defaultAccount;
59
- return typeof configured === "string" && configured.trim()
60
- ? configured.trim()
61
- : DEFAULT_ACCOUNT_ID;
62
- }
63
-
64
- function resolveSetupNostrAccount(params: {
65
- cfg: KlawConfig;
66
- accountId?: string | null;
67
- }): ResolvedNostrSetupAccount {
68
- const nostrCfg = getNostrConfig(params.cfg);
69
- const accountId = params.accountId?.trim() || resolveDefaultSetupNostrAccountId(params.cfg);
70
- const privateKey = typeof nostrCfg?.privateKey === "string" ? nostrCfg.privateKey.trim() : "";
71
- const configured = Boolean(privateKey);
72
- return {
73
- accountId,
74
- name: typeof nostrCfg?.name === "string" ? nostrCfg.name : undefined,
75
- enabled: nostrCfg?.enabled !== false,
76
- configured,
77
- privateKey,
78
- publicKey: "",
79
- relays: nostrCfg?.relays ?? DEFAULT_RELAYS,
80
- profile: nostrCfg?.profile,
81
- config: {
82
- enabled: nostrCfg?.enabled,
83
- name: nostrCfg?.name,
84
- privateKey: nostrCfg?.privateKey,
85
- relays: nostrCfg?.relays,
86
- dmPolicy: nostrCfg?.dmPolicy,
87
- allowFrom: nostrCfg?.allowFrom,
88
- profile: nostrCfg?.profile,
89
- },
90
- };
91
- }
92
-
93
- function buildNostrSetupPatch(accountId: string, patch: Record<string, unknown>) {
94
- return {
95
- ...(accountId !== DEFAULT_ACCOUNT_ID ? { defaultAccount: accountId } : {}),
96
- ...patch,
97
- };
98
- }
99
-
100
- function parseRelayUrls(raw: string): { relays: string[]; error?: string } {
101
- const entries = raw
102
- .split(/[,\n]/)
103
- .map((entry) => entry.trim())
104
- .filter(Boolean);
105
- const relays: string[] = [];
106
- for (const entry of entries) {
107
- try {
108
- const parsed = new URL(entry);
109
- if (parsed.protocol !== "ws:" && parsed.protocol !== "wss:") {
110
- return { relays: [], error: `Relay must use ws:// or wss:// (${entry})` };
111
- }
112
- } catch {
113
- return { relays: [], error: `Invalid relay URL: ${entry}` };
114
- }
115
- relays.push(entry);
116
- }
117
- return { relays: [...new Set(relays)] };
118
- }
119
-
120
- function looksLikeNostrPrivateKey(privateKey: string): boolean {
121
- return privateKey.startsWith("nsec1") || /^[0-9a-fA-F]{64}$/.test(privateKey);
122
- }
123
-
124
- const nostrSetupAdapter: ChannelSetupAdapter = {
125
- resolveAccountId: ({ cfg, accountId }) =>
126
- accountId?.trim() || resolveDefaultSetupNostrAccountId(cfg),
127
- applyAccountName: ({ cfg, accountId, name }) =>
128
- patchTopLevelChannelConfigSection({
129
- cfg,
130
- channel,
131
- patch: buildNostrSetupPatch(accountId, name?.trim() ? { name: name.trim() } : {}),
132
- }),
133
- validateInput: ({ input }) => {
134
- const typedInput = input as {
135
- useEnv?: boolean;
136
- privateKey?: string;
137
- relayUrls?: string;
138
- };
139
- if (!typedInput.useEnv) {
140
- const privateKey = typedInput.privateKey?.trim();
141
- if (!privateKey) {
142
- return "Nostr requires --private-key or --use-env.";
143
- }
144
- if (!looksLikeNostrPrivateKey(privateKey)) {
145
- return "Nostr private key must be valid nsec or 64-character hex.";
146
- }
147
- }
148
- if (typedInput.relayUrls?.trim()) {
149
- return parseRelayUrls(typedInput.relayUrls).error ?? null;
150
- }
151
- return null;
152
- },
153
- applyAccountConfig: ({ cfg, accountId, input }) => {
154
- const typedInput = input as {
155
- useEnv?: boolean;
156
- privateKey?: string;
157
- relayUrls?: string;
158
- };
159
- const relayResult = typedInput.relayUrls?.trim()
160
- ? parseRelayUrls(typedInput.relayUrls)
161
- : { relays: [] };
162
- return patchTopLevelChannelConfigSection({
163
- cfg,
164
- channel,
165
- enabled: true,
166
- clearFields: typedInput.useEnv ? ["privateKey"] : undefined,
167
- patch: buildNostrSetupPatch(accountId, {
168
- ...(typedInput.useEnv ? {} : { privateKey: typedInput.privateKey?.trim() }),
169
- ...(relayResult.relays.length > 0 ? { relays: relayResult.relays } : {}),
170
- }),
171
- });
172
- },
173
- };
174
-
175
- const nostrSetupWizard = createDelegatedSetupWizardProxy({
176
- channel,
177
- loadWizard: async () => (await import("./setup-surface.js")).nostrSetupWizard,
178
- status: {
179
- ...createStandardChannelSetupStatus({
180
- channelLabel: "Nostr",
181
- configuredLabel: t("wizard.channels.statusConfigured"),
182
- unconfiguredLabel: t("wizard.channels.statusNeedsPrivateKey"),
183
- configuredHint: t("wizard.channels.statusConfigured"),
184
- unconfiguredHint: t("wizard.channels.statusNeedsPrivateKey"),
185
- configuredScore: 1,
186
- unconfiguredScore: 0,
187
- includeStatusLine: true,
188
- resolveConfigured: ({ cfg, accountId }) =>
189
- resolveSetupNostrAccount({ cfg, accountId }).configured,
190
- resolveExtraStatusLines: ({ cfg }) => {
191
- const account = resolveSetupNostrAccount({ cfg });
192
- return [`Relays: ${account.relays.length || DEFAULT_RELAYS.length}`];
193
- },
194
- }),
195
- },
196
- resolveShouldPromptAccountIds: () => false,
197
- delegatePrepare: true,
198
- delegateFinalize: true,
199
- });
200
-
201
- export const nostrSetupPlugin: ChannelPlugin<ResolvedNostrSetupAccount> = {
202
- id: channel,
203
- meta: {
204
- id: channel,
205
- label: "Nostr",
206
- selectionLabel: "Nostr",
207
- docsPath: "/channels/nostr",
208
- docsLabel: "nostr",
209
- blurb: "Decentralized DMs via Nostr relays (NIP-04)",
210
- order: 100,
211
- },
212
- capabilities: {
213
- chatTypes: ["direct"],
214
- media: false,
215
- },
216
- reload: { configPrefixes: ["channels.nostr"] },
217
- configSchema: buildChannelConfigSchema(NostrConfigSchema),
218
- setup: nostrSetupAdapter,
219
- setupWizard: nostrSetupWizard,
220
- config: {
221
- listAccountIds: listSetupNostrAccountIds,
222
- resolveAccount: (cfg, accountId) => resolveSetupNostrAccount({ cfg, accountId }),
223
- defaultAccountId: resolveDefaultSetupNostrAccountId,
224
- isConfigured: (account) => account.configured,
225
- describeAccount: (account) =>
226
- describeAccountSnapshot({
227
- account,
228
- configured: account.configured,
229
- extra: {
230
- publicKey: account.publicKey,
231
- },
232
- }),
233
- },
234
- };