nostr-bray 3.8.0 → 3.9.0

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.
package/dist/bunker.d.ts CHANGED
@@ -27,10 +27,16 @@ export interface BunkerOptions {
27
27
  /** Called with the new link each time a client uses up the connect secret. */
28
28
  onUrlChange?: (url: string) => void;
29
29
  }
30
+ /**
31
+ * Remove a client pubkey from every bunker key's approvals in
32
+ * approved-clients.json. A running daemon notices on the client's next
33
+ * request. Returns how many bunker keys had approved it.
34
+ */
35
+ export declare function revokeClient(clientPk: string, stateDir?: string): number;
30
36
  export interface BunkerInstance {
31
37
  /** bunker:// URI for clients. Carries the current connect secret, so it changes once used. */
32
38
  readonly url: string;
33
- /** Client pubkeys approved in earlier runs (from approved-clients.json), at start. */
39
+ /** Client pubkeys approved for this bunker key (approved-clients.json). A revoked key leaves this list at that client's next request. */
34
40
  approvedClients: readonly string[];
35
41
  /** Whether a new client needs the connect secret (false with authorizedKeys or open). */
36
42
  requiresSecret: boolean;
package/dist/bunker.js CHANGED
@@ -15,7 +15,27 @@ import { finalizeEvent, getPublicKey, generateSecretKey } from 'nostr-tools/pure
15
15
  import { npubEncode } from 'nostr-tools/nip19';
16
16
  import { SimplePool } from 'nostr-tools/pool';
17
17
  import { webSocketFor } from './ws-safe.js';
18
- import { readStateFile, writeStateFile } from './state.js';
18
+ import { readStateFile, readStateFileStrict, writeStateFile } from './state.js';
19
+ /**
20
+ * Remove a client pubkey from every bunker key's approvals in
21
+ * approved-clients.json. A running daemon notices on the client's next
22
+ * request. Returns how many bunker keys had approved it.
23
+ */
24
+ export function revokeClient(clientPk, stateDir) {
25
+ const file = readStateFileStrict('approved-clients.json', stateDir);
26
+ if (file === null)
27
+ throw new Error('approved-clients.json in the bray state directory is unreadable; fix it or move it aside, then retry');
28
+ let removed = 0;
29
+ for (const [bunker, list] of Object.entries(file)) {
30
+ if (!Array.isArray(list) || !list.includes(clientPk))
31
+ continue;
32
+ file[bunker] = list.filter(pk => pk !== clientPk);
33
+ removed++;
34
+ }
35
+ if (removed > 0)
36
+ writeStateFile('approved-clients.json', file, stateDir);
37
+ return removed;
38
+ }
19
39
  export function startBunker(opts) {
20
40
  const { ctx, relays, quiet } = opts;
21
41
  const log = quiet ? () => { } : (...args) => console.error('[bunker]', ...args);
@@ -39,6 +59,9 @@ export function startBunker(opts) {
39
59
  // unless the daemon runs open. Clients approved earlier never need it.
40
60
  const allowList = cliAuthorizedKeys.size > 0;
41
61
  const requireSecret = !allowList && opts.open !== true;
62
+ // Open: any client is served and nothing is saved, so a stranger who found
63
+ // the link is not approved for a later daemon run without --open.
64
+ const openMode = !allowList && !requireSecret;
42
65
  // The unused secret is kept per bunker key, so a link handed out before a
43
66
  // restart still works. Only for a persistent bunker key: an ephemeral one
44
67
  // gets a new link every run anyway.
@@ -67,14 +90,22 @@ export function startBunker(opts) {
67
90
  return a.length === b.length && timingSafeEqual(a, b);
68
91
  }
69
92
  function approve(clientPk) {
93
+ if (openMode)
94
+ return;
70
95
  authorizedKeys.add(clientPk);
71
96
  if (bunkerApprovals.includes(clientPk))
72
97
  return;
73
98
  bunkerApprovals.push(clientPk);
74
- // Merge with the file, so a key added meanwhile by `bunker authorize` stays.
75
- const current = readStateFile(APPROVALS_FILE, opts.stateDir);
99
+ // Add just this key to the file, so a key added meanwhile by `bunker
100
+ // authorize` stays and a key removed by `bunker revoke` stays removed.
101
+ // An unreadable file is left alone rather than replaced by this one key.
102
+ const current = readStateFileStrict(APPROVALS_FILE, opts.stateDir);
103
+ if (current === null) {
104
+ log(`approved-clients.json is unreadable, so this approval is not saved; once the file is fixed this client needs a fresh link: ${clientPk.slice(0, 12)}...`);
105
+ return;
106
+ }
76
107
  const onDisk = Array.isArray(current[bunkerPk]) ? current[bunkerPk] : [];
77
- current[bunkerPk] = [...new Set([...onDisk, ...bunkerApprovals])];
108
+ current[bunkerPk] = [...new Set([...onDisk, clientPk])];
78
109
  writeStateFile(APPROVALS_FILE, current, opts.stateDir);
79
110
  log(`Approved and persisted client: ${clientPk.slice(0, 12)}...`);
80
111
  }
@@ -93,8 +124,35 @@ export function startBunker(opts) {
93
124
  }
94
125
  },
95
126
  });
127
+ // `bunker revoke` edits the approvals file while the daemon runs, so a
128
+ // client approved in memory is checked against the file before each request.
129
+ function stillApproved(clientPk) {
130
+ if (cliAuthorizedKeys.has(clientPk))
131
+ return true;
132
+ const current = readStateFileStrict(APPROVALS_FILE, opts.stateDir);
133
+ if (current === null) {
134
+ // Fail closed, but keep the in-memory approval for when the file is fixed.
135
+ log(`approved-clients.json is unreadable, so refused: ${clientPk.slice(0, 12)}...`);
136
+ return false;
137
+ }
138
+ const onDisk = Array.isArray(current[bunkerPk]) ? current[bunkerPk] : [];
139
+ if (onDisk.includes(clientPk))
140
+ return true;
141
+ authorizedKeys.delete(clientPk);
142
+ const i = bunkerApprovals.indexOf(clientPk);
143
+ if (i >= 0)
144
+ bunkerApprovals.splice(i, 1);
145
+ log(`Client no longer in approved-clients.json, so refused: ${clientPk.slice(0, 12)}...`);
146
+ return false;
147
+ }
96
148
  async function handleRequest(event) {
97
149
  const clientPk = event.pubkey;
150
+ if (!openMode && authorizedKeys.has(clientPk) && !stillApproved(clientPk)) {
151
+ // A revoked client has left authorizedKeys and meets the gates below as
152
+ // a stranger. One still there could not be checked (unreadable file).
153
+ if (authorizedKeys.has(clientPk))
154
+ return;
155
+ }
98
156
  // With --authorized-keys, drop anyone else unanswered, as before.
99
157
  if (allowList && !authorizedKeys.has(clientPk)) {
100
158
  log(`Rejected request from unauthorized key: ${clientPk.slice(0, 12)}...`);
@@ -302,7 +360,7 @@ export function startBunker(opts) {
302
360
  }
303
361
  return {
304
362
  get url() { return buildUrl(); },
305
- approvedClients: [...bunkerApprovals],
363
+ get approvedClients() { return [...bunkerApprovals]; },
306
364
  requiresSecret: requireSecret,
307
365
  pubkey: bunkerPk,
308
366
  npub: bunkerNpub,
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Bunker command module.
3
- * bunker sign, bunker connect, bunker pair, bunker authorize, bunker status, and bunker daemon
3
+ * bunker sign, bunker connect, bunker pair, bunker authorize, bunker revoke, bunker status, and bunker daemon
4
4
  * are all dispatched BEFORE ctx/pool setup in index.ts, so this module does its
5
5
  * own config/context construction.
6
6
  */
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Bunker command module.
3
- * bunker sign, bunker connect, bunker pair, bunker authorize, bunker status, and bunker daemon
3
+ * bunker sign, bunker connect, bunker pair, bunker authorize, bunker revoke, bunker status, and bunker daemon
4
4
  * are all dispatched BEFORE ctx/pool setup in index.ts, so this module does its
5
5
  * own config/context construction.
6
6
  */
@@ -237,6 +237,53 @@ export async function dispatch(args) {
237
237
  console.log(JSON.stringify({ authorised: normalised, total: entries.length }, null, 2));
238
238
  process.exit(0);
239
239
  }
240
+ // ── bunker revoke <app-pubkey|npub> ─────────────────────────────────────────
241
+ // Withdraw a client's approval, the reverse of `bunker authorize` and of a
242
+ // client connecting with the secret. Removes it for every bunker key; a
243
+ // running daemon refuses the client from its next request.
244
+ if (args[1] === 'revoke') {
245
+ const usage = 'usage: bray bunker revoke <64-char-hex-pubkey|npub>';
246
+ const given = args[2];
247
+ if (!given || args.length > 3) {
248
+ console.error(usage);
249
+ process.exit(1);
250
+ }
251
+ let clientPk;
252
+ if (/^[0-9a-f]{64}$/i.test(given)) {
253
+ clientPk = given.toLowerCase();
254
+ }
255
+ else {
256
+ const { decode } = await import('nostr-tools/nip19');
257
+ try {
258
+ const decoded = decode(given);
259
+ if (decoded.type === 'npub')
260
+ clientPk = decoded.data;
261
+ }
262
+ catch {
263
+ clientPk = undefined;
264
+ }
265
+ }
266
+ if (!clientPk) {
267
+ console.error(usage);
268
+ process.exit(1);
269
+ }
270
+ const { revokeClient } = await import('../../bunker.js');
271
+ let removed;
272
+ try {
273
+ removed = revokeClient(clientPk);
274
+ }
275
+ catch (err) {
276
+ console.error(`bunker revoke: ${err.message}`);
277
+ process.exit(1);
278
+ }
279
+ if (removed === 0) {
280
+ console.error(`bunker revoke: ${clientPk} is not an approved client (keys passed with --authorized-keys are set when the daemon starts; restart it without them)`);
281
+ process.exit(1);
282
+ }
283
+ console.error('A running bunker daemon refuses this client from its next request. A daemon started with --open serves any client, revoked or not; one started with --authorized-keys keeps the keys it was given.');
284
+ console.log(JSON.stringify({ revoked: clientPk, bunkers: removed }, null, 2));
285
+ process.exit(0);
286
+ }
240
287
  // ── bunker status ───────────────────────────────────────────────────────────
241
288
  // Report the saved bunker URI and its parsed details. Does NOT attempt a
242
289
  // live connection (use `bunker connect` to verify reachability).
@@ -360,7 +407,7 @@ export async function dispatch(args) {
360
407
  console.error('The link carries a one-time secret for one new client. Treat it like a password.');
361
408
  console.error('It survives restarts until used; each new client needs the next link the daemon prints.');
362
409
  if (bunker.approvedClients.length > 0) {
363
- console.error(`Clients approved earlier (they need no secret; remove any you do not recognise from approved-clients.json in bray's state directory):`);
410
+ console.error(`Clients approved earlier (they need no secret; remove any you do not recognise with "bray bunker revoke <pubkey>"):`);
364
411
  for (const pk of bunker.approvedClients)
365
412
  console.error(` ${pk}`);
366
413
  }
package/dist/cli/index.js CHANGED
@@ -251,6 +251,7 @@ Modes:
251
251
  bunker connect <bunker://…> Save remote bunker URI for future commands
252
252
  bunker pair [--relay <url>] [--expect <npub>] Pair with a signer app over nostrconnect:// and save it
253
253
  bunker authorize <hex-pubkey> Pre-authorise an app pubkey on the local bunker
254
+ bunker revoke <hex-pubkey|npub> Withdraw an app's approval, even from a running daemon
254
255
  bunker status Show saved bunker connection state
255
256
  bunker sign <file|-> One-shot: sign an event template via the bunker (or the saved signer)
256
257
  bunker daemon [--profile N] [--persona N] [--authorized-keys pk,pk] [--open] Start NIP-46 remote signer daemon
package/dist/help.js CHANGED
@@ -93,7 +93,7 @@ export const COMMAND_HELP = {
93
93
  shell: { usage: 'shell', description: 'Start an interactive REPL with a persistent relay connection. Supports tab autocomplete. Type "help" for commands, "exit" to quit.', examples: ['nostr-bray shell'] },
94
94
  'key-encrypt': { usage: 'key-encrypt <nsec-or-hex> <password>', description: 'Encrypt a secret key with a password using NIP-49. Returns an ncryptsec string safe for storage. Also returns the derived pubkey for verification.', examples: ['nostr-bray key-encrypt nsec1... mypassword'], notes: 'WARNING: secret key is passed as a CLI argument. Use only on trusted systems.' },
95
95
  'key-decrypt': { usage: 'key-decrypt <ncryptsec> <password>', description: 'Decrypt an ncryptsec (NIP-49) with a password. Returns the pubkey for verification — never the raw secret key.', examples: ['nostr-bray key-decrypt ncryptsec1... mypassword'] },
96
- bunker: { usage: 'bunker [--profile <name>] [--persona <name>] [--bunker-key-file <f>] [--authorized-keys pk1,pk2] [--open] [--quiet]', description: 'Start a NIP-46 remote signer daemon. Holds your secret key and responds to signing requests from authorised clients over Nostr relays. Clients never see your nsec. --profile <name> loads the master from an nsec-tree-cli profile (~/.nsec-tree/profiles/<name>.json); --persona <name> signs as a derived sub-identity (the same key as nsec-tree-cli "export nsec <name>"). The bunker:// key stays stable across restarts, since a per-identity connection key is persisted automatically. The printed link carries a one-time connect secret: a new client must connect with it, after which the daemon prints the next link, so one link cannot be shared between two apps. An unused secret survives restarts. Clients approved earlier reconnect without it and are listed at start.', examples: ['nostr-bray bunker', 'nostr-bray bunker --profile master --persona magazine', 'nostr-bray bunker --authorized-keys abc123...,def456...', 'nostr-bray bunker --quiet'], notes: 'With --authorized-keys, only those clients are accepted and no secret is used. --open approves any client that connects, with no secret, as earlier releases did; anyone who learns the link can then sign as you.' },
96
+ bunker: { usage: 'bunker [--profile <name>] [--persona <name>] [--bunker-key-file <f>] [--authorized-keys pk1,pk2] [--open] [--quiet]', description: 'Start a NIP-46 remote signer daemon. Holds your secret key and responds to signing requests from authorised clients over Nostr relays. Clients never see your nsec. --profile <name> loads the master from an nsec-tree-cli profile (~/.nsec-tree/profiles/<name>.json); --persona <name> signs as a derived sub-identity (the same key as nsec-tree-cli "export nsec <name>"). The bunker:// key stays stable across restarts, since a per-identity connection key is persisted automatically. The printed link carries a one-time connect secret: a new client must connect with it, after which the daemon prints the next link, so one link cannot be shared between two apps. An unused secret survives restarts. Clients approved earlier reconnect without it and are listed at start; `bunker revoke <pubkey>` withdraws one, and a running daemon refuses it from its next request (except with --open, which serves any client).', examples: ['nostr-bray bunker', 'nostr-bray bunker --profile master --persona magazine', 'nostr-bray bunker --authorized-keys abc123...,def456...', 'nostr-bray bunker --quiet'], notes: 'With --authorized-keys, only those clients are accepted and no secret is used. --open serves any client that connects, with no secret; anyone who learns the link can then sign as you. Clients that connect while it runs open are not saved, so a later daemon without --open still asks them for the secret.' },
97
97
  serve: { usage: 'serve [--port N] [--hostname H] [--events file.jsonl] [--quiet]', description: 'Start an in-memory Nostr relay for testing. Implements NIP-01 (events, subscriptions, EOSE) and NIP-11 (relay info). Events live in memory only — no persistence. Optionally pre-load events from a JSONL file.', examples: ['nostr-bray serve', 'nostr-bray serve --port 7777', 'nostr-bray serve --events test-data.jsonl', 'nostr-bray serve --port 7777 --quiet'] },
98
98
  };
99
99
  /** Get formatted help for a single command */
package/dist/state.d.ts CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Read a JSON state file that holds an object. Returns `{}` if the file is
3
+ * missing, and null if it exists but cannot be read or is not a JSON object,
4
+ * so a caller can refuse to overwrite it.
5
+ */
6
+ export declare function readStateFileStrict<T extends object = Record<string, unknown>>(name: string, stateDir?: string): T | null;
1
7
  /** Read and parse a JSON state file. Returns `{}` if missing or corrupt. */
2
8
  export declare function readStateFile<T = Record<string, unknown>>(name: string, stateDir?: string): T;
3
9
  /**
package/dist/state.js CHANGED
@@ -3,6 +3,23 @@ import { join } from 'node:path';
3
3
  import { homedir } from 'node:os';
4
4
  import { randomBytes } from 'node:crypto';
5
5
  const DEFAULT_STATE_DIR = join(process.env.XDG_CONFIG_HOME ?? join(homedir(), '.config'), 'bray');
6
+ /**
7
+ * Read a JSON state file that holds an object. Returns `{}` if the file is
8
+ * missing, and null if it exists but cannot be read or is not a JSON object,
9
+ * so a caller can refuse to overwrite it.
10
+ */
11
+ export function readStateFileStrict(name, stateDir = DEFAULT_STATE_DIR) {
12
+ const path = join(stateDir, name);
13
+ if (!existsSync(path))
14
+ return {};
15
+ try {
16
+ const parsed = JSON.parse(readFileSync(path, 'utf-8'));
17
+ return typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed) ? parsed : null;
18
+ }
19
+ catch {
20
+ return null;
21
+ }
22
+ }
6
23
  /** Read and parse a JSON state file. Returns `{}` if missing or corrupt. */
7
24
  export function readStateFile(name, stateDir = DEFAULT_STATE_DIR) {
8
25
  const path = join(stateDir, name);
package/llms-full.txt CHANGED
@@ -39,7 +39,7 @@ Single-process MCP server (stdio or HTTP transport) with 261 tools in 29 groups.
39
39
  *Authentication tiers (use exactly one):
40
40
  1. **Bunker** (NIP-46) -- most secure, keys never leave the signer
41
41
  For a signer app (Amber, nsec.app), `npx nostr-bray bunker pair --relay <wss://…>` prints a nostrconnect:// URI to paste or scan into the app. Once the app answers, bray saves the signer (a secret-free bunker:// URI marked paired, plus the client key the app approved) in its state directory, and the MCP server and CLI sign through it with no BUNKER_URI. Options: `--relay` (repeatable; defaults to NOSTR_RELAYS, then the config file relays), `--timeout <seconds>` (default 300), `--expect <npub>` (refuse any other account), `--yes` (skip the y/N prompt). In a terminal bray shows the account the signer offers and asks y/N before saving; outside one, `--expect` or `--yes` is required. The URI is a one-time credential: whoever answers it first becomes the signer. With TOR_PROXY set, pairing goes through the proxy and only .onion relays are accepted unless ALLOW_CLEARNET_WITH_TOR=1. `bunker status` reports `paired`; if the app forgets bray, start-up fails with a hint to run `bunker pair` again. An explicit BUNKER_URI or key takes precedence; `bunker connect` replaces the saved signer. A paired signer is checked with get_public_key on start, not sent connect again.
42
- bray can also be the signer: `npx nostr-bray bunker` runs a NIP-46 daemon holding your key and prints a bunker:// link with a one-time connect secret. A new client must connect with that link; the daemon then prints the next link, so one link serves one app. An unused secret survives restarts. Clients approved earlier reconnect without the secret and are listed at start. `--authorized-keys pk1,pk2` accepts only those clients; `--open` approves any client that connects, as earlier releases did, so anyone who learns the link can sign as you.
42
+ bray can also be the signer: `npx nostr-bray bunker` runs a NIP-46 daemon holding your key and prints a bunker:// link with a one-time connect secret. A new client must connect with that link; the daemon then prints the next link, so one link serves one app. An unused secret survives restarts. Clients approved earlier reconnect without the secret and are listed at start; `npx nostr-bray bunker revoke <pubkey|npub>` withdraws one, and a running daemon refuses it from its next request. `--authorized-keys pk1,pk2` accepts only those clients; `--open` serves any client that connects, so anyone who learns the link can sign as you; clients served while open are not saved as approved.
43
43
  2. **ncryptsec** (NIP-49) -- password-encrypted key, decrypted at startup
44
44
  3. **File** -- secret read from disk, deleted from process.env after parsing
45
45
  4. **Env var** -- quickest start, lowest security
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nostr-bray",
3
- "version": "3.8.0",
3
+ "version": "3.9.0",
4
4
  "mcpName": "dev.forgesworn/bray",
5
5
  "description": "Trust-aware Nostr MCP server, CLI and TypeScript SDK for AI agents and humans. Three dimensions of trust -- verification, proximity, and access -- woven into every interaction.",
6
6
  "type": "module",
@@ -180,7 +180,7 @@
180
180
  "farrier-kit": "1.1.3",
181
181
  "nostr-attestations": "^3.0.1",
182
182
  "nostr-tools": "2.24.1",
183
- "nostr-veil": "^0.30.0",
183
+ "nostr-veil": "^0.31.1",
184
184
  "nsec-tree": "^1.4.2",
185
185
  "signet-protocol": "^1.1.0",
186
186
  "socks-proxy-agent": "^10.0.0",