@actagent/nostr 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 (53) hide show
  1. package/README.md +142 -0
  2. package/actagent.plugin.json +17 -0
  3. package/api.ts +11 -0
  4. package/channel-plugin-api.ts +2 -0
  5. package/doctor-contract-api.test.ts +105 -0
  6. package/doctor-contract-api.ts +297 -0
  7. package/index.ts +96 -0
  8. package/npm-shrinkwrap.json +137 -0
  9. package/package.json +67 -0
  10. package/runtime-api.ts +6 -0
  11. package/setup-api.ts +2 -0
  12. package/setup-entry.ts +10 -0
  13. package/setup-plugin-api.ts +3 -0
  14. package/src/channel-api.ts +12 -0
  15. package/src/channel.inbound.test.ts +203 -0
  16. package/src/channel.lifecycle.test.ts +97 -0
  17. package/src/channel.outbound.test.ts +175 -0
  18. package/src/channel.setup.ts +161 -0
  19. package/src/channel.test.ts +527 -0
  20. package/src/channel.ts +215 -0
  21. package/src/config-schema.ts +99 -0
  22. package/src/default-relays.ts +2 -0
  23. package/src/gateway.ts +338 -0
  24. package/src/inbound-direct-dm-runtime.ts +2 -0
  25. package/src/metrics.ts +454 -0
  26. package/src/nostr-bus.fuzz.test.ts +383 -0
  27. package/src/nostr-bus.inbound.test.ts +598 -0
  28. package/src/nostr-bus.integration.test.ts +491 -0
  29. package/src/nostr-bus.test.ts +256 -0
  30. package/src/nostr-bus.ts +799 -0
  31. package/src/nostr-key-utils.ts +93 -0
  32. package/src/nostr-profile-core.ts +135 -0
  33. package/src/nostr-profile-http-runtime.ts +7 -0
  34. package/src/nostr-profile-http.test.ts +632 -0
  35. package/src/nostr-profile-http.ts +583 -0
  36. package/src/nostr-profile-import.test.ts +196 -0
  37. package/src/nostr-profile-import.ts +273 -0
  38. package/src/nostr-profile-url-safety.ts +22 -0
  39. package/src/nostr-profile.fuzz.test.ts +431 -0
  40. package/src/nostr-profile.test.ts +416 -0
  41. package/src/nostr-profile.ts +144 -0
  42. package/src/nostr-state-store.test.ts +172 -0
  43. package/src/nostr-state-store.ts +132 -0
  44. package/src/runtime.ts +10 -0
  45. package/src/seen-tracker.ts +291 -0
  46. package/src/session-route.ts +26 -0
  47. package/src/setup-adapter.ts +86 -0
  48. package/src/setup-surface.ts +204 -0
  49. package/src/test-fixtures.ts +46 -0
  50. package/src/types.ts +118 -0
  51. package/test/setup.ts +5 -0
  52. package/test-api.ts +2 -0
  53. package/tsconfig.json +16 -0
@@ -0,0 +1,175 @@
1
+ // Nostr tests cover channel.outbound plugin behavior.
2
+ import { verifyChannelMessageAdapterCapabilityProofs } from "actagent/plugin-sdk/channel-outbound";
3
+ import { createStartAccountContext } from "actagent/plugin-sdk/channel-test-helpers";
4
+ import type { ACTAgentConfig } from "actagent/plugin-sdk/config-contracts";
5
+ import { afterEach, describe, expect, it, vi } from "vitest";
6
+ import type { PluginRuntime } from "../runtime-api.js";
7
+ import { nostrPlugin } from "./channel.js";
8
+ import { nostrOutboundAdapter, startNostrGatewayAccount } from "./gateway.js";
9
+ import { setNostrRuntime } from "./runtime.js";
10
+ import { TEST_RESOLVED_PRIVATE_KEY, buildResolvedNostrAccount } from "./test-fixtures.js";
11
+
12
+ const mocks = vi.hoisted(() => ({
13
+ normalizePubkey: vi.fn((value: string) => `normalized-${value.toLowerCase()}`),
14
+ startNostrBus: vi.fn(),
15
+ }));
16
+
17
+ vi.mock("./nostr-bus.js", () => ({
18
+ DEFAULT_RELAYS: ["wss://relay.example.com"],
19
+ startNostrBus: mocks.startNostrBus,
20
+ }));
21
+
22
+ vi.mock("./nostr-key-utils.js", () => ({
23
+ getPublicKeyFromPrivate: vi.fn(() => "pubkey"),
24
+ normalizePubkey: mocks.normalizePubkey,
25
+ }));
26
+
27
+ function createCfg() {
28
+ return {
29
+ channels: {
30
+ nostr: {
31
+ privateKey: TEST_RESOLVED_PRIVATE_KEY, // pragma: allowlist secret
32
+ },
33
+ },
34
+ };
35
+ }
36
+
37
+ function installOutboundRuntime(convertMarkdownTables = vi.fn((text: string) => text)) {
38
+ const resolveMarkdownTableMode = vi.fn(() => "off");
39
+ setNostrRuntime({
40
+ channel: {
41
+ text: {
42
+ resolveMarkdownTableMode,
43
+ convertMarkdownTables,
44
+ },
45
+ },
46
+ reply: {},
47
+ } as unknown as PluginRuntime);
48
+ return { resolveMarkdownTableMode, convertMarkdownTables };
49
+ }
50
+
51
+ async function startOutboundAccount(accountId?: string) {
52
+ const sendDm = vi.fn(async () => {});
53
+ const bus = {
54
+ sendDm,
55
+ close: vi.fn(),
56
+ getMetrics: vi.fn(() => ({ counters: {} })),
57
+ publishProfile: vi.fn(),
58
+ getProfileState: vi.fn(async () => null),
59
+ };
60
+ mocks.startNostrBus.mockResolvedValueOnce(bus as unknown);
61
+ const abort = new AbortController();
62
+
63
+ const task = startNostrGatewayAccount(
64
+ createStartAccountContext({
65
+ account: buildResolvedNostrAccount(accountId ? { accountId } : undefined),
66
+ abortSignal: abort.signal,
67
+ }),
68
+ );
69
+ await vi.waitFor(() => {
70
+ expect(mocks.startNostrBus).toHaveBeenCalledTimes(1);
71
+ });
72
+ const cleanup = {
73
+ stop: async () => {
74
+ abort.abort();
75
+ await task;
76
+ },
77
+ };
78
+
79
+ return { cleanup, sendDm };
80
+ }
81
+
82
+ describe("nostr outbound cfg threading", () => {
83
+ afterEach(() => {
84
+ mocks.normalizePubkey.mockClear();
85
+ mocks.startNostrBus.mockReset();
86
+ });
87
+
88
+ it("uses resolved cfg when converting markdown tables before send", async () => {
89
+ const { resolveMarkdownTableMode, convertMarkdownTables } = installOutboundRuntime(
90
+ vi.fn((text: string) => `converted:${text}`),
91
+ );
92
+ const { cleanup, sendDm } = await startOutboundAccount();
93
+
94
+ const cfg = createCfg();
95
+ await nostrOutboundAdapter.sendText({
96
+ cfg: cfg as ACTAgentConfig,
97
+ to: "NPUB123",
98
+ text: "|a|b|",
99
+ accountId: "default",
100
+ });
101
+
102
+ expect(resolveMarkdownTableMode).toHaveBeenCalledWith({
103
+ cfg,
104
+ channel: "nostr",
105
+ accountId: "default",
106
+ });
107
+ expect(convertMarkdownTables).toHaveBeenCalledWith("|a|b|", "off");
108
+ expect(mocks.normalizePubkey).toHaveBeenCalledWith("NPUB123");
109
+ expect(sendDm).toHaveBeenCalledWith("normalized-npub123", "converted:|a|b|");
110
+
111
+ await cleanup.stop();
112
+ });
113
+
114
+ it("uses the configured defaultAccount when accountId is omitted", async () => {
115
+ const { resolveMarkdownTableMode } = installOutboundRuntime();
116
+ const { cleanup, sendDm } = await startOutboundAccount("work");
117
+
118
+ const cfg = {
119
+ channels: {
120
+ nostr: {
121
+ privateKey: TEST_RESOLVED_PRIVATE_KEY, // pragma: allowlist secret
122
+ defaultAccount: "work",
123
+ },
124
+ },
125
+ };
126
+
127
+ await nostrOutboundAdapter.sendText({
128
+ cfg: cfg as ACTAgentConfig,
129
+ to: "NPUB123",
130
+ text: "hello",
131
+ });
132
+
133
+ expect(resolveMarkdownTableMode).toHaveBeenCalledWith({
134
+ cfg,
135
+ channel: "nostr",
136
+ accountId: "work",
137
+ });
138
+ expect(sendDm).toHaveBeenCalledWith("normalized-npub123", "hello");
139
+
140
+ await cleanup.stop();
141
+ });
142
+
143
+ it("backs declared message adapter capabilities with outbound sends", async () => {
144
+ installOutboundRuntime();
145
+ const { cleanup, sendDm } = await startOutboundAccount();
146
+ const adapter = nostrPlugin.message;
147
+ if (!adapter?.send?.text) {
148
+ throw new Error("expected Nostr message adapter with text sender");
149
+ }
150
+ const sendText = adapter.send.text;
151
+ expect(adapter.send.media).toBeUndefined();
152
+
153
+ await verifyChannelMessageAdapterCapabilityProofs({
154
+ adapterName: "nostrMessageAdapter",
155
+ adapter,
156
+ proofs: {
157
+ text: async () => {
158
+ const result = await sendText({
159
+ cfg: createCfg() as ACTAgentConfig,
160
+ to: "NPUB123",
161
+ text: "hello",
162
+ accountId: "default",
163
+ });
164
+ expect(sendDm).toHaveBeenCalledWith("normalized-npub123", "hello");
165
+ expect(result.receipt.parts[0]?.kind).toBe("text");
166
+ },
167
+ messageSendingHooks: () => {
168
+ expect(sendText).toBeTypeOf("function");
169
+ },
170
+ },
171
+ });
172
+
173
+ await cleanup.stop();
174
+ });
175
+ });
@@ -0,0 +1,161 @@
1
+ // Nostr plugin module implements channel.setup behavior.
2
+ import { describeAccountSnapshot } from "actagent/plugin-sdk/account-helpers";
3
+ import type { ACTAgentConfig } from "actagent/plugin-sdk/config-contracts";
4
+ import {
5
+ createDelegatedSetupWizardProxy,
6
+ createStandardChannelSetupStatus,
7
+ DEFAULT_ACCOUNT_ID,
8
+ createSetupTranslator,
9
+ } from "actagent/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
+ import { createNostrSetupAdapter } from "./setup-adapter.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: ACTAgentConfig): NostrAccountConfig | undefined {
43
+ return (cfg.channels as Record<string, unknown> | undefined)?.nostr as
44
+ | NostrAccountConfig
45
+ | undefined;
46
+ }
47
+
48
+ function listSetupNostrAccountIds(cfg: ACTAgentConfig): 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: ACTAgentConfig): 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: ACTAgentConfig;
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 looksLikeNostrPrivateKey(privateKey: string): boolean {
94
+ return privateKey.startsWith("nsec1") || /^[0-9a-fA-F]{64}$/.test(privateKey);
95
+ }
96
+
97
+ const nostrSetupAdapter = createNostrSetupAdapter({
98
+ resolveAccountId: (cfg, accountId) => accountId?.trim() || resolveDefaultSetupNostrAccountId(cfg),
99
+ validatePrivateKey: looksLikeNostrPrivateKey,
100
+ });
101
+
102
+ const nostrSetupWizard = createDelegatedSetupWizardProxy({
103
+ channel,
104
+ loadWizard: async () => (await import("./setup-surface.js")).nostrSetupWizard,
105
+ status: {
106
+ ...createStandardChannelSetupStatus({
107
+ channelLabel: "Nostr",
108
+ configuredLabel: t("wizard.channels.statusConfigured"),
109
+ unconfiguredLabel: t("wizard.channels.statusNeedsPrivateKey"),
110
+ configuredHint: t("wizard.channels.statusConfigured"),
111
+ unconfiguredHint: t("wizard.channels.statusNeedsPrivateKey"),
112
+ configuredScore: 1,
113
+ unconfiguredScore: 0,
114
+ includeStatusLine: true,
115
+ resolveConfigured: ({ cfg, accountId }) =>
116
+ resolveSetupNostrAccount({ cfg, accountId }).configured,
117
+ resolveExtraStatusLines: ({ cfg }) => {
118
+ const account = resolveSetupNostrAccount({ cfg });
119
+ return [`Relays: ${account.relays.length || DEFAULT_RELAYS.length}`];
120
+ },
121
+ }),
122
+ },
123
+ resolveShouldPromptAccountIds: () => false,
124
+ delegatePrepare: true,
125
+ delegateFinalize: true,
126
+ });
127
+
128
+ export const nostrSetupPlugin: ChannelPlugin<ResolvedNostrSetupAccount> = {
129
+ id: channel,
130
+ meta: {
131
+ id: channel,
132
+ label: "Nostr",
133
+ selectionLabel: "Nostr",
134
+ docsPath: "/channels/nostr",
135
+ docsLabel: "nostr",
136
+ blurb: "Decentralized DMs via Nostr relays (NIP-04)",
137
+ order: 100,
138
+ },
139
+ capabilities: {
140
+ chatTypes: ["direct"],
141
+ media: false,
142
+ },
143
+ reload: { configPrefixes: ["channels.nostr"] },
144
+ configSchema: buildChannelConfigSchema(NostrConfigSchema),
145
+ setup: nostrSetupAdapter,
146
+ setupWizard: nostrSetupWizard,
147
+ config: {
148
+ listAccountIds: listSetupNostrAccountIds,
149
+ resolveAccount: (cfg, accountId) => resolveSetupNostrAccount({ cfg, accountId }),
150
+ defaultAccountId: resolveDefaultSetupNostrAccountId,
151
+ isConfigured: (account) => account.configured,
152
+ describeAccount: (account) =>
153
+ describeAccountSnapshot({
154
+ account,
155
+ configured: account.configured,
156
+ extra: {
157
+ publicKey: account.publicKey,
158
+ },
159
+ }),
160
+ },
161
+ };