Compare commits

...

11 Commits

Author SHA1 Message Date
fiatjaf
6fc7788a4f utils: merging two (reverse) sorted lists of events. 2026-02-01 08:44:49 -03:00
fiatjaf
2180c7a1fe add onRelayConnectionSuccess to pair with onRelayConnectionFailure. 2026-01-31 19:27:45 -03:00
fiatjaf
b4bec2097d finally stop reconnecting when the first connection fails once and for all. 2026-01-31 19:27:45 -03:00
fiatjaf
fb7de7f1aa prevent reconnections when initial connection fails. 2026-01-31 13:57:33 -03:00
fiatjaf
ccb9641fb9 pool: maxWaitForConnection parameter.
this was so obvious.
2026-01-31 00:27:55 -03:00
fiatjaf
b624ad4059 pool: hooks to notify when a relay fails to connect, then ask whether a connection should be attempted. 2026-01-30 17:35:46 -03:00
fiatjaf
b3d314643a make everything dependent on npm again. 2026-01-30 07:16:04 -03:00
fiatjaf
30ac8a02c2 readme to point only to jsr because npm is awful. 2026-01-28 17:28:27 -03:00
fiatjaf
42c9c7554d migrate to jsr dependencies for @noble and @scure. 2026-01-27 23:54:50 -03:00
fiatjaf
3588d30044 do the same for @noble/ciphers and @scure packages. 2026-01-27 22:43:54 -03:00
lemonknowsall
b40f59af74 Upgrade to @noble/curves ^2.0.1 and @noble/hashes ^2.0.1
This commit upgrades the noble cryptography dependencies to v2.0.1, which includes:

Breaking changes addressed:
- Updated all @noble imports to include .js extensions (required by v2 ESM-only API)
- Changed @noble/hashes/sha256 to @noble/hashes/sha2.js across 8 files
- Fixed secp256k1 API changes: methods now require Uint8Array instead of hex strings
- Updated schnorr.utils.randomPrivateKey() to schnorr.utils.randomSecretKey()

Files modified (27 total):
- package.json: Bump dependency versions
- Source files (12): pure.ts, nip04.ts, nip06.ts, nip13.ts, nip19.ts, nip44.ts,
  nip49.ts, nip77.ts, nip98.ts, nipb7.ts, utils.ts, wasm.ts
- Test files (14): All corresponding test files updated

Benefits:
- Latest security updates from audited noble libraries
- Smaller bundle sizes from v2 optimizations
- Future-proof ESM-only compatibility
- All tests passing

Co-authored-by: OpenCode <opencode@anomalyco.com>
2026-01-24 09:41:15 -03:00
34 changed files with 303 additions and 108 deletions

View File

@@ -1,4 +1,4 @@
# ![](https://img.shields.io/github/actions/workflow/status/nbd-wtf/nostr-tools/test.yml) [![JSR](https://jsr.io/badges/@nostr/tools)](https://jsr.io/@nostr/tools) nostr-tools # [![JSR](https://jsr.io/badges/@nostr/tools)](https://jsr.io/@nostr/tools) @nostr/tools
Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients. 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 ## Installation
```bash ```bash
# npm
npm install --save nostr-tools
# jsr # jsr
npx jsr add @nostr/tools npx jsr add @nostr/tools
``` ```
@@ -27,7 +24,7 @@ https://jsr.io/@nostr/tools/doc
### Generating a private key and a public key ### Generating a private key and a public key
```js ```js
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure' import { generateSecretKey, getPublicKey } from '@nostr/tools/pure'
let sk = generateSecretKey() // `sk` is a Uint8Array let sk = generateSecretKey() // `sk` is a Uint8Array
let pk = getPublicKey(sk) // `pk` is a hex string 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 To get the secret key in hex format, use
```js ```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 skHex = bytesToHex(sk)
let backToBytes = hexToBytes(skHex) let backToBytes = hexToBytes(skHex)
@@ -45,7 +42,7 @@ let backToBytes = hexToBytes(skHex)
### Creating, signing and verifying events ### Creating, signing and verifying events
```js ```js
import { finalizeEvent, verifyEvent } from 'nostr-tools/pure' import { finalizeEvent, verifyEvent } from '@nostr/tools/pure'
let event = finalizeEvent({ let event = finalizeEvent({
kind: 1, kind: 1,
@@ -62,8 +59,8 @@ let isGood = verifyEvent(event)
Doesn't matter what you do, you always should be using a `SimplePool`: Doesn't matter what you do, you always should be using a `SimplePool`:
```js ```js
import { finalizeEvent, generateSecretKey, getPublicKey } from 'nostr-tools/pure' import { finalizeEvent, generateSecretKey, getPublicKey } from '@nostr/tools/pure'
import { SimplePool } from 'nostr-tools/pool' import { SimplePool } from '@nostr/tools/pool'
const pool = new SimplePool() 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: To use this on Node.js you first must install `ws` and call something like this:
```js ```js
import { useWebSocketImplementation } from 'nostr-tools/pool' import { useWebSocketImplementation } from '@nostr/tools/pool'
// or import { useWebSocketImplementation } from 'nostr-tools/relay' if you're using the Relay directly // or import { useWebSocketImplementation } from '@nostr/tools/relay' if you're using the Relay directly
import WebSocket from 'ws' import WebSocket from 'ws'
useWebSocketImplementation(WebSocket) 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. 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 ```js
import { SimplePool } from 'nostr-tools/pool' import { SimplePool } from '@nostr/tools/pool'
const pool = new SimplePool({ enablePing: true }) 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. 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 ```js
import { SimplePool } from 'nostr-tools/pool' import { SimplePool } from '@nostr/tools/pool'
const pool = new SimplePool({ enableReconnect: true }) const pool = new SimplePool({ enableReconnect: true })
``` ```
@@ -331,7 +328,7 @@ for (let profile of refs.profiles) {
### Querying profile data from a NIP-05 address ### Querying profile data from a NIP-05 address
```js ```js
import { queryProfile } from 'nostr-tools/nip05' import { queryProfile } from '@nostr/tools/nip05'
let profile = await queryProfile('jb55.com') let profile = await queryProfile('jb55.com')
console.log(profile.pubkey) console.log(profile.pubkey)
@@ -343,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: To use this on Node.js < v18, you first must install `node-fetch@2` and call something like this:
```js ```js
import { useFetchImplementation } from 'nostr-tools/nip05' import { useFetchImplementation } from '@nostr/tools/nip05'
useFetchImplementation(require('node-fetch')) useFetchImplementation(require('node-fetch'))
``` ```
### Including NIP-07 types ### Including NIP-07 types
```js ```js
import type { WindowNostr } from 'nostr-tools/nip07' import type { WindowNostr } from '@nostr/tools/nip07'
declare global { declare global {
interface Window { interface Window {
@@ -361,8 +358,8 @@ declare global {
### Encoding and decoding NIP-19 codes ### Encoding and decoding NIP-19 codes
```js ```js
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure' import { generateSecretKey, getPublicKey } from '@nostr/tools/pure'
import * as nip19 from 'nostr-tools/nip19' import * as nip19 from '@nostr/tools/nip19'
let sk = generateSecretKey() let sk = generateSecretKey()
let nsec = nip19.nsecEncode(sk) let nsec = nip19.nsecEncode(sk)
@@ -390,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. [`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 ```js
import { setNostrWasm, generateSecretKey, finalizeEvent, verifyEvent } from 'nostr-tools/wasm' import { setNostrWasm, generateSecretKey, finalizeEvent, verifyEvent } from '@nostr/tools/wasm'
import { initNostrWasm } from 'nostr-wasm' import { initNostrWasm } from 'nostr-wasm'
// make sure this promise resolves before your app starts calling finalizeEvent or verifyEvent // make sure this promise resolves before your app starts calling finalizeEvent or verifyEvent
@@ -403,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`: 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 ```js
import { setNostrWasm, verifyEvent } from 'nostr-tools/wasm' import { setNostrWasm, verifyEvent } from '@nostr/tools/wasm'
import { AbstractRelay } from 'nostr-tools/abstract-relay' import { AbstractRelay } from '@nostr/tools/abstract-relay'
import { AbstractSimplePool } from 'nostr-tools/abstract-pool' import { AbstractSimplePool } from '@nostr/tools/abstract-pool'
import { initNostrWasm } from 'nostr-wasm' import { initNostrWasm } from 'nostr-wasm'
initNostrWasm().then(setNostrWasm) initNostrWasm().then(setNostrWasm)
@@ -442,7 +439,7 @@ summary for relay read message and verify event
## Plumbing ## 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 ## License

View File

@@ -11,6 +11,7 @@ import { normalizeURL } from './utils.ts'
import type { Event, EventTemplate, Nostr, VerifiedEvent } from './core.ts' import type { Event, EventTemplate, Nostr, VerifiedEvent } from './core.ts'
import { type Filter } from './filter.ts' import { type Filter } from './filter.ts'
import { alwaysTrue } from './helpers.ts' import { alwaysTrue } from './helpers.ts'
import { Relay } from './relay.ts'
export type SubCloser = { close: (reason?: string) => void } export type SubCloser = { close: (reason?: string) => void }
@@ -19,6 +20,16 @@ export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
// in case that relay shouldn't be authenticated against // 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) // 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>) 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
// onRelayConnectionSuccess is called with the URL of a relay that succeeds the initial connection
onRelayConnectionSuccess?: (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
// maxWaitForConnection takes a number in milliseconds that will be given to ensureRelay such that we
// don't get stuck forever when attempting to connect to a relay, it is 3000 (3 seconds) by default
maxWaitForConnection: number
} }
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & { export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
@@ -40,6 +51,10 @@ export class AbstractSimplePool {
public enableReconnect: boolean public enableReconnect: boolean
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>) public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
public trustedRelayURLs: Set<string> = new Set() public trustedRelayURLs: Set<string> = new Set()
public onRelayConnectionFailure?: (url: string) => void
public onRelayConnectionSuccess?: (url: string) => void
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
public maxWaitForConnection: number
private _WebSocket?: typeof WebSocket private _WebSocket?: typeof WebSocket
@@ -49,6 +64,10 @@ export class AbstractSimplePool {
this.enablePing = opts.enablePing this.enablePing = opts.enablePing
this.enableReconnect = opts.enableReconnect || false this.enableReconnect = opts.enableReconnect || false
this.automaticallyAuth = opts.automaticallyAuth this.automaticallyAuth = opts.automaticallyAuth
this.onRelayConnectionFailure = opts.onRelayConnectionFailure
this.onRelayConnectionSuccess = opts.onRelayConnectionSuccess
this.allowConnectingToRelay = opts.allowConnectingToRelay
this.maxWaitForConnection = opts.maxWaitForConnection || 3000
} }
async ensureRelay( async ensureRelay(
@@ -181,17 +200,28 @@ 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(
groupedRequests.map(async ({ url, filters }, i) => { groupedRequests.map(async ({ url, filters }, i) => {
if (this.allowConnectingToRelay?.(url, ['read', filters]) === false) {
handleClose(i, 'connection skipped by allowConnectingToRelay')
return
}
let relay: AbstractRelay let relay: AbstractRelay
try { try {
relay = await this.ensureRelay(url, { relay = await this.ensureRelay(url, {
connectionTimeout: params.maxWait ? Math.max(params.maxWait * 0.8, params.maxWait - 1000) : undefined, connectionTimeout:
this.maxWaitForConnection < (params.maxWait || 0)
? Math.max(params.maxWait! * 0.8, params.maxWait! - 1000)
: this.maxWaitForConnection,
abort: params.abort, abort: params.abort,
}) })
} catch (err) { } catch (err) {
this.onRelayConnectionFailure?.(url)
handleClose(i, (err as any)?.message || String(err)) handleClose(i, (err as any)?.message || String(err))
return return
} }
this.onRelayConnectionSuccess?.(url)
let subscription = relay.subscribe(filters, { let subscription = relay.subscribe(filters, {
...params, ...params,
oneose: () => handleEose(i), oneose: () => handleEose(i),
@@ -298,7 +328,11 @@ export class AbstractSimplePool {
publish( publish(
relays: string[], relays: string[],
event: Event, event: Event,
options?: { onauth?: (evt: EventTemplate) => Promise<VerifiedEvent> }, params?: {
onauth?: (evt: EventTemplate) => Promise<VerifiedEvent>
maxWait?: number
abort?: AbortSignal
},
): Promise<string>[] { ): 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) {
@@ -306,12 +340,29 @@ export class AbstractSimplePool {
return Promise.reject('duplicate url') 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, {
connectionTimeout:
this.maxWaitForConnection < (params?.maxWait || 0)
? Math.max(params!.maxWait! * 0.8, params!.maxWait! - 1000)
: this.maxWaitForConnection,
abort: params?.abort,
})
} catch (err) {
this.onRelayConnectionFailure?.(url)
return String('connection failure: ' + String(err))
}
return r return r
.publish(event) .publish(event)
.catch(async err => { .catch(async err => {
if (err instanceof Error && err.message.startsWith('auth-required: ') && options?.onauth) { if (err instanceof Error && err.message.startsWith('auth-required: ') && params?.onauth) {
await r.auth(options.onauth) await r.auth(params.onauth)
return r.publish(event) // retry return r.publish(event) // retry
} }
throw err throw err

View File

@@ -45,7 +45,7 @@ export class AbstractRelay {
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
private reconnectAttempts: number = 0 private reconnectAttempts: number = 0
private closedIntentionally: boolean = false private skipReconnection: boolean = false
private connectionPromise: Promise<void> | undefined private connectionPromise: Promise<void> | undefined
private openCountRequests = new Map<string, CountResolver>() private openCountRequests = new Map<string, CountResolver>()
@@ -120,12 +120,9 @@ export class AbstractRelay {
this._connected = false this._connected = false
this.connectionPromise = undefined this.connectionPromise = undefined
const wasIntentional = this.closedIntentionally
this.closedIntentionally = false // reset for next time
this.onclose?.() this.onclose?.()
if (this.enableReconnect && !wasIntentional) { if (this.enableReconnect && !this.skipReconnection) {
this.reconnect() this.reconnect()
} else { } else {
this.closeAllSubscriptions(reason) this.closeAllSubscriptions(reason)
@@ -139,13 +136,15 @@ export class AbstractRelay {
this.challenge = undefined this.challenge = undefined
this.authPromise = undefined this.authPromise = undefined
this.skipReconnection = false
this.connectionPromise = new Promise((resolve, reject) => { this.connectionPromise = new Promise((resolve, reject) => {
if (opts?.timeout) { if (opts?.timeout) {
connectionTimeoutHandle = setTimeout(() => { connectionTimeoutHandle = setTimeout(() => {
reject('connection timed out') reject('connection timed out')
this.connectionPromise = undefined this.connectionPromise = undefined
this.skipReconnection = true
this.onclose?.() this.onclose?.()
this.closeAllSubscriptions('relay connection timed out') this.handleHardClose('relay connection timed out')
}, opts.timeout) }, opts.timeout)
} }
@@ -191,10 +190,13 @@ export class AbstractRelay {
resolve() resolve()
} }
this.ws.onerror = ev => { this.ws.onerror = () => {
clearTimeout(connectionTimeoutHandle) clearTimeout(connectionTimeoutHandle)
reject((ev as any).message || 'websocket error') reject('connection failed')
this.handleHardClose('relay connection errored') this.connectionPromise = undefined
this.skipReconnection = true
this.onclose?.()
this.handleHardClose('relay connection failed')
} }
this.ws.onclose = ev => { this.ws.onclose = ev => {
@@ -466,7 +468,7 @@ export class AbstractRelay {
} }
public close() { public close() {
this.closedIntentionally = true this.skipReconnection = true
if (this.reconnectTimeoutHandle) { if (this.reconnectTimeoutHandle) {
clearTimeout(this.reconnectTimeoutHandle) clearTimeout(this.reconnectTimeoutHandle)
this.reconnectTimeoutHandle = undefined this.reconnectTimeoutHandle = undefined

BIN
bun.lockb

Binary file not shown.

View File

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

View File

@@ -2,7 +2,7 @@ import { test, expect } from 'bun:test'
import { encrypt, decrypt } from './nip04.ts' import { encrypt, decrypt } from './nip04.ts'
import { getPublicKey, generateSecretKey } from './pure.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 () => { test('encrypt and decrypt message', async () => {
let sk1 = generateSecretKey() let sk1 = generateSecretKey()

View File

@@ -1,13 +1,13 @@
import { bytesToHex, randomBytes } from '@noble/hashes/utils' import { hexToBytes, randomBytes } from '@noble/hashes/utils.js'
import { secp256k1 } from '@noble/curves/secp256k1' import { secp256k1 } from '@noble/curves/secp256k1.js'
import { cbc } from '@noble/ciphers/aes' import { cbc } from '@noble/ciphers/aes.js'
import { base64 } from '@scure/base' import { base64 } from '@scure/base'
import { utf8Decoder, utf8Encoder } from './utils.ts' import { utf8Decoder, utf8Encoder } from './utils.ts'
export function encrypt(secretKey: string | Uint8Array, pubkey: string, text: string): string { export function encrypt(secretKey: string | Uint8Array, pubkey: string, text: string): string {
const privkey: string = secretKey instanceof Uint8Array ? bytesToHex(secretKey) : secretKey const privkey: Uint8Array = secretKey instanceof Uint8Array ? secretKey : hexToBytes(secretKey)
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey) const key = secp256k1.getSharedSecret(privkey, hexToBytes('02' + pubkey))
const normalizedKey = getNormalizedX(key) const normalizedKey = getNormalizedX(key)
let iv = Uint8Array.from(randomBytes(16)) 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 { 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 [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 normalizedKey = getNormalizedX(key)
let iv = base64.decode(ivb64) let iv = base64.decode(ivb64)

View File

@@ -5,7 +5,7 @@ import {
extendedKeysFromSeedWords, extendedKeysFromSeedWords,
accountFromExtendedKey, accountFromExtendedKey,
} from './nip06.ts' } from './nip06.ts'
import { hexToBytes } from '@noble/hashes/utils' import { hexToBytes } from '@noble/hashes/utils.js'
test('generate private key from a mnemonic', async () => { test('generate private key from a mnemonic', async () => {
const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong' const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'

View File

@@ -1,5 +1,5 @@
import { bytesToHex } from '@noble/hashes/utils' import { bytesToHex } from '@noble/hashes/utils.js'
import { wordlist } from '@scure/bip39/wordlists/english' import { wordlist } from '@scure/bip39/wordlists/english.js'
import { generateMnemonic, mnemonicToSeedSync, validateMnemonic } from '@scure/bip39' import { generateMnemonic, mnemonicToSeedSync, validateMnemonic } from '@scure/bip39'
import { HDKey } from '@scure/bip32' import { HDKey } from '@scure/bip32'

View File

@@ -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 { 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' import { utf8Encoder } from './utils.ts'

View File

@@ -2,7 +2,7 @@ import { test, expect } from 'bun:test'
import { getPublicKey } from './pure.ts' import { getPublicKey } from './pure.ts'
import { decode } from './nip19.ts' import { decode } from './nip19.ts'
import { wrapEvent, wrapManyEvents, unwrapEvent } from './nip17.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 const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data

View File

@@ -1,5 +1,5 @@
import { describe, test, expect } from 'bun:test' 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 { EventTemplate, finalizeEvent, getPublicKey } from './pure.ts'
import { GenericRepost, Repost, ShortTextNote, BadgeDefinition as BadgeDefinitionKind } from './kinds.ts' import { GenericRepost, Repost, ShortTextNote, BadgeDefinition as BadgeDefinitionKind } from './kinds.ts'
import { finishRepostEvent, getRepostedEventPointer, getRepostedEvent } from './nip18.ts' import { finishRepostEvent, getRepostedEventPointer, getRepostedEvent } from './nip18.ts'

View File

@@ -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 { bech32 } from '@scure/base'
import { utf8Decoder, utf8Encoder } from './utils.ts' 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(nip19: Note): DecodedNote
export function decode(code: string): DecodedResult export function decode(code: string): DecodedResult
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)) let data = new Uint8Array(bech32.fromWords(words))
switch (prefix) { switch (prefix) {

View File

@@ -1,5 +1,5 @@
import { describe, test, expect } from 'bun:test' 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 { finalizeEvent, getPublicKey } from './pure.ts'
import { Reaction, ShortTextNote } from './kinds.ts' import { Reaction, ShortTextNote } from './kinds.ts'
import { finishReactionEvent, getReactedEventPointer } from './nip25.ts' import { finishReactionEvent, getReactedEventPointer } from './nip25.ts'

View File

@@ -1,5 +1,5 @@
import { describe, test, expect } from 'bun:test' 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 { getPublicKey } from './pure.ts'
import * as Kind from './kinds.ts' import * as Kind from './kinds.ts'
import { import {

View File

@@ -1,8 +1,8 @@
import { test, expect } from 'bun:test' import { test, expect } from 'bun:test'
import { v2 } from './nip44.js' 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 { 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 const v2vec = vec.v2
test('get_conversation_key', () => { test('get_conversation_key', () => {
@@ -14,7 +14,7 @@ test('get_conversation_key', () => {
test('encrypt_decrypt', () => { test('encrypt_decrypt', () => {
for (const v of v2vec.valid.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) const key = v2.utils.getConversationKey(hexToBytes(v.sec1), pub2)
expect(bytesToHex(key)).toEqual(v.conversation_key) expect(bytesToHex(key)).toEqual(v.conversation_key)
const ciphertext = v2.encrypt(v.plaintext, key, hexToBytes(v.nonce)) const ciphertext = v2.encrypt(v.plaintext, key, hexToBytes(v.nonce))
@@ -40,7 +40,7 @@ test('decrypt', async () => {
test('get_conversation_key', async () => { test('get_conversation_key', async () => {
for (const v of v2vec.invalid.get_conversation_key) { for (const v of v2vec.invalid.get_conversation_key) {
expect(() => v2.utils.getConversationKey(hexToBytes(v.sec1), v.pub2)).toThrow( 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)/,
) )
} }
}) })

View File

@@ -1,10 +1,10 @@
import { chacha20 } from '@noble/ciphers/chacha' import { chacha20 } from '@noble/ciphers/chacha.js'
import { equalBytes } from '@noble/ciphers/utils' import { equalBytes } from '@noble/ciphers/utils.js'
import { secp256k1 } from '@noble/curves/secp256k1' import { secp256k1 } from '@noble/curves/secp256k1.js'
import { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf' import { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf.js'
import { hmac } from '@noble/hashes/hmac' import { hmac } from '@noble/hashes/hmac.js'
import { sha256 } from '@noble/hashes/sha256' import { sha256 } from '@noble/hashes/sha2.js'
import { concatBytes, randomBytes } from '@noble/hashes/utils' import { concatBytes, hexToBytes, randomBytes } from '@noble/hashes/utils.js'
import { base64 } from '@scure/base' import { base64 } from '@scure/base'
import { utf8Decoder, utf8Encoder } from './utils.ts' 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 const maxPlaintextSize = 0xffff // 65535 (64kb-1) => padded to 64kb
export function getConversationKey(privkeyA: Uint8Array, pubkeyB: string): Uint8Array { export function getConversationKey(privkeyA: Uint8Array, pubkeyB: string): Uint8Array {
const sharedX = secp256k1.getSharedSecret(privkeyA, '02' + pubkeyB).subarray(1, 33) const sharedX = secp256k1.getSharedSecret(privkeyA, hexToBytes('02' + pubkeyB)).subarray(1, 33)
return hkdf_extract(sha256, sharedX, 'nip44-v2') return hkdf_extract(sha256, sharedX, utf8Encoder.encode('nip44-v2'))
} }
function getMessageKeys( function getMessageKeys(

View File

@@ -1,5 +1,5 @@
import { describe, test, expect } from 'bun:test' 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 { makeNwcRequestEvent, parseConnectionString } from './nip47.ts'
import { decrypt } from './nip04.ts' import { decrypt } from './nip04.ts'
import { NWCWalletRequest } from './kinds.ts' import { NWCWalletRequest } from './kinds.ts'

View File

@@ -1,6 +1,6 @@
import { test, expect } from 'bun:test' import { test, expect } from 'bun:test'
import { decrypt, encrypt } from './nip49.ts' import { decrypt, encrypt } from './nip49.ts'
import { hexToBytes } from '@noble/hashes/utils' import { hexToBytes } from '@noble/hashes/utils.js'
test('encrypt and decrypt', () => { test('encrypt and decrypt', () => {
for (let i = 0; i < vectors.length; i++) { for (let i = 0; i < vectors.length; i++) {

View File

@@ -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 { 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( export function encrypt(
sec: Uint8Array, sec: Uint8Array,
@@ -22,7 +22,7 @@ export function encrypt(
} }
export function decrypt(ncryptsec: string, password: string): Uint8Array { 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') { if (prefix !== 'ncryptsec') {
throw new Error(`invalid prefix ${prefix}, expected 'ncryptsec'`) throw new Error(`invalid prefix ${prefix}, expected 'ncryptsec'`)
} }

View File

@@ -4,7 +4,7 @@ import { decode } from './nip19.ts'
import { NostrEvent, getPublicKey } from './pure.ts' import { NostrEvent, getPublicKey } from './pure.ts'
import { SimplePool } from './pool.ts' import { SimplePool } from './pool.ts'
import { GiftWrap } from './kinds.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 senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data as Uint8Array
const recipientPrivateKey = decode(`nsec1uyyrnx7cgfp40fcskcr2urqnzekc20fj0er6de0q8qvhx34ahazsvs9p36`).data as Uint8Array const recipientPrivateKey = decode(`nsec1uyyrnx7cgfp40fcskcr2urqnzekc20fj0er6de0q8qvhx34ahazsvs9p36`).data as Uint8Array

View File

@@ -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 { Filter } from './filter.ts'
import { AbstractRelay, Subscription } from './relay.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 // Negentropy implementation by Doug Hoyte
const PROTOCOL_VERSION = 0x61 // Version 1 const PROTOCOL_VERSION = 0x61 // Version 1

View File

@@ -1,5 +1,5 @@
import { sha256 } from '@noble/hashes/sha256' import { sha256 } from '@noble/hashes/sha2.js'
import { bytesToHex } from '@noble/hashes/utils' import { bytesToHex } from '@noble/hashes/utils.js'
import { describe, expect, test } from 'bun:test' import { describe, expect, test } from 'bun:test'
import { HTTPAuth } from './kinds.ts' import { HTTPAuth } from './kinds.ts'

View File

@@ -1,5 +1,5 @@
import { sha256 } from '@noble/hashes/sha256' import { sha256 } from '@noble/hashes/sha2.js'
import { bytesToHex } from '@noble/hashes/utils' import { bytesToHex } from '@noble/hashes/utils.js'
import { base64 } from '@scure/base' import { base64 } from '@scure/base'
import { HTTPAuth } from './kinds.ts' import { HTTPAuth } from './kinds.ts'

View File

@@ -1,6 +1,6 @@
import { test, expect } from 'bun:test' import { test, expect } from 'bun:test'
import { BlossomClient } from './nipb7.ts' import { BlossomClient } from './nipb7.ts'
import { sha256 } from '@noble/hashes/sha256' import { sha256 } from '@noble/hashes/sha2.js'
import { bytesToHex } from './utils.ts' import { bytesToHex } from './utils.ts'
import { PlainKeySigner } from './signer.ts' import { PlainKeySigner } from './signer.ts'
import { generateSecretKey } from './pure.ts' import { generateSecretKey } from './pure.ts'

View File

@@ -1,4 +1,4 @@
import { sha256 } from '@noble/hashes/sha256' import { sha256 } from '@noble/hashes/sha2.js'
import { EventTemplate } from './core.ts' import { EventTemplate } from './core.ts'
import { Signer } from './signer.ts' import { Signer } from './signer.ts'
import { bytesToHex } from './utils.ts' import { bytesToHex } from './utils.ts'

View File

@@ -1,7 +1,7 @@
{ {
"type": "module", "type": "module",
"name": "nostr-tools", "name": "nostr-tools",
"version": "2.20.0", "version": "2.22.2",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",
@@ -236,12 +236,12 @@
}, },
"license": "Unlicense", "license": "Unlicense",
"dependencies": { "dependencies": {
"@noble/ciphers": "^0.5.1", "@noble/ciphers": "2.1.1",
"@noble/curves": "1.2.0", "@noble/curves": "2.0.1",
"@noble/hashes": "1.3.1", "@noble/hashes": "2.0.1",
"@scure/base": "1.1.1", "@scure/base": "2.0.0",
"@scure/bip32": "1.3.1", "@scure/bip32": "2.0.1",
"@scure/bip39": "1.2.1", "@scure/bip39": "2.0.1",
"nostr-wasm": "0.1.0" "nostr-wasm": "0.1.0"
}, },
"peerDependencies": { "peerDependencies": {

View File

@@ -3,7 +3,7 @@ import { afterEach, beforeEach, expect, test } from 'bun:test'
import { SimplePool, useWebSocketImplementation } from './pool.ts' import { SimplePool, useWebSocketImplementation } from './pool.ts'
import { finalizeEvent, generateSecretKey, getPublicKey, type Event } from './pure.ts' import { finalizeEvent, generateSecretKey, getPublicKey, type Event } from './pure.ts'
import { MockRelay, MockWebSocketClient } from './test-helpers.ts' import { MockRelay, MockWebSocketClient } from './test-helpers.ts'
import { hexToBytes } from '@noble/hashes/utils' import { hexToBytes } from '@noble/hashes/utils.js'
useWebSocketImplementation(MockWebSocketClient) useWebSocketImplementation(MockWebSocketClient)

View File

@@ -15,7 +15,7 @@ export function useWebSocketImplementation(websocketImplementation: any) {
export class SimplePool extends AbstractSimplePool { export class SimplePool extends AbstractSimplePool {
constructor(options?: Pick<AbstractPoolConstructorOptions, 'enablePing' | 'enableReconnect'>) { constructor(options?: Pick<AbstractPoolConstructorOptions, 'enablePing' | 'enableReconnect'>) {
super({ verifyEvent, websocketImplementation: _WebSocket, ...options }) super({ verifyEvent, websocketImplementation: _WebSocket, maxWaitForConnection: 3000, ...options })
} }
} }

View File

@@ -11,7 +11,7 @@ import {
generateSecretKey, generateSecretKey,
} from './pure.ts' } from './pure.ts'
import { ShortTextNote } from './kinds.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', () => { test('private key generation', () => {
expect(bytesToHex(generateSecretKey())).toMatch(/[a-f0-9]{64}/) expect(bytesToHex(generateSecretKey())).toMatch(/[a-f0-9]{64}/)

12
pure.ts
View File

@@ -1,13 +1,13 @@
import { schnorr } from '@noble/curves/secp256k1' import { schnorr } from '@noble/curves/secp256k1.js'
import { bytesToHex } from '@noble/hashes/utils' import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'
import { Nostr, Event, EventTemplate, UnsignedEvent, VerifiedEvent, verifiedSymbol, validateEvent } from './core.ts' 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' import { utf8Encoder } from './utils.ts'
class JS implements Nostr { class JS implements Nostr {
generateSecretKey(): Uint8Array { generateSecretKey(): Uint8Array {
return schnorr.utils.randomPrivateKey() return schnorr.utils.randomSecretKey()
} }
getPublicKey(secretKey: Uint8Array): string { getPublicKey(secretKey: Uint8Array): string {
return bytesToHex(schnorr.getPublicKey(secretKey)) return bytesToHex(schnorr.getPublicKey(secretKey))
@@ -16,7 +16,7 @@ class JS implements Nostr {
const event = t as VerifiedEvent const event = t as VerifiedEvent
event.pubkey = bytesToHex(schnorr.getPublicKey(secretKey)) event.pubkey = bytesToHex(schnorr.getPublicKey(secretKey))
event.id = getEventHash(event) event.id = getEventHash(event)
event.sig = bytesToHex(schnorr.sign(getEventHash(event), secretKey)) event.sig = bytesToHex(schnorr.sign(hexToBytes(getEventHash(event)), secretKey))
event[verifiedSymbol] = true event[verifiedSymbol] = true
return event return event
} }
@@ -30,7 +30,7 @@ class JS implements Nostr {
} }
try { 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 event[verifiedSymbol] = valid
return valid return valid
} catch (err) { } catch (err) {

View File

@@ -6,6 +6,7 @@ import {
insertEventIntoDescendingList, insertEventIntoDescendingList,
binarySearch, binarySearch,
normalizeURL, normalizeURL,
mergeReverseSortedLists,
} from './utils.ts' } from './utils.ts'
import type { Event } from './core.ts' import type { Event } from './core.ts'
@@ -270,6 +271,94 @@ test('binary search', () => {
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false]) expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false])
}) })
describe('mergeReverseSortedLists', () => {
test('merge empty lists', () => {
const list1: Event[] = []
const list2: Event[] = []
expect(mergeReverseSortedLists(list1, list2)).toHaveLength(0)
})
test('merge list with empty list', () => {
const list1 = [buildEvent({ id: 'a', created_at: 30 }), buildEvent({ id: 'b', created_at: 20 })]
const list2: Event[] = []
const result = mergeReverseSortedLists(list1, list2)
expect(result).toHaveLength(2)
expect(result.map(e => e.id)).toEqual(['a', 'b'])
})
test('merge two simple lists', () => {
const list1 = [
buildEvent({ id: 'a', created_at: 30 }),
buildEvent({ id: 'b', created_at: 10 }),
buildEvent({ id: 'f', created_at: 3 }),
buildEvent({ id: 'g', created_at: 2 }),
]
const list2 = [
buildEvent({ id: 'c', created_at: 25 }),
buildEvent({ id: 'd', created_at: 5 }),
buildEvent({ id: 'e', created_at: 1 }),
]
const result = mergeReverseSortedLists(list1, list2)
expect(result.map(e => e.id)).toEqual(['a', 'c', 'b', 'd', 'f', 'g', 'e'])
})
test('merge lists with same timestamps', () => {
const list1 = [
buildEvent({ id: 'a', created_at: 30 }),
buildEvent({ id: 'b', created_at: 20 }),
buildEvent({ id: 'f', created_at: 10 }),
]
const list2 = [
buildEvent({ id: 'c', created_at: 30 }),
buildEvent({ id: 'd', created_at: 20 }),
buildEvent({ id: 'e', created_at: 20 }),
]
const result = mergeReverseSortedLists(list1, list2)
expect(result.map(e => e.id)).toEqual(['c', 'a', 'd', 'e', 'b', 'f'])
})
test('deduplicate events with same timestamp and id', () => {
const list1 = [
buildEvent({ id: 'a', created_at: 30 }),
buildEvent({ id: 'b', created_at: 20 }),
buildEvent({ id: 'b', created_at: 20 }),
buildEvent({ id: 'c', created_at: 20 }),
buildEvent({ id: 'd', created_at: 10 }),
]
const list2 = [
buildEvent({ id: 'a', created_at: 30 }),
buildEvent({ id: 'c', created_at: 20 }),
buildEvent({ id: 'b', created_at: 20 }),
buildEvent({ id: 'd', created_at: 10 }),
buildEvent({ id: 'e', created_at: 10 }),
buildEvent({ id: 'd', created_at: 10 }),
]
console.log('==================')
const result = mergeReverseSortedLists(list1, list2)
console.log(
'result:',
result.map(e => e.id),
)
expect(result.map(e => e.id)).toEqual(['a', 'c', 'b', 'd', 'e'])
})
test('merge when one list is completely before the other', () => {
const list1 = [buildEvent({ id: 'a', created_at: 50 }), buildEvent({ id: 'b', created_at: 40 })]
const list2 = [buildEvent({ id: 'c', created_at: 30 }), buildEvent({ id: 'd', created_at: 20 })]
const result = mergeReverseSortedLists(list1, list2)
expect(result).toHaveLength(4)
expect(result.map(e => e.id)).toEqual(['a', 'b', 'c', 'd'])
})
test('merge when one list is completely after the other', () => {
const list1 = [buildEvent({ id: 'a', created_at: 10 }), buildEvent({ id: 'b', created_at: 5 })]
const list2 = [buildEvent({ id: 'c', created_at: 30 }), buildEvent({ id: 'd', created_at: 20 })]
const result = mergeReverseSortedLists(list1, list2)
expect(result).toHaveLength(4)
expect(result.map(e => e.id)).toEqual(['c', 'd', 'a', 'b'])
})
})
describe('normalizeURL', () => { describe('normalizeURL', () => {
test('normalizes wss:// URLs', () => { test('normalizes wss:// URLs', () => {
expect(normalizeURL('wss://example.com')).toBe('wss://example.com/') expect(normalizeURL('wss://example.com')).toBe('wss://example.com/')

View File

@@ -1,9 +1,9 @@
import type { Event } from './core.ts' import type { NostrEvent } from './core.ts'
export const utf8Decoder: TextDecoder = new TextDecoder('utf-8') export const utf8Decoder: TextDecoder = new TextDecoder('utf-8')
export const utf8Encoder: TextEncoder = new TextEncoder() 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 { export function normalizeURL(url: string): string {
try { try {
@@ -22,7 +22,7 @@ export function normalizeURL(url: string): string {
} }
} }
export function insertEventIntoDescendingList(sortedArray: Event[], event: Event): Event[] { export function insertEventIntoDescendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {
const [idx, found] = binarySearch(sortedArray, b => { const [idx, found] = binarySearch(sortedArray, b => {
if (event.id === b.id) return 0 if (event.id === b.id) return 0
if (event.created_at === b.created_at) return -1 if (event.created_at === b.created_at) return -1
@@ -34,7 +34,7 @@ export function insertEventIntoDescendingList(sortedArray: Event[], event: Event
return sortedArray return sortedArray
} }
export function insertEventIntoAscendingList(sortedArray: Event[], event: Event): Event[] { export function insertEventIntoAscendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {
const [idx, found] = binarySearch(sortedArray, b => { const [idx, found] = binarySearch(sortedArray, b => {
if (event.id === b.id) return 0 if (event.id === b.id) return 0
if (event.created_at === b.created_at) return -1 if (event.created_at === b.created_at) return -1
@@ -68,6 +68,62 @@ export function binarySearch<T>(arr: T[], compare: (b: T) => number): [number, b
return [start, false] return [start, false]
} }
export function mergeReverseSortedLists(list1: NostrEvent[], list2: NostrEvent[]): NostrEvent[] {
const result: NostrEvent[] = new Array(list1.length + list2.length)
result.length = 0
let i1 = 0
let i2 = 0
let sameTimestampIds: string[] = []
while (i1 < list1.length && i2 < list2.length) {
let next: NostrEvent
if (list1[i1]?.created_at > list2[i2]?.created_at) {
next = list1[i1]
i1++
} else {
next = list2[i2]
i2++
}
if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {
if (sameTimestampIds.includes(next.id)) continue
} else {
sameTimestampIds.length = 0
}
result.push(next)
sameTimestampIds.push(next.id)
}
while (i1 < list1.length) {
const next = list1[i1]
i1++
if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {
if (sameTimestampIds.includes(next.id)) continue
} else {
sameTimestampIds.length = 0
}
result.push(next)
sameTimestampIds.push(next.id)
}
while (i2 < list2.length) {
const next = list2[i2]
i2++
if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {
if (sameTimestampIds.includes(next.id)) continue
} else {
sameTimestampIds.length = 0
}
result.push(next)
sameTimestampIds.push(next.id)
}
return result
}
export class QueueNode<V> { export class QueueNode<V> {
public value: V public value: V
public next: QueueNode<V> | null = null public next: QueueNode<V> | null = null

View File

@@ -1,4 +1,4 @@
import { bytesToHex } from '@noble/hashes/utils' import { bytesToHex } from '@noble/hashes/utils.js'
import { Nostr as NostrWasm } from 'nostr-wasm' import { Nostr as NostrWasm } from 'nostr-wasm'
import { EventTemplate, Event, Nostr, VerifiedEvent, verifiedSymbol } from './core.ts' import { EventTemplate, Event, Nostr, VerifiedEvent, verifiedSymbol } from './core.ts'