Cotton code
58 lines · 3.9 KB · TypeScriptRaw
/** * The kit's person_key table: the owner's phone publishes * its person key once, by fingerprint; the window and the allowlist check * signatures against it. Synthetic keys only. */import { describe, expect, it } from "vitest";import { ADMIN, DATABASE_ID, http, mint, OWNER_PHONE, PERSONAL_TABLES, PERSONAL_TOOLS, publishPersonKey, tool } from "./personal.ts";async function freshKey(): Promise<string> {  const pair = (await crypto.subtle.generateKey({ name: "Ed25519" }, true, ["sign", "verify"])) as CryptoKeyPair;  const raw = new Uint8Array((await crypto.subtle.exportKey("raw", pair.publicKey)) as ArrayBuffer);  return btoa(String.fromCharCode(...raw)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");}async function object(slug: string): Promise<{ credential: string; owner: string }> {  expect((await http(slug, "PUT", "/v1/admin/business", { name: "Personal", kind: "personal", owner_phone: OWNER_PHONE, database_id: DATABASE_ID, timezone: "Asia/Kolkata" }, ADMIN)).status).toBe(200);  expect((await http(slug, "PUT", "/v1/admin/definitions", { tools: PERSONAL_TOOLS, tables: PERSONAL_TABLES, flavour: { flavour: "personal" } }, ADMIN)).status).toBe(200);  const code = await http<{ challenge_id: string }>(slug, "POST", "/v1/code", { phone: OWNER_PHONE, device_id: "phone" });  const signed = await http<{ credential: string }>(slug, "POST", "/v1/sign-in", { challenge_id: code.body.challenge_id, code: "123456", device_id: "phone" });  expect(signed.status, JSON.stringify(signed.body)).toBe(200);  return { credential: signed.body.credential, owner: await mint(slug, signed.body.credential, "Claude (claude.ai)", ["owner"]) };}const publish = (slug: string, bearer: string, id: string, data: Record<string, unknown>, act = "create") =>  http<{ code?: string }>(slug, "POST", "/v1/act", { act, args: { table: "person_key", id, data } }, { authorization: `Bearer ${bearer}` });describe("person_key", () => {  it("is published once by the owner's phone, again as it stands, and never replaced, changed, or written by a token or MCP", { timeout: 60_000 }, async () => {    const slug = "person-key-once";    const { credential, owner } = await object(slug);    const key = await freshKey();    const data = { public_key: key, sealed_private_enc: "v2:stand-in", vmk_version: 1 };    // A token, even the owner's, never publishes one; nor does MCP.    // ADMIN KEYS 1b: refused at the key gate first (person_key holds who may do what), before the owner-device rule.    expect((await publish(slug, owner, key.slice(0, 8), data)).body.code).toBe("access_act");    const viaMcp = await tool(slug, owner, "create_record", { table: "person_key", data });    expect(viaMcp.isError).toBe(true);    // The id is the fingerprint; the shape is checked.    expect((await publish(slug, credential, "nothere1", data)).body.code).toBe("bad_person_key");    expect((await publish(slug, credential, key.slice(0, 8), { ...data, public_key: "short" })).body.code).toBe("bad_person_key");    expect((await publish(slug, credential, key.slice(0, 8), { public_key: key, vmk_version: 1 })).body.code).toBe("bad_person_key");    await publishPersonKey(slug, credential, key);    // Asked again at the next sign-in: answered as it stands.    await publishPersonKey(slug, credential, key);    const other = await freshKey();    expect((await publish(slug, credential, other.slice(0, 8), { ...data, public_key: other })).body.code).toBe("person_key_held");    expect((await publish(slug, credential, key.slice(0, 8), { vmk_version: 2 }, "update")).body.code).toBe("person_key_fixed");    expect((await publish(slug, credential, key.slice(0, 8), {}, "delete")).body.code).toBe("person_key_fixed");    // The phone reads it back on its pull; MCP never sees the sealed half.    const read = await tool(slug, owner, "get_record_by_table", { table: "person_key", id: key.slice(0, 8) });    expect(JSON.stringify(read)).not.toContain("stand-in");  });});