mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-01-31 14:38:51 +00:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b624ad4059 | ||
|
|
b3d314643a | ||
|
|
30ac8a02c2 | ||
|
|
42c9c7554d | ||
|
|
3588d30044 | ||
|
|
b40f59af74 | ||
|
|
bfa40da316 | ||
|
|
9078f45a64 | ||
|
|
6ebe59f123 | ||
|
|
0235b490fa | ||
|
|
e290f98a86 | ||
|
|
7a50d9328d | ||
|
|
65412e5b85 | ||
|
|
ca36ae9530 | ||
|
|
0b6543e1a8 | ||
|
|
693b262b7c | ||
|
|
85c964be3d | ||
|
|
de7d459f6f | ||
|
|
21ec5bb2dc | ||
|
|
e959409c14 | ||
|
|
8a76c4e329 | ||
|
|
34a1d8db47 | ||
|
|
d3ddd490c2 | ||
|
|
7730e321a5 | ||
|
|
400d132612 | ||
|
|
01880b6fb5 | ||
|
|
e87ffc433c | ||
|
|
c45e861493 |
@@ -138,6 +138,7 @@
|
||||
"valid-typeof": 2,
|
||||
"wrap-iife": [2, "any"],
|
||||
"yield-star-spacing": [2, "both"],
|
||||
"yoda": [0]
|
||||
"yoda": [0],
|
||||
"no-labels": [0]
|
||||
}
|
||||
}
|
||||
|
||||
58
README.md
58
README.md
@@ -1,4 +1,4 @@
|
||||
#  [](https://jsr.io/@nostr/tools) nostr-tools
|
||||
# [](https://jsr.io/@nostr/tools) @nostr/tools
|
||||
|
||||
Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients.
|
||||
|
||||
@@ -9,9 +9,6 @@ This package is only providing lower-level functionality. If you want higher-lev
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
# npm
|
||||
npm install --save nostr-tools
|
||||
|
||||
# jsr
|
||||
npx jsr add @nostr/tools
|
||||
```
|
||||
@@ -27,7 +24,7 @@ https://jsr.io/@nostr/tools/doc
|
||||
### Generating a private key and a public key
|
||||
|
||||
```js
|
||||
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure'
|
||||
import { generateSecretKey, getPublicKey } from '@nostr/tools/pure'
|
||||
|
||||
let sk = generateSecretKey() // `sk` is a Uint8Array
|
||||
let pk = getPublicKey(sk) // `pk` is a hex string
|
||||
@@ -36,7 +33,7 @@ let pk = getPublicKey(sk) // `pk` is a hex string
|
||||
To get the secret key in hex format, use
|
||||
|
||||
```js
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils' // already an installed dependency
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js' // already an installed dependency
|
||||
|
||||
let skHex = bytesToHex(sk)
|
||||
let backToBytes = hexToBytes(skHex)
|
||||
@@ -45,7 +42,7 @@ let backToBytes = hexToBytes(skHex)
|
||||
### Creating, signing and verifying events
|
||||
|
||||
```js
|
||||
import { finalizeEvent, verifyEvent } from 'nostr-tools/pure'
|
||||
import { finalizeEvent, verifyEvent } from '@nostr/tools/pure'
|
||||
|
||||
let event = finalizeEvent({
|
||||
kind: 1,
|
||||
@@ -62,8 +59,8 @@ let isGood = verifyEvent(event)
|
||||
Doesn't matter what you do, you always should be using a `SimplePool`:
|
||||
|
||||
```js
|
||||
import { finalizeEvent, generateSecretKey, getPublicKey } from 'nostr-tools/pure'
|
||||
import { SimplePool } from 'nostr-tools/pool'
|
||||
import { finalizeEvent, generateSecretKey, getPublicKey } from '@nostr/tools/pure'
|
||||
import { SimplePool } from '@nostr/tools/pool'
|
||||
|
||||
const pool = new SimplePool()
|
||||
|
||||
@@ -126,8 +123,8 @@ relay.close()
|
||||
To use this on Node.js you first must install `ws` and call something like this:
|
||||
|
||||
```js
|
||||
import { useWebSocketImplementation } from 'nostr-tools/pool'
|
||||
// or import { useWebSocketImplementation } from 'nostr-tools/relay' if you're using the Relay directly
|
||||
import { useWebSocketImplementation } from '@nostr/tools/pool'
|
||||
// or import { useWebSocketImplementation } from '@nostr/tools/relay' if you're using the Relay directly
|
||||
|
||||
import WebSocket from 'ws'
|
||||
useWebSocketImplementation(WebSocket)
|
||||
@@ -138,7 +135,7 @@ 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, like Node.js, don't report websocket disconnections due to network issues, and enabling this can increase the reliability of the `onclose` event.
|
||||
|
||||
```js
|
||||
import { SimplePool } from 'nostr-tools/pool'
|
||||
import { SimplePool } from '@nostr/tools/pool'
|
||||
|
||||
const pool = new SimplePool({ enablePing: true })
|
||||
```
|
||||
@@ -148,7 +145,7 @@ const pool = new SimplePool({ enablePing: true })
|
||||
You can also enable automatic reconnection with the `enableReconnect` option. This will make the pool try to reconnect to relays with an exponential backoff delay if the connection is lost unexpectedly.
|
||||
|
||||
```js
|
||||
import { SimplePool } from 'nostr-tools/pool'
|
||||
import { SimplePool } from '@nostr/tools/pool'
|
||||
|
||||
const pool = new SimplePool({ enableReconnect: true })
|
||||
```
|
||||
@@ -160,16 +157,7 @@ Using both `enablePing: true` and `enableReconnect: true` is recommended as it w
|
||||
const pool = new SimplePool({ enablePing: true, enableReconnect: true })
|
||||
```
|
||||
|
||||
The `enableReconnect` option can also be a callback function which will receive the current subscription filters and should return a new set of filters. This is useful if you want to modify the subscription on reconnect, for example, to update the `since` parameter to fetch only new events.
|
||||
|
||||
```js
|
||||
const pool = new SimplePool({
|
||||
enableReconnect: (filters) => {
|
||||
const newSince = Math.floor(Date.now() / 1000)
|
||||
return filters.map(filter => ({ ...filter, since: newSince }))
|
||||
}
|
||||
})
|
||||
```
|
||||
When reconnecting, all existing subscriptions will have their filters automatically updated with `since:` set to the timestamp of the last event received on them `+1`, then restarted.
|
||||
|
||||
### Parsing references (mentions) from a content based on NIP-27
|
||||
|
||||
@@ -253,7 +241,7 @@ const event = await bunker.signEvent({
|
||||
await signer.close()
|
||||
pool.close([])
|
||||
```
|
||||
> **Note on Reconnecting:** Once a connection has been successfully established and the `BunkerPointer` is stored, you do **not** need to call `await bunker.connect()` on subsequent sessions.
|
||||
> **Note on Reconnecting:** Once a connection has been successfully established and the `BunkerPointer` is stored, you do **not** need to call `await bunker.connect()` on subsequent sessions.
|
||||
|
||||
### Method 2: Using a Client-generated URI (`nostrconnect://`)
|
||||
|
||||
@@ -293,7 +281,7 @@ const event = await signer.signEvent({
|
||||
await signer.close()
|
||||
pool.close([])
|
||||
```
|
||||
> **Note on Persistence:** This method is ideal for the initial sign-in. To allow users to stay logged in across sessions, you should store the connection details and use `Method 1` for subsequent reconnections.
|
||||
> **Note on Persistence:** This method is ideal for the initial sign-in. To allow users to stay logged in across sessions, you should store the connection details and use `Method 1` for subsequent reconnections.
|
||||
|
||||
### Parsing thread from any note based on NIP-10
|
||||
|
||||
@@ -340,7 +328,7 @@ for (let profile of refs.profiles) {
|
||||
### Querying profile data from a NIP-05 address
|
||||
|
||||
```js
|
||||
import { queryProfile } from 'nostr-tools/nip05'
|
||||
import { queryProfile } from '@nostr/tools/nip05'
|
||||
|
||||
let profile = await queryProfile('jb55.com')
|
||||
console.log(profile.pubkey)
|
||||
@@ -352,13 +340,13 @@ console.log(profile.relays)
|
||||
To use this on Node.js < v18, you first must install `node-fetch@2` and call something like this:
|
||||
|
||||
```js
|
||||
import { useFetchImplementation } from 'nostr-tools/nip05'
|
||||
import { useFetchImplementation } from '@nostr/tools/nip05'
|
||||
useFetchImplementation(require('node-fetch'))
|
||||
```
|
||||
|
||||
### Including NIP-07 types
|
||||
```js
|
||||
import type { WindowNostr } from 'nostr-tools/nip07'
|
||||
import type { WindowNostr } from '@nostr/tools/nip07'
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
@@ -370,8 +358,8 @@ declare global {
|
||||
### Encoding and decoding NIP-19 codes
|
||||
|
||||
```js
|
||||
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure'
|
||||
import * as nip19 from 'nostr-tools/nip19'
|
||||
import { generateSecretKey, getPublicKey } from '@nostr/tools/pure'
|
||||
import * as nip19 from '@nostr/tools/nip19'
|
||||
|
||||
let sk = generateSecretKey()
|
||||
let nsec = nip19.nsecEncode(sk)
|
||||
@@ -399,7 +387,7 @@ assert(data.relays.length === 2)
|
||||
[`nostr-wasm`](https://github.com/fiatjaf/nostr-wasm) is a thin wrapper over [libsecp256k1](https://github.com/bitcoin-core/secp256k1) compiled to WASM just for hashing, signing and verifying Nostr events.
|
||||
|
||||
```js
|
||||
import { setNostrWasm, generateSecretKey, finalizeEvent, verifyEvent } from 'nostr-tools/wasm'
|
||||
import { setNostrWasm, generateSecretKey, finalizeEvent, verifyEvent } from '@nostr/tools/wasm'
|
||||
import { initNostrWasm } from 'nostr-wasm'
|
||||
|
||||
// make sure this promise resolves before your app starts calling finalizeEvent or verifyEvent
|
||||
@@ -412,9 +400,9 @@ initNostrWasm().then(setNostrWasm)
|
||||
If you're going to use `Relay` and `SimplePool` you must also import `nostr-tools/abstract-relay` and/or `nostr-tools/abstract-pool` instead of the defaults and then instantiate them by passing the `verifyEvent`:
|
||||
|
||||
```js
|
||||
import { setNostrWasm, verifyEvent } from 'nostr-tools/wasm'
|
||||
import { AbstractRelay } from 'nostr-tools/abstract-relay'
|
||||
import { AbstractSimplePool } from 'nostr-tools/abstract-pool'
|
||||
import { setNostrWasm, verifyEvent } from '@nostr/tools/wasm'
|
||||
import { AbstractRelay } from '@nostr/tools/abstract-relay'
|
||||
import { AbstractSimplePool } from '@nostr/tools/abstract-pool'
|
||||
import { initNostrWasm } from 'nostr-wasm'
|
||||
|
||||
initNostrWasm().then(setNostrWasm)
|
||||
@@ -451,7 +439,7 @@ summary for relay read message and verify event
|
||||
|
||||
## Plumbing
|
||||
|
||||
To develop `nostr-tools`, install [`just`](https://just.systems/) and run `just -l` to see commands available.
|
||||
To develop `@nostr/tools`, install [`just`](https://just.systems/) and run `just -l` to see commands available.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -11,17 +11,27 @@ import { normalizeURL } from './utils.ts'
|
||||
import type { Event, EventTemplate, Nostr, VerifiedEvent } from './core.ts'
|
||||
import { type Filter } from './filter.ts'
|
||||
import { alwaysTrue } from './helpers.ts'
|
||||
import { Relay } from './relay.ts'
|
||||
|
||||
export type SubCloser = { close: (reason?: string) => void }
|
||||
|
||||
export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {}
|
||||
export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
|
||||
// automaticallyAuth takes a relay URL and should return null
|
||||
// in case that relay shouldn't be authenticated against
|
||||
// or a function to sign the AUTH event template otherwise (that function may still throw in case of failure)
|
||||
automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||
// onRelayConnectionFailure is called with the URL of a relay that failed the initial connection
|
||||
onRelayConnectionFailure?: (url: string) => void
|
||||
// allowConnectingToRelay takes a relay URL and the operation being performed
|
||||
// return false to skip connecting to that relay
|
||||
allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
||||
}
|
||||
|
||||
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
||||
maxWait?: number
|
||||
abort?: AbortSignal
|
||||
onclose?: (reasons: string[]) => void
|
||||
onauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||
// Deprecated: use onauth instead
|
||||
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||
id?: string
|
||||
label?: string
|
||||
}
|
||||
@@ -33,8 +43,11 @@ export class AbstractSimplePool {
|
||||
|
||||
public verifyEvent: Nostr['verifyEvent']
|
||||
public enablePing: boolean | undefined
|
||||
public enableReconnect: boolean | ((filters: Filter[]) => Filter[]) | undefined
|
||||
public enableReconnect: boolean
|
||||
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||
public trustedRelayURLs: Set<string> = new Set()
|
||||
public onRelayConnectionFailure?: (url: string) => void
|
||||
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
||||
|
||||
private _WebSocket?: typeof WebSocket
|
||||
|
||||
@@ -42,10 +55,19 @@ export class AbstractSimplePool {
|
||||
this.verifyEvent = opts.verifyEvent
|
||||
this._WebSocket = opts.websocketImplementation
|
||||
this.enablePing = opts.enablePing
|
||||
this.enableReconnect = opts.enableReconnect
|
||||
this.enableReconnect = opts.enableReconnect || false
|
||||
this.automaticallyAuth = opts.automaticallyAuth
|
||||
this.onRelayConnectionFailure = opts.onRelayConnectionFailure
|
||||
this.allowConnectingToRelay = opts.allowConnectingToRelay
|
||||
}
|
||||
|
||||
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
||||
async ensureRelay(
|
||||
url: string,
|
||||
params?: {
|
||||
connectionTimeout?: number
|
||||
abort?: AbortSignal
|
||||
},
|
||||
): Promise<AbstractRelay> {
|
||||
url = normalizeURL(url)
|
||||
|
||||
let relay = this.relays.get(url)
|
||||
@@ -61,10 +83,20 @@ export class AbstractSimplePool {
|
||||
this.relays.delete(url)
|
||||
}
|
||||
}
|
||||
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
||||
this.relays.set(url, relay)
|
||||
}
|
||||
await relay.connect()
|
||||
|
||||
if (this.automaticallyAuth) {
|
||||
const authSignerFn = this.automaticallyAuth(url)
|
||||
if (authSignerFn) {
|
||||
relay.onauth = authSignerFn
|
||||
}
|
||||
}
|
||||
|
||||
await relay.connect({
|
||||
timeout: params?.connectionTimeout,
|
||||
abort: params?.abort,
|
||||
})
|
||||
|
||||
return relay
|
||||
}
|
||||
@@ -77,8 +109,6 @@ export class AbstractSimplePool {
|
||||
}
|
||||
|
||||
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const request: { url: string; filter: Filter }[] = []
|
||||
for (let i = 0; i < relays.length; i++) {
|
||||
const url = normalizeURL(relays[i])
|
||||
@@ -91,8 +121,6 @@ export class AbstractSimplePool {
|
||||
}
|
||||
|
||||
subscribeMany(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const request: { url: string; filter: Filter }[] = []
|
||||
const uniqUrls: string[] = []
|
||||
for (let i = 0; i < relays.length; i++) {
|
||||
@@ -107,8 +135,6 @@ export class AbstractSimplePool {
|
||||
}
|
||||
|
||||
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const grouped = new Map<string, Filter[]>()
|
||||
for (const req of requests) {
|
||||
const { url, filter } = req
|
||||
@@ -165,12 +191,19 @@ export class AbstractSimplePool {
|
||||
// open a subscription in all given relays
|
||||
const allOpened = Promise.all(
|
||||
groupedRequests.map(async ({ url, filters }, i) => {
|
||||
if (this.allowConnectingToRelay?.(url, ['read', filters]) === false) {
|
||||
handleClose(i, 'connection skipped by allowConnectingToRelay')
|
||||
return
|
||||
}
|
||||
|
||||
let relay: AbstractRelay
|
||||
try {
|
||||
relay = await this.ensureRelay(url, {
|
||||
connectionTimeout: params.maxWait ? Math.max(params.maxWait * 0.8, params.maxWait - 1000) : undefined,
|
||||
abort: params.abort,
|
||||
})
|
||||
} catch (err) {
|
||||
this.onRelayConnectionFailure?.(url)
|
||||
handleClose(i, (err as any)?.message || String(err))
|
||||
return
|
||||
}
|
||||
@@ -191,6 +224,7 @@ export class AbstractSimplePool {
|
||||
},
|
||||
alreadyHaveEvent: localAlreadyHaveEventHandler,
|
||||
eoseTimeout: params.maxWait,
|
||||
abort: params.abort,
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
@@ -202,6 +236,7 @@ export class AbstractSimplePool {
|
||||
},
|
||||
alreadyHaveEvent: localAlreadyHaveEventHandler,
|
||||
eoseTimeout: params.maxWait,
|
||||
abort: params.abort,
|
||||
})
|
||||
|
||||
subs.push(subscription)
|
||||
@@ -221,10 +256,8 @@ export class AbstractSimplePool {
|
||||
subscribeEose(
|
||||
relays: string[],
|
||||
filter: Filter,
|
||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth'>,
|
||||
): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const subcloser = this.subscribe(relays, filter, {
|
||||
...params,
|
||||
oneose() {
|
||||
@@ -237,10 +270,8 @@ export class AbstractSimplePool {
|
||||
subscribeManyEose(
|
||||
relays: string[],
|
||||
filter: Filter,
|
||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth'>,
|
||||
): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const subcloser = this.subscribeMany(relays, filter, {
|
||||
...params,
|
||||
oneose() {
|
||||
@@ -291,7 +322,18 @@ export class AbstractSimplePool {
|
||||
return Promise.reject('duplicate url')
|
||||
}
|
||||
|
||||
let r = await this.ensureRelay(url)
|
||||
if (this.allowConnectingToRelay?.(url, ['write', event]) === false) {
|
||||
return Promise.reject('connection skipped by allowConnectingToRelay')
|
||||
}
|
||||
|
||||
let r: Relay
|
||||
try {
|
||||
r = await this.ensureRelay(url)
|
||||
} catch (err) {
|
||||
this.onRelayConnectionFailure?.(url)
|
||||
return String('connection failure: ' + String(err))
|
||||
}
|
||||
|
||||
return r
|
||||
.publish(event)
|
||||
.catch(async err => {
|
||||
|
||||
@@ -16,7 +16,7 @@ export type AbstractRelayConstructorOptions = {
|
||||
verifyEvent: Nostr['verifyEvent']
|
||||
websocketImplementation?: typeof WebSocket
|
||||
enablePing?: boolean
|
||||
enableReconnect?: boolean | ((filters: Filter[]) => Filter[])
|
||||
enableReconnect?: boolean
|
||||
}
|
||||
|
||||
export class SendingOnClosedConnection extends Error {
|
||||
@@ -32,19 +32,18 @@ export class AbstractRelay {
|
||||
|
||||
public onclose: (() => void) | null = null
|
||||
public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`)
|
||||
public onauth: undefined | ((evt: EventTemplate) => Promise<VerifiedEvent>)
|
||||
|
||||
public baseEoseTimeout: number = 4400
|
||||
public connectionTimeout: number = 4400
|
||||
public publishTimeout: number = 4400
|
||||
public pingFrequency: number = 20000
|
||||
public pingFrequency: number = 29000
|
||||
public pingTimeout: number = 20000
|
||||
public resubscribeBackoff: number[] = [10000, 10000, 10000, 20000, 20000, 30000, 60000]
|
||||
public openSubs: Map<string, Subscription> = new Map()
|
||||
public enablePing: boolean | undefined
|
||||
public enableReconnect: boolean | ((filters: Filter[]) => Filter[])
|
||||
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||
public enableReconnect: boolean
|
||||
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||
private pingTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
|
||||
private reconnectAttempts: number = 0
|
||||
private closedIntentionally: boolean = false
|
||||
|
||||
@@ -69,9 +68,12 @@ export class AbstractRelay {
|
||||
this.enableReconnect = opts.enableReconnect || false
|
||||
}
|
||||
|
||||
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
|
||||
static async connect(
|
||||
url: string,
|
||||
opts: AbstractRelayConstructorOptions & Parameters<AbstractRelay['connect']>[0],
|
||||
): Promise<AbstractRelay> {
|
||||
const relay = new AbstractRelay(url, opts)
|
||||
await relay.connect()
|
||||
await relay.connect(opts)
|
||||
return relay
|
||||
}
|
||||
|
||||
@@ -110,9 +112,9 @@ export class AbstractRelay {
|
||||
}
|
||||
|
||||
private handleHardClose(reason: string) {
|
||||
if (this.pingTimeoutHandle) {
|
||||
clearTimeout(this.pingTimeoutHandle)
|
||||
this.pingTimeoutHandle = undefined
|
||||
if (this.pingIntervalHandle) {
|
||||
clearInterval(this.pingIntervalHandle)
|
||||
this.pingIntervalHandle = undefined
|
||||
}
|
||||
|
||||
this._connected = false
|
||||
@@ -130,59 +132,84 @@ export class AbstractRelay {
|
||||
}
|
||||
}
|
||||
|
||||
public async connect(): Promise<void> {
|
||||
public async connect(opts?: { timeout?: number; abort?: AbortSignal }): Promise<void> {
|
||||
let connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||
|
||||
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')
|
||||
if (opts?.timeout) {
|
||||
connectionTimeoutHandle = setTimeout(() => {
|
||||
reject('connection timed out')
|
||||
this.connectionPromise = undefined
|
||||
this.onclose?.()
|
||||
this.handleHardClose('relay connection timed out')
|
||||
}, opts.timeout)
|
||||
}
|
||||
|
||||
if (opts?.abort) {
|
||||
opts.abort.onabort = reject
|
||||
}
|
||||
|
||||
const connectionFailed = () => {
|
||||
clearTimeout(connectionTimeoutHandle)
|
||||
reject('connection failed')
|
||||
this.connectionPromise = undefined
|
||||
this.onclose?.()
|
||||
this.closeAllSubscriptions('relay connection timed out')
|
||||
}, this.connectionTimeout)
|
||||
this.handleHardClose('relay connection failed')
|
||||
}
|
||||
|
||||
try {
|
||||
this.ws = new this._WebSocket(this.url)
|
||||
this.ws.addEventListener('error', connectionFailed)
|
||||
} catch (err) {
|
||||
clearTimeout(this.connectionTimeoutHandle)
|
||||
clearTimeout(connectionTimeoutHandle)
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.ws.onopen = () => {
|
||||
this.ws?.removeEventListener('error', connectionFailed)
|
||||
|
||||
if (this.reconnectTimeoutHandle) {
|
||||
clearTimeout(this.reconnectTimeoutHandle)
|
||||
this.reconnectTimeoutHandle = undefined
|
||||
}
|
||||
clearTimeout(this.connectionTimeoutHandle)
|
||||
clearTimeout(connectionTimeoutHandle)
|
||||
this._connected = true
|
||||
|
||||
const isReconnection = this.reconnectAttempts > 0
|
||||
this.reconnectAttempts = 0
|
||||
|
||||
// resubscribe to all open subscriptions
|
||||
for (const sub of this.openSubs.values()) {
|
||||
sub.eosed = false
|
||||
if (typeof this.enableReconnect === 'function') {
|
||||
sub.filters = this.enableReconnect(sub.filters)
|
||||
if (isReconnection) {
|
||||
for (let f = 0; f < sub.filters.length; f++) {
|
||||
if (sub.lastEmitted) {
|
||||
sub.filters[f].since = sub.lastEmitted + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
sub.fire()
|
||||
}
|
||||
|
||||
if (this.enablePing) {
|
||||
this.pingpong()
|
||||
this.pingIntervalHandle = setInterval(() => this.pingpong(), this.pingFrequency)
|
||||
}
|
||||
resolve()
|
||||
}
|
||||
|
||||
this.ws.onerror = ev => {
|
||||
clearTimeout(this.connectionTimeoutHandle)
|
||||
clearTimeout(connectionTimeoutHandle)
|
||||
reject((ev as any).message || 'websocket error')
|
||||
this.handleHardClose('relay connection errored')
|
||||
}
|
||||
|
||||
this.ws.onclose = ev => {
|
||||
clearTimeout(this.connectionTimeoutHandle)
|
||||
clearTimeout(connectionTimeoutHandle)
|
||||
reject((ev as any).message || 'websocket closed')
|
||||
this.handleHardClose('relay connection closed')
|
||||
}
|
||||
@@ -202,17 +229,31 @@ export class AbstractRelay {
|
||||
})
|
||||
}
|
||||
|
||||
private async waitForDummyReq() {
|
||||
return new Promise((resolve, _) => {
|
||||
private waitForDummyReq() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.connectionPromise) return reject(new Error(`no connection to ${this.url}, can't ping`))
|
||||
|
||||
// make a dummy request with expected empty eose reply
|
||||
// ["REQ", "_", {"ids":["aaaa...aaaa"]}]
|
||||
const sub = this.subscribe([{ ids: ['a'.repeat(64)] }], {
|
||||
oneose: () => {
|
||||
sub.close()
|
||||
resolve(true)
|
||||
},
|
||||
eoseTimeout: this.pingTimeout + 1000,
|
||||
})
|
||||
// ["REQ", "_", {"ids":["aaaa...aaaa"], "limit": 0}]
|
||||
try {
|
||||
const sub = this.subscribe(
|
||||
[{ ids: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], limit: 0 }],
|
||||
{
|
||||
label: 'forced-ping',
|
||||
oneose: () => {
|
||||
resolve(true)
|
||||
sub.close()
|
||||
},
|
||||
onclose() {
|
||||
// if we get a CLOSED it's because the relay is alive
|
||||
resolve(true)
|
||||
},
|
||||
eoseTimeout: this.pingTimeout + 1000,
|
||||
},
|
||||
)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -227,10 +268,8 @@ export class AbstractRelay {
|
||||
this.ws && this.ws.ping && (this.ws as any).once ? this.waitForPingPong() : this.waitForDummyReq(),
|
||||
new Promise(res => setTimeout(() => res(false), this.pingTimeout)),
|
||||
])
|
||||
if (result) {
|
||||
// schedule another pingpong
|
||||
this.pingTimeoutHandle = setTimeout(() => this.pingpong(), this.pingFrequency)
|
||||
} else {
|
||||
|
||||
if (!result) {
|
||||
// pingpong closing socket
|
||||
if (this.ws?.readyState === this._WebSocket.OPEN) {
|
||||
this.ws?.close()
|
||||
@@ -256,6 +295,7 @@ export class AbstractRelay {
|
||||
return false
|
||||
}
|
||||
|
||||
// shortcut EVENT sub
|
||||
const subid = getSubscriptionId(json)
|
||||
if (subid) {
|
||||
const so = this.openSubs.get(subid as string)
|
||||
@@ -292,6 +332,7 @@ export class AbstractRelay {
|
||||
if (this.verifyEvent(event) && matchFilters(so.filters, event)) {
|
||||
so.onevent(event)
|
||||
}
|
||||
if (!so.lastEmitted || so.lastEmitted < event.created_at) so.lastEmitted = event.created_at
|
||||
return
|
||||
}
|
||||
case 'COUNT': {
|
||||
@@ -337,6 +378,9 @@ export class AbstractRelay {
|
||||
}
|
||||
case 'AUTH': {
|
||||
this.challenge = data[1] as string
|
||||
if (this.onauth) {
|
||||
this.auth(this.onauth)
|
||||
}
|
||||
return
|
||||
}
|
||||
default: {
|
||||
@@ -411,7 +455,14 @@ export class AbstractRelay {
|
||||
filters: Filter[],
|
||||
params: Partial<SubscriptionParams> & { label?: string; id?: string },
|
||||
): Subscription {
|
||||
return this.prepareSubscription(filters, params)
|
||||
const sub = this.prepareSubscription(filters, params)
|
||||
sub.fire()
|
||||
|
||||
if (params.abort) {
|
||||
params.abort.onabort = () => sub.close(String(params.abort!.reason || '<aborted>'))
|
||||
}
|
||||
|
||||
return sub
|
||||
}
|
||||
|
||||
public prepareSubscription(
|
||||
@@ -431,9 +482,9 @@ export class AbstractRelay {
|
||||
clearTimeout(this.reconnectTimeoutHandle)
|
||||
this.reconnectTimeoutHandle = undefined
|
||||
}
|
||||
if (this.pingTimeoutHandle) {
|
||||
clearTimeout(this.pingTimeoutHandle)
|
||||
this.pingTimeoutHandle = undefined
|
||||
if (this.pingIntervalHandle) {
|
||||
clearInterval(this.pingIntervalHandle)
|
||||
this.pingIntervalHandle = undefined
|
||||
}
|
||||
this.closeAllSubscriptions('relay connection closed by us')
|
||||
this._connected = false
|
||||
@@ -457,6 +508,7 @@ export class Subscription {
|
||||
public readonly relay: AbstractRelay
|
||||
public readonly id: string
|
||||
|
||||
public lastEmitted: number | undefined
|
||||
public closed: boolean = false
|
||||
public eosed: boolean = false
|
||||
public filters: Filter[]
|
||||
@@ -536,6 +588,7 @@ export type SubscriptionParams = {
|
||||
alreadyHaveEvent?: (id: string) => boolean
|
||||
receivedEvent?: (relay: AbstractRelay, id: string) => void
|
||||
eoseTimeout?: number
|
||||
abort?: AbortSignal
|
||||
}
|
||||
|
||||
export type CountResolver = {
|
||||
|
||||
1
build.js
1
build.js
@@ -7,7 +7,6 @@ const entryPoints = fs
|
||||
.filter(
|
||||
file =>
|
||||
file.endsWith('.ts') &&
|
||||
file !== 'core.ts' &&
|
||||
file !== 'test-helpers.ts' &&
|
||||
file !== 'helpers.ts' &&
|
||||
file !== 'benchmarks.ts' &&
|
||||
|
||||
4
core.ts
4
core.ts
@@ -8,7 +8,7 @@ export interface Nostr {
|
||||
/** Designates a verified event signature. */
|
||||
export const verifiedSymbol = Symbol('verified')
|
||||
|
||||
export interface Event {
|
||||
export type NostrEvent = {
|
||||
kind: number
|
||||
tags: string[][]
|
||||
content: string
|
||||
@@ -19,7 +19,7 @@ export interface Event {
|
||||
[verifiedSymbol]?: boolean
|
||||
}
|
||||
|
||||
export type NostrEvent = Event
|
||||
export type Event = NostrEvent
|
||||
export type EventTemplate = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at'>
|
||||
export type UnsignedEvent = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at' | 'pubkey'>
|
||||
|
||||
|
||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nostr/tools",
|
||||
"version": "2.17.3",
|
||||
"version": "2.22.1",
|
||||
"exports": {
|
||||
".": "./index.ts",
|
||||
"./core": "./core.ts",
|
||||
|
||||
@@ -18,7 +18,7 @@ test('kind classification', () => {
|
||||
expect(classifyKind(30000)).toBe('parameterized')
|
||||
expect(classifyKind(39999)).toBe('parameterized')
|
||||
expect(classifyKind(40000)).toBe('unknown')
|
||||
expect(classifyKind(255)).toBe('unknown')
|
||||
expect(classifyKind(255)).toBe('regular')
|
||||
})
|
||||
|
||||
test('kind type guard', () => {
|
||||
|
||||
34
kinds.ts
34
kinds.ts
@@ -2,12 +2,12 @@ import { NostrEvent, validateEvent } from './pure.ts'
|
||||
|
||||
/** Events are **regular**, which means they're all expected to be stored by relays. */
|
||||
export function isRegularKind(kind: number): boolean {
|
||||
return (1000 <= kind && kind < 10000) || [1, 2, 4, 5, 6, 7, 8, 16, 40, 41, 42, 43, 44].includes(kind)
|
||||
return kind < 10000 && kind !== 0 && kind !== 3
|
||||
}
|
||||
|
||||
/** Events are **replaceable**, which means that, for each combination of `pubkey` and `kind`, only the latest event is expected to (SHOULD) be stored by relays, older versions are expected to be discarded. */
|
||||
export function isReplaceableKind(kind: number): boolean {
|
||||
return [0, 3].includes(kind) || (10000 <= kind && kind < 20000)
|
||||
return kind === 0 || kind === 3 || (10000 <= kind && kind < 20000)
|
||||
}
|
||||
|
||||
/** Events are **ephemeral**, which means they are not expected to be stored by relays. */
|
||||
@@ -55,12 +55,24 @@ export const Reaction = 7
|
||||
export type Reaction = typeof Reaction
|
||||
export const BadgeAward = 8
|
||||
export type BadgeAward = typeof BadgeAward
|
||||
export const ChatMessage = 9
|
||||
export type ChatMessage = typeof ChatMessage
|
||||
export const ForumThread = 11
|
||||
export type ForumThread = typeof ForumThread
|
||||
export const Seal = 13
|
||||
export type Seal = typeof Seal
|
||||
export const PrivateDirectMessage = 14
|
||||
export type PrivateDirectMessage = typeof PrivateDirectMessage
|
||||
export const FileMessage = 15
|
||||
export type FileMessage = typeof FileMessage
|
||||
export const GenericRepost = 16
|
||||
export type GenericRepost = typeof GenericRepost
|
||||
export const Photo = 20
|
||||
export type Photo = typeof Photo
|
||||
export const NormalVideo = 21
|
||||
export type NormalVideo = typeof NormalVideo
|
||||
export const ShortVideo = 22
|
||||
export type ShortVideo = typeof ShortVideo
|
||||
export const ChannelCreation = 40
|
||||
export type ChannelCreation = typeof ChannelCreation
|
||||
export const ChannelMetadata = 41
|
||||
@@ -75,10 +87,18 @@ export const OpenTimestamps = 1040
|
||||
export type OpenTimestamps = typeof OpenTimestamps
|
||||
export const GiftWrap = 1059
|
||||
export type GiftWrap = typeof GiftWrap
|
||||
export const Poll = 1068
|
||||
export type Poll = typeof Poll
|
||||
export const FileMetadata = 1063
|
||||
export type FileMetadata = typeof FileMetadata
|
||||
export const Comment = 1111
|
||||
export type Comment = typeof Comment
|
||||
export const LiveChatMessage = 1311
|
||||
export type LiveChatMessage = typeof LiveChatMessage
|
||||
export const Voice = 1222
|
||||
export type Voice = typeof Voice
|
||||
export const VoiceComment = 1244
|
||||
export type VoiceComment = typeof VoiceComment
|
||||
export const ProblemTracker = 1971
|
||||
export type ProblemTracker = typeof ProblemTracker
|
||||
export const Report = 1984
|
||||
@@ -103,6 +123,8 @@ export const Zap = 9735
|
||||
export type Zap = typeof Zap
|
||||
export const Highlights = 9802
|
||||
export type Highlights = typeof Highlights
|
||||
export const PollResponse = 1018
|
||||
export type PollResponse = typeof PollResponse
|
||||
export const Mutelist = 10000
|
||||
export type Mutelist = typeof Mutelist
|
||||
export const Pinlist = 10001
|
||||
@@ -119,6 +141,8 @@ export const BlockedRelaysList = 10006
|
||||
export type BlockedRelaysList = typeof BlockedRelaysList
|
||||
export const SearchRelaysList = 10007
|
||||
export type SearchRelaysList = typeof SearchRelaysList
|
||||
export const FavoriteRelays = 10012
|
||||
export type FavoriteRelays = typeof FavoriteRelays
|
||||
export const InterestsList = 10015
|
||||
export type InterestsList = typeof InterestsList
|
||||
export const UserEmojiList = 10030
|
||||
@@ -127,6 +151,8 @@ export const DirectMessageRelaysList = 10050
|
||||
export type DirectMessageRelaysList = typeof DirectMessageRelaysList
|
||||
export const FileServerPreference = 10096
|
||||
export type FileServerPreference = typeof FileServerPreference
|
||||
export const BlossomServerList = 10063
|
||||
export type BlossomServerList = typeof BlossomServerList
|
||||
export const NWCWalletInfo = 13194
|
||||
export type NWCWalletInfo = typeof NWCWalletInfo
|
||||
export const LightningPubRPC = 21000
|
||||
@@ -185,9 +211,13 @@ export const Calendar = 31924
|
||||
export type Calendar = typeof Calendar
|
||||
export const CalendarEventRSVP = 31925
|
||||
export type CalendarEventRSVP = typeof CalendarEventRSVP
|
||||
export const RelayReview = 31987
|
||||
export type RelayReview = typeof RelayReview
|
||||
export const Handlerrecommendation = 31989
|
||||
export type Handlerrecommendation = typeof Handlerrecommendation
|
||||
export const Handlerinformation = 31990
|
||||
export type Handlerinformation = typeof Handlerinformation
|
||||
export const CommunityDefinition = 34550
|
||||
export type CommunityDefinition = typeof CommunityDefinition
|
||||
export const GroupMetadata = 39000
|
||||
export type GroupMetadata = typeof GroupMetadata
|
||||
|
||||
@@ -2,7 +2,7 @@ import { test, expect } from 'bun:test'
|
||||
|
||||
import { encrypt, decrypt } from './nip04.ts'
|
||||
import { getPublicKey, generateSecretKey } from './pure.ts'
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils'
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'
|
||||
|
||||
test('encrypt and decrypt message', async () => {
|
||||
let sk1 = generateSecretKey()
|
||||
|
||||
14
nip04.ts
14
nip04.ts
@@ -1,13 +1,13 @@
|
||||
import { bytesToHex, randomBytes } from '@noble/hashes/utils'
|
||||
import { secp256k1 } from '@noble/curves/secp256k1'
|
||||
import { cbc } from '@noble/ciphers/aes'
|
||||
import { hexToBytes, randomBytes } from '@noble/hashes/utils.js'
|
||||
import { secp256k1 } from '@noble/curves/secp256k1.js'
|
||||
import { cbc } from '@noble/ciphers/aes.js'
|
||||
import { base64 } from '@scure/base'
|
||||
|
||||
import { utf8Decoder, utf8Encoder } from './utils.ts'
|
||||
|
||||
export function encrypt(secretKey: string | Uint8Array, pubkey: string, text: string): string {
|
||||
const privkey: string = secretKey instanceof Uint8Array ? bytesToHex(secretKey) : secretKey
|
||||
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||
const privkey: Uint8Array = secretKey instanceof Uint8Array ? secretKey : hexToBytes(secretKey)
|
||||
const key = secp256k1.getSharedSecret(privkey, hexToBytes('02' + pubkey))
|
||||
const normalizedKey = getNormalizedX(key)
|
||||
|
||||
let iv = Uint8Array.from(randomBytes(16))
|
||||
@@ -22,9 +22,9 @@ export function encrypt(secretKey: string | Uint8Array, pubkey: string, text: st
|
||||
}
|
||||
|
||||
export function decrypt(secretKey: string | Uint8Array, pubkey: string, data: string): string {
|
||||
const privkey: string = secretKey instanceof Uint8Array ? bytesToHex(secretKey) : secretKey
|
||||
const privkey: Uint8Array = secretKey instanceof Uint8Array ? secretKey : hexToBytes(secretKey)
|
||||
let [ctb64, ivb64] = data.split('?iv=')
|
||||
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||
let key = secp256k1.getSharedSecret(privkey, hexToBytes('02' + pubkey))
|
||||
let normalizedKey = getNormalizedX(key)
|
||||
|
||||
let iv = base64.decode(ivb64)
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
extendedKeysFromSeedWords,
|
||||
accountFromExtendedKey,
|
||||
} from './nip06.ts'
|
||||
import { hexToBytes } from '@noble/hashes/utils'
|
||||
import { hexToBytes } from '@noble/hashes/utils.js'
|
||||
|
||||
test('generate private key from a mnemonic', async () => {
|
||||
const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'
|
||||
|
||||
4
nip06.ts
4
nip06.ts
@@ -1,5 +1,5 @@
|
||||
import { bytesToHex } from '@noble/hashes/utils'
|
||||
import { wordlist } from '@scure/bip39/wordlists/english'
|
||||
import { bytesToHex } from '@noble/hashes/utils.js'
|
||||
import { wordlist } from '@scure/bip39/wordlists/english.js'
|
||||
import { generateMnemonic, mnemonicToSeedSync, validateMnemonic } from '@scure/bip39'
|
||||
import { HDKey } from '@scure/bip32'
|
||||
|
||||
|
||||
4
nip13.ts
4
nip13.ts
@@ -1,6 +1,6 @@
|
||||
import { bytesToHex } from '@noble/hashes/utils'
|
||||
import { bytesToHex } from '@noble/hashes/utils.js'
|
||||
import { type UnsignedEvent, type Event } from './pure.ts'
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { sha256 } from '@noble/hashes/sha2.js'
|
||||
|
||||
import { utf8Encoder } from './utils.ts'
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { test, expect } from 'bun:test'
|
||||
import { getPublicKey } from './pure.ts'
|
||||
import { decode } from './nip19.ts'
|
||||
import { wrapEvent, wrapManyEvents, unwrapEvent } from './nip17.ts'
|
||||
import { hexToBytes } from '@noble/hashes/utils'
|
||||
import { hexToBytes } from '@noble/hashes/utils.js'
|
||||
|
||||
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, test, expect } from 'bun:test'
|
||||
import { hexToBytes } from '@noble/hashes/utils'
|
||||
import { hexToBytes } from '@noble/hashes/utils.js'
|
||||
import { EventTemplate, finalizeEvent, getPublicKey } from './pure.ts'
|
||||
import { GenericRepost, Repost, ShortTextNote, BadgeDefinition as BadgeDefinitionKind } from './kinds.ts'
|
||||
import { finishRepostEvent, getRepostedEventPointer, getRepostedEvent } from './nip18.ts'
|
||||
|
||||
4
nip19.ts
4
nip19.ts
@@ -1,4 +1,4 @@
|
||||
import { bytesToHex, concatBytes, hexToBytes } from '@noble/hashes/utils'
|
||||
import { bytesToHex, concatBytes, hexToBytes } from '@noble/hashes/utils.js'
|
||||
import { bech32 } from '@scure/base'
|
||||
|
||||
import { utf8Decoder, utf8Encoder } from './utils.ts'
|
||||
@@ -110,7 +110,7 @@ export function decode(nip19: NPub): DecodedNpub
|
||||
export function decode(nip19: Note): DecodedNote
|
||||
export function decode(code: string): DecodedResult
|
||||
export function decode(code: string): DecodedResult {
|
||||
let { prefix, words } = bech32.decode(code, Bech32MaxSize)
|
||||
let { prefix, words } = bech32.decode(code as `${string}1${string}`, Bech32MaxSize)
|
||||
let data = new Uint8Array(bech32.fromWords(words))
|
||||
|
||||
switch (prefix) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, test, expect } from 'bun:test'
|
||||
import { hexToBytes } from '@noble/hashes/utils'
|
||||
import { hexToBytes } from '@noble/hashes/utils.js'
|
||||
import { finalizeEvent, getPublicKey } from './pure.ts'
|
||||
import { Reaction, ShortTextNote } from './kinds.ts'
|
||||
import { finishReactionEvent, getReactedEventPointer } from './nip25.ts'
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { test, expect } from 'bun:test'
|
||||
import { parse } from './nip27.ts'
|
||||
import { NostrEvent } from './core.ts'
|
||||
|
||||
test('first: parse simple content with 1 url and 1 nostr uri', () => {
|
||||
const content = `nostr:npub1hpslpc8c5sp3e2nhm2fr7swsfqpys5vyjar5dwpn7e7decps6r8qkcln63 check out my profile:nostr:npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s; and this cool image https://images.com/image.jpg`
|
||||
@@ -15,14 +16,14 @@ test('first: parse simple content with 1 url and 1 nostr uri', () => {
|
||||
})
|
||||
|
||||
test('second: parse content with 3 urls of different types', () => {
|
||||
const content = `:wss://oa.ao; this was a relay and now here's a video -> https://videos.com/video.mp4! and some music:
|
||||
const content = `:wss://oa.ao/a/; this was a relay and now here's a video -> https://videos.com/video.mp4! and some music:
|
||||
http://music.com/song.mp3
|
||||
and a regular link: https://regular.com/page?ok=true. and now a broken link: https://kjxkxk and a broken nostr ref: nostr:nevent1qqsr0f9w78uyy09qwmjt0kv63j4l7sxahq33725lqyyp79whlfjurwspz4mhxue69uhh56nzv34hxcfwv9ehw6nyddhq0ag9xg and a fake nostr ref: nostr:llll ok but finally https://ok.com!`
|
||||
const blocks = Array.from(parse(content))
|
||||
|
||||
expect(blocks).toEqual([
|
||||
{ type: 'text', text: ':' },
|
||||
{ type: 'relay', url: 'wss://oa.ao/' },
|
||||
{ type: 'relay', url: 'wss://oa.ao/a/' },
|
||||
{ type: 'text', text: "; this was a relay and now here's a video -> " },
|
||||
{ type: 'video', url: 'https://videos.com/video.mp4' },
|
||||
{ type: 'text', text: '! and some music:\n' },
|
||||
@@ -75,3 +76,55 @@ test('third: parse complex content with 4 nostr uris and 3 urls', () => {
|
||||
{ type: 'url', url: 'https://example.com/docs' },
|
||||
])
|
||||
})
|
||||
|
||||
test('parse content with hashtags and emoji shortcodes', () => {
|
||||
const event: NostrEvent = {
|
||||
kind: 1,
|
||||
tags: [
|
||||
['emoji', 'star', 'https://example.com/star.png'],
|
||||
['emoji', 'alpaca', 'https://example.com/alpaca.png'],
|
||||
],
|
||||
content:
|
||||
'hey nostr:npub1hpslpc8c5sp3e2nhm2fr7swsfqpys5vyjar5dwpn7e7decps6r8qkcln63 check out :alpaca::alpaca: #alpaca at wss://alpaca.com! :star:',
|
||||
created_at: 1234567890,
|
||||
pubkey: 'dummy',
|
||||
id: 'dummy',
|
||||
sig: 'dummy',
|
||||
}
|
||||
const blocks = Array.from(parse(event))
|
||||
|
||||
expect(blocks).toEqual([
|
||||
{ type: 'text', text: 'hey ' },
|
||||
{ type: 'reference', pointer: { pubkey: 'b861f0e0f8a4031caa77da923f41d04802485184974746b833f67cdce030d0ce' } },
|
||||
{ type: 'text', text: ' check out ' },
|
||||
{ type: 'emoji', shortcode: 'alpaca', url: 'https://example.com/alpaca.png' },
|
||||
{ type: 'emoji', shortcode: 'alpaca', url: 'https://example.com/alpaca.png' },
|
||||
{ type: 'text', text: ' ' },
|
||||
{ type: 'hashtag', value: 'alpaca' },
|
||||
{ type: 'text', text: ' at ' },
|
||||
{ type: 'relay', url: 'wss://alpaca.com/' },
|
||||
{ type: 'text', text: '! ' },
|
||||
{ type: 'emoji', shortcode: 'star', url: 'https://example.com/star.png' },
|
||||
])
|
||||
})
|
||||
|
||||
test('emoji shortcodes are treated as text if no event tags', () => {
|
||||
const blocks = Array.from(parse('hello :alpaca:'))
|
||||
|
||||
expect(blocks).toEqual([{ type: 'text', text: 'hello :alpaca:' }])
|
||||
})
|
||||
|
||||
test("a thing that didn't work well in the wild", () => {
|
||||
const blocks = Array.from(
|
||||
parse(
|
||||
`Crowdsourcing doesn't mean just users clicking, by the way (although that could be possible too), it means a bunch of machines competing: https://leaderboard.sbstats.uk/`,
|
||||
),
|
||||
)
|
||||
expect(blocks).toEqual([
|
||||
{
|
||||
type: 'text',
|
||||
text: `Crowdsourcing doesn't mean just users clicking, by the way (although that could be possible too), it means a bunch of machines competing: `,
|
||||
},
|
||||
{ type: 'url', url: 'https://leaderboard.sbstats.uk/' },
|
||||
])
|
||||
})
|
||||
|
||||
127
nip27.ts
127
nip27.ts
@@ -1,3 +1,4 @@
|
||||
import { NostrEvent } from './core.ts'
|
||||
import { AddressPointer, EventPointer, ProfilePointer, decode } from './nip19.ts'
|
||||
|
||||
export type Block =
|
||||
@@ -29,34 +30,76 @@ export type Block =
|
||||
type: 'audio'
|
||||
url: string
|
||||
}
|
||||
| {
|
||||
type: 'emoji'
|
||||
shortcode: string
|
||||
url: string
|
||||
}
|
||||
| {
|
||||
type: 'hashtag'
|
||||
value: string
|
||||
}
|
||||
|
||||
const noCharacter = /\W/m
|
||||
const noURLCharacter = /\W |\W$|$|,| /m
|
||||
const noURLCharacter = /[^\w\/] |[^\w\/]$|$|,| /m
|
||||
const MAX_HASHTAG_LENGTH = 42
|
||||
|
||||
export function* parse(content: string | NostrEvent): Iterable<Block> {
|
||||
let emojis: { type: 'emoji'; shortcode: string; url: string }[] = []
|
||||
if (typeof content !== 'string') {
|
||||
for (let i = 0; i < content.tags.length; i++) {
|
||||
const tag = content.tags[i]
|
||||
if (tag[0] === 'emoji' && tag.length >= 3) {
|
||||
emojis.push({ type: 'emoji', shortcode: tag[1], url: tag[2] })
|
||||
}
|
||||
}
|
||||
content = content.content
|
||||
}
|
||||
|
||||
export function* parse(content: string): Iterable<Block> {
|
||||
const max = content.length
|
||||
let prevIndex = 0
|
||||
let index = 0
|
||||
while (index < max) {
|
||||
let u = content.indexOf(':', index)
|
||||
if (u === -1) {
|
||||
mainloop: while (index < max) {
|
||||
const u = content.indexOf(':', index)
|
||||
const h = content.indexOf('#', index)
|
||||
if (u === -1 && h === -1) {
|
||||
// reached end
|
||||
break
|
||||
break mainloop
|
||||
}
|
||||
|
||||
if (content.substring(u - 5, u) === 'nostr') {
|
||||
const m = content.substring(u + 60).match(noCharacter)
|
||||
if (u === -1 || (h >= 0 && h < u)) {
|
||||
// parse hashtag
|
||||
if (h === 0 || content[h - 1] === ' ') {
|
||||
const m = content.slice(h + 1, h + MAX_HASHTAG_LENGTH).match(noCharacter)
|
||||
const end = m ? h + 1 + m.index! : max
|
||||
yield { type: 'text', text: content.slice(prevIndex, h) }
|
||||
yield { type: 'hashtag', value: content.slice(h + 1, end) }
|
||||
index = end
|
||||
prevIndex = index
|
||||
continue mainloop
|
||||
}
|
||||
|
||||
// ignore this, it is nothing
|
||||
index = h + 1
|
||||
continue mainloop
|
||||
}
|
||||
|
||||
// otherwise parse things that have an ":"
|
||||
if (content.slice(u - 5, u) === 'nostr') {
|
||||
const m = content.slice(u + 60).match(noCharacter)
|
||||
const end = m ? u + 60 + m.index! : max
|
||||
try {
|
||||
let pointer: ProfilePointer | AddressPointer | EventPointer
|
||||
let { data, type } = decode(content.substring(u + 1, end))
|
||||
let { data, type } = decode(content.slice(u + 1, end))
|
||||
|
||||
switch (type) {
|
||||
case 'npub':
|
||||
pointer = { pubkey: data } as ProfilePointer
|
||||
break
|
||||
case 'nsec':
|
||||
case 'note':
|
||||
pointer = { id: data } as EventPointer
|
||||
break
|
||||
case 'nsec':
|
||||
// ignore this, treat it as not a valid uri
|
||||
index = end + 1
|
||||
continue
|
||||
@@ -65,89 +108,107 @@ export function* parse(content: string): Iterable<Block> {
|
||||
}
|
||||
|
||||
if (prevIndex !== u - 5) {
|
||||
yield { type: 'text', text: content.substring(prevIndex, u - 5) }
|
||||
yield { type: 'text', text: content.slice(prevIndex, u - 5) }
|
||||
}
|
||||
yield { type: 'reference', pointer }
|
||||
index = end
|
||||
prevIndex = index
|
||||
continue
|
||||
continue mainloop
|
||||
} catch (_err) {
|
||||
// ignore this, not a valid nostr uri
|
||||
index = u + 1
|
||||
continue
|
||||
continue mainloop
|
||||
}
|
||||
} else if (content.substring(u - 5, u) === 'https' || content.substring(u - 4, u) === 'http') {
|
||||
const m = content.substring(u + 4).match(noURLCharacter)
|
||||
} else if (content.slice(u - 5, u) === 'https' || content.slice(u - 4, u) === 'http') {
|
||||
const m = content.slice(u + 4).match(noURLCharacter)
|
||||
const end = m ? u + 4 + m.index! : max
|
||||
const prefixLen = content[u - 1] === 's' ? 5 : 4
|
||||
try {
|
||||
let url = new URL(content.substring(u - prefixLen, end))
|
||||
let url = new URL(content.slice(u - prefixLen, end))
|
||||
if (url.hostname.indexOf('.') === -1) {
|
||||
throw new Error('invalid url')
|
||||
}
|
||||
|
||||
if (prevIndex !== u - prefixLen) {
|
||||
yield { type: 'text', text: content.substring(prevIndex, u - prefixLen) }
|
||||
yield { type: 'text', text: content.slice(prevIndex, u - prefixLen) }
|
||||
}
|
||||
|
||||
if (/\.(png|jpe?g|gif|webp)$/i.test(url.pathname)) {
|
||||
if (/\.(png|jpe?g|gif|webp|heic|svg)$/i.test(url.pathname)) {
|
||||
yield { type: 'image', url: url.toString() }
|
||||
index = end
|
||||
prevIndex = index
|
||||
continue
|
||||
continue mainloop
|
||||
}
|
||||
if (/\.(mp4|avi|webm|mkv)$/i.test(url.pathname)) {
|
||||
if (/\.(mp4|avi|webm|mkv|mov)$/i.test(url.pathname)) {
|
||||
yield { type: 'video', url: url.toString() }
|
||||
index = end
|
||||
prevIndex = index
|
||||
continue
|
||||
continue mainloop
|
||||
}
|
||||
if (/\.(mp3|aac|ogg|opus)$/i.test(url.pathname)) {
|
||||
if (/\.(mp3|aac|ogg|opus|wav|flac)$/i.test(url.pathname)) {
|
||||
yield { type: 'audio', url: url.toString() }
|
||||
index = end
|
||||
prevIndex = index
|
||||
continue
|
||||
continue mainloop
|
||||
}
|
||||
|
||||
yield { type: 'url', url: url.toString() }
|
||||
index = end
|
||||
prevIndex = index
|
||||
continue
|
||||
continue mainloop
|
||||
} catch (_err) {
|
||||
// ignore this, not a valid url
|
||||
index = end + 1
|
||||
continue
|
||||
continue mainloop
|
||||
}
|
||||
} else if (content.substring(u - 3, u) === 'wss' || content.substring(u - 2, u) === 'ws') {
|
||||
const m = content.substring(u + 4).match(noURLCharacter)
|
||||
} else if (content.slice(u - 3, u) === 'wss' || content.slice(u - 2, u) === 'ws') {
|
||||
const m = content.slice(u + 4).match(noURLCharacter)
|
||||
const end = m ? u + 4 + m.index! : max
|
||||
const prefixLen = content[u - 1] === 's' ? 3 : 2
|
||||
try {
|
||||
let url = new URL(content.substring(u - prefixLen, end))
|
||||
let url = new URL(content.slice(u - prefixLen, end))
|
||||
if (url.hostname.indexOf('.') === -1) {
|
||||
throw new Error('invalid ws url')
|
||||
}
|
||||
|
||||
if (prevIndex !== u - prefixLen) {
|
||||
yield { type: 'text', text: content.substring(prevIndex, u - prefixLen) }
|
||||
yield { type: 'text', text: content.slice(prevIndex, u - prefixLen) }
|
||||
}
|
||||
yield { type: 'relay', url: url.toString() }
|
||||
index = end
|
||||
prevIndex = index
|
||||
continue
|
||||
continue mainloop
|
||||
} catch (_err) {
|
||||
// ignore this, not a valid url
|
||||
index = end + 1
|
||||
continue
|
||||
continue mainloop
|
||||
}
|
||||
} else {
|
||||
// try to parse an emoji shortcode
|
||||
for (let e = 0; e < emojis.length; e++) {
|
||||
const emoji = emojis[e]
|
||||
if (
|
||||
content[u + emoji.shortcode.length + 1] === ':' &&
|
||||
content.slice(u + 1, u + emoji.shortcode.length + 1) === emoji.shortcode
|
||||
) {
|
||||
// found an emoji
|
||||
if (prevIndex !== u) {
|
||||
yield { type: 'text', text: content.slice(prevIndex, u) }
|
||||
}
|
||||
yield emoji
|
||||
index = u + emoji.shortcode.length + 2
|
||||
prevIndex = index
|
||||
continue mainloop
|
||||
}
|
||||
}
|
||||
|
||||
// ignore this, it is nothing
|
||||
index = u + 1
|
||||
continue
|
||||
continue mainloop
|
||||
}
|
||||
}
|
||||
|
||||
if (prevIndex !== max) {
|
||||
yield { type: 'text', text: content.substring(prevIndex) }
|
||||
yield { type: 'text', text: content.slice(prevIndex) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, test, expect } from 'bun:test'
|
||||
import { hexToBytes } from '@noble/hashes/utils'
|
||||
import { hexToBytes } from '@noble/hashes/utils.js'
|
||||
import { getPublicKey } from './pure.ts'
|
||||
import * as Kind from './kinds.ts'
|
||||
import {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { test, expect } from 'bun:test'
|
||||
import { v2 } from './nip44.js'
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils'
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'
|
||||
import { default as vec } from './nip44.vectors.json' with { type: 'json' }
|
||||
import { schnorr } from '@noble/curves/secp256k1'
|
||||
import { schnorr } from '@noble/curves/secp256k1.js'
|
||||
const v2vec = vec.v2
|
||||
|
||||
test('get_conversation_key', () => {
|
||||
@@ -14,7 +14,7 @@ test('get_conversation_key', () => {
|
||||
|
||||
test('encrypt_decrypt', () => {
|
||||
for (const v of v2vec.valid.encrypt_decrypt) {
|
||||
const pub2 = bytesToHex(schnorr.getPublicKey(v.sec2))
|
||||
const pub2 = bytesToHex(schnorr.getPublicKey(hexToBytes(v.sec2)))
|
||||
const key = v2.utils.getConversationKey(hexToBytes(v.sec1), pub2)
|
||||
expect(bytesToHex(key)).toEqual(v.conversation_key)
|
||||
const ciphertext = v2.encrypt(v.plaintext, key, hexToBytes(v.nonce))
|
||||
@@ -40,7 +40,7 @@ test('decrypt', async () => {
|
||||
test('get_conversation_key', async () => {
|
||||
for (const v of v2vec.invalid.get_conversation_key) {
|
||||
expect(() => v2.utils.getConversationKey(hexToBytes(v.sec1), v.pub2)).toThrow(
|
||||
/(Point is not on curve|Cannot find square root)/,
|
||||
/(Point is not on curve|Cannot find square root|invalid field element)/,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
18
nip44.ts
18
nip44.ts
@@ -1,10 +1,10 @@
|
||||
import { chacha20 } from '@noble/ciphers/chacha'
|
||||
import { equalBytes } from '@noble/ciphers/utils'
|
||||
import { secp256k1 } from '@noble/curves/secp256k1'
|
||||
import { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf'
|
||||
import { hmac } from '@noble/hashes/hmac'
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { concatBytes, randomBytes } from '@noble/hashes/utils'
|
||||
import { chacha20 } from '@noble/ciphers/chacha.js'
|
||||
import { equalBytes } from '@noble/ciphers/utils.js'
|
||||
import { secp256k1 } from '@noble/curves/secp256k1.js'
|
||||
import { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf.js'
|
||||
import { hmac } from '@noble/hashes/hmac.js'
|
||||
import { sha256 } from '@noble/hashes/sha2.js'
|
||||
import { concatBytes, hexToBytes, randomBytes } from '@noble/hashes/utils.js'
|
||||
import { base64 } from '@scure/base'
|
||||
|
||||
import { utf8Decoder, utf8Encoder } from './utils.ts'
|
||||
@@ -13,8 +13,8 @@ const minPlaintextSize = 0x0001 // 1b msg => padded to 32b
|
||||
const maxPlaintextSize = 0xffff // 65535 (64kb-1) => padded to 64kb
|
||||
|
||||
export function getConversationKey(privkeyA: Uint8Array, pubkeyB: string): Uint8Array {
|
||||
const sharedX = secp256k1.getSharedSecret(privkeyA, '02' + pubkeyB).subarray(1, 33)
|
||||
return hkdf_extract(sha256, sharedX, 'nip44-v2')
|
||||
const sharedX = secp256k1.getSharedSecret(privkeyA, hexToBytes('02' + pubkeyB)).subarray(1, 33)
|
||||
return hkdf_extract(sha256, sharedX, utf8Encoder.encode('nip44-v2'))
|
||||
}
|
||||
|
||||
function getMessageKeys(
|
||||
|
||||
157
nip46.ts
157
nip46.ts
@@ -87,31 +87,7 @@ export type NostrConnectParams = {
|
||||
image?: string
|
||||
}
|
||||
|
||||
export type ParsedNostrConnectURI = {
|
||||
protocol: 'nostrconnect'
|
||||
clientPubkey: string
|
||||
params: {
|
||||
relays: string[]
|
||||
secret: string
|
||||
perms?: string[]
|
||||
name?: string
|
||||
url?: string
|
||||
image?: string
|
||||
}
|
||||
originalString: string
|
||||
}
|
||||
|
||||
export function createNostrConnectURI(params: NostrConnectParams): string {
|
||||
if (!params.clientPubkey) {
|
||||
throw new Error('clientPubkey is required.')
|
||||
}
|
||||
if (!params.relays || params.relays.length === 0) {
|
||||
throw new Error('At least one relay is required.')
|
||||
}
|
||||
if (!params.secret) {
|
||||
throw new Error('secret is required.')
|
||||
}
|
||||
|
||||
const queryParams = new URLSearchParams()
|
||||
|
||||
params.relays.forEach(relay => {
|
||||
@@ -136,55 +112,6 @@ export function createNostrConnectURI(params: NostrConnectParams): string {
|
||||
return `nostrconnect://${params.clientPubkey}?${queryParams.toString()}`
|
||||
}
|
||||
|
||||
export function parseNostrConnectURI(uri: string): ParsedNostrConnectURI {
|
||||
if (!uri.startsWith('nostrconnect://')) {
|
||||
throw new Error('Invalid nostrconnect URI: Must start with "nostrconnect://".')
|
||||
}
|
||||
|
||||
const [protocolAndPubkey, queryString] = uri.split('?')
|
||||
if (!protocolAndPubkey || !queryString) {
|
||||
throw new Error('Invalid nostrconnect URI: Missing query string.')
|
||||
}
|
||||
|
||||
const clientPubkey = protocolAndPubkey.substring('nostrconnect://'.length)
|
||||
if (!clientPubkey) {
|
||||
throw new Error('Invalid nostrconnect URI: Missing client-pubkey.')
|
||||
}
|
||||
|
||||
const queryParams = new URLSearchParams(queryString)
|
||||
|
||||
const relays = queryParams.getAll('relay')
|
||||
if (relays.length === 0) {
|
||||
throw new Error('Invalid nostrconnect URI: Missing "relay" parameter.')
|
||||
}
|
||||
|
||||
const secret = queryParams.get('secret')
|
||||
if (!secret) {
|
||||
throw new Error('Invalid nostrconnect URI: Missing "secret" parameter.')
|
||||
}
|
||||
|
||||
const permsString = queryParams.get('perms')
|
||||
const perms = permsString ? permsString.split(',') : undefined
|
||||
|
||||
const name = queryParams.get('name') || undefined
|
||||
const url = queryParams.get('url') || undefined
|
||||
const image = queryParams.get('image') || undefined
|
||||
|
||||
return {
|
||||
protocol: 'nostrconnect',
|
||||
clientPubkey,
|
||||
params: {
|
||||
relays,
|
||||
secret,
|
||||
perms,
|
||||
name,
|
||||
url,
|
||||
image,
|
||||
},
|
||||
originalString: uri,
|
||||
}
|
||||
}
|
||||
|
||||
export type BunkerSignerParams = {
|
||||
pool?: AbstractSimplePool
|
||||
onauth?: (url: string) => void
|
||||
@@ -238,7 +165,7 @@ export class BunkerSigner implements Signer {
|
||||
params: BunkerSignerParams = {},
|
||||
): BunkerSigner {
|
||||
if (bp.relays.length === 0) {
|
||||
throw new Error('No relays specified for this bunker')
|
||||
throw new Error('no relays specified for this bunker')
|
||||
}
|
||||
|
||||
const signer = new BunkerSigner(clientSecretKey, params)
|
||||
@@ -246,7 +173,7 @@ export class BunkerSigner implements Signer {
|
||||
signer.conversationKey = getConversationKey(clientSecretKey, bp.pubkey)
|
||||
signer.bp = bp
|
||||
|
||||
signer.setupSubscription(params)
|
||||
signer.setupSubscription()
|
||||
return signer
|
||||
}
|
||||
|
||||
@@ -257,22 +184,22 @@ export class BunkerSigner implements Signer {
|
||||
public static async fromURI(
|
||||
clientSecretKey: Uint8Array,
|
||||
connectionURI: string,
|
||||
params: BunkerSignerParams = {},
|
||||
maxWait: number = 300_000,
|
||||
bunkerParams: BunkerSignerParams = {},
|
||||
maxWaitOrAbort: number | AbortSignal = 300_000,
|
||||
): Promise<BunkerSigner> {
|
||||
const signer = new BunkerSigner(clientSecretKey, params)
|
||||
const parsedURI = parseNostrConnectURI(connectionURI)
|
||||
const signer = new BunkerSigner(clientSecretKey, bunkerParams)
|
||||
const uri = new URL(connectionURI)
|
||||
const clientPubkey = getPublicKey(clientSecretKey)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
sub.close()
|
||||
reject(new Error(`Connection timed out after ${maxWait / 1000} seconds`))
|
||||
}, maxWait)
|
||||
|
||||
let success = false
|
||||
const sub = signer.pool.subscribe(
|
||||
parsedURI.params.relays,
|
||||
{ kinds: [NostrConnect], '#p': [clientPubkey] },
|
||||
uri.searchParams.getAll('relay'),
|
||||
{
|
||||
kinds: [NostrConnect],
|
||||
'#p': [clientPubkey],
|
||||
limit: 0,
|
||||
},
|
||||
{
|
||||
onevent: async (event: NostrEvent) => {
|
||||
try {
|
||||
@@ -281,41 +208,48 @@ export class BunkerSigner implements Signer {
|
||||
|
||||
const response = JSON.parse(decryptedContent)
|
||||
|
||||
if (response.result === parsedURI.params.secret) {
|
||||
clearTimeout(timer)
|
||||
if (response.result === uri.searchParams.get('secret')) {
|
||||
sub.close()
|
||||
|
||||
signer.bp = {
|
||||
pubkey: event.pubkey,
|
||||
relays: parsedURI.params.relays,
|
||||
secret: parsedURI.params.secret,
|
||||
relays: uri.searchParams.getAll('relay'),
|
||||
secret: uri.searchParams.get('secret'),
|
||||
}
|
||||
signer.conversationKey = getConversationKey(clientSecretKey, event.pubkey)
|
||||
signer.setupSubscription(params)
|
||||
signer.setupSubscription()
|
||||
|
||||
success = true
|
||||
await Promise.race([new Promise(resolve => setTimeout(resolve, 1000)), signer.switchRelays()])
|
||||
resolve(signer)
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Failed to process potential connection event', e)
|
||||
console.warn('failed to process potential connection event', e)
|
||||
}
|
||||
},
|
||||
onclose: () => {
|
||||
clearTimeout(timer)
|
||||
reject(new Error('Subscription closed before connection was established.'))
|
||||
if (!success) reject(new Error('subscription closed before connection was established.'))
|
||||
},
|
||||
maxWait,
|
||||
maxWait: typeof maxWaitOrAbort === 'number' ? maxWaitOrAbort : undefined,
|
||||
abort: typeof maxWaitOrAbort !== 'number' ? maxWaitOrAbort : undefined,
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
private setupSubscription(params: BunkerSignerParams) {
|
||||
private setupSubscription() {
|
||||
const listeners = this.listeners
|
||||
const waitingForAuth = this.waitingForAuth
|
||||
const convKey = this.conversationKey
|
||||
|
||||
this.subCloser = this.pool.subscribe(
|
||||
this.bp.relays,
|
||||
{ kinds: [NostrConnect], authors: [this.bp.pubkey], '#p': [getPublicKey(this.secretKey)] },
|
||||
{
|
||||
kinds: [NostrConnect],
|
||||
authors: [this.bp.pubkey],
|
||||
'#p': [getPublicKey(this.secretKey)],
|
||||
limit: 0,
|
||||
},
|
||||
{
|
||||
onevent: async (event: NostrEvent) => {
|
||||
const o = JSON.parse(decrypt(event.content, convKey))
|
||||
@@ -324,8 +258,8 @@ export class BunkerSigner implements Signer {
|
||||
if (result === 'auth_url' && waitingForAuth[id]) {
|
||||
delete waitingForAuth[id]
|
||||
|
||||
if (params.onauth) {
|
||||
params.onauth(error)
|
||||
if (this.params.onauth) {
|
||||
this.params.onauth(error)
|
||||
} else {
|
||||
console.warn(
|
||||
`nostr-tools/nip46: remote signer ${this.bp.pubkey} tried to send an "auth_url"='${error}' but there was no onauth() callback configured.`,
|
||||
@@ -349,6 +283,27 @@ export class BunkerSigner implements Signer {
|
||||
this.isOpen = true
|
||||
}
|
||||
|
||||
async switchRelays(): Promise<boolean> {
|
||||
try {
|
||||
const switchResp = await this.sendRequest('switch_relays', [])
|
||||
let relays = JSON.parse(switchResp) as string[] | null
|
||||
if (!relays) return false
|
||||
if (JSON.stringify(relays.sort()) === JSON.stringify(this.bp.relays)) return false
|
||||
|
||||
this.bp.relays = relays
|
||||
let previousCloser = this.subCloser!
|
||||
setTimeout(() => {
|
||||
previousCloser.close()
|
||||
}, 5000)
|
||||
|
||||
this.subCloser = undefined
|
||||
this.setupSubscription()
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// closes the subscription -- this object can't be used anymore after this
|
||||
async close() {
|
||||
this.isOpen = false
|
||||
@@ -359,7 +314,7 @@ export class BunkerSigner implements Signer {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
if (!this.isOpen) throw new Error('this signer is not open anymore, create a new one')
|
||||
if (!this.subCloser) this.setupSubscription(this.params)
|
||||
if (!this.subCloser) this.setupSubscription()
|
||||
|
||||
this.serial++
|
||||
const id = `${this.idPrefix}-${this.serial}`
|
||||
@@ -469,7 +424,7 @@ export async function createAccount(
|
||||
email?: string,
|
||||
localSecretKey: Uint8Array = generateSecretKey(),
|
||||
): Promise<BunkerSigner> {
|
||||
if (email && !EMAIL_REGEX.test(email)) throw new Error('Invalid email')
|
||||
if (email && !EMAIL_REGEX.test(email)) throw new Error('invalid email')
|
||||
|
||||
let rpc = BunkerSigner.fromBunker(localSecretKey, bunker.bunkerPointer, params)
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, test, expect } from 'bun:test'
|
||||
import { hexToBytes } from '@noble/hashes/utils'
|
||||
import { hexToBytes } from '@noble/hashes/utils.js'
|
||||
import { makeNwcRequestEvent, parseConnectionString } from './nip47.ts'
|
||||
import { decrypt } from './nip04.ts'
|
||||
import { NWCWalletRequest } from './kinds.ts'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { test, expect } from 'bun:test'
|
||||
import { decrypt, encrypt } from './nip49.ts'
|
||||
import { hexToBytes } from '@noble/hashes/utils'
|
||||
import { hexToBytes } from '@noble/hashes/utils.js'
|
||||
|
||||
test('encrypt and decrypt', () => {
|
||||
for (let i = 0; i < vectors.length; i++) {
|
||||
|
||||
10
nip49.ts
10
nip49.ts
@@ -1,8 +1,8 @@
|
||||
import { scrypt } from '@noble/hashes/scrypt'
|
||||
import { xchacha20poly1305 } from '@noble/ciphers/chacha'
|
||||
import { concatBytes, randomBytes } from '@noble/hashes/utils'
|
||||
import { Bech32MaxSize, Ncryptsec, encodeBytes } from './nip19.ts'
|
||||
import { bech32 } from '@scure/base'
|
||||
import { scrypt } from '@noble/hashes/scrypt.js'
|
||||
import { xchacha20poly1305 } from '@noble/ciphers/chacha.js'
|
||||
import { concatBytes, randomBytes } from '@noble/hashes/utils.js'
|
||||
import { Bech32MaxSize, Ncryptsec, encodeBytes } from './nip19.ts'
|
||||
|
||||
export function encrypt(
|
||||
sec: Uint8Array,
|
||||
@@ -22,7 +22,7 @@ export function encrypt(
|
||||
}
|
||||
|
||||
export function decrypt(ncryptsec: string, password: string): Uint8Array {
|
||||
let { prefix, words } = bech32.decode(ncryptsec, Bech32MaxSize)
|
||||
let { prefix, words } = bech32.decode(ncryptsec as `${string}1${string}`, Bech32MaxSize)
|
||||
if (prefix !== 'ncryptsec') {
|
||||
throw new Error(`invalid prefix ${prefix}, expected 'ncryptsec'`)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { decode } from './nip19.ts'
|
||||
import { NostrEvent, getPublicKey } from './pure.ts'
|
||||
import { SimplePool } from './pool.ts'
|
||||
import { GiftWrap } from './kinds.ts'
|
||||
import { hexToBytes } from '@noble/hashes/utils'
|
||||
import { hexToBytes } from '@noble/hashes/utils.js'
|
||||
|
||||
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data as Uint8Array
|
||||
const recipientPrivateKey = decode(`nsec1uyyrnx7cgfp40fcskcr2urqnzekc20fj0er6de0q8qvhx34ahazsvs9p36`).data as Uint8Array
|
||||
|
||||
114
nip77.test.ts
Normal file
114
nip77.test.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { describe, test, expect } from 'bun:test'
|
||||
import { NegentropySync, NegentropyStorageVector } from './nip77.ts'
|
||||
import { Relay } from './relay.ts'
|
||||
import { NostrEvent } from './core.ts'
|
||||
|
||||
// const RELAY = 'ws://127.0.0.1:10547'
|
||||
const RELAY = 'wss://relay.damus.io'
|
||||
|
||||
describe('NegentropySync', () => {
|
||||
test('syncs events from ' + RELAY, async () => {
|
||||
const relay = await Relay.connect(RELAY)
|
||||
|
||||
const storage = new NegentropyStorageVector()
|
||||
storage.seal()
|
||||
const filter = {
|
||||
authors: ['3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'],
|
||||
kinds: [30617, 30618],
|
||||
}
|
||||
|
||||
let ids1: string[] = []
|
||||
const done1 = Promise.withResolvers<void>()
|
||||
const sync1 = new NegentropySync(relay, storage, filter, {
|
||||
onneed: (id: string) => {
|
||||
ids1.push(id)
|
||||
},
|
||||
onclose: err => {
|
||||
expect(err).toBeUndefined()
|
||||
done1.resolve()
|
||||
},
|
||||
})
|
||||
|
||||
await sync1.start()
|
||||
await done1.promise
|
||||
|
||||
expect(ids1.length).toBeGreaterThan(10)
|
||||
|
||||
sync1.close()
|
||||
|
||||
// fetch events
|
||||
const events1: NostrEvent[] = []
|
||||
const fetched = Promise.withResolvers()
|
||||
const sub = relay.subscribe([{ ids: ids1 }], {
|
||||
onevent(evt) {
|
||||
events1.push(evt)
|
||||
},
|
||||
oneose() {
|
||||
sub.close()
|
||||
fetched.resolve()
|
||||
},
|
||||
})
|
||||
await fetched.promise
|
||||
expect(events1.map(evt => evt.id).sort()).toEqual(ids1.sort())
|
||||
|
||||
// Second sync with local events
|
||||
await relay.connect()
|
||||
|
||||
const storage2 = new NegentropyStorageVector()
|
||||
for (const evt of events1) {
|
||||
storage2.insert(evt.created_at, evt.id)
|
||||
}
|
||||
storage2.seal()
|
||||
|
||||
let ids2: string[] = []
|
||||
let done2 = Promise.withResolvers()
|
||||
const sync2 = new NegentropySync(relay, storage2, filter, {
|
||||
onneed: (id: string) => {
|
||||
ids2.push(id)
|
||||
},
|
||||
onclose: err => {
|
||||
expect(err).toBeUndefined()
|
||||
done2.resolve()
|
||||
},
|
||||
})
|
||||
|
||||
await sync2.start()
|
||||
await done2.promise
|
||||
|
||||
expect(ids2.length).toBe(0)
|
||||
|
||||
sync2.close()
|
||||
|
||||
// third sync with 4 events removed
|
||||
const storage3 = new NegentropyStorageVector()
|
||||
|
||||
// shuffle
|
||||
ids1.sort(() => Math.random() - 0.5)
|
||||
const removedEvents = ids1.slice(0, 1 + Math.floor(Math.random() * ids1.length - 1))
|
||||
for (const evt of events1) {
|
||||
if (!removedEvents.includes(evt.id)) {
|
||||
storage3.insert(evt.created_at, evt.id)
|
||||
}
|
||||
}
|
||||
storage3.seal()
|
||||
|
||||
let ids3: string[] = []
|
||||
const done3 = Promise.withResolvers()
|
||||
const sync3 = new NegentropySync(relay, storage3, filter, {
|
||||
onneed: (id: string) => {
|
||||
ids3.push(id)
|
||||
},
|
||||
onclose: err => {
|
||||
expect(err).toBeUndefined()
|
||||
done3.resolve()
|
||||
},
|
||||
})
|
||||
|
||||
await sync3.start()
|
||||
await done3.promise
|
||||
|
||||
expect(ids3.sort()).toEqual(removedEvents.sort())
|
||||
|
||||
sync3.close()
|
||||
})
|
||||
})
|
||||
16
nip77.ts
16
nip77.ts
@@ -1,7 +1,7 @@
|
||||
import { bytesToHex, hexToBytes } from '@noble/ciphers/utils'
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'
|
||||
import { Filter } from './filter.ts'
|
||||
import { AbstractRelay, Subscription } from './relay.ts'
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { sha256 } from '@noble/hashes/sha2.js'
|
||||
|
||||
// Negentropy implementation by Doug Hoyte
|
||||
const PROTOCOL_VERSION = 0x61 // Version 1
|
||||
@@ -537,6 +537,7 @@ export class NegentropySync {
|
||||
relay: AbstractRelay
|
||||
storage: NegentropyStorageVector
|
||||
private neg: Negentropy
|
||||
private filter: Filter
|
||||
private subscription: Subscription
|
||||
private onhave?: (id: string) => void
|
||||
private onneed?: (id: string) => void
|
||||
@@ -557,8 +558,10 @@ export class NegentropySync {
|
||||
this.neg = new Negentropy(storage)
|
||||
this.onhave = params.onhave
|
||||
this.onneed = params.onneed
|
||||
this.filter = filter
|
||||
|
||||
this.subscription = this.relay.prepareSubscription([filter], { label: params.label || 'negentropy' })
|
||||
// we prepare a subscription with an empty filter, but it will not be used
|
||||
this.subscription = this.relay.prepareSubscription([{}], { label: params.label || 'negentropy' })
|
||||
this.subscription.oncustom = (data: string[]) => {
|
||||
switch (data[0]) {
|
||||
case 'NEG-MSG': {
|
||||
@@ -569,6 +572,9 @@ export class NegentropySync {
|
||||
const response = this.neg.reconcile(data[2], this.onhave, this.onneed)
|
||||
if (response) {
|
||||
this.relay.send(`["NEG-MSG", "${this.subscription.id}", "${response}"]`)
|
||||
} else {
|
||||
this.close()
|
||||
params.onclose?.()
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('negentropy reconcile error:', error)
|
||||
@@ -591,9 +597,7 @@ export class NegentropySync {
|
||||
|
||||
async start(): Promise<void> {
|
||||
const initMsg = this.neg.initiate()
|
||||
if (initMsg) {
|
||||
this.relay.send(`["NEG-OPEN","${this.subscription.id}",${initMsg}]`)
|
||||
}
|
||||
this.relay.send(`["NEG-OPEN","${this.subscription.id}",${JSON.stringify(this.filter)},"${initMsg}"]`)
|
||||
}
|
||||
|
||||
close(): void {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { bytesToHex } from '@noble/hashes/utils'
|
||||
import { sha256 } from '@noble/hashes/sha2.js'
|
||||
import { bytesToHex } from '@noble/hashes/utils.js'
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
|
||||
import { HTTPAuth } from './kinds.ts'
|
||||
|
||||
4
nip98.ts
4
nip98.ts
@@ -1,5 +1,5 @@
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { bytesToHex } from '@noble/hashes/utils'
|
||||
import { sha256 } from '@noble/hashes/sha2.js'
|
||||
import { bytesToHex } from '@noble/hashes/utils.js'
|
||||
import { base64 } from '@scure/base'
|
||||
|
||||
import { HTTPAuth } from './kinds.ts'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { test, expect } from 'bun:test'
|
||||
import { BlossomClient } from './nipb7.ts'
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { sha256 } from '@noble/hashes/sha2.js'
|
||||
import { bytesToHex } from './utils.ts'
|
||||
import { PlainKeySigner } from './signer.ts'
|
||||
import { generateSecretKey } from './pure.ts'
|
||||
|
||||
2
nipb7.ts
2
nipb7.ts
@@ -1,4 +1,4 @@
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { sha256 } from '@noble/hashes/sha2.js'
|
||||
import { EventTemplate } from './core.ts'
|
||||
import { Signer } from './signer.ts'
|
||||
import { bytesToHex } from './utils.ts'
|
||||
|
||||
14
package.json
14
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"type": "module",
|
||||
"name": "nostr-tools",
|
||||
"version": "2.17.3",
|
||||
"version": "2.22.1",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -236,12 +236,12 @@
|
||||
},
|
||||
"license": "Unlicense",
|
||||
"dependencies": {
|
||||
"@noble/ciphers": "^0.5.1",
|
||||
"@noble/curves": "1.2.0",
|
||||
"@noble/hashes": "1.3.1",
|
||||
"@scure/base": "1.1.1",
|
||||
"@scure/bip32": "1.3.1",
|
||||
"@scure/bip39": "1.2.1",
|
||||
"@noble/ciphers": "2.1.1",
|
||||
"@noble/curves": "2.0.1",
|
||||
"@noble/hashes": "2.0.1",
|
||||
"@scure/base": "2.0.0",
|
||||
"@scure/bip32": "2.0.1",
|
||||
"@scure/bip39": "2.0.1",
|
||||
"nostr-wasm": "0.1.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { afterEach, beforeEach, expect, test } from 'bun:test'
|
||||
import { SimplePool, useWebSocketImplementation } from './pool.ts'
|
||||
import { finalizeEvent, generateSecretKey, getPublicKey, type Event } from './pure.ts'
|
||||
import { MockRelay, MockWebSocketClient } from './test-helpers.ts'
|
||||
import { hexToBytes } from '@noble/hashes/utils'
|
||||
import { hexToBytes } from '@noble/hashes/utils.js'
|
||||
|
||||
useWebSocketImplementation(MockWebSocketClient)
|
||||
|
||||
@@ -306,12 +306,9 @@ test('reconnect on disconnect in pool', async () => {
|
||||
|
||||
test('reconnect with filter update in pool', async () => {
|
||||
const mockRelay = mockRelays[0]
|
||||
const newSince = Math.floor(Date.now() / 1000)
|
||||
pool = new SimplePool({
|
||||
enablePing: true,
|
||||
enableReconnect: filters => {
|
||||
return filters.map(f => ({ ...f, since: newSince }))
|
||||
},
|
||||
enableReconnect: true,
|
||||
})
|
||||
const relay = await pool.ensureRelay(mockRelay.url)
|
||||
relay.pingTimeout = 50
|
||||
@@ -364,7 +361,7 @@ test('reconnect with filter update in pool', async () => {
|
||||
expect(closes).toBe(1)
|
||||
|
||||
// check if filter was updated
|
||||
expect(sub.filters[0].since).toBe(newSince)
|
||||
expect(sub.filters[0].since).toBeGreaterThan(1)
|
||||
})
|
||||
|
||||
test('track relays when publishing', async () => {
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
generateSecretKey,
|
||||
} from './pure.ts'
|
||||
import { ShortTextNote } from './kinds.ts'
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils'
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'
|
||||
|
||||
test('private key generation', () => {
|
||||
expect(bytesToHex(generateSecretKey())).toMatch(/[a-f0-9]{64}/)
|
||||
|
||||
12
pure.ts
12
pure.ts
@@ -1,13 +1,13 @@
|
||||
import { schnorr } from '@noble/curves/secp256k1'
|
||||
import { bytesToHex } from '@noble/hashes/utils'
|
||||
import { schnorr } from '@noble/curves/secp256k1.js'
|
||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'
|
||||
import { Nostr, Event, EventTemplate, UnsignedEvent, VerifiedEvent, verifiedSymbol, validateEvent } from './core.ts'
|
||||
import { sha256 } from '@noble/hashes/sha256'
|
||||
import { sha256 } from '@noble/hashes/sha2.js'
|
||||
|
||||
import { utf8Encoder } from './utils.ts'
|
||||
|
||||
class JS implements Nostr {
|
||||
generateSecretKey(): Uint8Array {
|
||||
return schnorr.utils.randomPrivateKey()
|
||||
return schnorr.utils.randomSecretKey()
|
||||
}
|
||||
getPublicKey(secretKey: Uint8Array): string {
|
||||
return bytesToHex(schnorr.getPublicKey(secretKey))
|
||||
@@ -16,7 +16,7 @@ class JS implements Nostr {
|
||||
const event = t as VerifiedEvent
|
||||
event.pubkey = bytesToHex(schnorr.getPublicKey(secretKey))
|
||||
event.id = getEventHash(event)
|
||||
event.sig = bytesToHex(schnorr.sign(getEventHash(event), secretKey))
|
||||
event.sig = bytesToHex(schnorr.sign(hexToBytes(getEventHash(event)), secretKey))
|
||||
event[verifiedSymbol] = true
|
||||
return event
|
||||
}
|
||||
@@ -30,7 +30,7 @@ class JS implements Nostr {
|
||||
}
|
||||
|
||||
try {
|
||||
const valid = schnorr.verify(event.sig, hash, event.pubkey)
|
||||
const valid = schnorr.verify(hexToBytes(event.sig), hexToBytes(hash), hexToBytes(event.pubkey))
|
||||
event[verifiedSymbol] = valid
|
||||
return valid
|
||||
} catch (err) {
|
||||
|
||||
@@ -336,66 +336,3 @@ test('reconnect on disconnect', async () => {
|
||||
expect(relay.connected).toBeTrue()
|
||||
expect(closes).toBe(1) // should not have closed again
|
||||
})
|
||||
|
||||
test('reconnect with filter update', async () => {
|
||||
const mockRelay = new MockRelay()
|
||||
const newSince = Math.floor(Date.now() / 1000)
|
||||
const relay = new Relay(mockRelay.url, {
|
||||
enablePing: true,
|
||||
enableReconnect: filters => {
|
||||
return filters.map(f => ({ ...f, since: newSince }))
|
||||
},
|
||||
})
|
||||
relay.pingTimeout = 50
|
||||
relay.pingFrequency = 50
|
||||
relay.resubscribeBackoff = [50, 100]
|
||||
|
||||
let closes = 0
|
||||
relay.onclose = () => {
|
||||
closes++
|
||||
}
|
||||
|
||||
await relay.connect()
|
||||
expect(relay.connected).toBeTrue()
|
||||
|
||||
const sub = relay.subscribe([{ kinds: [1], since: 0 }], { onevent: () => {} })
|
||||
expect(sub.filters[0].since).toBe(0)
|
||||
|
||||
// wait for the first ping to succeed
|
||||
await new Promise(resolve => setTimeout(resolve, 75))
|
||||
expect(closes).toBe(0)
|
||||
|
||||
// now make it unresponsive
|
||||
mockRelay.unresponsive = true
|
||||
|
||||
// wait for the second ping to fail, which will trigger a close
|
||||
await new Promise(resolve => {
|
||||
const interval = setInterval(() => {
|
||||
if (closes > 0) {
|
||||
clearInterval(interval)
|
||||
resolve(null)
|
||||
}
|
||||
}, 10)
|
||||
})
|
||||
expect(closes).toBe(1)
|
||||
expect(relay.connected).toBeFalse()
|
||||
|
||||
// now make it responsive again
|
||||
mockRelay.unresponsive = false
|
||||
|
||||
// wait for reconnect
|
||||
await new Promise(resolve => {
|
||||
const interval = setInterval(() => {
|
||||
if (relay.connected) {
|
||||
clearInterval(interval)
|
||||
resolve(null)
|
||||
}
|
||||
}, 10)
|
||||
})
|
||||
|
||||
expect(relay.connected).toBeTrue()
|
||||
expect(closes).toBe(1)
|
||||
|
||||
// check if filter was updated
|
||||
expect(sub.filters[0].since).toBe(newSince)
|
||||
})
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import { describe, test, expect } from 'bun:test'
|
||||
import { buildEvent } from './test-helpers.ts'
|
||||
import { Queue, insertEventIntoAscendingList, insertEventIntoDescendingList, binarySearch } from './utils.ts'
|
||||
import {
|
||||
Queue,
|
||||
insertEventIntoAscendingList,
|
||||
insertEventIntoDescendingList,
|
||||
binarySearch,
|
||||
normalizeURL,
|
||||
} from './utils.ts'
|
||||
|
||||
import type { Event } from './core.ts'
|
||||
|
||||
@@ -263,3 +269,43 @@ test('binary search', () => {
|
||||
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('a' < b ? -1 : 'a' === b ? 0 : 1))).toEqual([0, true])
|
||||
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false])
|
||||
})
|
||||
|
||||
describe('normalizeURL', () => {
|
||||
test('normalizes wss:// URLs', () => {
|
||||
expect(normalizeURL('wss://example.com')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('wss://example.com/')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('wss://example.com//path')).toBe('wss://example.com/path')
|
||||
expect(normalizeURL('wss://example.com:443')).toBe('wss://example.com/')
|
||||
})
|
||||
|
||||
test('normalizes https:// URLs', () => {
|
||||
expect(normalizeURL('https://example.com')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('https://example.com/')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('http://example.com//path')).toBe('ws://example.com/path')
|
||||
})
|
||||
|
||||
test('normalizes ws:// URLs', () => {
|
||||
expect(normalizeURL('ws://example.com')).toBe('ws://example.com/')
|
||||
expect(normalizeURL('ws://example.com/')).toBe('ws://example.com/')
|
||||
expect(normalizeURL('ws://example.com//path')).toBe('ws://example.com/path')
|
||||
expect(normalizeURL('ws://example.com:80')).toBe('ws://example.com/')
|
||||
})
|
||||
|
||||
test('adds wss:// to URLs without scheme', () => {
|
||||
expect(normalizeURL('example.com')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('example.com/')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('example.com//path')).toBe('wss://example.com/path')
|
||||
})
|
||||
|
||||
test('handles query parameters', () => {
|
||||
expect(normalizeURL('wss://example.com?z=1&a=2')).toBe('wss://example.com/?a=2&z=1')
|
||||
})
|
||||
|
||||
test('removes hash', () => {
|
||||
expect(normalizeURL('wss://example.com#hash')).toBe('wss://example.com/')
|
||||
})
|
||||
|
||||
test('throws on invalid URL', () => {
|
||||
expect(() => normalizeURL('http://')).toThrow('Invalid URL: http://')
|
||||
})
|
||||
})
|
||||
|
||||
4
utils.ts
4
utils.ts
@@ -3,12 +3,14 @@ import type { Event } from './core.ts'
|
||||
export const utf8Decoder: TextDecoder = new TextDecoder('utf-8')
|
||||
export const utf8Encoder: TextEncoder = new TextEncoder()
|
||||
|
||||
export { bytesToHex, hexToBytes } from '@noble/hashes/utils'
|
||||
export { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'
|
||||
|
||||
export function normalizeURL(url: string): string {
|
||||
try {
|
||||
if (url.indexOf('://') === -1) url = 'wss://' + url
|
||||
let p = new URL(url)
|
||||
if (p.protocol === 'http:') p.protocol = 'ws:'
|
||||
else if (p.protocol === 'https:') p.protocol = 'wss:'
|
||||
p.pathname = p.pathname.replace(/\/+/g, '/')
|
||||
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
|
||||
if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = ''
|
||||
|
||||
Reference in New Issue
Block a user