Commit Graph
859 Commits
Author SHA1 Message Date
hoppeandfiatjaf 455124eecd fix(reconnect): don't permanently kill subscriptions on a single failed reconnect attempt
A single failed reconnect attempt (`ws.onerror` or connect timeout)
set `skipReconnection = true`, which made `handleHardClose` close
every subscription on the relay and remove it from the pool. The
intent of fb7de7f was to give up on the *initial* connect failure
(a relay that's dead from the start) — but the same code path also
fires for retries, so any outage longer than the first 10s backoff
slot turned a transient drop into permanent silence on every
long-running client.

Symptom in the wild: a long-running daemon goes silently deaf on a
subscription after the relay operator restarts the relay, with no
error from the SDK because the pool still considers the relay
connected after a later `ensureRelay`. Only `skipReconnection` on
the first attempt (`reconnectAttempts === 0`), so retries fall
through to `handleHardClose` and schedule another attempt as the
`resubscribeBackoff` array clearly intended.
2026-06-20 13:40:04 -03:00
fiatjaf 3575368176 nip19: TLV infinite loop again.
probably fixes https://github.com/nbd-wtf/nostr-tools/issues/223
2026-06-15 18:22:50 -03:00
fiatjaf e17366cd2e add AUTH recommendations to README.
fixes https://github.com/nbd-wtf/nostr-tools/issues/537
2026-06-10 22:09:28 -03:00
hzrd149andfiatjaf 7b1574d45c Fix linting 2026-06-10 21:47:21 -03:00
hzrd149andfiatjaf 3defeeb796 nip46: add logout method 2026-06-10 21:47:21 -03:00
fiatjaf 5de4f4549b fix jsr export. 2026-05-18 21:11:05 -03:00
fiatjaf ed47ef8ef7 bump version. v2.23.5 2026-05-18 20:52:28 -03:00
fiatjaf a9e89e16e3 nip22: like nip10, but for kind:1111 and adjacent kinds. 2026-05-18 20:52:05 -03:00
fiatjaf 3cd26d7d76 nip44: fix test sha256. 2026-05-18 20:52:05 -03:00
Kaiandfiatjaf e1f3ffdece fix: format pool.test.ts and relay.test.ts (pre-existing) 2026-04-25 13:41:06 -03:00
Kaiandfiatjaf 1f2c967b04 feat(nip47): support multiple relays in parseConnectionString
NIP-47 connection strings can specify multiple relay params, but
parseConnectionString only returned the first one.

Adds a 'relays' field (string[]) to NWCConnection while keeping the
existing 'relay' field (deprecated) for backward compatibility.

Fixes #494
2026-04-25 13:41:06 -03:00
Kaiandfiatjaf_ 6b8c242282 fix(nip42): catch SendingOnClosedConnection during automatic AUTH
Fixes #531. When automaticallyAuth is configured and a relay closes the
connection between sending the AUTH challenge and the client sending
the signed response, the SendingOnClosedConnection error was propagating
as an unhandled promise rejection.

This is a race condition that commonly occurs with relays that enforce
WoT or whitelists - they accept the WebSocket, send an AUTH challenge,
but close the connection before the client can respond.

The fix catches the SendingOnClosedConnection error specifically in the
AUTH case handler, preventing unhandled rejections while still propagating
other errors.
2026-04-14 08:24:47 -03:00
Alex GleasonandGitHub 8260e6245d nip44: support encryption of payloads larger than 65535 bytes (#527)
* nip44: support encryption of payloads larger than 65535 bytes

Implement the extended prefix format from nostr-protocol/nips#1907
with bug fixes:

- When plaintext length >= 65536, pad with a 6-byte prefix (2 zero
  sentinel bytes + 4-byte u32 BE length) instead of the 2-byte u16
- Detect the sentinel in unpad() and use dynamic prefix_len (2 or 6)
  for both data extraction and padding validation
- Remove upper-bound size limits in decodePayload() (was rejecting
  payloads > 87472 chars / 65603 bytes)
- Raise maxPlaintextSize from 0xffff to 0xffffffff (2^32-1)
- Add writeU32BE() helper for encoding large lengths
- Fix unpad() to use padded.byteOffset for correct DataView on
  subarrays (was using padded.buffer directly which could be wrong
  if padded is a subarray of a larger buffer)

Tests: add boundary tests at 65535/65536/65537 bytes and full
encrypt/decrypt round-trips at 65536 and 100000 bytes.
All 10 tests pass (5 existing + 5 new).

* nip44: fix calcPaddedLen overflow, enforce canonical prefix, add thorough tests

Fix three bugs found during security audit:

- calcPaddedLen: use 2** instead of 1<< to avoid signed 32-bit
  overflow in JS for plaintext lengths above 2^30
- unpad: reject extended 6-byte prefix when decoded length is below
  65536 (enforce canonical encoding, prevent ambiguous padding)
- decodePayload: document that callers should validate payload size
  before calling to prevent DoS from oversized inputs
- Fix stale size-range comments to match corrected spec arithmetic

Add 10 new tests covering:
- Non-canonical extended prefix rejection (len=1, 1000, 65535)
- Truncated extended prefix buffer
- calcPaddedLen at 2^30+1, 2^31, and 2^32-1
- Multi-byte UTF-8 at the 65536 byte boundary (pad/unpad + e2e)
- Spec test vectors with SHA-256 checksums for 65535/65536/65537
2026-04-10 23:21:52 -03:00
Kaiandfiatjaf_ 007e0b41d0 feat(nip46): add skipSwitchRelays option to BunkerSignerParams
Adds a skipSwitchRelays option that prevents automatic switchRelays()
call in fromURI. This is useful when users want to call switchRelays
manually at their own pace, avoiding race conditions when making
immediate RPC calls after connection.

Fixes #526
2026-03-28 10:14:35 -03:00
Kaiandfiatjaf_ 8be6d08a43 fix(nip47): support multiple relays in parseConnectionString
Addresses #494. The NIP-47 spec allows multiple relay parameters in
connection strings, but parseConnectionString only returned the first one.

Changes:
- Add 'relays' field to NWCConnection interface (string array)
- Use searchParams.getAll('relay') to capture all relays
- Keep 'relay' field for backwards compatibility (returns first relay)

This is backwards compatible - existing code using connection.relay
will continue to work, while new code can use connection.relays to
access all specified relays.
2026-03-27 07:02:47 -03:00
d3vv3andfiatjaf 1b955bdbcb fix: window.printer is undefined in AbstractRelay._onmessage 2026-02-28 01:01:07 -03:00
fiatjaf 1c1e2cc51a verify event should catch exceptions from basic validation. v2.23.2 2026-02-26 18:10:39 -03:00
arthurfrancaandGitHub 54199504a7 oninvalidevent 2026-02-26 18:08:17 -03:00
fiatjaf e45a9987e2 pool: prevent edge case with undefined subcloser. v2.23.1 2026-02-12 15:08:13 -03:00
fiatjaf 0c2c2cd4d8 nip13: improve mining by skipping hex. 2026-02-08 01:06:01 -03:00
fiatjaf 28f7553187 fix a type so jsr is happy. v2.23.0 2026-02-02 18:49:17 -03:00
fiatjaf ca29d9b515 ok, we need the prepareSubscription method. 2026-02-02 18:46:50 -03:00
fiatjaf ab802c8dbe automatic prune broken relay objects and keep track of relay idleness so they can be pruned. 2026-02-02 18:44:52 -03:00
fiatjaf 9db705d86c delete queue test since we don't have queues anymore. 2026-02-02 17:01:59 -03:00
fiatjaf be9b91318f relay: get rid of the message queue, because js is single-threaded. 2026-02-02 09:06:56 -03:00
fiatjaf c2423f7f31 nip27: fix hashtag parsing after newline or other characters. 2026-02-02 08:56:18 -03:00
fiatjaf 05b1fba511 export source files so they can be imported by other ts apps and libraries better. 2026-02-02 00:38:15 -03:00
fiatjaf 2d1345096b subscribeMany and subscribeManyEose are the same as subscribe/subscribeEose. 2026-02-01 17:19:58 -03:00
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. v2.22.1 2026-01-30 17:35:46 -03:00
fiatjaf b3d314643a make everything dependent on npm again. v2.22.0 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
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
fiatjaf bfa40da316 nip46: improve fromURI() and implement "switch_relays". v2.20.0 2026-01-22 21:50:30 -03:00
fiatjaf 9078f45a64 optionally take an AbortSignal on subscriptions. 2026-01-22 21:49:39 -03:00
fiatjaf 6ebe59f123 nip27: support note1 entities for now, but treat them like nevent. v2.19.4 2025-12-17 17:48:35 -03:00
fiatjaf 0235b490fa nip27: fix trailing / in urls. 2025-12-11 21:23:32 -03:00
fiatjaf e290f98a86 label forced ping subscription. 2025-12-10 21:06:31 -03:00
fiatjaf 7a50d9328d add some more kinds. v2.19.3 2025-12-09 10:56:43 -03:00
fiatjaf 65412e5b85 NostrEvent > Event. 2025-12-06 12:21:56 -03:00
fiatjaf ca36ae9530 update README with new enableReconnect() behavior. 2025-12-05 21:59:52 -03:00
fiatjaf 0b6543e1a8 use setInterval() instead of nested setTimeout()s for pingpong.
prevent call stacks from building up
v2.19.2
2025-12-05 21:46:16 -03:00
fiatjaf 693b262b7c CLOSED answers to dummyReq are also ok. 2025-12-05 21:03:34 -03:00
fiatjaf 85c964be3d enableReconnect() always update the filters to the time of the last received event on each subscription. v2.19.1 2025-12-05 13:24:49 -03:00