aboutsummaryrefslogtreecommitdiffstats
path: root/src/nostr.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-10-10 07:03:30 -0700
committerAdam Soltys <[email protected]>2026-10-10 07:03:30 -0700
commit572361e2bd1ab16b4618517c2d9531689c6f46a8 (patch)
tree5ca9210aa9b7414a85027b3afb223be1e2cfee9f /src/nostr.js
parenta70f34f2a5d35352f99bae8f10cbab12637e08ee (diff)
downloadcoinosv3-572361e2bd1ab16b4618517c2d9531689c6f46a8.tar.gz
coinosv3-572361e2bd1ab16b4618517c2d9531689c6f46a8.zip
Messages: NIP-42 inbox relays. isolabellart's kind-10050 inbox is auth.nostr1.com and a personal relay, both of which answer a REQ for their gift wraps with CLOSED "auth-required" — we never authenticated, so DMs delivered only there (anything sent from Amethyst and other 10050-only clients) never reached v3. The DM subscription on our own inbox relays now authenticates as the DM identity (subscribeAuthed: per relay, AUTH then ask again, still closable; the AUTH event is signed before nostr-tools' cached auth promise so a failing signer can't wedge the socket), sends to a peer's inbox authenticate when a relay refuses the write with auth-required, and our sent copy also goes to our own inbox relays so other clients reading that list see it. Never Amber by intent (an app trip per connection). Checked live: auth.nostr1.com accepts the handshake. tools/dm-nip42-test.js
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Vps2H27rhbG9ZqseGfMrc3
Diffstat (limited to 'src/nostr.js')
-rw-r--r--src/nostr.js66
1 files changed, 64 insertions, 2 deletions
diff --git a/src/nostr.js b/src/nostr.js
index 2ab0b05..e414d57 100644
--- a/src/nostr.js
+++ b/src/nostr.js
@@ -301,6 +301,58 @@ export function subscribeOn(relays, filter, onEvent, extra = {}) {
return () => { try { sub.close(); } catch {} };
} catch { return () => {}; }
}
+// NIP-42: a subscription on relays that may answer CLOSED "auth-required"
+// — a NIP-17 inbox such as auth.nostr1.com shows a user's gift wraps only
+// to that user. `onauth(template)` signs the AUTH event; the relay is then
+// asked again. Per relay rather than the pool's own retry, whose re-asked
+// subscription escapes the closer (a resubscribe would stack copies).
+// `onclose` hears only real closes, not the auth handshake.
+export function subscribeAuthed(relays, filter, onEvent, { onauth = null, onclose = null } = {}) {
+ let closed = false;
+ const subs = [];
+ const gated = (event) => { verifyOneAsync(event).then((ok) => { if (ok) onEvent(event); }); };
+ probeRelays(relays);
+ for (const url of liveRelays(relays)) {
+ (async () => {
+ let relay;
+ try { relay = await pool.ensureRelay(url, { connectionTimeout: 8000 }); } catch (e) { if (!closed) onclose?.([String(e?.message || e)]); return; }
+ let tried = false;
+ const open = () => {
+ if (closed) return;
+ subs.push(relay.subscribe([filter], {
+ onevent: gated,
+ onclose: (reason) => {
+ if (closed) return;
+ if (!tried && onauth && /^auth-required:/.test(String(reason))) {
+ tried = true;
+ // a refused or unsignable auth is not a dropped socket:
+ // rebuilding would only ask again
+ authRelay(relay, onauth).then(open, (e) => dlog('nostr: auth to ' + url + ' failed: ' + (e?.message || e)));
+ return;
+ }
+ onclose?.([reason]);
+ },
+ }));
+ };
+ open();
+ })();
+ }
+ return () => { closed = true; for (const s of subs) { try { s.close(); } catch {} } };
+}
+// The relay's challenge can trail its CLOSED: wait a beat for it.
+// Signed BEFORE handing it to the relay: nostr-tools caches its auth promise
+// per connection, and a signer that throws inside it leaves that promise
+// pending forever — every later auth on the socket would hang.
+async function authRelay(relay, onauth) {
+ for (let i = 0; i < 10 && !relay.challenge; i++) await new Promise((r) => setTimeout(r, 300));
+ if (!relay.challenge) throw new Error('no auth challenge');
+ if (!relay.authPromise) {
+ const signed = await onauth({ kind: 22242, created_at: Math.floor(Date.now() / 1000),
+ tags: [['relay', relay.url], ['challenge', relay.challenge]], content: '' });
+ return relay.auth(async () => signed);
+ }
+ return relay.auth(onauth);
+}
export async function queryOn(relays, filter, maxWait = 1500) {
probeRelays(relays);
try {
@@ -352,14 +404,24 @@ export async function queryStreamOn(relays, filter, onEvent, maxWait = 1500) {
});
} catch {} finally { await Promise.all(pending); }
}
-export async function publishOn(relays, evt) {
+export async function publishOn(relays, evt, { onauth = null } = {}) {
try {
const live = liveRelays(relays);
// A rejected publish is a relay refusing the event (policy, rate limit,
// ban). Swallowing that silently once hid a relay-side rejection behind
// what looked like a client bug — always say which relay refused and why.
+ // with onauth, a relay that answers "auth-required" (NIP-42) is
+ // authenticated and asked again
const pubs = pool.publish(live, evt).map((p, i) =>
- Promise.resolve(p).then(() => true, (reason) => {
+ Promise.resolve(p).then(() => true, async (reason) => {
+ if (onauth && /^auth-required:/.test(reason?.message || String(reason || ''))) {
+ try {
+ const r = await pool.ensureRelay(live[i]);
+ await authRelay(r, onauth);
+ await r.publish(evt);
+ return true;
+ } catch (e) { reason = e; }
+ }
const msg = reason?.message || String(reason || '');
// "relay connection errored/closed" means the socket never came up —
// that's a sick relay, not a rejected event.