Answers 'how do you get keys to login elsewhere' — both mechanisms, Signal/WhatsApp style: Server: - migration 0002: key_backups (recovery blobs) + provisions (QR relay), ciphertext only - routes/backup.js: POST/GET /api/backup, /backup/exists; provision new/get/complete - routes/keys.js: POST /api/keys/reset — atomically wipe stale prekeys + install fresh (fixes Bad MAC: restore regenerates prekey IDs that INSERT-OR-IGNORE kept stale) Crypto (crypto.js): - exportIdentityBlob/importIdentityBlob (identity travels only encrypted) - generateRecoveryCode (120-bit Crockford base32) + encrypt/decryptWithCode (PBKDF2 250k) - ephemeral ECDH (P-256) encrypt/decrypt for QR transfer Client (app.js): account-menu 'Set up recovery code' + 'Link a device' (camera scan via BarcodeDetector or paste); auth-screen 'Restore with recovery code' + 'Link from another device' (shows QR + code, polls, restores). Vendored qrcode-generator. All verified live end-to-end (3-process isolated-store integration tests): restore-from-code and QR-link both let a fresh device receive + decrypt messages. SW cache v3->v4. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
// Service worker: offline app shell + Web Push wake-up.
|
|
// It never touches message plaintext — decryption happens only in the page.
|
|
|
|
const CACHE = "cipher-v4";
|
|
const SHELL = [
|
|
"/",
|
|
"/index.html",
|
|
"/manifest.webmanifest",
|
|
"/js/app.js",
|
|
"/js/api.js",
|
|
"/js/crypto.js",
|
|
"/js/store.js",
|
|
"/js/accounts.js",
|
|
"/js/vendor/libsignal.js",
|
|
"/js/vendor/qrcode.js",
|
|
"/icons/icon.svg",
|
|
];
|
|
|
|
self.addEventListener("install", (e) => {
|
|
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL)).then(() => self.skipWaiting()));
|
|
});
|
|
|
|
self.addEventListener("activate", (e) => {
|
|
e.waitUntil(
|
|
caches.keys().then((keys) =>
|
|
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
|
|
).then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener("fetch", (e) => {
|
|
const url = new URL(e.request.url);
|
|
// Never cache API or WebSocket traffic.
|
|
if (url.pathname.startsWith("/api/")) return;
|
|
// Cache-first for the shell, network fallback.
|
|
e.respondWith(
|
|
caches.match(e.request).then((hit) => hit || fetch(e.request))
|
|
);
|
|
});
|
|
|
|
// Payloadless push: just wake the client, which pulls ciphertext over the socket.
|
|
self.addEventListener("push", (e) => {
|
|
e.waitUntil(
|
|
self.registration.showNotification("Cipher", {
|
|
body: "New encrypted message",
|
|
icon: "/icons/icon.svg",
|
|
badge: "/icons/icon.svg",
|
|
tag: "cipher-msg",
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener("notificationclick", (e) => {
|
|
e.notification.close();
|
|
e.waitUntil(
|
|
self.clients.matchAll({ type: "window" }).then((cl) => {
|
|
for (const c of cl) if ("focus" in c) return c.focus();
|
|
return self.clients.openWindow("/");
|
|
})
|
|
);
|
|
});
|