sync-types.ts
1// Vault sync — type definitions (Master Brief v2).2//3// v3 — code pairing once per device pair (Phase 1). All ongoing vault sync4// after that runs over a direct WebSocket on the local network (Phase 2).5// The two phases never overlap.67// ---------------------------------------------------------------------------8// Device types9// ---------------------------------------------------------------------------1011export type DeviceType = "extension" | "android" | "ios";1213// Numeric tags for DeviceType on the wire — not the string form.14export const DEVICE_TYPE_EXTENSION = 0x01 as const;15export const DEVICE_TYPE_ANDROID = 0x02 as const;16export const DEVICE_TYPE_IOS = 0x03 as const;1718export type DeviceTypeByte =19 | typeof DEVICE_TYPE_EXTENSION20 | typeof DEVICE_TYPE_ANDROID21 | typeof DEVICE_TYPE_IOS;2223// ---------------------------------------------------------------------------24// Long-term paired device registry25// ---------------------------------------------------------------------------2627/**28 * Trust source of a paired-device entry — per v2 brief §4.2.29 *30 * "direct" — established via a pairing-code session with this device.31 * "vouched" — introduced by a directly-paired peer via a signed device32 * manifest during a normal sync, then confirmed by the user33 * (MV-7). Functionally equivalent to direct for sync purposes;34 * surfaced with an "Added automatically" badge in Settings.35 */36export type TrustSource = "direct" | "vouched";3738/**39 * v3 paired-device record. Stored in `chrome.storage.local` — cryptographic40 * keys must not leave the device via a third-party cloud service.41 *42 * `longTermPublicKeyHex` is the counterpart's vault-derived X25519 sync43 * identity static key — see `deriveSyncIdentityKeypair`. Every WebSocket44 * reconnect runs Noise NK against this key (authenticating the responder45 * only) followed by a PAIR_IDENTITY exchange checked against this registry —46 * see HANDSHAKE_STANDARDIZE_ON_NK.md.47 *48 * `lastKnownIp` is intentionally NOT stored here — it's per-network and49 * lives in `chrome.storage.local` keyed by `longTermPublicKeyHex`. See50 * `getLastKnownIp` / `setLastKnownIp` in `paired-devices.ts`.51 */52export interface PairedDevice {53 deviceId: string;54 deviceName?: string;55 deviceType?: DeviceType;56 longTermPublicKeyHex?: string;57 /**58 * Long-term Ed25519 verification key for the peer's device-manifest59 * signatures (v2 brief §4.3). Derived deterministically from the same60 * master key as `longTermPublicKeyHex`, via `deriveSyncSigningKeypair`.61 */62 signingPublicKeyHex?: string;63 /** v2 brief §4.2 — distinguishes direct-paired vs vouched-via-manifest. */64 trustSource?: TrustSource;65 /**66 * The peer's WebSocket sync port. Mobile hosts pick a random high port67 * (49152–65535) at first launch and announce it inside the encrypted68 * pairing channel; the extension caches it here.69 */70 wsPort?: number;71 pairedAt: number;72 lastSyncedAt?: number;73}7475// ---------------------------------------------------------------------------76// v3 wire protocol — phase-2 WebSocket manifest sync77// ---------------------------------------------------------------------------7879/**80 * v3 wire protocol — after Noise NK is open and PAIR_IDENTITY has been81 * exchanged (see HANDSHAKE_STANDARDIZE_ON_NK.md).82 *83 * Every frame is JSON, then ChaCha20-Poly1305 encrypted by Noise NK. The84 * WebSocket transport sees only ciphertext.85 *86 * The manifest carries per-entry timestamps; the receiver requests entries87 * where the remote timestamp beats its own. Entries traverse as opaque88 * encrypted blobs — the sync layer never decrypts vault data.89 */90export type SyncMessage =91 | { type: "SYNC_HELLO"; vaultId: string; deviceId: string; version: number; deviceTime: number }92 | { type: "SYNC_MANIFEST"; entries: Record<string, number>; tombstones: Record<string, number> }93 | { type: "SYNC_REQUEST"; entryIds: string[] }94 | { type: "SYNC_ENTRIES"; entries: Record<string, string> }95 | { type: "SYNC_TOMBSTONES"; tombstones: Record<string, number> }96 | { type: "SYNC_DEVICE_MANIFEST"; manifest: SignedDeviceManifest | null }97 | { type: "SYNC_DONE"; entriesReceived?: number; entriesSent?: number }98 | { type: "SYNC_ERROR"; error: string };99100// ---------------------------------------------------------------------------101// Browser-to-browser relay frames (phone forwards; see peach-android's102// RelayFrames.kt / RelayHub.kt, the source of truth this mirrors — NOT the103// older 4-frame PEACH_SYNC_RELAY_DESIGN.md sketch, which is stale).104//105// Type-only for now: nothing in this repo sends or handles these yet — no106// browser can listen, so relaying laptop-to-laptop traffic through a shared107// phone is unbuilt browser-side work, tracked separately. This exists so108// that work starts from the real wire contract instead of the stale doc.109// ---------------------------------------------------------------------------110111/** RelayMessage.targetDeviceId/peerDeviceId are the destination's Noise NK112 * static public key as lowercase hex — NOT `PairedDevice.deviceId`. */113export type RelayMessage =114 | { type: "RELAY_REQUEST"; targetDeviceId: string }115 | { type: "RELAY_ACCEPT" }116 | { type: "RELAY_UNAVAILABLE" }117 | { type: "RELAY_DISABLED" }118 | { type: "RELAY_OPEN"; peerDeviceId: string }119 | { type: "RELAY_FRAME"; payload: string }120 | { type: "RELAY_CLOSE" };121122// ---------------------------------------------------------------------------123// v3 device manifest — propagates trust between mobile devices (v2 brief §4.3)124// ---------------------------------------------------------------------------125126export const DEVICE_MANIFEST_VERSION = 0x01 as const;127/** Manifests older than this are silently rejected (v2 brief §4.3). */128export const DEVICE_MANIFEST_FRESHNESS_MS = 5 * 60_000;129130/**131 * One device entry inside a signed manifest. `longTermPublicKeyHex` is the132 * X25519 sync identity key the receiver will use for Noise NK, exactly as133 * if the entry had been audio-paired locally. `signingPublicKeyHex` is the134 * peer's Ed25519 manifest-verification key — needed so a vouched mobile135 * can in turn verify manifests issued by the same vault from a different136 * device.137 */138export interface ManifestDeviceEntry {139 deviceId: string;140 deviceName: string;141 deviceType: DeviceType;142 longTermPublicKeyHex: string;143 signingPublicKeyHex: string;144 /**145 * Optional sync WebSocket port for native/mobile hosts. Preserving this in146 * vouched entries lets mobile peers connect without another audio pairing.147 */148 wsPort?: number;149 pairedAt: number;150}151152/** Body that gets canonicalised + signed. Wire format JSON-stable. */153export interface DeviceManifestBody {154 manifestVersion: number;155 issuerDeviceId: string;156 issuedAt: number;157 devices: ManifestDeviceEntry[];158}159160export interface SignedDeviceManifest {161 body: DeviceManifestBody;162 /** Hex-encoded 64-byte Ed25519 signature over canonicalised body. */163 signatureHex: string;164}165166export type SyncStatusV3 = "up_to_date" | "syncing" | "unreachable" | "never";