@unicitylabs/nostr-js-sdk 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5591,6 +5591,8 @@
5591
5591
  const GIFT_WRAP = 1059;
5592
5592
  /** NIP-65: Relay list metadata */
5593
5593
  const RELAY_LIST = 10002;
5594
+ /** NIP-42: Client authentication to relay */
5595
+ const AUTH = 22242;
5594
5596
  /** NIP-78: Application-specific data (parameterized replaceable) */
5595
5597
  const APP_DATA = 30078;
5596
5598
  // ============================================================================
@@ -5704,6 +5706,7 @@
5704
5706
  AGENT_LOCATION: AGENT_LOCATION,
5705
5707
  AGENT_PROFILE: AGENT_PROFILE,
5706
5708
  APP_DATA: APP_DATA,
5709
+ AUTH: AUTH,
5707
5710
  CHAT_MESSAGE: CHAT_MESSAGE,
5708
5711
  CONTACTS: CONTACTS,
5709
5712
  DELETION: DELETION,
@@ -6004,6 +6007,13 @@
6004
6007
  const DEFAULT_RECONNECT_INTERVAL_MS = 1000;
6005
6008
  const DEFAULT_MAX_RECONNECT_INTERVAL_MS = 30000;
6006
6009
  const DEFAULT_PING_INTERVAL_MS = 30000;
6010
+ /**
6011
+ * Delay before resubscribing after NIP-42 authentication.
6012
+ * This gives the relay time to process the AUTH response before we send
6013
+ * subscription requests. Without this delay, some relays may still reject
6014
+ * the subscriptions as the AUTH hasn't been fully processed yet.
6015
+ */
6016
+ const AUTH_RESUBSCRIBE_DELAY_MS = 100;
6007
6017
  /**
6008
6018
  * NostrClient provides the main interface for Nostr protocol operations.
6009
6019
  */
@@ -6348,6 +6358,9 @@
6348
6358
  case 'CLOSED':
6349
6359
  this.handleClosedMessage(json);
6350
6360
  break;
6361
+ case 'AUTH':
6362
+ this.handleAuthMessage(_url, json);
6363
+ break;
6351
6364
  }
6352
6365
  }
6353
6366
  catch {
@@ -6428,6 +6441,33 @@
6428
6441
  subscription.listener.onError(subscriptionId, `Subscription closed: ${message}`);
6429
6442
  }
6430
6443
  }
6444
+ /**
6445
+ * Handle AUTH message from relay (NIP-42 authentication challenge).
6446
+ */
6447
+ handleAuthMessage(relayUrl, json) {
6448
+ if (json.length < 2)
6449
+ return;
6450
+ const challenge = json[1];
6451
+ const relay = this.relays.get(relayUrl);
6452
+ if (!relay?.socket || !relay.connected)
6453
+ return;
6454
+ // Create and sign the auth event (kind 22242)
6455
+ const authEvent = Event.create(this.keyManager, {
6456
+ kind: AUTH,
6457
+ tags: [
6458
+ ['relay', relayUrl],
6459
+ ['challenge', challenge],
6460
+ ],
6461
+ content: '',
6462
+ });
6463
+ // Send AUTH response
6464
+ const message = JSON.stringify(['AUTH', authEvent.toJSON()]);
6465
+ relay.socket.send(message);
6466
+ // Re-send subscriptions after auth (relay may have ignored pre-auth requests)
6467
+ setTimeout(() => {
6468
+ this.resubscribeAll(relayUrl);
6469
+ }, AUTH_RESUBSCRIBE_DELAY_MS);
6470
+ }
6431
6471
  /**
6432
6472
  * Disconnect from all relays.
6433
6473
  */
@@ -10663,6 +10703,7 @@
10663
10703
  exports.AGENT_LOCATION = AGENT_LOCATION;
10664
10704
  exports.AGENT_PROFILE = AGENT_PROFILE;
10665
10705
  exports.APP_DATA = APP_DATA;
10706
+ exports.AUTH = AUTH;
10666
10707
  exports.Bech32 = bech32;
10667
10708
  exports.CHAT_MESSAGE = CHAT_MESSAGE;
10668
10709
  exports.CLOSED = CLOSED;