sync-vault-adapter.test.ts

48.8 KB · raw

1import { beforeEach, describe, expect, it, vi } from "vitest";23type StorageShape = Record<string, unknown>;4const MESH_RELAY = "wss://relay.peachpasswords.com";56function makeChromeMock(local: StorageShape, sync: StorageShape): typeof chrome {7  const area = (store: StorageShape) => ({8    get: vi.fn(async (key?: string | string[]) => {9      if (!key) return store;10      if (typeof key === "string") return { [key]: store[key] };11      return key.reduce<Record<string, unknown>>((acc, k) => { acc[k] = store[k]; return acc; }, {});12    }),13    set: vi.fn(async (next: Record<string, unknown>) => { Object.assign(store, next); }),14    remove: vi.fn(async (keys: string | string[]) => {15      const list = Array.isArray(keys) ? keys : [keys];16      for (const k of list) delete store[k];17    }),18  });19  return {20    storage: { local: area(local), sync: area(sync) },21    alarms: {22      clear: vi.fn(),23      create: vi.fn(),24      onAlarm: { addListener: vi.fn() },25    },26    idle: { setDetectionInterval: vi.fn(), onStateChanged: { addListener: vi.fn() } },27    runtime: { sendMessage: vi.fn(async () => undefined) },28  } as unknown as typeof chrome;29}3031describe("SyncVaultAdapter", () => {32  let local: StorageShape;33  let sync: StorageShape;3435  beforeEach(() => {36    vi.resetModules();37    local = {};38    sync = {};39    (globalThis as { chrome?: typeof chrome }).chrome = makeChromeMock(local, sync);40    vi.stubGlobal("fetch", vi.fn(async () => { throw new Error("network disabled"); }));41  });4243  async function setupVault(): Promise<{ vault: typeof import("./vault"); adapter: import("./sync-coordinator").SyncVaultAdapter }> {44    const vault = await import("./vault");45    await vault.createVault("correct horse battery staple");46    const { createSyncVaultAdapter } = await import("./sync-vault-adapter");47    const adapter = createSyncVaultAdapter();48    return { vault, adapter };49  }5051  async function signingPubkeyHex(deviceId: string): Promise<string> {52    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");53    const { pubkeyToHex } = await import("../shared/sync-identity");54    const kp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x42), deviceId);55    return pubkeyToHex(kp.publicKeyRaw);56  }5758  it("computeVaultId returns a stable 64-char hex string for the same masterKey", async () => {59    const { adapter } = await setupVault();60    const id1 = await adapter.computeVaultId();61    const id2 = await adapter.computeVaultId();62    expect(id1).toBe(id2);63    expect(id1).toMatch(/^[0-9a-f]{64}$/);64  });6566  it("vault_id and sync signing key are unchanged after a password change (SEV-2-B)", async () => {67    const { vault, adapter } = await setupVault();68    const deviceId = await adapter.getOurDeviceId();69    const vaultIdBefore = await adapter.computeVaultId();7071    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");72    const { pubkeyToHex } = await import("../shared/sync-identity");73    const rootBefore = vault.getRootKeyHex()!;74    const unhex = (h: string) => Uint8Array.from(h.match(/../g)!.map((b) => parseInt(b, 16)));75    const signBefore = pubkeyToHex(76      (await deriveSyncSigningKeypair(unhex(rootBefore), deviceId)).publicKeyRaw,77    );7879    expect(await vault.changePassword("correct horse battery staple", "a different passphrase")).toBe(true);80    vault.lock();81    expect(await vault.unlock("a different passphrase")).toBe(true);8283    // vault_id derives from the stable root key, so it must not move.84    expect(await adapter.computeVaultId()).toBe(vaultIdBefore);85    const rootAfter = vault.getRootKeyHex()!;86    expect(rootAfter).toBe(rootBefore);87    const signAfter = pubkeyToHex(88      (await deriveSyncSigningKeypair(unhex(rootAfter), deviceId)).publicKeyRaw,89    );90    expect(signAfter).toBe(signBefore);91  });9293  it("getOurDeviceId is stable across calls and a fresh UUID v4 on first call", async () => {94    const { adapter } = await setupVault();95    const id1 = await adapter.getOurDeviceId();96    const id2 = await adapter.getOurDeviceId();97    expect(id1).toBe(id2);98    expect(id1).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);99  });100101  it("buildEntryManifest reports {id: modifiedAt} for live entries only", async () => {102    const { vault, adapter } = await setupVault();103    const a = await vault.addEntry({ type: "login", title: "Alpha", password: "pw" });104    const b = await vault.addEntry({ type: "login", title: "Beta", password: "pw" });105    await vault.deleteEntry(b.id);106    const m = await adapter.buildEntryManifest();107    expect(Object.keys(m).sort()).toEqual([a.id]);108    expect(m[a.id]).toBe(a.modifiedAt);109  });110111  it("buildTombstoneManifest reports {id: deletedAt} for soft-deleted entries", async () => {112    const { vault, adapter } = await setupVault();113    const a = await vault.addEntry({ type: "login", title: "Alpha", password: "pw" });114    await vault.deleteEntry(a.id);115    const t = await adapter.buildTombstoneManifest();116    expect(Object.keys(t)).toEqual([a.id]);117    expect(typeof t[a.id]).toBe("number");118    expect(t[a.id]).toBeGreaterThan(0);119  });120121  it("buildTombstoneManifest retains ids after a full-entry purge", async () => {122    const { vault, adapter } = await setupVault();123    const entry = await vault.addEntry({ type: "login", title: "Purged", password: "pw" });124    await vault.deleteEntry(entry.id);125    const deletedAt = vault.getDeletedEntries().find((item) => item.id === entry.id)!.deletedAt!;126127    await vault.purgeEntry(entry.id);128129    expect(await adapter.buildTombstoneManifest()).toEqual({ [entry.id]: deletedAt });130  });131132  it("getEncryptedEntries returns opaque base64 strings keyed by id", async () => {133    const { vault, adapter } = await setupVault();134    const a = await vault.addEntry({ type: "login", title: "Alpha", password: "pw" });135    const env = await adapter.getEncryptedEntries([a.id]);136    expect(Object.keys(env)).toEqual([a.id]);137    expect(env[a.id]).toMatch(/^[A-Za-z0-9+/]+=*$/);138    // Plaintext title must not be visible in the envelope.139    expect(env[a.id]).not.toContain("Alpha");140  });141142  it("getEncryptedEntries silently skips unknown ids (peer may have a manifest item we dropped)", async () => {143    const { adapter } = await setupVault();144    const env = await adapter.getEncryptedEntries(["does-not-exist"]);145    expect(env).toEqual({});146  });147148  it("encryption uses a fresh nonce per call — same entry yields different envelopes", async () => {149    const { vault, adapter } = await setupVault();150    const a = await vault.addEntry({ type: "login", title: "Alpha", password: "pw" });151    const e1 = await adapter.getEncryptedEntries([a.id]);152    const e2 = await adapter.getEncryptedEntries([a.id]);153    expect(e1[a.id]).not.toBe(e2[a.id]);154  });155156  it("round-trips: encrypt on one adapter, apply on a fresh import of the same vault", async () => {157    const { vault, adapter } = await setupVault();158    const a = await vault.addEntry({ type: "login", title: "Alpha", password: "secret", url: "https://a.example" });159    const envelopes = await adapter.getEncryptedEntries([a.id]);160    // Simulate a remote vault by bumping modifiedAt on the envelope's source161    // and then applying it back — we can't easily fork the module's storage,162    // so we just verify decrypt path by overwriting modifiedAt locally.163    // Delete the local entry first, then apply the envelope — applyMergedRemoteEntries164    // will re-add it because the local copy is tombstoned with an OLDER ts (since165    // we set modifiedAt forward by 1s in the new envelope payload).166    // Instead, simpler proof: apply the envelope as-is to a fresh adapter import.167    vault.lock();168    await vault.unlock("correct horse battery staple");169    const { createSyncVaultAdapter } = await import("./sync-vault-adapter");170    const adapter2 = createSyncVaultAdapter();171    // applyEntries should be a no-op when local mtime >= envelope mtime172    await adapter2.applyEntries(envelopes);173    expect(vault.getEntries().find((e) => e.id === a.id)?.title).toBe("Alpha");174  });175176  it("applyEntries decrypts and merges newer remote entries (last-write-wins)", async () => {177    const { vault, adapter } = await setupVault();178    const local = await vault.addEntry({ type: "login", title: "Original", password: "pw" });179    // Manually produce a fresher remote envelope by encrypting an updated entry shape.180    // We can't easily synthesise this without the entry key — so we use a second181    // adapter instance backed by the same vault, after mutating the underlying entry.182    await vault.updateEntry(local.id, { title: "Updated" });183    const newer = vault.getEntries().find((e) => e.id === local.id);184    expect(newer?.title).toBe("Updated");185    const envelopes = await adapter.getEncryptedEntries([local.id]);186    // Now revert the local entry to an older mtime by re-creating from scratch via187    // applyMergedRemoteEntries — but we'd loop. The most reliable proof of the188    // decrypt+merge path is that applyEntries on a fresher envelope keeps the189    // newer state, and on an older envelope is a no-op. We've covered the no-op190    // path above; for the apply path, simulate by replacing the in-memory entry191    // with an older copy and re-running applyEntries.192    const older: typeof local = { ...local, modifiedAt: local.modifiedAt - 1000, title: "Stale" };193    await vault.restoreEntries([older], "replace");194    expect(vault.getEntries().find((e) => e.id === local.id)?.title).toBe("Stale");195    await adapter.applyEntries(envelopes);196    expect(vault.getEntries().find((e) => e.id === local.id)?.title).toBe("Updated");197  });198199  it("applyEntries rejects envelopes whose decrypted id differs from the map key", async () => {200    const { vault, adapter } = await setupVault();201    const a = await vault.addEntry({ type: "login", title: "Alpha", password: "pw" });202    const env = await adapter.getEncryptedEntries([a.id]);203    // Re-key the envelope under a different id — adapter must refuse to merge it.204    const swapped: Record<string, string> = { "wrong-id": env[a.id] };205    await adapter.applyEntries(swapped);206    // Local entry remains unchanged; no new entry under "wrong-id"207    expect(vault.getEntries().map((e) => e.id)).toEqual([a.id]);208  });209210  it("entry_envelope_roundtrips_with_separated_info (HKDF separator)", async () => {211    const { vault, adapter } = await setupVault();212    const a = await vault.addEntry({ type: "login", title: "Original", password: "pw" });213    await vault.updateEntry(a.id, { title: "Fresh" });214    const envelopes = await adapter.getEncryptedEntries([a.id]);215    // Roll local back to an older copy, then apply: encrypt+decrypt both use the216    // null-byte-separated info, so the round-trip must succeed.217    const stale = { ...a, modifiedAt: a.modifiedAt - 1000, title: "Stale" };218    await vault.restoreEntries([stale], "replace");219    await adapter.applyEntries(envelopes);220    expect(vault.getEntries().find((e) => e.id === a.id)?.title).toBe("Fresh");221  });222223  it("legacy_info_envelope_fails_closed (HKDF separator wire break)", async () => {224    // Mint an envelope under the OLD, unseparated info (`entryId + suffix`) and225    // assert the patched adapter cannot decrypt it — it drops silently. Locks in226    // the conscious wire break and serves as the fixture for any future migration.227    const { vault, adapter } = await setupVault();228    const { HKDF } = await import("@stablelib/hkdf");229    const { SHA256 } = await import("@stablelib/sha256");230231    const masterHex = vault.getRootKeyHex()!;232    const masterKey = Uint8Array.from(masterHex.match(/../g)!.map((b) => parseInt(b, 16)));233    const entryId = crypto.randomUUID();234    const legacyInfo = new TextEncoder().encode(entryId + "sync-entry-v1"); // no \x00235    const raw = new HKDF(SHA256, masterKey, new Uint8Array(0), legacyInfo).expand(32);236    const key = await crypto.subtle.importKey("raw", raw.buffer as ArrayBuffer, { name: "AES-GCM" }, false, ["encrypt"]);237238    const entry = { id: entryId, type: "login", title: "LegacyOnly", password: "pw", createdAt: Date.now(), modifiedAt: Date.now() };239    const nonce = crypto.getRandomValues(new Uint8Array(12));240    const aad = new TextEncoder().encode("peach-sync-entry-v1");241    const ct = new Uint8Array(242      await crypto.subtle.encrypt(243        { name: "AES-GCM", iv: nonce.buffer as ArrayBuffer, additionalData: aad.buffer as ArrayBuffer, tagLength: 128 },244        key,245        new TextEncoder().encode(JSON.stringify(entry)).buffer as ArrayBuffer,246      ),247    );248    const envelope = new Uint8Array(nonce.length + ct.length);249    envelope.set(nonce, 0);250    envelope.set(ct, nonce.length);251    const legacyB64 = Buffer.from(envelope).toString("base64");252253    await adapter.applyEntries({ [entryId]: legacyB64 });254    // Decrypt fails under the new separated info → envelope dropped, entry absent.255    expect(vault.getEntries().some((e) => e.id === entryId)).toBe(false);256  });257258  it("applyTombstones soft-deletes locally-live entries with newer deletedAt", async () => {259    const { vault, adapter } = await setupVault();260    const a = await vault.addEntry({ type: "login", title: "Alpha", password: "pw" });261    await adapter.applyTombstones({ [a.id]: a.modifiedAt + 10_000 });262    expect(vault.getEntries().find((e) => e.id === a.id)).toBeUndefined();263    expect(vault.getDeletedEntries().find((e) => e.id === a.id)).toBeDefined();264  });265266  it("applyTombstones leaves entries alone when local modifiedAt is newer", async () => {267    const { vault, adapter } = await setupVault();268    const a = await vault.addEntry({ type: "login", title: "Alpha", password: "pw" });269    await adapter.applyTombstones({ [a.id]: a.modifiedAt - 10_000 });270    expect(vault.getEntries().find((e) => e.id === a.id)).toBeDefined();271  });272273  // --- MV-3: sync receive bounds ---274275  it("MV-3: applyEntries throws when the batch exceeds MAX_ENTRY_COUNT", async () => {276    const { adapter } = await setupVault();277    const { SYNC_LIMITS } = await import("../shared/sync-limits");278    const tooMany: Record<string, string> = {};279    for (let i = 0; i <= SYNC_LIMITS.MAX_ENTRY_COUNT; i++) tooMany[`id-${i}`] = "AAAA";280    await expect(adapter.applyEntries(tooMany)).rejects.toThrow(/exceeds limit/);281  });282283  it("MV-3: applyEntries skips an oversized envelope but still applies valid ones", async () => {284    const { vault, adapter } = await setupVault();285    const { SYNC_LIMITS } = await import("../shared/sync-limits");286    const local = await vault.addEntry({ type: "login", title: "Original", password: "pw" });287    await vault.updateEntry(local.id, { title: "Updated" });288    const envelopes = await adapter.getEncryptedEntries([local.id]);289    // Make the local copy stale so the valid envelope would win on merge.290    const older = { ...local, modifiedAt: local.modifiedAt - 1000, title: "Stale" };291    await vault.restoreEntries([older], "replace");292    expect(vault.getEntries().find((e) => e.id === local.id)?.title).toBe("Stale");293294    const oversized = "A".repeat(295      Math.ceil((SYNC_LIMITS.MAX_ENTRY_PLAINTEXT_BYTES * 4) / 3) + 128,296    );297    await adapter.applyEntries({ ...envelopes, "oversized-id": oversized });298299    // The valid envelope applied; the oversized one was skipped (never decrypted).300    expect(vault.getEntries().find((e) => e.id === local.id)?.title).toBe("Updated");301    expect(vault.getEntries().some((e) => e.id === "oversized-id")).toBe(false);302  });303304  it("MV-3: applyTombstones throws when the batch exceeds MAX_TOMBSTONE_COUNT", async () => {305    const { adapter } = await setupVault();306    const { SYNC_LIMITS } = await import("../shared/sync-limits");307    const tooMany: Record<string, number> = {};308    for (let i = 0; i <= SYNC_LIMITS.MAX_TOMBSTONE_COUNT; i++) tooMany[`id-${i}`] = i + 1;309    await expect(adapter.applyTombstones(tooMany)).rejects.toThrow(/exceeds limit/);310  });311312  it("buildOutgoingDeviceManifest signs manifestable paired devices and skips legacy entries without signing keys", async () => {313    const { adapter } = await setupVault();314    const { upsertPairedDevice } = await import("./paired-devices");315    await upsertPairedDevice({316      deviceId: "android-1",317      deviceName: "Pixel",318      deviceType: "android",319      longTermPublicKeyHex: "a".repeat(64),320      signingPublicKeyHex: await signingPubkeyHex("android-1"),321      wsPort: 49152,322      relayUrl: MESH_RELAY,323      pairedAt: 1_700_000_000_000,324      trustSource: "direct",325    });326    await upsertPairedDevice({327      deviceId: "legacy-android",328      deviceName: "Legacy Pixel",329      deviceType: "android",330      longTermPublicKeyHex: "b".repeat(64),331      pairedAt: 1_700_000_000_100,332      trustSource: "direct",333    });334335    const manifest = await adapter.buildOutgoingDeviceManifest?.();336    expect(manifest).toBeDefined();337    expect(manifest?.signatureHex).toMatch(/^[0-9a-f]{128}$/);338    expect(manifest?.body.devices).toHaveLength(1);339    expect(manifest?.body.devices[0]).toMatchObject({340      deviceId: "android-1",341      deviceName: "Pixel",342      deviceType: "android",343      longTermPublicKeyHex: "a".repeat(64),344      wsPort: 49152,345      relayUrl: MESH_RELAY,346    });347  });348349  it("v3 auto-admits same-vault manifest peers with an independent mesh route", async () => {350    const { vault, adapter } = await setupVault();351    const { buildSignedDeviceManifest } = await import("../shared/device-manifest");352    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");353    const { pubkeyToHex } = await import("../shared/sync-identity");354    const {355      findPairedDeviceById,356      listPairedDevices,357      upsertPairedDevice,358    } = await import("./paired-devices");359    const { listPendingVouchedDevices } = await import(360      "./pending-vouched-devices"361    );362363    const issuerId = "extension-issuer";364    const issuerKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x24), issuerId);365    await upsertPairedDevice({366      deviceId: issuerId,367      deviceName: "Firefox",368      deviceType: "extension",369      longTermPublicKeyHex: "c".repeat(64),370      signingPublicKeyHex: pubkeyToHex(issuerKp.publicKeyRaw),371      pairedAt: 1_700_000_000_000,372      trustSource: "direct",373    });374    await upsertPairedDevice({375      deviceId: "android-existing",376      deviceName: "Existing Pixel",377      deviceType: "android",378      longTermPublicKeyHex: "d".repeat(64),379      signingPublicKeyHex: await signingPubkeyHex("android-existing-old"),380      pairedAt: 1_700_000_000_010,381      trustSource: "direct",382    });383384    const manifest = await buildSignedDeviceManifest(385      issuerId,386      [387        {388          deviceId: "android-new",389          deviceName: "New Pixel",390          deviceType: "android",391          longTermPublicKeyHex: "e".repeat(64),392          signingPublicKeyHex: await signingPubkeyHex("android-new"),393          wsPort: 51234,394          relayUrl: MESH_RELAY,395          pairedAt: 1_700_000_000_020,396        },397        {398          deviceId: "android-existing",399          deviceName: "Existing Pixel renamed",400          deviceType: "android",401          longTermPublicKeyHex: "d".repeat(64),402          signingPublicKeyHex: await signingPubkeyHex("android-existing-old"),403          wsPort: 52345,404          relayUrl: MESH_RELAY,405          pairedAt: 1_700_000_000_030,406        },407      ],408      issuerKp.privateKey,409    );410411    await adapter.applyIncomingDeviceManifest?.(412      manifest,413      new Uint8Array(32).fill(0xcc),414    );415416    expect(await findPairedDeviceById("android-new")).toMatchObject({417      deviceName: "New Pixel",418      trustSource: "vouched",419      relayUrl: MESH_RELAY,420    });421    // PB-26-213: the row carries no reusable secret; it resolves from the422    // encrypted pair-secret record instead.423    expect((await findPairedDeviceById("android-new"))?.rendezvousSecretHex).toBeUndefined();424    const { getPairSecret } = await import("./pair-secrets");425    await expect(getPairSecret("android-new", vault.getRootKeyHex())).resolves.toMatch(426      /^[0-9a-f]{64}$/,427    );428    const pending = await listPendingVouchedDevices();429    expect(pending).toEqual([]);430431    // Immutable identity binding rejects remote key rewrites; metadata and432    // routing fields from the signed manifest still update.433    const existing = await findPairedDeviceById("android-existing");434    expect(existing).toMatchObject({435      deviceName: "Existing Pixel",436      longTermPublicKeyHex: "d".repeat(64),437      trustSource: "direct",438      wsPort: 52345,439    });440    expect((await listPairedDevices()).map((d) => d.deviceId).sort()).toEqual([441      "android-existing",442      "android-new",443      "extension-issuer",444    ]);445  });446447  it("rejects manifest key replacement for an existing device", async () => {448    const { adapter } = await setupVault();449    const { buildSignedDeviceManifest } = await import("../shared/device-manifest");450    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");451    const { pubkeyToHex } = await import("../shared/sync-identity");452    const { findPairedDeviceById, upsertPairedDevice } = await import("./paired-devices");453    const issuerId = "extension-issuer";454    const issuerKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x24), issuerId);455    await upsertPairedDevice({456      deviceId: issuerId,457      deviceName: "Firefox",458      deviceType: "extension",459      longTermPublicKeyHex: "c".repeat(64),460      signingPublicKeyHex: pubkeyToHex(issuerKp.publicKeyRaw),461      pairedAt: 1,462      trustSource: "direct",463    });464    await upsertPairedDevice({465      deviceId: "android-existing",466      deviceName: "Pixel",467      deviceType: "android",468      longTermPublicKeyHex: "d".repeat(64),469      signingPublicKeyHex: await signingPubkeyHex("android-existing-old"),470      pairedAt: 2,471      trustSource: "direct",472    });473    const manifest = await buildSignedDeviceManifest(474      issuerId,475      [{476        deviceId: "android-existing",477        deviceName: "Pixel",478        deviceType: "android",479        longTermPublicKeyHex: "f".repeat(64),480        signingPublicKeyHex: await signingPubkeyHex("android-existing-new"),481        wsPort: 52345,482        pairedAt: 3,483      }],484      issuerKp.privateKey,485    );486    await expect(487      adapter.applyIncomingDeviceManifest?.(manifest, new Uint8Array(32).fill(0xcc)),488    ).rejects.toThrow("device_identity_immutable");489    await expect(findPairedDeviceById("android-existing")).resolves.toMatchObject({490      longTermPublicKeyHex: "d".repeat(64),491    });492  });493494  it("rejects manifest replacement even when the device name changes", async () => {495    const { adapter } = await setupVault();496    const { buildSignedDeviceManifest } = await import("../shared/device-manifest");497    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");498    const { pubkeyToHex } = await import("../shared/sync-identity");499    const { findPairedDeviceById, upsertPairedDevice } = await import("./paired-devices");500    const issuerId = "hostile-issuer";501    const issuerKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x26), issuerId);502    await upsertPairedDevice({503      deviceId: issuerId,504      deviceName: "Old Laptop",505      deviceType: "extension",506      longTermPublicKeyHex: "c".repeat(64),507      signingPublicKeyHex: pubkeyToHex(issuerKp.publicKeyRaw),508      pairedAt: 1,509      trustSource: "direct",510    });511    await upsertPairedDevice({512      deviceId: "android-existing",513      deviceName: "Spare Tablet",514      deviceType: "android",515      longTermPublicKeyHex: "d".repeat(64),516      signingPublicKeyHex: await signingPubkeyHex("android-existing-old"),517      pairedAt: 2,518      trustSource: "direct",519    });520    const manifest = await buildSignedDeviceManifest(521      issuerId,522      [{523        deviceId: "android-existing",524        deviceName: "June's Primary Phone",525        deviceType: "android",526        longTermPublicKeyHex: "f".repeat(64),527        signingPublicKeyHex: await signingPubkeyHex("android-existing-new"),528        wsPort: 52345,529        pairedAt: 3,530      }],531      issuerKp.privateKey,532    );533    await expect(534      adapter.applyIncomingDeviceManifest?.(manifest, new Uint8Array(32).fill(0xcc)),535    ).rejects.toThrow("device_identity_immutable");536    await expect(findPairedDeviceById("android-existing")).resolves.toMatchObject({537      deviceName: "Spare Tablet",538      longTermPublicKeyHex: "d".repeat(64),539    });540  });541542  it("§C: a vouched peer's revocation evicts the device and permanently blocks re-vouching", async () => {543    const { adapter } = await setupVault();544    const { buildSignedDeviceManifest, signDeviceRevocation } = await import("../shared/device-manifest");545    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");546    const { pubkeyToHex } = await import("../shared/sync-identity");547    const { findPairedDeviceById, isDeviceRevoked, upsertPairedDevice } =548      await import("./paired-devices");549550    const issuerId = "phone-issuer";551    const issuerKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x27), issuerId);552    await upsertPairedDevice({553      deviceId: issuerId,554      deviceName: "Pixel",555      deviceType: "android",556      longTermPublicKeyHex: "c".repeat(64),557      signingPublicKeyHex: pubkeyToHex(issuerKp.publicKeyRaw),558      pairedAt: 1,559      trustSource: "vouched",560    });561    await upsertPairedDevice({562      deviceId: "stolen-laptop",563      deviceName: "Old Laptop",564      deviceType: "extension",565      longTermPublicKeyHex: "d".repeat(64),566      signingPublicKeyHex: await signingPubkeyHex("stolen-laptop"),567      pairedAt: 2,568      trustSource: "direct",569    });570571    const signedRevocation = await signDeviceRevocation(572      {573        deviceId: "stolen-laptop",574        longTermPublicKeyHex: "d".repeat(64),575        revokedAt: Date.now(),576        revokedByDeviceId: issuerId,577      },578      issuerKp.privateKey,579    );580    const revocation = await buildSignedDeviceManifest(581      issuerId,582      [],583      issuerKp.privateKey,584      { revoked: [signedRevocation] },585    );586    await adapter.applyIncomingDeviceManifest?.(587      revocation,588      new Uint8Array(32).fill(0xcc),589    );590591    expect(await findPairedDeviceById("stolen-laptop")).toBeNull();592    expect(await isDeviceRevoked("stolen-laptop")).toBe(true);593594    // The stolen device is now vouched back in by a later manifest carrying a595    // fresh pairedAt — exactly the resurrection a timestamp-ordered design596    // would allow. It must not come back.597    const revouch = await buildSignedDeviceManifest(598      issuerId,599      [600        {601          deviceId: "stolen-laptop",602          deviceName: "Old Laptop",603          deviceType: "extension",604          longTermPublicKeyHex: "d".repeat(64),605          signingPublicKeyHex: await signingPubkeyHex("stolen-laptop"),606          pairedAt: Date.now() + 60_000,607        },608      ],609      issuerKp.privateKey,610    );611    await adapter.applyIncomingDeviceManifest?.(612      revouch,613      new Uint8Array(32).fill(0xcc),614    );615616    expect(await findPairedDeviceById("stolen-laptop")).toBeNull();617    const { listPendingVouchedDevices } = await import("./pending-vouched-devices");618    expect(await listPendingVouchedDevices()).toHaveLength(0);619  });620621  it("§C: a peer cannot revoke US, and our outgoing manifest carries our revocations", async () => {622    const { adapter } = await setupVault();623    const { buildSignedDeviceManifest } = await import("../shared/device-manifest");624    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");625    const { pubkeyToHex } = await import("../shared/sync-identity");626    const {627      isDeviceRevoked,628      revokeDeviceById,629      upsertPairedDevice,630    } = await import("./paired-devices");631    const { getOrCreateDeviceId } = await import("./device-identity");632633    const ourDeviceId = await getOrCreateDeviceId();634    const issuerId = "phone-issuer";635    const issuerKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x28), issuerId);636    await upsertPairedDevice({637      deviceId: issuerId,638      deviceName: "Pixel",639      deviceType: "android",640      longTermPublicKeyHex: "c".repeat(64),641      signingPublicKeyHex: pubkeyToHex(issuerKp.publicKeyRaw),642      pairedAt: 1,643      trustSource: "direct",644    });645646    // A compromised peer tries to sever the mesh by revoking this device.647    const selfRevoke = await buildSignedDeviceManifest(648      issuerId,649      [],650      issuerKp.privateKey,651      {652        revoked: [653          {654            deviceId: ourDeviceId,655            longTermPublicKeyHex: "e".repeat(64),656            revokedAt: Date.now(),657            revokedByDeviceId: issuerId,658          },659        ],660      },661    );662    await adapter.applyIncomingDeviceManifest?.(663      selfRevoke,664      new Uint8Array(32).fill(0xcc),665    );666    expect(await isDeviceRevoked(ourDeviceId)).toBe(false);667668    // Our own revocations ride out in the manifest we build, and the revoked669    // device is absent from the vouched list.670    await upsertPairedDevice({671      deviceId: "retired-tablet",672      deviceName: "Retired Tablet",673      deviceType: "android",674      longTermPublicKeyHex: "f".repeat(64),675      signingPublicKeyHex: await signingPubkeyHex("retired-tablet"),676      pairedAt: 3,677      trustSource: "direct",678    });679    await revokeDeviceById("retired-tablet", ourDeviceId);680681    const outgoing = await adapter.buildOutgoingDeviceManifest?.();682    expect(outgoing?.body.revoked.map((r) => r.deviceId)).toEqual(["retired-tablet"]);683    expect(outgoing?.body.devices.map((d) => d.deviceId)).not.toContain("retired-tablet");684  });685686  // PB-26-116: rows written before `trustSource` existed carry no provenance.687  // The manifest-apply path used to default them to "direct", which is the688  // opposite of the fail-closed rule PB-26-006 established for the mesh routing689  // gate — a legacy row became a dial target, manifest issuer, and revocation-690  // repair participant on the strength of a signed manifest alone.691  it("a legacy row with no trustSource is not upgraded to direct by a manifest apply", async () => {692    const { adapter } = await setupVault();693    const { buildSignedDeviceManifest } = await import("../shared/device-manifest");694    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");695    const { pubkeyToHex } = await import("../shared/sync-identity");696    const { findPairedDeviceById, upsertPairedDevice } = await import("./paired-devices");697    const { isRouteCapableMeshPeer } = await import("../shared/mesh-peer-role");698699    const issuerId = "android-legacy-issuer";700    const issuerKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x25), issuerId);701    await upsertPairedDevice({702      deviceId: issuerId,703      deviceName: "Issuer Pixel",704      deviceType: "android",705      longTermPublicKeyHex: "1".repeat(64),706      signingPublicKeyHex: pubkeyToHex(issuerKp.publicKeyRaw),707      pairedAt: 1_700_000_000_000,708      trustSource: "direct",709    });710711    // A row predating the field: present in the registry, no recorded712    // provenance. Seeded through storage so no writer stamps a default.713    const legacyId = "legacy-peer";714    const registry = local.peach_paired_devices_v3 as Array<Record<string, unknown>>;715    registry.push({716      deviceId: legacyId,717      deviceName: "Legacy Peer",718      deviceType: "android",719      longTermPublicKeyHex: "3".repeat(64),720      signingPublicKeyHex: await signingPubkeyHex(legacyId),721      rendezvousSecretHex: "9".repeat(64),722      relayUrl: MESH_RELAY,723      pairedAt: 1_700_000_000_000,724    });725726    const manifest = await buildSignedDeviceManifest(727      issuerId,728      [{729        deviceId: legacyId,730        deviceName: "Legacy Peer",731        deviceType: "android",732        longTermPublicKeyHex: "3".repeat(64),733        signingPublicKeyHex: await signingPubkeyHex(legacyId),734        relayUrl: MESH_RELAY,735        pairedAt: 1_700_000_000_000,736      }],737      issuerKp.privateKey,738    );739    await adapter.applyIncomingDeviceManifest?.(740      manifest,741      new Uint8Array(32).fill(0x11),742    );743744    const row = await findPairedDeviceById(legacyId);745    expect(row).not.toBeNull();746    // Not silently promoted: unknown provenance stays unknown.747    expect(row?.trustSource).toBeUndefined();748    expect(row?.trustSource).not.toBe("direct");749    // And therefore not a routing authority.750    expect(isRouteCapableMeshPeer(row!)).toBe(false);751  });752753  it("accepts a manifest from the phone it paired with — mesh introduction runs in that direction", async () => {754    // The browser can never listen, so it joins the mesh THROUGH a native peer;755    // that peer is the one holding the peer list. A phone-issued manifest must756    // therefore be honoured. MV-7 still applies: the introduced device lands in757    // the pending queue, not the registry.758    const { adapter } = await setupVault();759    const { buildSignedDeviceManifest } = await import("../shared/device-manifest");760    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");761    const { pubkeyToHex } = await import("../shared/sync-identity");762    const { findPairedDeviceById, upsertPairedDevice } = await import("./paired-devices");763    const { listPendingVouchedDevices } = await import("./pending-vouched-devices");764    const vault = await import("./vault");765766    const mobileIssuerId = "android-issuer";767    const mobileIssuerKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x25), mobileIssuerId);768    await upsertPairedDevice({769      deviceId: mobileIssuerId,770      deviceName: "Issuer Pixel",771      deviceType: "android",772      longTermPublicKeyHex: "1".repeat(64),773      signingPublicKeyHex: pubkeyToHex(mobileIssuerKp.publicKeyRaw),774      pairedAt: 1_700_000_000_000,775      trustSource: "direct",776    });777    const mobileManifest = await buildSignedDeviceManifest(778      mobileIssuerId,779      [{780        deviceId: "mesh-peer",781        deviceName: "Other Pixel",782        deviceType: "android",783        longTermPublicKeyHex: "2".repeat(64),784        signingPublicKeyHex: await signingPubkeyHex("mesh-peer"),785        relayUrl: MESH_RELAY,786        pairedAt: 1_700_000_000_010,787      }],788      mobileIssuerKp.privateKey,789    );790    await adapter.applyIncomingDeviceManifest?.(791      mobileManifest,792      new Uint8Array(32).fill(0x11),793    );794795    expect(await findPairedDeviceById("mesh-peer")).toMatchObject({796      trustSource: "vouched",797      relayUrl: MESH_RELAY,798    });799    expect((await findPairedDeviceById("mesh-peer"))?.rendezvousSecretHex).toBeUndefined();800    const { getPairSecret: getPairSecretMesh } = await import("./pair-secrets");801    await expect(getPairSecretMesh("mesh-peer", vault.getRootKeyHex())).resolves.toMatch(802      /^[0-9a-f]{64}$/,803    );804    expect(await listPendingVouchedDevices()).toEqual([]);805  });806807  it("applyIncomingDeviceManifest accepts vouched issuers but rejects unauthenticated and bad signatures", async () => {808    const { vault, adapter } = await setupVault();809    const { buildSignedDeviceManifest } = await import("../shared/device-manifest");810    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");811    const { pubkeyToHex } = await import("../shared/sync-identity");812    const { findPairedDeviceById, upsertPairedDevice } = await import("./paired-devices");813    const { listPendingVouchedDevices } = await import("./pending-vouched-devices");814815    // Once admitted, a peer propagates the pool without relying on the device816    // that introduced it. The Noise-authenticated key and stored signing key817    // still bind the manifest to this known mesh member.818    const vouchedIssuerId = "android-vouched-issuer";819    const vouchedKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x25), vouchedIssuerId);820    await upsertPairedDevice({821      deviceId: vouchedIssuerId,822      deviceName: "Second-hand Pixel",823      deviceType: "android",824      longTermPublicKeyHex: "1".repeat(64),825      signingPublicKeyHex: pubkeyToHex(vouchedKp.publicKeyRaw),826      pairedAt: 1_700_000_000_000,827      trustSource: "vouched",828    });829    const onward = await buildSignedDeviceManifest(830      vouchedIssuerId,831      [{832        deviceId: "onward-mesh-peer",833        deviceName: "Other Pixel",834        deviceType: "android",835        longTermPublicKeyHex: "2".repeat(64),836        signingPublicKeyHex: await signingPubkeyHex("onward-mesh-peer"),837        relayUrl: MESH_RELAY,838        pairedAt: 1_700_000_000_010,839      }],840      vouchedKp.privateKey,841    );842    await adapter.applyIncomingDeviceManifest?.(843      onward,844      new Uint8Array(32).fill(0x11),845    );846    expect(await findPairedDeviceById("onward-mesh-peer")).toMatchObject({847      trustSource: "vouched",848      relayUrl: MESH_RELAY,849    });850    expect((await findPairedDeviceById("onward-mesh-peer"))?.rendezvousSecretHex).toBeUndefined();851    const { getPairSecret: getPairSecretOnward } = await import("./pair-secrets");852    await expect(getPairSecretOnward("onward-mesh-peer", vault.getRootKeyHex())).resolves.toMatch(853      /^[0-9a-f]{64}$/,854    );855    expect(await listPendingVouchedDevices()).toEqual([]);856857    const extensionIssuerId = "extension-bad-sig";858    const extensionKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x26), extensionIssuerId);859    await upsertPairedDevice({860      deviceId: extensionIssuerId,861      deviceName: "Chrome",862      deviceType: "extension",863      longTermPublicKeyHex: "3".repeat(64),864      signingPublicKeyHex: pubkeyToHex(extensionKp.publicKeyRaw),865      pairedAt: 1_700_000_000_020,866      trustSource: "direct",867    });868    const good = await buildSignedDeviceManifest(869      extensionIssuerId,870      [{871        deviceId: "forged-add",872        deviceName: "Forged Pixel",873        deviceType: "android",874        longTermPublicKeyHex: "4".repeat(64),875        signingPublicKeyHex: await signingPubkeyHex("forged-add"),876        pairedAt: 1_700_000_000_030,877      }],878      extensionKp.privateKey,879    );880    await adapter.applyIncomingDeviceManifest?.(881      good,882      new Uint8Array(32).fill(0x44),883    );884    expect(await findPairedDeviceById("forged-add")).toBeNull();885886    const forged = {887      ...good,888      signatureHex:889        good.signatureHex.slice(0, 2) +890        ((parseInt(good.signatureHex.slice(2, 4), 16) ^ 0xff).toString(16).padStart(2, "0")) +891        good.signatureHex.slice(4),892    };893    await adapter.applyIncomingDeviceManifest?.(894      forged,895      new Uint8Array(32).fill(0x33),896    );897    expect(await findPairedDeviceById("forged-add")).toBeNull();898  });899900  it("rejects a manifest when the stored issuer signing key is stale", async () => {901    const { vault, adapter } = await setupVault();902    const { buildSignedDeviceManifest } = await import("../shared/device-manifest");903    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");904    const { pubkeyToHex } = await import("../shared/sync-identity");905    const { findPairedDeviceById, upsertPairedDevice } = await import("./paired-devices");906907    const issuerId = "stale-key-android";908    const rootHex = vault.getRootKeyHex()!;909    const root = Uint8Array.from(rootHex.match(/../g)!.map((b) => parseInt(b, 16)));910    const issuerKp = await deriveSyncSigningKeypair(root, issuerId);911912    await upsertPairedDevice({913      deviceId: issuerId,914      deviceName: "Pixel",915      deviceType: "android",916      longTermPublicKeyHex: "c".repeat(64),917      signingPublicKeyHex: "ff".repeat(32),918      pairedAt: 1_700_000_000_000,919      trustSource: "direct",920    });921922    const manifest = await buildSignedDeviceManifest(923      issuerId,924      [{925        deviceId: "onward-peer",926        deviceName: "Forwarded iPad",927        deviceType: "extension",928        longTermPublicKeyHex: "e".repeat(64),929        signingPublicKeyHex: pubkeyToHex(930          (await deriveSyncSigningKeypair(root, "onward-peer")).publicKeyRaw,931        ),932        relayUrl: MESH_RELAY,933        pairedAt: 1_700_000_000_010,934      }],935      issuerKp.privateKey,936    );937938    await adapter.applyIncomingDeviceManifest?.(manifest, new Uint8Array(32).fill(0xcc));939940    expect(await findPairedDeviceById("onward-peer")).toBeNull();941  });942943  it("S1: a manifest signed by one paired peer is refused over a session authenticated as another", async () => {944    const { adapter } = await setupVault();945    const { buildSignedDeviceManifest } = await import("../shared/device-manifest");946    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");947    const { pubkeyToHex } = await import("../shared/sync-identity");948    const { upsertPairedDevice } = await import("./paired-devices");949    const { listPendingVouchedDevices } = await import("./pending-vouched-devices");950951    // Both peers are DIRECTLY paired, so neither the trustSource gate nor an952    // unknown-key lookup can be what rejects this — only the binding between953    // the Noise-authenticated static key and the manifest's claimed issuer.954    const alphaKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x51), "peer-alpha");955    const betaKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x52), "peer-beta");956    await upsertPairedDevice({957      deviceId: "peer-alpha",958      deviceName: "Alpha",959      deviceType: "android",960      longTermPublicKeyHex: "a".repeat(64),961      signingPublicKeyHex: pubkeyToHex(alphaKp.publicKeyRaw),962      pairedAt: 1,963      trustSource: "direct",964    });965    await upsertPairedDevice({966      deviceId: "peer-beta",967      deviceName: "Beta",968      deviceType: "android",969      longTermPublicKeyHex: "b".repeat(64),970      signingPublicKeyHex: pubkeyToHex(betaKp.publicKeyRaw),971      pairedAt: 2,972      trustSource: "direct",973    });974975    const fromBeta = await buildSignedDeviceManifest(976      "peer-beta",977      [{978        deviceId: "smuggled",979        deviceName: "Smuggled",980        deviceType: "android",981        longTermPublicKeyHex: "5".repeat(64),982        signingPublicKeyHex: await signingPubkeyHex("smuggled"),983        relayUrl: MESH_RELAY,984        pairedAt: 3,985      }],986      betaKp.privateKey,987    );988989    // Delivered over a session Noise-authenticated as ALPHA.990    await adapter.applyIncomingDeviceManifest?.(fromBeta, new Uint8Array(32).fill(0xaa));991    expect(await listPendingVouchedDevices()).toEqual([]);992993    // Same manifest, same devices, delivered over BETA's session: it must land.994    // Without this control the assertion above could pass for any reason.995    await adapter.applyIncomingDeviceManifest?.(fromBeta, new Uint8Array(32).fill(0xbb));996    const { findPairedDeviceById } = await import("./paired-devices");997    expect(await findPairedDeviceById("smuggled")).toMatchObject({998      trustSource: "vouched",999      relayUrl: MESH_RELAY,1000    });1001    expect(await listPendingVouchedDevices()).toEqual([]);1002  });1003  it("S1: a revoked long-term key cannot walk back in under a fresh deviceId", async () => {1004    const { adapter } = await setupVault();1005    const { buildSignedDeviceManifest, signDeviceRevocation } = await import("../shared/device-manifest");1006    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");1007    const { pubkeyToHex } = await import("../shared/sync-identity");1008    const { findPairedDeviceById, upsertPairedDevice } = await import("./paired-devices");1009    const { listPendingVouchedDevices } = await import("./pending-vouched-devices");10101011    const issuerId = "phone-issuer";1012    const issuerKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x53), issuerId);1013    const stolenKey = "d".repeat(64);1014    await upsertPairedDevice({1015      deviceId: issuerId,1016      deviceName: "Pixel",1017      deviceType: "android",1018      longTermPublicKeyHex: "c".repeat(64),1019      signingPublicKeyHex: pubkeyToHex(issuerKp.publicKeyRaw),1020      pairedAt: 1,1021      trustSource: "direct",1022    });1023    await upsertPairedDevice({1024      deviceId: "stolen-laptop",1025      deviceName: "Old Laptop",1026      deviceType: "extension",1027      longTermPublicKeyHex: stolenKey,1028      signingPublicKeyHex: await signingPubkeyHex("stolen-laptop"),1029      pairedAt: 2,1030      trustSource: "direct",1031    });1032    const signedRevocation = await signDeviceRevocation({1033      deviceId: "stolen-laptop",1034      longTermPublicKeyHex: stolenKey,1035      revokedAt: Date.now(),1036      revokedByDeviceId: issuerId,1037    }, issuerKp.privateKey);1038    const revoke = await buildSignedDeviceManifest(issuerId, [], issuerKp.privateKey, {1039      revoked: [signedRevocation],1040    });1041    await adapter.applyIncomingDeviceManifest?.(revoke, new Uint8Array(32).fill(0xcc));1042    expect(await findPairedDeviceById("stolen-laptop")).toBeNull();10431044    // deviceId is the issuer's to choose, so re-vouching the same key under a1045    // new one is free. Terminality has to follow the key, not the label.1046    const renamed = await buildSignedDeviceManifest(1047      issuerId,1048      [{1049        deviceId: "stolen-laptop-mk2",1050        deviceName: "New Laptop",1051        deviceType: "extension",1052        longTermPublicKeyHex: stolenKey,1053        signingPublicKeyHex: await signingPubkeyHex("stolen-laptop"),1054        relayUrl: MESH_RELAY,1055        pairedAt: Date.now(),1056      }],1057      issuerKp.privateKey,1058    );1059    await adapter.applyIncomingDeviceManifest?.(renamed, new Uint8Array(32).fill(0xcc));1060    expect(await listPendingVouchedDevices()).toEqual([]);1061    expect(await findPairedDeviceById("stolen-laptop-mk2")).toBeNull();10621063    // Control: the same fresh id carrying a key that was never revoked queues1064    // normally, so the rejection above is the key check and not the id.1065    const clean = await buildSignedDeviceManifest(1066      issuerId,1067      [{1068        deviceId: "stolen-laptop-mk2",1069        deviceName: "New Laptop",1070        deviceType: "extension",1071        longTermPublicKeyHex: "9".repeat(64),1072        signingPublicKeyHex: await signingPubkeyHex("stolen-laptop"),1073        relayUrl: MESH_RELAY,1074        pairedAt: Date.now(),1075      }],1076      issuerKp.privateKey,1077    );1078    await adapter.applyIncomingDeviceManifest?.(clean, new Uint8Array(32).fill(0xcc));1079    expect(await findPairedDeviceById("stolen-laptop-mk2")).toMatchObject({1080      trustSource: "vouched",1081      relayUrl: MESH_RELAY,1082    });1083    expect(await listPendingVouchedDevices()).toEqual([]);1084  });10851086  it("S1: a revoked key cannot be written onto a device we still trust", async () => {1087    const { adapter } = await setupVault();1088    const { buildSignedDeviceManifest, signDeviceRevocation } = await import("../shared/device-manifest");1089    const { deriveSyncSigningKeypair } = await import("../shared/legacy-identity-derivation.test-support");1090    const { pubkeyToHex } = await import("../shared/sync-identity");1091    const { findPairedDeviceById, upsertPairedDevice } = await import("./paired-devices");10921093    const issuerId = "phone-issuer";1094    const issuerKp = await deriveSyncSigningKeypair(new Uint8Array(32).fill(0x54), issuerId);1095    const revokedKey = "d".repeat(64);1096    await upsertPairedDevice({1097      deviceId: issuerId,1098      deviceName: "Pixel",1099      deviceType: "android",1100      longTermPublicKeyHex: "c".repeat(64),1101      signingPublicKeyHex: pubkeyToHex(issuerKp.publicKeyRaw),1102      pairedAt: 1,1103      trustSource: "direct",1104    });1105    await upsertPairedDevice({1106      deviceId: "trusted-tablet",1107      deviceName: "Tablet",1108      deviceType: "android",1109      longTermPublicKeyHex: "7".repeat(64),1110      signingPublicKeyHex: await signingPubkeyHex("trusted-tablet"),1111      pairedAt: 2,1112      trustSource: "direct",1113    });11141115    const signedRevocation = await signDeviceRevocation({1116      deviceId: "stolen-laptop",1117      longTermPublicKeyHex: revokedKey,1118      revokedAt: Date.now(),1119      revokedByDeviceId: issuerId,1120    }, issuerKp.privateKey);1121    const revoke = await buildSignedDeviceManifest(issuerId, [], issuerKp.privateKey, {1122      revoked: [signedRevocation],1123    });1124    await adapter.applyIncomingDeviceManifest?.(revoke, new Uint8Array(32).fill(0xcc));11251126    // The manifest rotates a device we already trust onto the revoked key —1127    // the same re-admission dressed as a key rotation.1128    const rotate = await buildSignedDeviceManifest(1129      issuerId,1130      [{1131        deviceId: "trusted-tablet",1132        deviceName: "Tablet",1133        deviceType: "android",1134        longTermPublicKeyHex: revokedKey,1135        signingPublicKeyHex: await signingPubkeyHex("trusted-tablet"),1136        pairedAt: 2,1137      }],1138      issuerKp.privateKey,1139    );1140    await adapter.applyIncomingDeviceManifest?.(rotate, new Uint8Array(32).fill(0xcc));1141    expect(await findPairedDeviceById("trusted-tablet")).toMatchObject({1142      longTermPublicKeyHex: "7".repeat(64),1143    });1144  });11451146  it("methods throw VaultLockedError when the vault is locked", async () => {1147    const { vault, adapter } = await setupVault();1148    vault.lock();1149    await expect(adapter.computeVaultId()).rejects.toMatchObject({ message: "vault_locked" });1150    await expect(adapter.buildEntryManifest()).rejects.toMatchObject({ message: "vault_locked" });1151    await expect(adapter.buildTombstoneManifest()).rejects.toMatchObject({ message: "vault_locked" });1152    await expect(adapter.getEncryptedEntries(["x"])).rejects.toMatchObject({ message: "vault_locked" });1153    await expect(adapter.applyEntries({ x: "AAAA" })).rejects.toMatchObject({ message: "vault_locked" });1154    await expect(adapter.applyTombstones({ x: 1 })).rejects.toMatchObject({ message: "vault_locked" });1155    await expect(adapter.buildOutgoingDeviceManifest?.()).rejects.toMatchObject({ message: "vault_locked" });1156  });1157});