mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 17:18:51 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cbe3a9d683 | ||
|
|
2944a932b8 | ||
|
|
6b39de04d7 |
@@ -8,7 +8,7 @@ import {
|
|||||||
} from './abstract-relay.ts'
|
} from './abstract-relay.ts'
|
||||||
import { normalizeURL } from './utils.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 { type Filter } from './filter.ts'
|
||||||
import { alwaysTrue } from './helpers.ts'
|
import { alwaysTrue } from './helpers.ts'
|
||||||
|
|
||||||
@@ -19,6 +19,7 @@ export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
|
|||||||
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
||||||
maxWait?: number
|
maxWait?: number
|
||||||
onclose?: (reasons: string[]) => void
|
onclose?: (reasons: string[]) => void
|
||||||
|
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||||
id?: string
|
id?: string
|
||||||
label?: string
|
label?: string
|
||||||
}
|
}
|
||||||
@@ -137,7 +138,28 @@ export class AbstractSimplePool {
|
|||||||
let subscription = relay.subscribe(filters, {
|
let subscription = relay.subscribe(filters, {
|
||||||
...params,
|
...params,
|
||||||
oneose: () => handleEose(i),
|
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,
|
alreadyHaveEvent: localAlreadyHaveEventHandler,
|
||||||
eoseTimeout: params.maxWait,
|
eoseTimeout: params.maxWait,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ export class AbstractRelay {
|
|||||||
private incomingMessageQueue = new Queue<string>()
|
private incomingMessageQueue = new Queue<string>()
|
||||||
private queueRunning = false
|
private queueRunning = false
|
||||||
private challenge: string | undefined
|
private challenge: string | undefined
|
||||||
|
private authPromise: Promise<string> | undefined
|
||||||
private serial: number = 0
|
private serial: number = 0
|
||||||
private verifyEvent: Nostr['verifyEvent']
|
private verifyEvent: Nostr['verifyEvent']
|
||||||
|
|
||||||
@@ -77,6 +78,7 @@ export class AbstractRelay {
|
|||||||
if (this.connectionPromise) return this.connectionPromise
|
if (this.connectionPromise) return this.connectionPromise
|
||||||
|
|
||||||
this.challenge = undefined
|
this.challenge = undefined
|
||||||
|
this.authPromise = undefined
|
||||||
this.connectionPromise = new Promise((resolve, reject) => {
|
this.connectionPromise = new Promise((resolve, reject) => {
|
||||||
this.connectionTimeoutHandle = setTimeout(() => {
|
this.connectionTimeoutHandle = setTimeout(() => {
|
||||||
reject('connection timed out')
|
reject('connection timed out')
|
||||||
@@ -220,6 +222,7 @@ export class AbstractRelay {
|
|||||||
return
|
return
|
||||||
case 'AUTH': {
|
case 'AUTH': {
|
||||||
this.challenge = data[1] as string
|
this.challenge = data[1] as string
|
||||||
|
this.authPromise = undefined
|
||||||
this._onauth?.(data[1] as string)
|
this._onauth?.(data[1] as string)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -239,8 +242,9 @@ export class AbstractRelay {
|
|||||||
|
|
||||||
public async auth(signAuthEvent: (evt: EventTemplate) => Promise<VerifiedEvent>): Promise<string> {
|
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.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 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 timeout = setTimeout(() => {
|
||||||
const ep = this.openEventPublishes.get(evt.id) as EventPublishResolver
|
const ep = this.openEventPublishes.get(evt.id) as EventPublishResolver
|
||||||
if (ep) {
|
if (ep) {
|
||||||
@@ -251,7 +255,7 @@ export class AbstractRelay {
|
|||||||
this.openEventPublishes.set(evt.id, { resolve, reject, timeout })
|
this.openEventPublishes.set(evt.id, { resolve, reject, timeout })
|
||||||
})
|
})
|
||||||
this.send('["AUTH",' + JSON.stringify(evt) + ']')
|
this.send('["AUTH",' + JSON.stringify(evt) + ']')
|
||||||
return ret
|
return this.authPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
public async publish(event: Event): Promise<string> {
|
public async publish(event: Event): Promise<string> {
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.11.0",
|
"version": "2.11.1",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
3
nip46.ts
3
nip46.ts
@@ -150,6 +150,9 @@ export class BunkerSigner {
|
|||||||
delete listeners[id]
|
delete listeners[id]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
onclose: () => {
|
||||||
|
this.isOpen = false
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
this.isOpen = true
|
this.isOpen = true
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.11.0",
|
"version": "2.11.1",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
Reference in New Issue
Block a user