Contracts 1.4.0: crosscode.link/1 and a public key in the v2 heartbeat

- crosscode.link/1 (link.connect / link.connected / link.disconnect), with
  signLink / verifyLink, PEM conversion, link secrets and nonces.
- The v2 heartbeat can carry the install's publicKey; the reply says
  saved / matches / mismatch (Hub 2.2.0). Never a failed heartbeat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 11:58:26 +02:00
co-authored by Claude Opus 5.5
parent 9d585c94aa
commit 4952e2cb0f
21 changed files with 595 additions and 5 deletions
+65 -2
View File
@@ -1,5 +1,6 @@
import { createHmac } from "node:crypto";
import { hub } from "../src";
import { createHmac, generateKeyPairSync } from "node:crypto";
import { hub, linkConnectSchema, linkConnectedSchema } from "../src";
import * as s from "../src/signing";
import { HUB_HEADERS, LINK_HEADERS, sign, signedHeaders, verify } from "../src/signing";
import { sample } from "./helpers";
@@ -76,3 +77,65 @@ describe("signing", () => {
expect(verify({ secret, timestamp: "yesterday", signature: sign(secret, "yesterday", body), rawBody: body, now })).toEqual({ ok: false, code: "STALE_REQUEST" });
});
});
describe("product links (crosscode.link/1)", () => {
const a = s.generateEd25519();
const b = s.generateEd25519();
const aPem = s.ed25519PublicToPem(a.publicKey);
const now = new Date("2026-11-20T10:00:00.000Z");
const connect = () =>
s.signLink(
{
schema: "crosscode.link/1",
type: "link.connect",
from: { product: "events", installId: "9a4c2f1e-7b3d-4e5a-8c6f-0d1e2f3a4b5c", url: "https://events.hopefamily.co.za" },
to: { installId: "3b0f6a52-6c1e-4b7a-9d0e-2f1a8c4b7e02" },
secret: s.newLinkSecret(),
issuedAt: now.toISOString(),
nonce: s.newLinkNonce(),
},
a.privateKeyPem,
);
it("round-trips a key through PEM", () => {
expect(aPem.startsWith("-----BEGIN PUBLIC KEY-----")).toBe(true);
expect(s.ed25519PublicFromPem(aPem)).toBe(a.publicKey);
expect(() => s.ed25519PublicFromPem(generateKeyPairSync("x25519").publicKey.export({ format: "pem", type: "spki" }).toString())).toThrow();
});
it("signs a connect message that passes the schema and verifies with the sender's PEM", () => {
const msg = connect();
expect(linkConnectSchema.safeParse(msg).success).toBe(true);
expect(s.verifyLink(msg, aPem, { now })).toEqual({ ok: true });
});
it("refuses another key, a changed body and a stale message", () => {
const msg = connect();
expect(s.verifyLink(msg, s.ed25519PublicToPem(b.publicKey), { now })).toEqual({ ok: false, code: "BAD_SIGNATURE" });
expect(s.verifyLink({ ...msg, secret: s.newLinkSecret() }, aPem, { now })).toEqual({ ok: false, code: "BAD_SIGNATURE" });
expect(s.verifyLink({ ...msg, to: { installId: "3b0f6a52-6c1e-4b7a-9d0e-2f1a8c4b7e03" } }, aPem, { now })).toEqual({ ok: false, code: "BAD_SIGNATURE" });
expect(s.verifyLink(msg, aPem, { now: new Date(now.getTime() + 6 * 60 * 1000) })).toEqual({ ok: false, code: "STALE_REQUEST" });
expect(s.verifyLink(msg, "not a key", { now })).toEqual({ ok: false, code: "BAD_SIGNATURE" });
});
it("makes secrets and nonces the schema accepts", () => {
const reply = s.signLink(
{
schema: "crosscode.link/1",
type: "link.connected",
from: { product: "website", installId: "3b0f6a52-6c1e-4b7a-9d0e-2f1a8c4b7e02", url: "https://www.hopefamily.co.za" },
to: { installId: "9a4c2f1e-7b3d-4e5a-8c6f-0d1e2f3a4b5c" },
inReplyTo: s.newLinkNonce(),
issuedAt: now.toISOString(),
nonce: s.newLinkNonce(),
},
b.privateKeyPem,
);
expect(linkConnectedSchema.safeParse(reply).success).toBe(true);
});
it("lets a heartbeat carry the install's public key", () => {
expect(hub.heartbeatRequestSchema.safeParse({ ...sample("hub-heartbeat-events.json"), publicKey: aPem }).success).toBe(true);
expect(hub.heartbeatRequestSchema.safeParse({ ...sample("hub-heartbeat-events.json"), publicKey: "nope" }).success).toBe(false);
});
});