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