Confirm reconnect pairings and add connection discovery settings

Setup now waits for both IndexedDB writes: the acceptor acknowledges its
write, a failure on either side is reported to both people, and the
proposer removes its copy if the acceptor could not save. Every session
that authenticates the paired device compares a check value derived from
the stored pairing, so a one-sided pairing (interrupted setup, or Turn off
reconnect on one device) is turned off on the other side too.

The tracker is chosen under Network settings > Connection discovery
(manual, default from VITE_TRACKER_URL, or custom), stored per browser and
editable during a session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SchluffeandClaude Opus 5.5 committed 2026-10-21 00:30:04 +02:00
1 parent 3659956967
commit acba97a7e1
10 files changed
+309 -55

No files matched your search

+33 -1
View File
@@ -1,6 +1,6 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { derivePairing, discoveryId, openSignal, parsePairing, pairingNonce, sealSignal, signalingKey, signalRole, trackerUrl, CLOCK_TOLERANCE, SIGNAL_LIFETIME, type PairingContext, type SignalRoute } from '../src/lib/reconnect.ts';
import { derivePairing, discoveryId, discoveryTracker, loadDiscovery, openSignal, pairingCheck, parseCheck, parsePairing, pairingNonce, saveDiscovery, sealSignal, signalingKey, signalRole, trackerUrl, CLOCK_TOLERANCE, SIGNAL_LIFETIME, type PairingContext, type SignalRoute } from '../src/lib/reconnect.ts';
import { emptyConnection, updateConnection } from '../src/lib/connections.ts';
const fingerprint = (byte: string) => Array(32).fill(byte).join(' ');
@@ -85,3 +85,35 @@ test('stored pairings are dropped when the paired device changes', async () => {
assert.equal(parsePairing({ ...pairing, trackers: ['http://x'] }, peerKey), null);
assert.equal(parsePairing({ ...pairing, key: 'secret' }, peerKey), null);
});
test('pairing checks match only for the same pairing', async () => {
const [first, second] = await Promise.all([derivePairing(context), derivePairing({ ...context })]);
const check = await pairingCheck(first);
assert.match(check, /^[0-9a-f]{32}$/);
assert.equal(await pairingCheck(second), check);
assert.notEqual(await pairingCheck(await derivePairing({ ...context, acceptorNonce: pairingNonce() })), check);
assert.equal(parseCheck(check), check);
assert.equal(parseCheck(''), '');
for (const bad of ['zz'.repeat(16), 'ab', 42, null]) assert.throws(() => parseCheck(bad));
});
test('discovery preferences resolve to one concrete tracker', () => {
const values = new Map<string, string>();
const storage = { getItem: (key: string) => values.get(key) ?? null, setItem: (key: string, value: string) => { values.set(key, value); } };
Object.defineProperty(globalThis, 'localStorage', { value: storage, configurable: true });
try {
assert.deepEqual(loadDiscovery(''), { choice: 'manual', custom: '' });
assert.deepEqual(loadDiscovery('wss://default.example'), { choice: 'default', custom: '' });
saveDiscovery({ choice: 'custom', custom: 'wss://mine.example/announce' });
const custom = loadDiscovery('wss://default.example');
assert.equal(discoveryTracker(custom, 'wss://default.example'), 'wss://mine.example/announce');
// A fork without a default tracker never resolves a stored Default choice.
saveDiscovery({ choice: 'default', custom: '' });
assert.equal(loadDiscovery('').choice, 'manual');
assert.equal(discoveryTracker(loadDiscovery('wss://default.example'), 'wss://default.example'), 'wss://default.example/');
assert.equal(discoveryTracker({ choice: 'manual', custom: 'wss://ignored.example' }, ''), '');
assert.throws(() => discoveryTracker({ choice: 'custom', custom: 'http://insecure.example' }, ''));
values.set('hellcon-discovery', '{broken');
assert.equal(loadDiscovery('').choice, 'manual');
} finally { Reflect.deleteProperty(globalThis, 'localStorage'); }
});