nostr-bray 1.13.0 → 1.14.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.
@@ -0,0 +1,25 @@
1
+ /**
2
+ * NIP-46 Bunker — remote signing daemon.
3
+ *
4
+ * Holds the user's secret key and responds to signing requests from
5
+ * authorised clients over Nostr relays. Clients never see the nsec.
6
+ *
7
+ * Protocol: NIP-46 (Nostr Connect)
8
+ * - Client sends kind 24133 encrypted request
9
+ * - Bunker responds with kind 24133 encrypted response
10
+ * - All communication NIP-44 encrypted between client and bunker
11
+ */
12
+ import type { IdentityContext } from './context.js';
13
+ export interface BunkerOptions {
14
+ ctx: IdentityContext;
15
+ relays: string[];
16
+ authorizedKeys?: string[];
17
+ quiet?: boolean;
18
+ }
19
+ export interface BunkerInstance {
20
+ url: string;
21
+ pubkey: string;
22
+ npub: string;
23
+ close: () => void;
24
+ }
25
+ export declare function startBunker(opts: BunkerOptions): BunkerInstance;
package/dist/bunker.js ADDED
@@ -0,0 +1,140 @@
1
+ /**
2
+ * NIP-46 Bunker — remote signing daemon.
3
+ *
4
+ * Holds the user's secret key and responds to signing requests from
5
+ * authorised clients over Nostr relays. Clients never see the nsec.
6
+ *
7
+ * Protocol: NIP-46 (Nostr Connect)
8
+ * - Client sends kind 24133 encrypted request
9
+ * - Bunker responds with kind 24133 encrypted response
10
+ * - All communication NIP-44 encrypted between client and bunker
11
+ */
12
+ import { getConversationKey, encrypt, decrypt } from 'nostr-tools/nip44';
13
+ import { finalizeEvent, getPublicKey, generateSecretKey } from 'nostr-tools/pure';
14
+ import { npubEncode } from 'nostr-tools/nip19';
15
+ import { useWebSocketImplementation, SimplePool } from 'nostr-tools/pool';
16
+ import WebSocket from 'ws';
17
+ useWebSocketImplementation(WebSocket);
18
+ export function startBunker(opts) {
19
+ const { ctx, relays, quiet } = opts;
20
+ const authorizedKeys = new Set(opts.authorizedKeys ?? []);
21
+ const log = quiet ? () => { } : (...args) => console.error('[bunker]', ...args);
22
+ // Generate a bunker-specific keypair for NIP-46 communication
23
+ const bunkerSk = generateSecretKey();
24
+ const bunkerPk = getPublicKey(bunkerSk);
25
+ const bunkerNpub = npubEncode(bunkerPk);
26
+ const pool = new SimplePool();
27
+ // Subscribe to kind 24133 events addressed to us
28
+ const sub = pool.subscribeMany(relays, { kinds: [24133], '#p': [bunkerPk] }, {
29
+ onevent: async (event) => {
30
+ try {
31
+ await handleRequest(event);
32
+ }
33
+ catch (e) {
34
+ log(`Error handling request: ${e.message}`);
35
+ }
36
+ },
37
+ });
38
+ async function handleRequest(event) {
39
+ const clientPk = event.pubkey;
40
+ // Check authorization
41
+ if (authorizedKeys.size > 0 && !authorizedKeys.has(clientPk)) {
42
+ log(`Rejected request from unauthorized key: ${clientPk.slice(0, 12)}...`);
43
+ return;
44
+ }
45
+ // Decrypt the request
46
+ const conversationKey = getConversationKey(bunkerSk, clientPk);
47
+ let request;
48
+ try {
49
+ const plaintext = decrypt(event.content, conversationKey);
50
+ request = JSON.parse(plaintext);
51
+ }
52
+ catch {
53
+ log('Failed to decrypt request');
54
+ return;
55
+ }
56
+ log(`Request ${request.id}: ${request.method}`);
57
+ let result;
58
+ let error;
59
+ switch (request.method) {
60
+ case 'connect':
61
+ result = 'ack';
62
+ break;
63
+ case 'ping':
64
+ result = 'pong';
65
+ break;
66
+ case 'get_public_key':
67
+ result = ctx.activePublicKeyHex;
68
+ break;
69
+ case 'sign_event': {
70
+ const template = JSON.parse(request.params[0]);
71
+ const sign = ctx.getSigningFunction();
72
+ const signed = await sign(template);
73
+ result = JSON.stringify(signed);
74
+ break;
75
+ }
76
+ case 'nip04_encrypt': {
77
+ const [thirdPartyPk, plaintext] = request.params;
78
+ const { encrypt: nip04encrypt } = await import('nostr-tools/nip04');
79
+ result = nip04encrypt(ctx.activePrivateKey, thirdPartyPk, plaintext);
80
+ break;
81
+ }
82
+ case 'nip04_decrypt': {
83
+ const [thirdPartyPk, ciphertext] = request.params;
84
+ const { decrypt: nip04decrypt } = await import('nostr-tools/nip04');
85
+ result = nip04decrypt(ctx.activePrivateKey, thirdPartyPk, ciphertext);
86
+ break;
87
+ }
88
+ case 'nip44_encrypt': {
89
+ const [thirdPartyPk, plaintext] = request.params;
90
+ const ck = getConversationKey(ctx.activePrivateKey, thirdPartyPk);
91
+ result = encrypt(plaintext, ck);
92
+ break;
93
+ }
94
+ case 'nip44_decrypt': {
95
+ const [thirdPartyPk, ciphertext] = request.params;
96
+ const ck = getConversationKey(ctx.activePrivateKey, thirdPartyPk);
97
+ result = decrypt(ciphertext, ck);
98
+ break;
99
+ }
100
+ default:
101
+ result = '';
102
+ error = `unsupported method: ${request.method}`;
103
+ }
104
+ // Encrypt and send response
105
+ const response = error
106
+ ? JSON.stringify({ id: request.id, result: '', error })
107
+ : JSON.stringify({ id: request.id, result });
108
+ const encrypted = encrypt(response, conversationKey);
109
+ const responseEvent = finalizeEvent({
110
+ kind: 24133,
111
+ created_at: Math.floor(Date.now() / 1000),
112
+ tags: [['p', clientPk]],
113
+ content: encrypted,
114
+ }, bunkerSk);
115
+ await Promise.any(pool.publish(relays, responseEvent));
116
+ log(`Response ${request.id}: ${error ?? 'ok'}`);
117
+ }
118
+ // Build bunker:// URI
119
+ const relayParams = relays.map(r => `relay=${encodeURIComponent(r)}`).join('&');
120
+ const bunkerUrl = `bunker://${bunkerPk}?${relayParams}`;
121
+ log(`Bunker started: ${bunkerNpub}`);
122
+ log(`URI: ${bunkerUrl}`);
123
+ log(`Signing as: ${ctx.activeNpub}`);
124
+ if (authorizedKeys.size > 0) {
125
+ log(`Authorized keys: ${authorizedKeys.size}`);
126
+ }
127
+ else {
128
+ log('WARNING: No authorized keys set — accepting requests from anyone');
129
+ }
130
+ return {
131
+ url: bunkerUrl,
132
+ pubkey: bunkerPk,
133
+ npub: bunkerNpub,
134
+ close: () => {
135
+ sub.close();
136
+ pool.destroy();
137
+ bunkerSk.fill(0);
138
+ },
139
+ };
140
+ }
package/dist/cli.js CHANGED
@@ -19,6 +19,7 @@ import { handleTrustSpokenChallenge, handleTrustSpokenVerify } from './trust/spo
19
19
  import { handleDuressConfigure, handleDuressActivate } from './safety/handlers.js';
20
20
  import { handleZapSend, handleZapBalance, handleZapMakeInvoice, handleZapLookupInvoice, handleZapListTransactions, handleZapReceipts, handleZapDecode } from './zap/handlers.js';
21
21
  import { handleDecode, handleEncodeNpub, handleEncodeNote, handleEncodeNprofile, handleEncodeNevent, handleVerify, handleEncrypt, handleDecrypt, handleCount, handleFetch, handleKeyPublic, handleEncodeNsec, handleFilter, handleNipList, handleNipShow } from './util/handlers.js';
22
+ import { handleKeyEncrypt, handleKeyDecrypt } from './util/ncryptsec.js';
22
23
  import { getCommandHelp } from './help.js';
23
24
  import * as fmt from './format.js';
24
25
  const args = process.argv.slice(2);
@@ -28,6 +29,30 @@ if (!command) {
28
29
  await import('./index.js');
29
30
  process.exit(0);
30
31
  }
32
+ // Bunker = NIP-46 remote signer
33
+ if (command === 'bunker' && !args.includes('--help')) {
34
+ const { startBunker } = await import('./bunker.js');
35
+ const config = (await import('./config.js')).loadConfig();
36
+ const { IdentityContext: IC } = await import('./context.js');
37
+ const bCtx = new IC(config.secretKey, config.secretFormat);
38
+ config.secretKey = '';
39
+ const authorizedKeys = args.includes('--authorized-keys')
40
+ ? args[args.indexOf('--authorized-keys') + 1].split(',')
41
+ : undefined;
42
+ const bunker = startBunker({
43
+ ctx: bCtx,
44
+ relays: config.relays,
45
+ authorizedKeys,
46
+ quiet: args.includes('--quiet'),
47
+ });
48
+ console.error(`nostr-bray bunker running`);
49
+ console.error(`URI: ${bunker.url}`);
50
+ console.error(`Signing as: ${bCtx.activeNpub}`);
51
+ console.error('Press Ctrl+C to stop');
52
+ process.on('SIGINT', () => { bunker.close(); bCtx.destroy(); process.exit(0); });
53
+ process.on('SIGTERM', () => { bunker.close(); bCtx.destroy(); process.exit(0); });
54
+ await new Promise(() => { });
55
+ }
31
56
  // Serve = in-memory test relay
32
57
  if (command === 'serve' && !args.includes('--help')) {
33
58
  const { startRelay } = await import('./serve.js');
@@ -133,6 +158,8 @@ Utility:
133
158
  encode-nevent <hex> [relay,...] Encode event ID + relays as nevent
134
159
  encode-nsec <hex> Encode hex private key as nsec
135
160
  key-public <nsec-or-hex> Derive pubkey from secret key
161
+ key-encrypt <nsec-or-hex> <pass> Encrypt key as ncryptsec (NIP-49)
162
+ key-decrypt <ncryptsec> <pass> Decrypt ncryptsec to verify pubkey
136
163
  filter <event-json> <filter-json> Test if event matches filter
137
164
  nips List all official NIPs
138
165
  nip <number> Show a specific NIP
@@ -145,6 +172,7 @@ Utility:
145
172
  Modes:
146
173
  (no command) Start MCP server (stdio)
147
174
  serve [--port N] [--events file] Start in-memory test relay
175
+ bunker [--authorized-keys pk,pk] Start NIP-46 remote signer daemon
148
176
  shell Interactive REPL (persistent relay connection)
149
177
 
150
178
  Environment:
@@ -601,6 +629,12 @@ async function run(cmdArgs) {
601
629
  case 'key-public':
602
630
  out(handleKeyPublic(req(1, 'key-public <nsec-or-hex>')));
603
631
  break;
632
+ case 'key-encrypt':
633
+ out(handleKeyEncrypt(req(1, 'key-encrypt <nsec-or-hex> <password>'), req(2, 'key-encrypt <nsec-or-hex> <password>')));
634
+ break;
635
+ case 'key-decrypt':
636
+ out(handleKeyDecrypt(req(1, 'key-decrypt <ncryptsec> <password>'), req(2, 'key-decrypt <ncryptsec> <password>')));
637
+ break;
604
638
  case 'filter':
605
639
  out(handleFilter(JSON.parse(req(1, 'filter <event-json> <filter-json>')), JSON.parse(req(2, 'filter <event-json> <filter-json>'))));
606
640
  break;
@@ -660,7 +694,8 @@ const ALL_COMMANDS = [
660
694
  'blossom-upload', 'blossom-list', 'blossom-delete',
661
695
  'group-info', 'group-chat', 'group-send', 'group-members',
662
696
  'decode', 'encode-npub', 'encode-note', 'encode-nprofile', 'encode-nevent', 'encode-nsec',
663
- 'key-public', 'filter', 'nips', 'nip', 'verify', 'encrypt', 'decrypt', 'count', 'fetch',
697
+ 'key-public', 'key-encrypt', 'key-decrypt', 'filter', 'nips', 'nip', 'verify', 'encrypt', 'decrypt', 'count', 'fetch',
698
+ 'bunker',
664
699
  'help', 'exit',
665
700
  ];
666
701
  async function shell() {
package/dist/help.js CHANGED
@@ -84,6 +84,9 @@ export const COMMAND_HELP = {
84
84
  count: { usage: 'count [--kinds N,N] [--authors X,X] [--since N]', description: 'Count events matching a filter on relays.', examples: ['nostr-bray count --kinds 1', 'nostr-bray count --kinds 1,7 --authors abc123...'] },
85
85
  fetch: { usage: 'fetch <nip19>', description: 'Fetch events by nip19 code (note, nevent, nprofile, npub, naddr). Resolves the entity and queries relays.', examples: ['nostr-bray fetch note1...', 'nostr-bray fetch nevent1...', 'nostr-bray fetch npub1...'] },
86
86
  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'] },
87
+ '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.' },
88
+ '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'] },
89
+ bunker: { usage: 'bunker [--authorized-keys pk1,pk2] [--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.', examples: ['nostr-bray bunker', 'nostr-bray bunker --authorized-keys abc123...,def456...', 'nostr-bray bunker --quiet'], notes: 'Without --authorized-keys, accepts requests from anyone. Set authorized keys for production use.' },
87
90
  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'] },
88
91
  };
89
92
  /** Get formatted help for a single command */
@@ -0,0 +1,17 @@
1
+ /**
2
+ * NIP-49 ncryptsec — password-encrypted secret keys.
3
+ *
4
+ * Encrypt an nsec with a passphrase for safe storage.
5
+ * Decrypt an ncryptsec back to the raw key.
6
+ */
7
+ /** Encrypt a secret key (nsec or hex) with a password. Returns ncryptsec string. */
8
+ export declare function handleKeyEncrypt(secret: string, password: string): {
9
+ ncryptsec: string;
10
+ pubkeyHex: string;
11
+ npub: string;
12
+ };
13
+ /** Decrypt an ncryptsec with a password. Returns the derived pubkey (never the raw key). */
14
+ export declare function handleKeyDecrypt(ncryptsec: string, password: string): {
15
+ pubkeyHex: string;
16
+ npub: string;
17
+ };
@@ -0,0 +1,39 @@
1
+ /**
2
+ * NIP-49 ncryptsec — password-encrypted secret keys.
3
+ *
4
+ * Encrypt an nsec with a passphrase for safe storage.
5
+ * Decrypt an ncryptsec back to the raw key.
6
+ */
7
+ import { encrypt, decrypt } from 'nostr-tools/nip49';
8
+ import { decode } from 'nostr-tools/nip19';
9
+ import { getPublicKey } from 'nostr-tools/pure';
10
+ import { npubEncode } from 'nostr-tools/nip19';
11
+ /** Encrypt a secret key (nsec or hex) with a password. Returns ncryptsec string. */
12
+ export function handleKeyEncrypt(secret, password) {
13
+ let bytes;
14
+ if (secret.startsWith('nsec1')) {
15
+ bytes = decode(secret).data;
16
+ }
17
+ else {
18
+ bytes = Buffer.from(secret, 'hex');
19
+ }
20
+ try {
21
+ const ncryptsec = encrypt(bytes, password);
22
+ const pubkeyHex = getPublicKey(bytes);
23
+ return { ncryptsec, pubkeyHex, npub: npubEncode(pubkeyHex) };
24
+ }
25
+ finally {
26
+ bytes.fill(0);
27
+ }
28
+ }
29
+ /** Decrypt an ncryptsec with a password. Returns the derived pubkey (never the raw key). */
30
+ export function handleKeyDecrypt(ncryptsec, password) {
31
+ const bytes = decrypt(ncryptsec, password);
32
+ try {
33
+ const pubkeyHex = getPublicKey(bytes);
34
+ return { pubkeyHex, npub: npubEncode(pubkeyHex) };
35
+ }
36
+ finally {
37
+ bytes.fill(0);
38
+ }
39
+ }
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { hexId } from '../validation.js';
3
+ import { handleKeyEncrypt, handleKeyDecrypt } from './ncryptsec.js';
3
4
  import { handleDecode, handleEncodeNpub, handleEncodeNote, handleEncodeNprofile, handleEncodeNevent, handleEncodeNaddr, handleVerify, handleEncrypt, handleDecrypt, handleCount, handleFetch, handleKeyPublic, handleEncodeNsec, handleFilter, handleNipList, handleNipShow, } from './handlers.js';
4
5
  export function registerUtilTools(server, deps) {
5
6
  server.registerTool('decode', {
@@ -167,4 +168,26 @@ export function registerUtilTools(server, deps) {
167
168
  const nip = await handleNipShow(number);
168
169
  return { content: [{ type: 'text', text: nip.content }] };
169
170
  });
171
+ server.registerTool('key_encrypt', {
172
+ description: 'Encrypt a secret key with a password (NIP-49 ncryptsec). Returns the ncryptsec string and the derived pubkey. WARNING: secret key is transmitted through the MCP transport.',
173
+ inputSchema: {
174
+ secret: z.string().describe('Secret key (nsec or hex)'),
175
+ password: z.string().describe('Password to encrypt with'),
176
+ },
177
+ annotations: { readOnlyHint: true },
178
+ }, async ({ secret, password }) => {
179
+ const result = handleKeyEncrypt(secret, password);
180
+ return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
181
+ });
182
+ server.registerTool('key_decrypt', {
183
+ description: 'Decrypt an ncryptsec (NIP-49) with a password. Returns the derived pubkey for verification — never the raw key.',
184
+ inputSchema: {
185
+ ncryptsec: z.string().describe('ncryptsec string to decrypt'),
186
+ password: z.string().describe('Password used during encryption'),
187
+ },
188
+ annotations: { readOnlyHint: true },
189
+ }, async ({ ncryptsec, password }) => {
190
+ const result = handleKeyDecrypt(ncryptsec, password);
191
+ return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
192
+ });
170
193
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nostr-bray",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "description": "MCP server giving AI agents sovereign Nostr identities — key derivation, personas, attestations, duress detection, zaps",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",