nostr-bray 3.14.2 → 3.14.3
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-context.d.ts +2 -0
- package/dist/bunker-context.js +2 -0
- package/dist/bunker.d.ts +6 -0
- package/dist/bunker.js +81 -8
- package/dist/cli/commands/bunker.js +9 -3
- package/dist/hold-open.d.ts +13 -0
- package/dist/hold-open.js +20 -0
- package/dist/relay/auth.d.ts +9 -1
- package/dist/relay/auth.js +10 -2
- package/dist/relay/tools.js +4 -3
- package/dist/relay-pool.d.ts +8 -0
- package/dist/relay-pool.js +64 -2
- package/dist/signing-context.d.ts +6 -0
- package/llms-full.txt +2 -2
- package/package.json +1 -1
package/dist/bunker-context.d.ts
CHANGED
|
@@ -151,6 +151,8 @@ export declare class BunkerContext implements SigningContext {
|
|
|
151
151
|
get activePublicKeyHex(): string;
|
|
152
152
|
/** Sign an event via the remote bunker */
|
|
153
153
|
getSigningFunction(): SignFn;
|
|
154
|
+
/** Signatures come from the remote signer over a relay. A getter, so a Heartwood context re-classed from this one has it too. */
|
|
155
|
+
get remoteSigner(): boolean;
|
|
154
156
|
/** List identities — bunker mode only has one (the remote key) */
|
|
155
157
|
listIdentities(): Promise<PublicIdentity[]>;
|
|
156
158
|
/** NIP-44 encrypt via the remote bunker */
|
package/dist/bunker-context.js
CHANGED
|
@@ -230,6 +230,8 @@ export class BunkerContext {
|
|
|
230
230
|
return withTimeout(this.signer.signEvent(template), REQUEST_TIMEOUT_MS, 'bunker sign_event');
|
|
231
231
|
};
|
|
232
232
|
}
|
|
233
|
+
/** Signatures come from the remote signer over a relay. A getter, so a Heartwood context re-classed from this one has it too. */
|
|
234
|
+
get remoteSigner() { return true; }
|
|
233
235
|
/** List identities — bunker mode only has one (the remote key) */
|
|
234
236
|
async listIdentities() {
|
|
235
237
|
return [{ npub: this.activeNpub, purpose: 'bunker', index: 0 }];
|
package/dist/bunker.d.ts
CHANGED
|
@@ -26,6 +26,12 @@ export interface BunkerOptions {
|
|
|
26
26
|
open?: boolean;
|
|
27
27
|
/** Called with the new link each time a client uses up the connect secret. */
|
|
28
28
|
onUrlChange?: (url: string) => void;
|
|
29
|
+
/**
|
|
30
|
+
* Milliseconds before subscribing again to a relay that ended the
|
|
31
|
+
* subscription (default 5000), doubling up to a minute while the relay
|
|
32
|
+
* keeps refusing or dropping it.
|
|
33
|
+
*/
|
|
34
|
+
resubscribeDelayMs?: number;
|
|
29
35
|
}
|
|
30
36
|
/**
|
|
31
37
|
* Remove a client pubkey from every bunker key's approvals in
|
package/dist/bunker.js
CHANGED
|
@@ -17,6 +17,17 @@ import { SimplePool } from 'nostr-tools/pool';
|
|
|
17
17
|
import { webSocketFor } from './ws-safe.js';
|
|
18
18
|
import { isConnectionFailure, setAutomaticAuth } from './nostr-tools-guards.js';
|
|
19
19
|
import { readStateFile, readStateFileStrict, writeStateFile } from './state.js';
|
|
20
|
+
const MAX_RESUBSCRIBE_DELAY_MS = 60_000;
|
|
21
|
+
// A subscription that lasted this long was healthy: the next drop starts the
|
|
22
|
+
// backoff again instead of continuing it.
|
|
23
|
+
const HEALTHY_SUBSCRIPTION_MS = 30_000;
|
|
24
|
+
// A resubscription asks for requests from this far back, the longest a client
|
|
25
|
+
// waits on a signature (the bunker client's own timeout), so a request sent
|
|
26
|
+
// during a short outage is still answered.
|
|
27
|
+
const RESUBSCRIBE_WINDOW_SECS = 60;
|
|
28
|
+
// Request ids kept to answer each event once: the same request can arrive from
|
|
29
|
+
// several relays, and again when a resubscription replays it.
|
|
30
|
+
const SEEN_EVENTS_MAX = 2048;
|
|
20
31
|
/**
|
|
21
32
|
* Remove a client pubkey from every bunker key's approvals in
|
|
22
33
|
* approved-clients.json. A running daemon notices on the client's next
|
|
@@ -115,17 +126,73 @@ export function startBunker(opts) {
|
|
|
115
126
|
const pool = new SimplePool({ websocketImplementation: webSocketFor(opts.torProxy) });
|
|
116
127
|
const answerChallenge = async (template) => finalizeEvent(template, bunkerSk);
|
|
117
128
|
setAutomaticAuth(pool, () => answerChallenge);
|
|
118
|
-
// Subscribe to kind 24133 events addressed to us
|
|
119
|
-
|
|
120
|
-
|
|
129
|
+
// Subscribe to kind 24133 events addressed to us, one subscription per relay
|
|
130
|
+
// so that each can be restored on its own. nostr-tools reports the end of a
|
|
131
|
+
// multi-relay subscription only once every relay has ended it, and it does
|
|
132
|
+
// not reconnect by default, so a relay that drops would otherwise stay
|
|
133
|
+
// unlistened to for as long as another one stayed up.
|
|
134
|
+
const baseResubscribeDelay = opts.resubscribeDelayMs ?? 5_000;
|
|
135
|
+
const seenEvents = new Set();
|
|
136
|
+
const subscriptions = new Map();
|
|
137
|
+
const retries = new Map();
|
|
138
|
+
let closed = false;
|
|
139
|
+
const onEvent = async (event) => {
|
|
140
|
+
if (seenEvents.has(event.id))
|
|
141
|
+
return;
|
|
142
|
+
seenEvents.add(event.id);
|
|
143
|
+
if (seenEvents.size > SEEN_EVENTS_MAX)
|
|
144
|
+
seenEvents.delete(seenEvents.values().next().value);
|
|
145
|
+
try {
|
|
146
|
+
await handleRequest(event);
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
log(`Error handling request: ${e.message}`);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
// `delayMs` is the wait to use if this subscription ends before it proved
|
|
153
|
+
// healthy; the wait doubles while a relay keeps dropping it.
|
|
154
|
+
function listen(url, delayMs, since) {
|
|
155
|
+
const startedAt = Date.now();
|
|
156
|
+
let ended = false;
|
|
157
|
+
const filter = { kinds: [24133], '#p': [bunkerPk], ...(since === undefined ? {} : { since }) };
|
|
158
|
+
const sub = pool.subscribeMany([url], filter, {
|
|
159
|
+
onevent: onEvent,
|
|
160
|
+
// A members-only relay refuses a REQ that beats the AUTH with
|
|
161
|
+
// `auth-required:`; nostr-tools then authenticates and subscribes again
|
|
162
|
+
// at once, instead of the drop path below waiting out its delay.
|
|
163
|
+
onauth: answerChallenge,
|
|
164
|
+
onclose: reasons => {
|
|
165
|
+
// close() ends every subscription itself; that is not a drop.
|
|
166
|
+
if (closed || ended)
|
|
167
|
+
return;
|
|
168
|
+
ended = true;
|
|
169
|
+
subscriptions.delete(url);
|
|
170
|
+
const why = reasons.map(r => r.reason).join('; ') || 'no reason given';
|
|
171
|
+
const wait = Date.now() - startedAt >= HEALTHY_SUBSCRIPTION_MS ? baseResubscribeDelay : delayMs;
|
|
172
|
+
log(`Subscription on ${url} ended (${why}); subscribing again in ${wait} ms`);
|
|
173
|
+
retry(url, wait);
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
subscriptions.set(url, sub);
|
|
177
|
+
}
|
|
178
|
+
function retry(url, waitMs) {
|
|
179
|
+
const nextMs = Math.min(waitMs * 2, MAX_RESUBSCRIBE_DELAY_MS);
|
|
180
|
+
retries.set(url, setTimeout(() => {
|
|
181
|
+
retries.delete(url);
|
|
182
|
+
if (closed)
|
|
183
|
+
return;
|
|
121
184
|
try {
|
|
122
|
-
|
|
185
|
+
listen(url, nextMs, Math.floor(Date.now() / 1000) - RESUBSCRIBE_WINDOW_SECS);
|
|
186
|
+
log(`Subscribed again on ${url}`);
|
|
123
187
|
}
|
|
124
188
|
catch (e) {
|
|
125
|
-
log(`
|
|
189
|
+
log(`Could not subscribe again on ${url}: ${e.message}; trying again in ${nextMs} ms`);
|
|
190
|
+
retry(url, nextMs);
|
|
126
191
|
}
|
|
127
|
-
},
|
|
128
|
-
}
|
|
192
|
+
}, waitMs));
|
|
193
|
+
}
|
|
194
|
+
for (const url of new Set(relays))
|
|
195
|
+
listen(url, baseResubscribeDelay);
|
|
129
196
|
// `bunker revoke` edits the approvals file while the daemon runs, so a
|
|
130
197
|
// client approved in memory is checked against the file before each request.
|
|
131
198
|
function stillApproved(clientPk) {
|
|
@@ -393,7 +460,13 @@ export function startBunker(opts) {
|
|
|
393
460
|
pubkey: bunkerPk,
|
|
394
461
|
npub: bunkerNpub,
|
|
395
462
|
close: () => {
|
|
396
|
-
|
|
463
|
+
closed = true;
|
|
464
|
+
for (const timer of retries.values())
|
|
465
|
+
clearTimeout(timer);
|
|
466
|
+
retries.clear();
|
|
467
|
+
for (const sub of subscriptions.values())
|
|
468
|
+
sub.close();
|
|
469
|
+
subscriptions.clear();
|
|
397
470
|
pool.destroy();
|
|
398
471
|
bunkerSk.fill(0);
|
|
399
472
|
},
|
|
@@ -403,6 +403,7 @@ export async function dispatch(args) {
|
|
|
403
403
|
}
|
|
404
404
|
// Bunker daemon
|
|
405
405
|
const { startBunker } = await import('../../bunker.js');
|
|
406
|
+
const { holdProcessOpen } = await import('../../hold-open.js');
|
|
406
407
|
const { readNsecTreeProfileMnemonic, getOrCreateBunkerKey } = await import('../../bunker-helpers.js');
|
|
407
408
|
// --profile <name>: load the master straight from an nsec-tree-cli profile
|
|
408
409
|
// (~/.nsec-tree/profiles/<name>.json). loadConfig scrubs NOSTR_SECRET_KEY after.
|
|
@@ -477,7 +478,12 @@ export async function dispatch(args) {
|
|
|
477
478
|
}
|
|
478
479
|
console.error(`Signing as: ${bCtx.activeNpub}`);
|
|
479
480
|
console.error('Press Ctrl+C to stop');
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
481
|
+
// The daemon is held up by a timer, not by its relay sockets: a relay that
|
|
482
|
+
// restarts or an idle close must not leave the process with nothing to wait
|
|
483
|
+
// for (Node then exits with code 13). The bunker resubscribes on its own.
|
|
484
|
+
const hold = holdProcessOpen();
|
|
485
|
+
const stop = () => { hold.release(); bunker.close(); bCtx.destroy(); process.exit(0); };
|
|
486
|
+
process.on('SIGINT', stop);
|
|
487
|
+
process.on('SIGTERM', stop);
|
|
488
|
+
await hold.done;
|
|
483
489
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keep the process alive until released.
|
|
3
|
+
*
|
|
4
|
+
* A long-running daemon must not end with `await new Promise(() => {})`: that
|
|
5
|
+
* promise holds no event-loop handle, so the moment the last socket closes
|
|
6
|
+
* Node finds nothing left to wait for and exits with code 13 ("unsettled
|
|
7
|
+
* top-level await"). A timer is a handle, so this holds the loop open however
|
|
8
|
+
* the sockets behave. Release it from the shutdown path.
|
|
9
|
+
*/
|
|
10
|
+
export declare function holdProcessOpen(): {
|
|
11
|
+
done: Promise<void>;
|
|
12
|
+
release: () => void;
|
|
13
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keep the process alive until released.
|
|
3
|
+
*
|
|
4
|
+
* A long-running daemon must not end with `await new Promise(() => {})`: that
|
|
5
|
+
* promise holds no event-loop handle, so the moment the last socket closes
|
|
6
|
+
* Node finds nothing left to wait for and exits with code 13 ("unsettled
|
|
7
|
+
* top-level await"). A timer is a handle, so this holds the loop open however
|
|
8
|
+
* the sockets behave. Release it from the shutdown path.
|
|
9
|
+
*/
|
|
10
|
+
export function holdProcessOpen() {
|
|
11
|
+
let release;
|
|
12
|
+
const done = new Promise(resolve => {
|
|
13
|
+
const timer = setInterval(() => { }, 60_000);
|
|
14
|
+
release = () => {
|
|
15
|
+
clearInterval(timer);
|
|
16
|
+
resolve();
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
return { done, release };
|
|
20
|
+
}
|
package/dist/relay/auth.d.ts
CHANGED
|
@@ -18,6 +18,12 @@ export interface RelayAuthResult {
|
|
|
18
18
|
* ignores TOR_PROXY, and closes that connection once authenticated, so the
|
|
19
19
|
* AUTH applies to nothing else. It only shows that the relay accepts the key.
|
|
20
20
|
*
|
|
21
|
+
* Waits: connecting and the challenge each get `timeoutMs` (default 5 s).
|
|
22
|
+
* The signature gets the same, except that a remote signer (NIP-46) gets the
|
|
23
|
+
* bunker client's own request limit, since it signs over a relay and may wait
|
|
24
|
+
* on a person to approve. An explicit `timeoutMs` applies to all three. The
|
|
25
|
+
* relay's OK keeps nostr-tools' own 4.4 s. The error says which step timed out.
|
|
26
|
+
*
|
|
21
27
|
* Flow:
|
|
22
28
|
* 1. Connect via WebSocket
|
|
23
29
|
* 2. Wait for ["AUTH", challenge] message
|
|
@@ -25,4 +31,6 @@ export interface RelayAuthResult {
|
|
|
25
31
|
* 4. Send ["AUTH", signedEvent]
|
|
26
32
|
* 5. Wait for ["OK", eventId, true, ...] confirmation
|
|
27
33
|
*/
|
|
28
|
-
export declare function handleRelayAuth(ctx: SigningContext, relay: string, pool?: RelayPool
|
|
34
|
+
export declare function handleRelayAuth(ctx: SigningContext, relay: string, pool?: RelayPool, opts?: {
|
|
35
|
+
timeoutMs?: number;
|
|
36
|
+
}): Promise<RelayAuthResult>;
|
package/dist/relay/auth.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { validateRelayUrl } from './handlers.js';
|
|
2
2
|
import { errorMessage } from '../error-message.js';
|
|
3
|
+
import { REQUEST_TIMEOUT_MS } from '../bunker-context.js';
|
|
3
4
|
/**
|
|
4
5
|
* Authenticate to a relay that requires NIP-42 AUTH.
|
|
5
6
|
*
|
|
@@ -12,6 +13,12 @@ import { errorMessage } from '../error-message.js';
|
|
|
12
13
|
* ignores TOR_PROXY, and closes that connection once authenticated, so the
|
|
13
14
|
* AUTH applies to nothing else. It only shows that the relay accepts the key.
|
|
14
15
|
*
|
|
16
|
+
* Waits: connecting and the challenge each get `timeoutMs` (default 5 s).
|
|
17
|
+
* The signature gets the same, except that a remote signer (NIP-46) gets the
|
|
18
|
+
* bunker client's own request limit, since it signs over a relay and may wait
|
|
19
|
+
* on a person to approve. An explicit `timeoutMs` applies to all three. The
|
|
20
|
+
* relay's OK keeps nostr-tools' own 4.4 s. The error says which step timed out.
|
|
21
|
+
*
|
|
15
22
|
* Flow:
|
|
16
23
|
* 1. Connect via WebSocket
|
|
17
24
|
* 2. Wait for ["AUTH", challenge] message
|
|
@@ -19,10 +26,11 @@ import { errorMessage } from '../error-message.js';
|
|
|
19
26
|
* 4. Send ["AUTH", signedEvent]
|
|
20
27
|
* 5. Wait for ["OK", eventId, true, ...] confirmation
|
|
21
28
|
*/
|
|
22
|
-
export async function handleRelayAuth(ctx, relay, pool) {
|
|
29
|
+
export async function handleRelayAuth(ctx, relay, pool, opts = {}) {
|
|
23
30
|
if (pool) {
|
|
24
31
|
validateRelayUrl(relay, { allowPrivate: pool.allowsPrivateRelays?.() === true });
|
|
25
|
-
|
|
32
|
+
const signTimeoutMs = opts.timeoutMs === undefined && ctx.remoteSigner === true ? REQUEST_TIMEOUT_MS : undefined;
|
|
33
|
+
return pool.authenticate(relay, { timeoutMs: opts.timeoutMs, signTimeoutMs });
|
|
26
34
|
}
|
|
27
35
|
validateRelayUrl(relay); // throws on private IPs -- intentionally not caught
|
|
28
36
|
return new Promise((resolve) => {
|
package/dist/relay/tools.js
CHANGED
|
@@ -207,14 +207,15 @@ export function registerRelayTools(server, deps) {
|
|
|
207
207
|
};
|
|
208
208
|
});
|
|
209
209
|
server.registerTool('relay-auth', {
|
|
210
|
-
description: 'Authenticate (NIP-42) bray\'s own connection to a relay as the active identity, so later queries and publishes to that relay on it are served. Waits for the relay\'s AUTH challenge, signs a kind 22242 event and sends it back. Returns { authenticated: true/false }. Use this when a relay-query fails due to AUTH requirements, then retry the query. Switching identity drops a connection that authenticated as another identity, so authenticate again after a switch.',
|
|
210
|
+
description: 'Authenticate (NIP-42) bray\'s own connection to a relay as the active identity, so later queries and publishes to that relay on it are served. Waits for the relay\'s AUTH challenge, signs a kind 22242 event and sends it back. Returns { authenticated: true/false }. Use this when a relay-query fails due to AUTH requirements, then retry the query. Switching identity drops a connection that authenticated as another identity, so authenticate again after a switch. Connecting, the challenge and the signature each wait 5 seconds, except that a remote signer (NIP-46 bunker or hardware signer) gets up to 60 seconds to sign; the relay then has 4.4 seconds to accept the signed AUTH. A timeout names the step. timeout_ms sets the wait for connecting, the challenge and the signature.',
|
|
211
211
|
inputSchema: {
|
|
212
212
|
relay: relayUrl.describe('Relay WebSocket URL to authenticate with'),
|
|
213
|
+
timeout_ms: z.number().int().min(1_000).max(120_000).optional().describe('Wait in milliseconds for connecting, the challenge and the signature (1000 to 120000). Default 5000, and 60000 for the signature when the identity signs through a remote signer.'),
|
|
213
214
|
},
|
|
214
215
|
annotations: { readOnlyHint: false },
|
|
215
|
-
}, async ({ relay }) => {
|
|
216
|
+
}, async ({ relay, timeout_ms }) => {
|
|
216
217
|
// On the pool's own connection, so the retried query is authenticated.
|
|
217
|
-
const result = await handleRelayAuth(deps.ctx, relay, deps.pool);
|
|
218
|
+
const result = await handleRelayAuth(deps.ctx, relay, deps.pool, { timeoutMs: timeout_ms });
|
|
218
219
|
return {
|
|
219
220
|
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
|
220
221
|
};
|
package/dist/relay-pool.d.ts
CHANGED
|
@@ -223,9 +223,17 @@ export declare class RelayPool {
|
|
|
223
223
|
* that carried another identity's traffic is replaced first; after a refusal
|
|
224
224
|
* the connection is dropped, since nostr-tools keeps its first attempt and a
|
|
225
225
|
* second try on it would only repeat the refusal.
|
|
226
|
+
*
|
|
227
|
+
* Each step has its own wait, and a timeout names the step. `timeoutMs`
|
|
228
|
+
* (default 5 s) bounds the connection and the challenge. `signTimeoutMs`
|
|
229
|
+
* bounds the signature and defaults to `timeoutMs`: a signer that answers
|
|
230
|
+
* over a relay (NIP-46) needs far longer than a local key, so the caller
|
|
231
|
+
* gives it the remote signer's own limit. The relay's OK is bounded by
|
|
232
|
+
* nostr-tools itself (the connection's publish timeout, 4.4 s by default).
|
|
226
233
|
*/
|
|
227
234
|
authenticate(url: string, opts?: {
|
|
228
235
|
timeoutMs?: number;
|
|
236
|
+
signTimeoutMs?: number;
|
|
229
237
|
}): Promise<{
|
|
230
238
|
authenticated: boolean;
|
|
231
239
|
relay: string;
|
package/dist/relay-pool.js
CHANGED
|
@@ -88,6 +88,22 @@ async function createRealPool(torProxy) {
|
|
|
88
88
|
destroy: () => pool.destroy(),
|
|
89
89
|
};
|
|
90
90
|
}
|
|
91
|
+
/** An AUTH step that did not finish in time. */
|
|
92
|
+
class AuthTimeout extends Error {
|
|
93
|
+
constructor(step, ms) {
|
|
94
|
+
super(step === 'signature'
|
|
95
|
+
? `AUTH timed out: no signature within ${ms} ms (the signer did not answer; if it is a remote signer, approve the request there)`
|
|
96
|
+
: `AUTH timed out: the relay did not answer the signed AUTH within ${ms} ms`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** The signer refused or failed. nostr-tools drops that error, so it is carried out separately. */
|
|
100
|
+
class AuthNotSigned extends Error {
|
|
101
|
+
constructor(reason) {
|
|
102
|
+
super(`AUTH not signed: ${errorMessage(reason)}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** nostr-tools' own wait for the relay's OK to a signed AUTH (its `publishTimeout`). */
|
|
106
|
+
const NOSTR_TOOLS_OK_WAIT_MS = 4_400;
|
|
91
107
|
export class RelayPool {
|
|
92
108
|
pool;
|
|
93
109
|
poolReady;
|
|
@@ -523,6 +539,13 @@ export class RelayPool {
|
|
|
523
539
|
* that carried another identity's traffic is replaced first; after a refusal
|
|
524
540
|
* the connection is dropped, since nostr-tools keeps its first attempt and a
|
|
525
541
|
* second try on it would only repeat the refusal.
|
|
542
|
+
*
|
|
543
|
+
* Each step has its own wait, and a timeout names the step. `timeoutMs`
|
|
544
|
+
* (default 5 s) bounds the connection and the challenge. `signTimeoutMs`
|
|
545
|
+
* bounds the signature and defaults to `timeoutMs`: a signer that answers
|
|
546
|
+
* over a relay (NIP-46) needs far longer than a local key, so the caller
|
|
547
|
+
* gives it the remote signer's own limit. The relay's OK is bounded by
|
|
548
|
+
* nostr-tools itself (the connection's publish timeout, 4.4 s by default).
|
|
526
549
|
*/
|
|
527
550
|
async authenticate(url, opts = {}) {
|
|
528
551
|
this.#validateDirectRelays([url]);
|
|
@@ -532,6 +555,7 @@ export class RelayPool {
|
|
|
532
555
|
if (!pool.ensureRelay)
|
|
533
556
|
return { authenticated: false, relay: url, error: 'the underlying relay pool cannot authenticate' };
|
|
534
557
|
const timeoutMs = opts.timeoutMs ?? 5_000;
|
|
558
|
+
const signTimeoutMs = opts.signTimeoutMs ?? timeoutMs;
|
|
535
559
|
const { normalizeURL } = await import('nostr-tools/utils');
|
|
536
560
|
const current = this.#activePubkey();
|
|
537
561
|
const owner = this.#connectionOwners.get(normalizeURL(url));
|
|
@@ -551,21 +575,59 @@ export class RelayPool {
|
|
|
551
575
|
let probed = false;
|
|
552
576
|
while (!challenge()) {
|
|
553
577
|
if (Date.now() >= deadline)
|
|
554
|
-
return { authenticated: false, relay: url, error:
|
|
578
|
+
return { authenticated: false, relay: url, error: `AUTH handshake timed out (no challenge received within ${timeoutMs} ms)` };
|
|
555
579
|
if (!probed && Date.now() >= probeAt) {
|
|
556
580
|
probed = true;
|
|
557
581
|
const probe = relay.subscribe([{ limit: 0 }], { onevent: () => { }, oneose: () => probe.close(), onclose: () => { } });
|
|
558
582
|
}
|
|
559
583
|
await new Promise(resolve => setTimeout(resolve, 25));
|
|
560
584
|
}
|
|
585
|
+
// relay.auth signs, sends and waits for the relay's OK. nostr-tools 2.24
|
|
586
|
+
// bounds the OK itself, from the moment the signature is in, but it catches
|
|
587
|
+
// an error from the signer, logs it and never settles. So bray bounds the
|
|
588
|
+
// signature, carries a refusal out of the signing wrapper, and labels the
|
|
589
|
+
// outcome by what that wrapper saw.
|
|
590
|
+
let timer;
|
|
591
|
+
let signed = false;
|
|
592
|
+
let settled = false;
|
|
593
|
+
let refuse = () => { };
|
|
594
|
+
const stopped = new Promise((_, reject) => {
|
|
595
|
+
refuse = reject;
|
|
596
|
+
timer = setTimeout(() => reject(new AuthTimeout('signature', signTimeoutMs)), signTimeoutMs);
|
|
597
|
+
});
|
|
598
|
+
const signForRelay = async (evt) => {
|
|
599
|
+
try {
|
|
600
|
+
const event = await this.#signAuth(evt);
|
|
601
|
+
if (!settled) {
|
|
602
|
+
signed = true;
|
|
603
|
+
clearTimeout(timer);
|
|
604
|
+
}
|
|
605
|
+
return event;
|
|
606
|
+
}
|
|
607
|
+
catch (err) {
|
|
608
|
+
if (!settled)
|
|
609
|
+
refuse(new AuthNotSigned(err));
|
|
610
|
+
throw err;
|
|
611
|
+
}
|
|
612
|
+
};
|
|
561
613
|
try {
|
|
562
|
-
await
|
|
614
|
+
await Promise.race([relay.auth(signForRelay), stopped]);
|
|
563
615
|
return { authenticated: true, relay: url, pubkey: this.#connectionOwners.get(relay.url)?.authedBy ?? current };
|
|
564
616
|
}
|
|
565
617
|
catch (err) {
|
|
566
618
|
pool.forget?.(url);
|
|
619
|
+
if (err instanceof AuthTimeout || err instanceof AuthNotSigned)
|
|
620
|
+
return { authenticated: false, relay: url, error: err.message };
|
|
621
|
+
if (signed && errorMessage(err) === 'auth timed out') {
|
|
622
|
+
const okWait = relay.publishTimeout ?? NOSTR_TOOLS_OK_WAIT_MS;
|
|
623
|
+
return { authenticated: false, relay: url, error: new AuthTimeout('relay OK', okWait).message };
|
|
624
|
+
}
|
|
567
625
|
return { authenticated: false, relay: url, error: `AUTH rejected: ${errorMessage(err)}` };
|
|
568
626
|
}
|
|
627
|
+
finally {
|
|
628
|
+
settled = true;
|
|
629
|
+
clearTimeout(timer);
|
|
630
|
+
}
|
|
569
631
|
}
|
|
570
632
|
/** Reconcile local event IDs with one relay using NIP-77 Negentropy. */
|
|
571
633
|
async reconcileDirect(relayUrl, items, filter, opts = {}) {
|
|
@@ -4,6 +4,12 @@ import type { PublicIdentity, SignFn } from './types.js';
|
|
|
4
4
|
export interface SigningContext {
|
|
5
5
|
readonly activeNpub: string;
|
|
6
6
|
readonly activePublicKeyHex: string;
|
|
7
|
+
/**
|
|
8
|
+
* True when signatures come from another device or process over a relay
|
|
9
|
+
* (NIP-46), so a signature takes seconds, not microseconds, and may wait on
|
|
10
|
+
* a person. Unset for a local key.
|
|
11
|
+
*/
|
|
12
|
+
readonly remoteSigner?: boolean;
|
|
7
13
|
getSigningFunction(): SignFn;
|
|
8
14
|
listIdentities(): Promise<PublicIdentity[]>;
|
|
9
15
|
nip44Encrypt(recipientPubkey: string, plaintext: string): Promise<string>;
|
package/llms-full.txt
CHANGED
|
@@ -594,8 +594,8 @@ Count events matching a filter without fetching (NIP-45). Falls back to fetch-an
|
|
|
594
594
|
- Input: relays, kinds (optional), authors (optional), tags (optional), since (optional), until (optional)
|
|
595
595
|
|
|
596
596
|
#### relay-auth
|
|
597
|
-
Authenticate to a relay requiring NIP-42 AUTH. Signs a kind 22242 event.
|
|
598
|
-
- Input: relay (URL)
|
|
597
|
+
Authenticate to a relay requiring NIP-42 AUTH. Signs a kind 22242 event. Connecting, the challenge and the signature wait 5 s each; a remote signer gets 60 s to sign, and a timeout names the step.
|
|
598
|
+
- Input: relay (URL), timeout_ms (optional, 1000-120000, wait for connecting, the challenge and the signature)
|
|
599
599
|
|
|
600
600
|
#### relay-discover
|
|
601
601
|
Discover relays used by your contacts. Fetches NIP-65 relay lists from your follow list, ranks by popularity.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nostr-bray",
|
|
3
|
-
"version": "3.14.
|
|
3
|
+
"version": "3.14.3",
|
|
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",
|