From cbe3a9d683300c3cacfb9f237f96f8553d42492c Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 1 Apr 2025 19:16:12 -0300 Subject: [PATCH] pool subscribe methods accept an `onauth` param. --- abstract-pool.ts | 26 ++++++++++++++++++++++++-- jsr.json | 2 +- package.json | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/abstract-pool.ts b/abstract-pool.ts index 48a66a4..a5b6976 100644 --- a/abstract-pool.ts +++ b/abstract-pool.ts @@ -8,7 +8,7 @@ import { } from './abstract-relay.ts' import { normalizeURL } from './utils.ts' -import type { Event, Nostr } from './core.ts' +import type { Event, EventTemplate, Nostr, VerifiedEvent } from './core.ts' import { type Filter } from './filter.ts' import { alwaysTrue } from './helpers.ts' @@ -19,6 +19,7 @@ export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & { export type SubscribeManyParams = Omit & { maxWait?: number onclose?: (reasons: string[]) => void + doauth?: (event: EventTemplate) => Promise id?: string label?: string } @@ -137,7 +138,28 @@ export class AbstractSimplePool { let subscription = relay.subscribe(filters, { ...params, oneose: () => handleEose(i), - onclose: reason => handleClose(i, reason), + onclose: reason => { + if (reason.startsWith('auth-required:') && params.doauth) { + relay + .auth(params.doauth) + .then(() => { + relay.subscribe(filters, { + ...params, + oneose: () => handleEose(i), + onclose: reason => { + handleClose(i, reason) // the second time we won't try to auth anymore + }, + alreadyHaveEvent: localAlreadyHaveEventHandler, + eoseTimeout: params.maxWait, + }) + }) + .catch(err => { + handleClose(i, `auth was required and attempted, but failed with: ${err}`) + }) + } else { + handleClose(i, reason) + } + }, alreadyHaveEvent: localAlreadyHaveEventHandler, eoseTimeout: params.maxWait, }) diff --git a/jsr.json b/jsr.json index f79baa0..46ef08a 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@nostr/tools", - "version": "2.11.0", + "version": "2.11.1", "exports": { ".": "./index.ts", "./core": "./core.ts", diff --git a/package.json b/package.json index 11db211..e6f8e25 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "nostr-tools", - "version": "2.11.0", + "version": "2.11.1", "description": "Tools for making a Nostr client.", "repository": { "type": "git",