uniwrtc 1.3.0 → 1.4.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/main.js +45 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uniwrtc",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "A universal WebRTC signaling service",
5
5
  "main": "server.js",
6
6
  "type": "module",
package/src/main.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import './style.css';
2
2
  import UniWRTCClient from '../client-browser.js';
3
3
  import { createNostrClient } from './nostr/nostrClient.js';
4
+ import { generateSecretKey, getPublicKey } from 'nostr-tools/pure';
4
5
 
5
6
  // Make UniWRTCClient available globally for backwards compatibility
6
7
  window.UniWRTCClient = UniWRTCClient;
@@ -31,6 +32,35 @@ const dataChannels = new Map();
31
32
  const pendingIce = new Map();
32
33
  const outboundIceBatches = new Map();
33
34
 
35
+ function bytesToHex(bytes) {
36
+ return Array.from(bytes)
37
+ .map((b) => b.toString(16).padStart(2, '0'))
38
+ .join('');
39
+ }
40
+
41
+ function isHex64(s) {
42
+ return typeof s === 'string' && /^[0-9a-fA-F]{64}$/.test(s);
43
+ }
44
+
45
+ function ensureIdentity() {
46
+ // Must match the key used in createNostrClient() so the pubkey stays consistent.
47
+ const storageKey = 'nostr-secret-key-tab';
48
+ let stored = sessionStorage.getItem(storageKey);
49
+
50
+ if (stored && stored.includes(',')) {
51
+ sessionStorage.removeItem(storageKey);
52
+ stored = null;
53
+ }
54
+
55
+ if (!isHex64(stored)) {
56
+ const secretBytes = generateSecretKey();
57
+ stored = bytesToHex(secretBytes);
58
+ sessionStorage.setItem(storageKey, stored);
59
+ }
60
+
61
+ return getPublicKey(stored);
62
+ }
63
+
34
64
  // Initialize app
35
65
  document.getElementById('app').innerHTML = `
36
66
  <div class="container">
@@ -112,6 +142,19 @@ if (urlRoom) {
112
142
  log(`Using default room ID: ${defaultRoom}`, 'info');
113
143
  }
114
144
 
145
+ // Client/session identity should not depend on relay connectivity.
146
+ try {
147
+ myPeerId = ensureIdentity();
148
+ document.getElementById('clientId').textContent = myPeerId.substring(0, 16) + '...';
149
+ } catch {
150
+ // Leave as-is if identity init fails.
151
+ }
152
+
153
+ document.getElementById('sessionId').textContent = roomInput.value || 'Not joined';
154
+ roomInput.addEventListener('input', () => {
155
+ document.getElementById('sessionId').textContent = roomInput.value.trim() || 'Not joined';
156
+ });
157
+
115
158
  // STUN-only ICE servers (no TURN)
116
159
  const ICE_SERVERS = [
117
160
  { urls: ['stun:stun.l.google.com:19302', 'stun:stun1.l.google.com:19302'] },
@@ -354,7 +397,7 @@ async function connectNostr() {
354
397
  }
355
398
 
356
399
  // Reset local peer state to avoid stale sessions targeting the wrong browser tab.
357
- myPeerId = null;
400
+ myPeerId = myPeerId || ensureIdentity();
358
401
  mySessionNonce = null;
359
402
  peerSessions.clear();
360
403
  peerProbeState.clear();
@@ -389,8 +432,7 @@ async function connectNostr() {
389
432
 
390
433
  // Identity must be ready before we connect/subscribe; inbound events can arrive immediately.
391
434
  // Any client instance will derive the same per-tab keypair.
392
- const identityClient = createNostrClient({ relayUrl: relayCandidates[0], room: effectiveRoom });
393
- const myPubkey = identityClient.getPublicKey();
435
+ const myPubkey = myPeerId || ensureIdentity();
394
436
  myPeerId = myPubkey;
395
437
  mySessionNonce = Math.random().toString(36).slice(2) + Date.now().toString(36);
396
438
  document.getElementById('clientId').textContent = myPubkey.substring(0, 16) + '...';
@@ -793,7 +835,6 @@ window.disconnect = function() {
793
835
  if (nostrClient) {
794
836
  nostrClient.disconnect().catch(() => {});
795
837
  nostrClient = null;
796
- myPeerId = null;
797
838
  mySessionNonce = null;
798
839
  peerSessions.clear();
799
840
  peerProbeState.clear();