sync-types.ts
1// Vault sync — type definitions.2//3// Pairing happens once per device pair over a relay rendezvous; all ongoing4// sync afterwards runs over that same relay with Noise KK. There is one5// transport — LAN peer-to-peer sync is retired6// (docs/sync-lan-retirement.md).78// ---------------------------------------------------------------------------9// Device types10// ---------------------------------------------------------------------------1112export type DeviceType = "extension" | "android" | "ios";1314// Numeric tags for DeviceType on the wire — not the string form.15export const DEVICE_TYPE_EXTENSION = 0x01 as const;16export const DEVICE_TYPE_ANDROID = 0x02 as const;17export const DEVICE_TYPE_IOS = 0x03 as const;1819export type DeviceTypeByte =20 | typeof DEVICE_TYPE_EXTENSION21 | typeof DEVICE_TYPE_ANDROID22 | typeof DEVICE_TYPE_IOS;2324// ---------------------------------------------------------------------------25// Long-term paired device registry26// ---------------------------------------------------------------------------2728/**29 * Trust source of a paired-device entry — per v2 brief §4.2.30 *31 * "direct" — established via a pairing-code session with this device.32 * "vouched" — introduced by a directly-paired peer via a signed device33 * manifest during a normal sync. A valid v3 route is admitted34 * automatically as same-vault membership. It is equivalent to35 * direct for sync, but cannot issue manifests itself;36 * surfaced with an "Added automatically" badge in Settings.37 */38export type TrustSource = "direct" | "vouched";3940/**41 * v3 paired-device record. Stored in `chrome.storage.local` — cryptographic42 * keys must not leave the device via a third-party cloud service.43 *44 * `longTermPublicKeyHex` is the counterpart's vault-derived X25519 sync45 * identity static key — see `deriveSyncIdentityKeypair`. Every WebSocket46 * reconnect runs Noise KK with both local static keys, followed by a47 * PAIR_IDENTITY exchange bound to the authenticated keys.48 *49 * `lastKnownIp` is intentionally NOT stored here — it's per-network and50 * lives in `chrome.storage.local` keyed by `longTermPublicKeyHex`. See51 * `getLastKnownIp` / `setLastKnownIp` in `paired-devices.ts`.52 */53export interface PairedDevice {54 deviceId: string;55 deviceName?: string;56 deviceType?: DeviceType;57 longTermPublicKeyHex?: string;58 /**59 * Long-term Ed25519 verification key for the peer's device-manifest60 * signatures (v2 brief §4.3). Derived deterministically from the same61 * master key as `longTermPublicKeyHex`, via `deriveSyncSigningKeypair`.62 */63 signingPublicKeyHex?: string;64 /** v2 brief §4.2 — distinguishes direct-paired vs vouched-via-manifest. */65 trustSource?: TrustSource;66 /**67 * The peer's WebSocket sync port. Current Android hosts use fixed port 4914268 * and announce it inside the encrypted pairing channel.69 */70 wsPort?: number;71 /** Pair-only 32-byte reconnect capability, derived from the first handshake. */72 rendezvousSecretHex?: string;73 /** Validated wss relay used by this pair. */74 relayUrl?: string;75 pairedAt: number;76 lastSyncedAt?: number;77}7879// ---------------------------------------------------------------------------80// v3 wire protocol — phase-2 WebSocket manifest sync81// ---------------------------------------------------------------------------8283/**84 * v3 wire protocol — after reconnect Noise KK is open and PAIR_IDENTITY has85 * been bound to both authenticated static keys.86 *87 * Every frame is JSON, then ChaCha20-Poly1305 encrypted by Noise KK. The88 * WebSocket transport sees only ciphertext.89 *90 * The manifest carries per-entry timestamps; the receiver requests entries91 * where the remote timestamp beats its own. Entries traverse as opaque92 * encrypted blobs — the sync layer never decrypts vault data.93 */94export type SyncMessage =95 | { type: "SYNC_HELLO"; vaultId: string; deviceId: string; version: number; deviceTime: number }96 | { type: "SYNC_MANIFEST"; entries: Record<string, number>; tombstones: Record<string, number> }97 | { type: "SYNC_REQUEST"; entryIds: string[] }98 | { type: "SYNC_ENTRIES"; entries: Record<string, string> }99 | { type: "SYNC_TOMBSTONES"; tombstones: Record<string, number> }100 | { type: "SYNC_DEVICE_MANIFEST"; manifest: SignedDeviceManifest | null }101 | { type: "SYNC_DONE"; entriesReceived?: number; entriesSent?: number }102 | { type: "SYNC_ERROR"; error: string };103104// ---------------------------------------------------------------------------105// Browser-to-browser relay frames (phone forwards; see peach-android's106// RelayFrames.kt / RelayHub.kt, the source of truth this mirrors — NOT the107// older 4-frame PEACH_SYNC_RELAY_DESIGN.md sketch, which is stale).108//109// Type-only for now: nothing in this repo sends or handles these yet — no110// browser can listen, so relaying laptop-to-laptop traffic through a shared111// phone is unbuilt browser-side work, tracked separately. This exists so112// that work starts from the real wire contract instead of the stale doc.113// ---------------------------------------------------------------------------114115/** RelayMessage.targetDeviceId/peerDeviceId are the destination's Noise116 * static public key as lowercase hex — NOT `PairedDevice.deviceId`. */117export type RelayMessage =118 | { type: "RELAY_REQUEST"; targetDeviceId: string }119 | { type: "RELAY_ACCEPT" }120 | { type: "RELAY_UNAVAILABLE" }121 | { type: "RELAY_DISABLED" }122 | { type: "RELAY_OPEN"; peerDeviceId: string }123 | { type: "RELAY_FRAME"; payload: string }124 | { type: "RELAY_CLOSE" };125126// ---------------------------------------------------------------------------127// v3 device manifest — propagates trust between mobile devices (v2 brief §4.3)128// ---------------------------------------------------------------------------129130/** 0x03 adds each peer edge's relay URL so a newly introduced pair can agree131 * on a route without depending on the introducing device. Coordinated wire132 * change with peach-android. */133export const DEVICE_MANIFEST_VERSION = 0x03 as const;134/** Manifests older than this are silently rejected (v2 brief §4.3). */135export const DEVICE_MANIFEST_FRESHNESS_MS = 5 * 60_000;136137/**138 * One device entry inside a signed manifest. `longTermPublicKeyHex` is the139 * X25519 sync identity key the receiver will use for Noise KK reconnect.140 * `signingPublicKeyHex` is the141 * peer's Ed25519 manifest-verification key — needed so a vouched mobile142 * can in turn verify manifests issued by the same vault from a different143 * device.144 */145export interface ManifestDeviceEntry {146 deviceId: string;147 deviceName: string;148 deviceType: DeviceType;149 longTermPublicKeyHex: string;150 signingPublicKeyHex: string;151 /**152 * Optional sync WebSocket port for native/mobile hosts. Preserving this in153 * vouched entries lets mobile peers connect without another audio pairing.154 */155 wsPort?: number;156 /** Relay the issuer currently uses for this peer edge. */157 relayUrl?: string;158 pairedAt: number;159}160161/**162 * A device that must no longer be synced with. Revocation is TERMINAL: once a163 * deviceId OR its long-term key appears here, no manifest may re-vouch it and164 * no pending entry carrying it may be confirmed. That is deliberate —165 * tombstone-style "newest timestamp wins" ordering would let a peer resurrect a166 * revoked device just by issuing a manifest with a fresh `pairedAt`, and every167 * timestamp in a manifest is peer-supplied. Re-admitting a device requires a168 * direct, SAS-confirmed re-pair with a physically present user.169 *170 * HONEST LIMIT: every mesh member holds the vault root key, and signing keys171 * are derived from it (`deriveSyncSigningKeypair(rootKey, deviceId)`), so these172 * signatures prove *vault membership*, not device identity. A revoked device173 * still holds the root key and, running modified software, can ignore its own174 * revocation. This mechanism stops honest peers from talking to a revoked175 * device; it is not a cryptographic ejection. See HOLE 4 — a genuinely176 * compromised device needs the master password and recovery words rotated.177 */178export interface DeviceRevocation {179 deviceId: string;180 /** X25519 sync identity key at the moment of revocation. Load-bearing, not181 * descriptive: `deviceId` is whatever an issuing manifest says it is, so the182 * key is what re-vouching is actually matched against. `0`×64 means no key183 * was ever stored for the device and matches nothing. */184 longTermPublicKeyHex: string;185 revokedAt: number;186 /** deviceId that issued the revocation, for display only. */187 revokedByDeviceId: string;188}189190/** Body that gets canonicalised + signed. Wire format JSON-stable. */191export interface DeviceManifestBody {192 manifestVersion: number;193 issuerDeviceId: string;194 issuedAt: number;195 devices: ManifestDeviceEntry[];196 revoked: DeviceRevocation[];197}198199export interface SignedDeviceManifest {200 body: DeviceManifestBody;201 /** Hex-encoded 64-byte Ed25519 signature over canonicalised body. */202 signatureHex: string;203}204205export type SyncStatusV3 = "up_to_date" | "syncing" | "unreachable" | "never";