Files
loki/scripts/test/solon-grant.ts
CatoandClaude Opus 5 2a8aa17d6f feat(solon): found an organization from a project, and attribute it by consent (#759)
Solon can now create organizations (bitbaum/solon#166). This is Loki's half: the
handoff that starts it, and the register join that records the result.

A HANDOFF, NOT A PUBLISH

Publishing to OrangeCat works because Loki holds the user's OIDC token and acts
as them. That does not transfer: founding a Solon organization needs a signature
from the founder's own Bitcoin wallet, and Solon's key custody is "never Solon —
SELF is a human's own wallet". So Loki cannot do this for anyone. It mints a
link, pre-filled with the project, and the owner signs on Solon.

ATTRIBUTION BY GRANT, NOT BY NAME

Founding is permissionless, so a register that paired a project with the
organization of the same name would let whoever founds `loki` appear to govern
Loki. The register now joins on Solon's `claimedProject`, and Solon sets that
only from a grant this signs for the founder's own OrangeCat identity, checked
against their session. Squatting a name costs a vanity slug, never attribution.

The grant is HMAC-SHA256 over a domain-separated line, 30-minute TTL, signed
with SOLON_WEBHOOK_SECRET — which both deployments already hold, and which
cannot collide with a webhook body because that is JSON. Both ports are pinned
to ONE shared vector; drift turns both repos' suites red rather than silently
refusing every founding link with "issued to a different identity".

AND A PROBE THAT COULD ONLY EVER FIND WHAT IT KNEW

solonOrgSlugs() asked `GET /api/orgs/<slug>` per candidate slug — and all three
callers passed no candidates, so it only ever probed `orangecat`. An
organization for any other project could have existed and the register would
never have seen it. It reads Solon's new list endpoint once instead. A fetch
that fails is still UNCHECKED, never "no organisation".

The button renders nothing when Solon is unreachable: the project may already
have an organization we could not see, and offering to found a second one is the
wrong guess. The grant is minted on click, not on page load, so a tab left open
past the TTL does not hand Solon an expired link.

Checked: tsc, eslint, design-system, 191/191 unit test files — including the
shared-vector test and a register test pinning that an organization which merely
shares a project's name attributes nothing.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 02:16:05 -07:00

121 lines
3.9 KiB
TypeScript

// The grant Solon verifies when a project owner founds an organization from
// Loki — a cross-repo contract, pinned the way solon-message.ts pins votes.
//
// The vector was generated by Solon's own signLokiGrant() (src/lib/loki-grant.ts)
// and Solon's src/lib/__tests__/loki-grant.test.ts pins the identical bytes.
// HMAC is deterministic, so any drift in either side's format breaks this test
// instead of breaking attribution silently in prod: Solon would refuse every
// founding link with "issued to a different OrangeCat identity", which is the
// one message nobody would think to trace back to a formatting change.
// Pure: no DB, no network.
import assert from "node:assert";
import {
SOLON_GRANT_TTL_SECS,
signSolonGrant,
solonFoundingUrl,
solonGrantMessage,
} from "../../src/lib/integrations/solon-grant";
const VECTOR = {
secret: "solon-loki-grant-test-vector",
project: "heidi",
actorId: "5f2716c8-1111-2222-3333-444455556666",
exp: 1790000000,
message:
"solon-org-grant\nproject:heidi\nactor:5f2716c8-1111-2222-3333-444455556666\nexp:1790000000",
sig: "337bffe53d4a68dbb26769e1ba5394bad0e86abdfd5e013095ad76d8c219f75d",
};
// Canonical bytes are Solon's lokiGrantMessage(), byte for byte.
assert.strictEqual(solonGrantMessage(VECTOR), VECTOR.message, "grant message drifted from Solon");
// And the signature matches Solon's signer.
assert.strictEqual(
signSolonGrant(VECTOR, VECTOR.secret),
VECTOR.sig,
"grant signature drifted from Solon",
);
// A grant is bound to one identity: the same project for someone else signs differently.
assert.notStrictEqual(
signSolonGrant({ ...VECTOR, actorId: "someone-else" }, VECTOR.secret),
VECTOR.sig,
"a grant must be bound to the actor it was issued for",
);
// The founding link carries everything Solon's /orgs/new reads.
const nowSecs = 1_789_000_000;
const url = solonFoundingUrl({
base: "https://solon.example/",
project: "heidi",
name: "Heidi",
description: "Understand Zurich German, then take part.",
actorId: VECTOR.actorId,
secret: VECTOR.secret,
nowSecs,
});
assert.ok(url, "a founding link is produced when the secret is configured");
const parsed = new URL(url!);
assert.strictEqual(
parsed.origin + parsed.pathname,
"https://solon.example/orgs/new",
"a trailing slash on the base must not double",
);
assert.strictEqual(parsed.searchParams.get("slug"), "heidi");
assert.strictEqual(parsed.searchParams.get("name"), "Heidi");
assert.strictEqual(parsed.searchParams.get("project"), "heidi");
const exp = Number(parsed.searchParams.get("exp"));
assert.strictEqual(exp, nowSecs + SOLON_GRANT_TTL_SECS, "the grant expires one TTL from now");
assert.strictEqual(
parsed.searchParams.get("grant"),
signSolonGrant({ project: "heidi", actorId: VECTOR.actorId, exp }, VECTOR.secret),
"the link's grant is the signature over the link's own project and expiry",
);
// No description, no parameter — never the string "null".
const bare = new URL(
solonFoundingUrl({
base: "https://solon.example",
project: "p",
name: "P",
description: null,
actorId: "a",
secret: "s",
nowSecs,
})!,
);
assert.strictEqual(bare.searchParams.has("description"), false);
// A description Solon would refuse is cut to what it accepts.
const long = new URL(
solonFoundingUrl({
base: "https://solon.example",
project: "p",
name: "P",
description: "x".repeat(900),
actorId: "a",
secret: "s",
nowSecs,
})!,
);
assert.strictEqual(long.searchParams.get("description")!.length, 500);
// Without the shared secret Loki cannot vouch, so it offers no link at all.
for (const secret of [undefined, ""]) {
assert.strictEqual(
solonFoundingUrl({
base: "https://solon.example",
project: "p",
name: "P",
description: null,
actorId: "a",
secret,
nowSecs,
}),
null,
"no secret must mean no founding link, not an unattributable one",
);
}
console.log("solon-grant: ok");