mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b076c34a2f | ||
|
|
4bb3eb2d40 | ||
|
|
87f2c74bb3 | ||
|
|
4b6cc19b9c | ||
|
|
b2f3a01439 | ||
|
|
6ec19b618c | ||
|
|
b3cc9f50e5 | ||
|
|
de1cf0ed60 | ||
|
|
d706ef961f | ||
|
|
2f529b3f8a | ||
|
|
f0357805c3 | ||
|
|
ffa7fb926e | ||
|
|
12acb900ab | ||
|
|
d773012658 | ||
|
|
b8f91c37fa |
10
README.md
10
README.md
@@ -4,7 +4,7 @@ Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients.
|
|||||||
|
|
||||||
Only depends on _@scure_ and _@noble_ packages.
|
Only depends on _@scure_ and _@noble_ packages.
|
||||||
|
|
||||||
This package is only providing lower-level functionality. If you want more higher-level features, take a look at [Nostrify](https://nostrify.dev), or if you want an easy-to-use fully-fledged solution that abstracts the hard parts of Nostr and makes decisions on your behalf, take a look at [NDK](https://github.com/nostr-dev-kit/ndk) and [@snort/system](https://www.npmjs.com/package/@snort/system).
|
This package is only providing lower-level functionality. If you want higher-level features, take a look at [@nostr/gadgets](https://jsr.io/@nostr/gadgets) which is based on this library and expands upon it and has other goodies (it's only available on jsr).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -133,6 +133,14 @@ import WebSocket from 'ws'
|
|||||||
useWebSocketImplementation(WebSocket)
|
useWebSocketImplementation(WebSocket)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can enable regular pings of connected relays with the `enablePing` option. This will set up a heartbeat that closes the websocket if it doesn't receive a response in time. Some platforms don't report websocket disconnections due to network issues, and enabling this can increase reliability.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { SimplePool } from 'nostr-tools/pool'
|
||||||
|
|
||||||
|
const pool = new SimplePool({ enablePing: true })
|
||||||
|
```
|
||||||
|
|
||||||
### Parsing references (mentions) from a content based on NIP-27
|
### Parsing references (mentions) from a content based on NIP-27
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@@ -12,13 +12,15 @@ 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'
|
||||||
|
|
||||||
export type SubCloser = { close: () => void }
|
export type SubCloser = { close: (reason?: string) => void }
|
||||||
|
|
||||||
export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {}
|
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
|
||||||
|
onauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||||
|
// Deprecated: use onauth instead
|
||||||
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||||
id?: string
|
id?: string
|
||||||
label?: string
|
label?: string
|
||||||
@@ -30,6 +32,7 @@ export class AbstractSimplePool {
|
|||||||
public trackRelays: boolean = false
|
public trackRelays: boolean = false
|
||||||
|
|
||||||
public verifyEvent: Nostr['verifyEvent']
|
public verifyEvent: Nostr['verifyEvent']
|
||||||
|
public enablePing: boolean | undefined
|
||||||
public trustedRelayURLs: Set<string> = new Set()
|
public trustedRelayURLs: Set<string> = new Set()
|
||||||
|
|
||||||
private _WebSocket?: typeof WebSocket
|
private _WebSocket?: typeof WebSocket
|
||||||
@@ -37,6 +40,7 @@ export class AbstractSimplePool {
|
|||||||
constructor(opts: AbstractPoolConstructorOptions) {
|
constructor(opts: AbstractPoolConstructorOptions) {
|
||||||
this.verifyEvent = opts.verifyEvent
|
this.verifyEvent = opts.verifyEvent
|
||||||
this._WebSocket = opts.websocketImplementation
|
this._WebSocket = opts.websocketImplementation
|
||||||
|
this.enablePing = opts.enablePing
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
||||||
@@ -47,7 +51,11 @@ export class AbstractSimplePool {
|
|||||||
relay = new AbstractRelay(url, {
|
relay = new AbstractRelay(url, {
|
||||||
verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
|
verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
|
||||||
websocketImplementation: this._WebSocket,
|
websocketImplementation: this._WebSocket,
|
||||||
|
enablePing: this.enablePing,
|
||||||
})
|
})
|
||||||
|
relay.onclose = () => {
|
||||||
|
this.relays.delete(url)
|
||||||
|
}
|
||||||
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
||||||
this.relays.set(url, relay)
|
this.relays.set(url, relay)
|
||||||
}
|
}
|
||||||
@@ -59,10 +67,13 @@ export class AbstractSimplePool {
|
|||||||
close(relays: string[]) {
|
close(relays: string[]) {
|
||||||
relays.map(normalizeURL).forEach(url => {
|
relays.map(normalizeURL).forEach(url => {
|
||||||
this.relays.get(url)?.close()
|
this.relays.get(url)?.close()
|
||||||
|
this.relays.delete(url)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
return this.subscribeMap(
|
return this.subscribeMap(
|
||||||
relays.map(url => ({ url, filter })),
|
relays.map(url => ({ url, filter })),
|
||||||
params,
|
params,
|
||||||
@@ -70,6 +81,8 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser {
|
subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
return this.subscribeMap(
|
return this.subscribeMap(
|
||||||
relays.flatMap(url => filters.map(filter => ({ url, filter }))),
|
relays.flatMap(url => filters.map(filter => ({ url, filter }))),
|
||||||
params,
|
params,
|
||||||
@@ -77,6 +90,8 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
if (this.trackRelays) {
|
if (this.trackRelays) {
|
||||||
params.receivedEvent = (relay: AbstractRelay, id: string) => {
|
params.receivedEvent = (relay: AbstractRelay, id: string) => {
|
||||||
let set = this.seenOn.get(id)
|
let set = this.seenOn.get(id)
|
||||||
@@ -125,8 +140,6 @@ export class AbstractSimplePool {
|
|||||||
// open a subscription in all given relays
|
// open a subscription in all given relays
|
||||||
const allOpened = Promise.all(
|
const allOpened = Promise.all(
|
||||||
requests.map(async ({ url, filter }, i) => {
|
requests.map(async ({ url, filter }, i) => {
|
||||||
url = normalizeURL(url)
|
|
||||||
|
|
||||||
let relay: AbstractRelay
|
let relay: AbstractRelay
|
||||||
try {
|
try {
|
||||||
relay = await this.ensureRelay(url, {
|
relay = await this.ensureRelay(url, {
|
||||||
@@ -141,9 +154,9 @@ export class AbstractSimplePool {
|
|||||||
...params,
|
...params,
|
||||||
oneose: () => handleEose(i),
|
oneose: () => handleEose(i),
|
||||||
onclose: reason => {
|
onclose: reason => {
|
||||||
if (reason.startsWith('auth-required:') && params.doauth) {
|
if (reason.startsWith('auth-required: ') && params.onauth) {
|
||||||
relay
|
relay
|
||||||
.auth(params.doauth)
|
.auth(params.onauth)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
relay.subscribe([filter], {
|
relay.subscribe([filter], {
|
||||||
...params,
|
...params,
|
||||||
@@ -171,10 +184,10 @@ export class AbstractSimplePool {
|
|||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
async close() {
|
async close(reason?: string) {
|
||||||
await allOpened
|
await allOpened
|
||||||
subs.forEach(sub => {
|
subs.forEach(sub => {
|
||||||
sub.close()
|
sub.close(reason)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -183,12 +196,14 @@ export class AbstractSimplePool {
|
|||||||
subscribeEose(
|
subscribeEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'doauth'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
const subcloser = this.subscribe(relays, filter, {
|
const subcloser = this.subscribe(relays, filter, {
|
||||||
...params,
|
...params,
|
||||||
oneose() {
|
oneose() {
|
||||||
subcloser.close()
|
subcloser.close('closed automatically on eose')
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return subcloser
|
return subcloser
|
||||||
@@ -197,12 +212,14 @@ export class AbstractSimplePool {
|
|||||||
subscribeManyEose(
|
subscribeManyEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filters: Filter[],
|
filters: Filter[],
|
||||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'doauth'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
const subcloser = this.subscribeMany(relays, filters, {
|
const subcloser = this.subscribeMany(relays, filters, {
|
||||||
...params,
|
...params,
|
||||||
oneose() {
|
oneose() {
|
||||||
subcloser.close()
|
subcloser.close('closed automatically on eose')
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return subcloser
|
return subcloser
|
||||||
@@ -238,7 +255,11 @@ export class AbstractSimplePool {
|
|||||||
return events[0] || null
|
return events[0] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
publish(relays: string[], event: Event): Promise<string>[] {
|
publish(
|
||||||
|
relays: string[],
|
||||||
|
event: Event,
|
||||||
|
options?: { onauth?: (evt: EventTemplate) => Promise<VerifiedEvent> },
|
||||||
|
): Promise<string>[] {
|
||||||
return relays.map(normalizeURL).map(async (url, i, arr) => {
|
return relays.map(normalizeURL).map(async (url, i, arr) => {
|
||||||
if (arr.indexOf(url) !== i) {
|
if (arr.indexOf(url) !== i) {
|
||||||
// duplicate
|
// duplicate
|
||||||
@@ -246,17 +267,26 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let r = await this.ensureRelay(url)
|
let r = await this.ensureRelay(url)
|
||||||
return r.publish(event).then(reason => {
|
return r
|
||||||
if (this.trackRelays) {
|
.publish(event)
|
||||||
let set = this.seenOn.get(event.id)
|
.catch(async err => {
|
||||||
if (!set) {
|
if (err instanceof Error && err.message.startsWith('auth-required: ') && options?.onauth) {
|
||||||
set = new Set()
|
await r.auth(options.onauth)
|
||||||
this.seenOn.set(event.id, set)
|
return r.publish(event) // retry
|
||||||
}
|
}
|
||||||
set.add(r)
|
throw err
|
||||||
}
|
})
|
||||||
return reason
|
.then(reason => {
|
||||||
})
|
if (this.trackRelays) {
|
||||||
|
let set = this.seenOn.get(event.id)
|
||||||
|
if (!set) {
|
||||||
|
set = new Set()
|
||||||
|
this.seenOn.set(event.id, set)
|
||||||
|
}
|
||||||
|
set.add(r)
|
||||||
|
}
|
||||||
|
return reason
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,15 @@ import { Queue, normalizeURL } from './utils.ts'
|
|||||||
import { makeAuthEvent } from './nip42.ts'
|
import { makeAuthEvent } from './nip42.ts'
|
||||||
import { yieldThread } from './helpers.ts'
|
import { yieldThread } from './helpers.ts'
|
||||||
|
|
||||||
|
type RelayWebSocket = WebSocket & {
|
||||||
|
ping?(): void
|
||||||
|
on?(event: 'pong', listener: () => void): any
|
||||||
|
}
|
||||||
|
|
||||||
export type AbstractRelayConstructorOptions = {
|
export type AbstractRelayConstructorOptions = {
|
||||||
verifyEvent: Nostr['verifyEvent']
|
verifyEvent: Nostr['verifyEvent']
|
||||||
websocketImplementation?: typeof WebSocket
|
websocketImplementation?: typeof WebSocket
|
||||||
|
enablePing?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SendingOnClosedConnection extends Error {
|
export class SendingOnClosedConnection extends Error {
|
||||||
@@ -26,19 +32,19 @@ export class AbstractRelay {
|
|||||||
public onclose: (() => void) | null = null
|
public onclose: (() => void) | null = null
|
||||||
public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`)
|
public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`)
|
||||||
|
|
||||||
// this is exposed just to help in ndk migration, shouldn't be relied upon
|
|
||||||
public _onauth: ((challenge: string) => void) | null = null
|
|
||||||
|
|
||||||
public baseEoseTimeout: number = 4400
|
public baseEoseTimeout: number = 4400
|
||||||
public connectionTimeout: number = 4400
|
public connectionTimeout: number = 4400
|
||||||
public publishTimeout: number = 4400
|
public publishTimeout: number = 4400
|
||||||
|
public pingFrequency: number = 20000
|
||||||
|
public pingTimeout: number = 20000
|
||||||
public openSubs: Map<string, Subscription> = new Map()
|
public openSubs: Map<string, Subscription> = new Map()
|
||||||
|
public enablePing: boolean | undefined
|
||||||
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||||
|
|
||||||
private connectionPromise: Promise<void> | undefined
|
private connectionPromise: Promise<void> | undefined
|
||||||
private openCountRequests = new Map<string, CountResolver>()
|
private openCountRequests = new Map<string, CountResolver>()
|
||||||
private openEventPublishes = new Map<string, EventPublishResolver>()
|
private openEventPublishes = new Map<string, EventPublishResolver>()
|
||||||
private ws: WebSocket | undefined
|
private ws: RelayWebSocket | undefined
|
||||||
private incomingMessageQueue = new Queue<string>()
|
private incomingMessageQueue = new Queue<string>()
|
||||||
private queueRunning = false
|
private queueRunning = false
|
||||||
private challenge: string | undefined
|
private challenge: string | undefined
|
||||||
@@ -52,6 +58,7 @@ export class AbstractRelay {
|
|||||||
this.url = normalizeURL(url)
|
this.url = normalizeURL(url)
|
||||||
this.verifyEvent = opts.verifyEvent
|
this.verifyEvent = opts.verifyEvent
|
||||||
this._WebSocket = opts.websocketImplementation || WebSocket
|
this._WebSocket = opts.websocketImplementation || WebSocket
|
||||||
|
this.enablePing = opts.enablePing
|
||||||
}
|
}
|
||||||
|
|
||||||
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
|
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
|
||||||
@@ -105,6 +112,9 @@ export class AbstractRelay {
|
|||||||
this.ws.onopen = () => {
|
this.ws.onopen = () => {
|
||||||
clearTimeout(this.connectionTimeoutHandle)
|
clearTimeout(this.connectionTimeoutHandle)
|
||||||
this._connected = true
|
this._connected = true
|
||||||
|
if (this.enablePing) {
|
||||||
|
this.pingpong()
|
||||||
|
}
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,6 +146,53 @@ export class AbstractRelay {
|
|||||||
return this.connectionPromise
|
return this.connectionPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async waitForPingPong() {
|
||||||
|
return new Promise((res, err) => {
|
||||||
|
// listen for pong
|
||||||
|
;(this.ws && this.ws.on && this.ws.on('pong', () => res(true))) || err("ws can't listen for pong")
|
||||||
|
// send a ping
|
||||||
|
this.ws && this.ws.ping && this.ws.ping()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private async waitForDummyReq() {
|
||||||
|
return new Promise((res, err) => {
|
||||||
|
// make a dummy request with expected empty eose reply
|
||||||
|
// ["REQ", "_", {"ids":["aaaa...aaaa"]}]
|
||||||
|
const sub = this.subscribe([{ ids: ['a'.repeat(64)] }], {
|
||||||
|
oneose: () => {
|
||||||
|
sub.close()
|
||||||
|
res(true)
|
||||||
|
},
|
||||||
|
eoseTimeout: this.pingTimeout + 1000,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// nodejs requires this magic here to ensure connections are closed when internet goes off and stuff
|
||||||
|
// in browsers it's done automatically. see https://github.com/nbd-wtf/nostr-tools/issues/491
|
||||||
|
private async pingpong() {
|
||||||
|
// if the websocket is connected
|
||||||
|
if (this.ws?.readyState === 1) {
|
||||||
|
// wait for either a ping-pong reply or a timeout
|
||||||
|
const result = await Promise.any([
|
||||||
|
// browsers don't have ping so use a dummy req
|
||||||
|
this.ws && this.ws.ping && this.ws.on ? this.waitForPingPong() : this.waitForDummyReq(),
|
||||||
|
new Promise(res => setTimeout(() => res(false), this.pingTimeout)),
|
||||||
|
])
|
||||||
|
if (result) {
|
||||||
|
// schedule another pingpong
|
||||||
|
setTimeout(() => this.pingpong(), this.pingFrequency)
|
||||||
|
} else {
|
||||||
|
// pingpong closing socket
|
||||||
|
this.closeAllSubscriptions('pingpong timed out')
|
||||||
|
this._connected = false
|
||||||
|
this.ws?.close()
|
||||||
|
this.onclose?.()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async runQueue() {
|
private async runQueue() {
|
||||||
this.queueRunning = true
|
this.queueRunning = true
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -233,7 +290,6 @@ export class AbstractRelay {
|
|||||||
return
|
return
|
||||||
case 'AUTH': {
|
case 'AUTH': {
|
||||||
this.challenge = data[1] as string
|
this.challenge = data[1] as string
|
||||||
this._onauth?.(data[1] as string)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,16 +312,20 @@ export class AbstractRelay {
|
|||||||
if (this.authPromise) return this.authPromise
|
if (this.authPromise) return this.authPromise
|
||||||
|
|
||||||
this.authPromise = new Promise<string>(async (resolve, reject) => {
|
this.authPromise = new Promise<string>(async (resolve, reject) => {
|
||||||
const evt = await signAuthEvent(makeAuthEvent(this.url, challenge))
|
try {
|
||||||
const timeout = setTimeout(() => {
|
let evt = await signAuthEvent(makeAuthEvent(this.url, challenge))
|
||||||
const ep = this.openEventPublishes.get(evt.id) as EventPublishResolver
|
let timeout = setTimeout(() => {
|
||||||
if (ep) {
|
let ep = this.openEventPublishes.get(evt.id) as EventPublishResolver
|
||||||
ep.reject(new Error('auth timed out'))
|
if (ep) {
|
||||||
this.openEventPublishes.delete(evt.id)
|
ep.reject(new Error('auth timed out'))
|
||||||
}
|
this.openEventPublishes.delete(evt.id)
|
||||||
}, this.publishTimeout)
|
}
|
||||||
this.openEventPublishes.set(evt.id, { resolve, reject, timeout })
|
}, this.publishTimeout)
|
||||||
this.send('["AUTH",' + JSON.stringify(evt) + ']')
|
this.openEventPublishes.set(evt.id, { resolve, reject, timeout })
|
||||||
|
this.send('["AUTH",' + JSON.stringify(evt) + ']')
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('subscribe auth function failed:', err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return this.authPromise
|
return this.authPromise
|
||||||
}
|
}
|
||||||
@@ -319,6 +379,7 @@ export class AbstractRelay {
|
|||||||
this.closeAllSubscriptions('relay connection closed by us')
|
this.closeAllSubscriptions('relay connection closed by us')
|
||||||
this._connected = false
|
this._connected = false
|
||||||
this.ws?.close()
|
this.ws?.close()
|
||||||
|
this.onclose?.()
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is the function assigned to this.ws.onmessage
|
// this is the function assigned to this.ws.onmessage
|
||||||
|
|||||||
6
jsr.json
6
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.14.1",
|
"version": "2.16.0",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
@@ -42,9 +42,9 @@
|
|||||||
"./nip94": "./nip94.ts",
|
"./nip94": "./nip94.ts",
|
||||||
"./nip98": "./nip98.ts",
|
"./nip98": "./nip98.ts",
|
||||||
"./nip99": "./nip99.ts",
|
"./nip99": "./nip99.ts",
|
||||||
"./nip99": "./nipb7.ts",
|
"./nipb7": "./nipb7.ts",
|
||||||
"./fakejson": "./fakejson.ts",
|
"./fakejson": "./fakejson.ts",
|
||||||
"./utils": "./utils.ts"
|
"./utils": "./utils.ts",
|
||||||
"./signer": "./signer.ts"
|
"./signer": "./signer.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { describe, expect, test } from 'bun:test'
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
// prettier-ignore
|
||||||
import {
|
import {
|
||||||
decode,
|
decode,
|
||||||
naddrEncode,
|
naddrEncode,
|
||||||
|
|||||||
2
nip29.ts
2
nip29.ts
@@ -2,7 +2,7 @@ import { AbstractSimplePool } from './abstract-pool.ts'
|
|||||||
import { Subscription } from './abstract-relay.ts'
|
import { Subscription } from './abstract-relay.ts'
|
||||||
import type { Event, EventTemplate } from './core.ts'
|
import type { Event, EventTemplate } from './core.ts'
|
||||||
import { fetchRelayInformation, RelayInformation } from './nip11.ts'
|
import { fetchRelayInformation, RelayInformation } from './nip11.ts'
|
||||||
import { AddressPointer, decode, NostrTypeGuard } from './nip19.ts'
|
import { decode, NostrTypeGuard } from './nip19.ts'
|
||||||
import { normalizeURL } from './utils.ts'
|
import { normalizeURL } from './utils.ts'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
8
nip46.ts
8
nip46.ts
@@ -5,7 +5,6 @@ import { getConversationKey, decrypt, encrypt } from './nip44.ts'
|
|||||||
import { NIP05_REGEX } from './nip05.ts'
|
import { NIP05_REGEX } from './nip05.ts'
|
||||||
import { SimplePool } from './pool.ts'
|
import { SimplePool } from './pool.ts'
|
||||||
import { Handlerinformation, NostrConnect } from './kinds.ts'
|
import { Handlerinformation, NostrConnect } from './kinds.ts'
|
||||||
import type { RelayRecord } from './relay.ts'
|
|
||||||
import { Signer } from './signer.ts'
|
import { Signer } from './signer.ts'
|
||||||
|
|
||||||
var _fetch: any
|
var _fetch: any
|
||||||
@@ -238,13 +237,6 @@ export class BunkerSigner implements Signer {
|
|||||||
return this.cachedPubKey
|
return this.cachedPubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated removed from NIP
|
|
||||||
*/
|
|
||||||
async getRelays(): Promise<RelayRecord> {
|
|
||||||
return JSON.parse(await this.sendRequest('get_relays', []))
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Signs an event using the remote private key.
|
* Signs an event using the remote private key.
|
||||||
* @param event - The event to sign.
|
* @param event - The event to sign.
|
||||||
|
|||||||
@@ -5,6 +5,16 @@ import { decrypt } from './nip04.ts'
|
|||||||
import { NWCWalletRequest } from './kinds.ts'
|
import { NWCWalletRequest } from './kinds.ts'
|
||||||
|
|
||||||
describe('parseConnectionString', () => {
|
describe('parseConnectionString', () => {
|
||||||
|
test('returns pubkey, relay, and secret if connection string has double slash', () => {
|
||||||
|
const connectionString =
|
||||||
|
'nostr+walletconnect://b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
|
||||||
|
const { pubkey, relay, secret } = parseConnectionString(connectionString)
|
||||||
|
|
||||||
|
expect(pubkey).toBe('b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4')
|
||||||
|
expect(relay).toBe('wss://relay.damus.io')
|
||||||
|
expect(secret).toBe('71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c')
|
||||||
|
})
|
||||||
|
|
||||||
test('returns pubkey, relay, and secret if connection string is valid', () => {
|
test('returns pubkey, relay, and secret if connection string is valid', () => {
|
||||||
const connectionString =
|
const connectionString =
|
||||||
'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
|
'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
|
||||||
|
|||||||
4
nip47.ts
4
nip47.ts
@@ -9,8 +9,8 @@ interface NWCConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function parseConnectionString(connectionString: string): NWCConnection {
|
export function parseConnectionString(connectionString: string): NWCConnection {
|
||||||
const { pathname, searchParams } = new URL(connectionString)
|
const { host, pathname, searchParams } = new URL(connectionString)
|
||||||
const pubkey = pathname
|
const pubkey = pathname || host
|
||||||
const relay = searchParams.get('relay')
|
const relay = searchParams.get('relay')
|
||||||
const secret = searchParams.get('secret')
|
const secret = searchParams.get('secret')
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.14.1",
|
"version": "2.16.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
4
pool.ts
4
pool.ts
@@ -14,8 +14,8 @@ export function useWebSocketImplementation(websocketImplementation: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class SimplePool extends AbstractSimplePool {
|
export class SimplePool extends AbstractSimplePool {
|
||||||
constructor() {
|
constructor(options?: { enablePing?: boolean }) {
|
||||||
super({ verifyEvent, websocketImplementation: _WebSocket })
|
super({ verifyEvent, websocketImplementation: _WebSocket, ...options })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user