Cotton code
31 lines · 1.5 KB · TypeScriptRaw
/** * The phone's core (Rust, cotton-core chat/seal.rs) and the Worker tests' * phone (WebCrypto, test/chat.ts) agree on the `c1:` box byte for byte: each * vector the core records opens here, says its sender, and a box sealed here * opens with the vectors' keys. */import { describe, expect, it } from "vitest";import vectors from "../../../crates/cotton-core/src/chat/vectors.json";import { ChatPhone, openBox, pairOf, unb64url } from "./chat.ts";describe("the c1: box, two implementations", () => {  it("opens every vector the core sealed", async () => {    expect(vectors.vectors.length).toBeGreaterThan(0);    for (const v of vectors.vectors) {      const recipient = await pairOf(v.recipient_secret, v.recipient_public);      expect(await openBox(recipient, v.box)).toBe(v.payload);      // The box names the device that sealed it.      expect(unb64url(v.box.slice(3)).slice(32, 64)).toEqual(unb64url(v.sender_public));      // Another device's key opens nothing.      expect(await openBox(await pairOf(v.sender_secret, v.sender_public), v.box)).toBeNull();    }  });  it("seals a box the vectors' recipient opens", async () => {    const v = vectors.vectors[0]!;    const sender = new ChatPhone("+15550100001", "vector-sender", "", await pairOf(v.sender_secret, v.sender_public));    const box = await sender.seal(v.recipient_public, { hello: "back" });    expect(await openBox(await pairOf(v.recipient_secret, v.recipient_public), box)).toBe('{"hello":"back"}');  });});