Cotton code
64 lines · 4.6 KB · TypeScriptRaw
/** * The passwords' own switch (LOCK TABLES 2, 28 Nov 2026; src/open_tables_lock.ts SECRET_META): `secret` sealed alone, * ahead of the session tables. No grant carries its key, so no gate waits: on, every device reads its seal block at * once and seals each row; the session tables stay as they are; off is refused once a row is sealed. A phone still * holding the open list is taken, never refused (SEALING-CONTRACT.md). Every name and site is invented. */import { describe, expect, it } from "vitest";import { SEALS } from "../src/open_tables_lock.ts";import { ADMIN, http, personalObject } from "./personal.ts";import { sealRow } from "./seal-device.ts";const DK = Uint8Array.from({ length: 32 }, (_, i) => (i * 13 + 7) & 0xff);type Lock = { lock: unknown; state: string; open_rows: number; sealed_rows: number };describe("the passwords' own lock", { timeout: 120_000 }, () => {  it("seals secret alone with no gate, keeps the session tables open, and stays on once a row is sealed", async () => {    const slug = "secret-lock";    const { credential } = await personalObject(slug, { arrival: true });    const device = { authorization: `Bearer ${credential}` };    const act = (name: string, args: Record<string, unknown>) => http<Record<string, unknown>>(slug, "POST", "/v1/act", { act: name, args }, device);    const lockOf = () => http<Lock>(slug, "GET", "/v1/admin/secret-lock", undefined, ADMIN);    const sealOf = async (table: string) => (await http<{ tables: { id: string; seal?: { clear: string[] } }[] }>(slug, "GET", "/v1/definitions", undefined, device)).body.tables.find((t) => t.id === table)?.seal;    // Before: an open password, as every account holds them today.    const made = await act("create", { table: "secret", data: { kind: "password", name: "Invented Bank", site: "bank.invented.example", username: "asha" } });    expect(made.status, JSON.stringify(made.body)).toBe(200);    const id = (made.body.made as { id: string }).id;    expect((await lockOf()).body).toMatchObject({ lock: null, state: "off", open_rows: 1 });    expect(await sealOf("secret")).toBeUndefined();    // On: no gate to pass; every device reads secret's seal block, and the session tables are untouched.    const on = await http<Lock>(slug, "POST", "/v1/admin/secret-lock", { on: true }, ADMIN);    expect(on.status, JSON.stringify(on.body)).toBe(200);    expect(on.body).toMatchObject({ state: "sealing", open_rows: 1 });    expect((await sealOf("secret"))?.clear).toEqual(SEALS.secret!.clear);    expect(await sealOf("session")).toBeUndefined();    // The phone seals the row at once (no grant waits for its key), the words inside the blob.    const version = Number(((await http<{ rows: { id: string; version: number }[] }>(slug, "GET", "/v1/sync?since=0&tables=secret", undefined, device)).body.rows.find((r) => r.id === id))!.version);    const sealed = await act("seal", { table: "secret", id, expect_version: version, data: { kind: "password", ...(await sealRow(DK, "secret", id, 1, { name: "Invented Bank", site: "bank.invented.example", username: "asha" })) } });    expect(sealed.status, JSON.stringify(sealed.body)).toBe(200);    const lock = await lockOf();    expect(lock.body).toMatchObject({ state: "sealed", open_rows: 0, sealed_rows: 1 });    expect(JSON.stringify((await http(slug, "GET", "/v1/sync?since=0&tables=secret", undefined, device)).body)).not.toContain("Invented Bank");    // A phone that has not pulled the lock yet writes a password in the clear: taken, sealed at arrival, told to pull.    const late = await act("create", { table: "secret", data: { kind: "password", name: "Invented Grocer", site: "grocer.invented.example" } });    expect(late.status, JSON.stringify(late.body)).toBe(200);    expect(late.body.definitions_stale).toBe(true);    expect(JSON.stringify((await http(slug, "GET", "/v1/sync?since=0&tables=secret", undefined, device)).body)).not.toContain("Invented Grocer");    // Off is refused once a row is sealed: no device opens a sealed password back to clear.    expect((await http(slug, "POST", "/v1/admin/secret-lock", { on: false }, ADMIN)).body).toMatchObject({ code: "secret_lock_sealing" });    expect((await http(slug, "POST", "/v1/admin/secret-lock", { on: true }, ADMIN)).status).toBe(200);  });  it("goes off again while nothing is sealed yet", async () => {    const slug = "secret-lock-off";    await personalObject(slug, { arrival: true });    expect((await http(slug, "POST", "/v1/admin/secret-lock", { on: true }, ADMIN)).status).toBe(200);    expect((await http<Lock>(slug, "POST", "/v1/admin/secret-lock", { on: false }, ADMIN)).body).toMatchObject({ lock: null, state: "off" });  });});