Compare commits

...

3 Commits

Author SHA1 Message Date
fiatjaf
cbe3a9d683 pool subscribe methods accept an onauth param. 2025-04-01 19:16:42 -03:00
fiatjaf
2944a932b8 nip46: mark connection as closed when relays disconnect. 2025-03-29 18:03:39 -03:00
codytseng
6b39de04d7 Fix auth() not returning on consecutive calls 2025-03-17 13:31:24 -03:00
5 changed files with 35 additions and 6 deletions

View File

@@ -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<SubscriptionParams, 'onclose'> & {
maxWait?: number
onclose?: (reasons: string[]) => void
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
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,
})

View File

@@ -35,6 +35,7 @@ export class AbstractRelay {
private incomingMessageQueue = new Queue<string>()
private queueRunning = false
private challenge: string | undefined
private authPromise: Promise<string> | undefined
private serial: number = 0
private verifyEvent: Nostr['verifyEvent']
@@ -77,6 +78,7 @@ export class AbstractRelay {
if (this.connectionPromise) return this.connectionPromise
this.challenge = undefined
this.authPromise = undefined
this.connectionPromise = new Promise((resolve, reject) => {
this.connectionTimeoutHandle = setTimeout(() => {
reject('connection timed out')
@@ -220,6 +222,7 @@ export class AbstractRelay {
return
case 'AUTH': {
this.challenge = data[1] as string
this.authPromise = undefined
this._onauth?.(data[1] as string)
return
}
@@ -239,8 +242,9 @@ export class AbstractRelay {
public async auth(signAuthEvent: (evt: EventTemplate) => Promise<VerifiedEvent>): Promise<string> {
if (!this.challenge) throw new Error("can't perform auth, no challenge was received")
if (this.authPromise) return this.authPromise
const evt = await signAuthEvent(makeAuthEvent(this.url, this.challenge))
const ret = new Promise<string>((resolve, reject) => {
this.authPromise = new Promise<string>((resolve, reject) => {
const timeout = setTimeout(() => {
const ep = this.openEventPublishes.get(evt.id) as EventPublishResolver
if (ep) {
@@ -251,7 +255,7 @@ export class AbstractRelay {
this.openEventPublishes.set(evt.id, { resolve, reject, timeout })
})
this.send('["AUTH",' + JSON.stringify(evt) + ']')
return ret
return this.authPromise
}
public async publish(event: Event): Promise<string> {

View File

@@ -1,6 +1,6 @@
{
"name": "@nostr/tools",
"version": "2.11.0",
"version": "2.11.1",
"exports": {
".": "./index.ts",
"./core": "./core.ts",

View File

@@ -150,6 +150,9 @@ export class BunkerSigner {
delete listeners[id]
}
},
onclose: () => {
this.isOpen = false
},
},
)
this.isOpen = true

View File

@@ -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",