Files
solon/scripts/amendment-file.ts
CatoandClaude ffc16763ce feat(law): Zürich's official routes as data, and the letter that carries a proposal (#law-route, #law-handoff) (#619)
* feat(law-route): the City of Zürich's routes as sourced data, on every proposal

Petition, Einzelinitiative, Volksinitiative (3,000 in six months), motion or
postulate, and contesting one's own fine, each with whom it is open to and the
provision that says so. A proposal shows them as "What it takes to make this
real here", the case routes apart.

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(law-handoff): write the petition or Einzelinitiative from a proposal, as a PDF made on the writer's device

The writer's details stay in the browser: no request carries them, nothing is
stored, the page is not indexed. Whoever may not take a voters-only route is
shown the petition.

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(law): the act API gives the version id, so a signer can file a change with their own key

scripts/amendment-file.ts pins each change to its provision's hash, computes the
revision hash as the server does, signs and files. First spec: Art. 8 of the
City of Zürich's Parkkartenverordnung.

Co-Authored-By: Claude <noreply@anthropic.com>

* docs(law): roadmap, changelog and design for #law-route and #law-handoff

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-26 12:17:29 +01:00

131 lines
4.7 KiB
TypeScript

/**
* File a proposed change to the law with one's own Bitcoin key, as an agent
* or a person without an account does (design:
* docs/design/2026-11-amending-the-law.md, "Interfaces").
*
* The spec names the act, the language, the title, the rationale and the
* changes by provision path. The script reads the text in force from Solon's
* public API, pins each change to the hash of the provision it amends,
* computes the revision's content hash the way the server does, signs it
* (BIP-137) and posts it. The private key never leaves this machine.
*
* Usage:
* SOLON_AGENT_PRIVKEY=<hex> pnpm exec tsx scripts/amendment-file.ts \
* --spec data/law/amendments/<file>.json [--base-url https://solon.orangecat.ch] [--dry-run]
*/
import { readFileSync } from "node:fs";
import { parseArgs } from "node:util";
import * as secp from "@noble/secp256k1";
import { deriveAddresses, signMessage } from "../src/lib/bitcoin/message";
import { sha256Hex } from "../src/lib/domain/canonical";
import type { Change } from "../src/lib/law/amend/changes";
import { amendmentMessage, revisionHash } from "../src/lib/law/amend/hash";
interface Spec {
/** The amendment's id, fixed in the spec so a second run is refused instead of filing twice. */
id: string;
act: string;
lang: string;
title: string;
rationale: string;
changes: (
| { op: "replace"; path: string; heading?: string; text: string }
| { op: "repeal"; path: string }
| { op: "insert"; after: string | null; heading: string; text: string }
)[];
}
const { values } = parseArgs({
options: {
spec: { type: "string" },
"base-url": { type: "string", default: "https://solon.orangecat.ch" },
"key-env": { type: "string", default: "SOLON_AGENT_PRIVKEY" },
"dry-run": { type: "boolean", default: false },
},
});
async function main() {
if (!values.spec) throw new Error("required: --spec <file.json>");
const spec = JSON.parse(readFileSync(values.spec, "utf8")) as Spec;
const base = values["base-url"]!.replace(/\/$/, "");
const res = await fetch(`${base}/api/v1/law/acts/${spec.act}?lang=${spec.lang}`);
if (!res.ok) throw new Error(`the act could not be read: ${res.status}`);
const act = (await res.json()) as {
authority: string;
version: { id?: string; lang: string } | null;
provisions: { path: string; text: string }[];
};
if (!act.version?.id) throw new Error("the API does not give the version's id; deploy it first");
if (act.version.lang !== spec.lang) throw new Error(`no text in force in ${spec.lang}`);
const held = new Map(act.provisions.map((p) => [p.path, p.text]));
const baseHash = (path: string) => {
const text = held.get(path);
if (text === undefined) throw new Error(`no provision ${path} in the text in force`);
return sha256Hex(text);
};
const changes: Change[] = spec.changes.map((c) =>
c.op === "insert"
? c
: c.op === "repeal"
? { op: "repeal", path: c.path, baseHash: baseHash(c.path) }
: {
op: "replace",
path: c.path,
baseHash: baseHash(c.path),
heading: c.heading,
text: c.text,
},
);
const contentHash = revisionHash({
authority: act.authority,
act: spec.act,
baseVersion: act.version.id,
lang: spec.lang,
title: spec.title.trim(),
rationale: spec.rationale,
changes,
});
const keyEnv = values["key-env"]!;
const privateKeyHex = process.env[keyEnv];
if (!privateKeyHex) throw new Error(`env ${keyEnv} is not set`);
const publicKeyHex = secp.etc.bytesToHex(
secp.getPublicKey(secp.etc.hexToBytes(privateKeyHex), true),
);
const address = deriveAddresses(publicKeyHex).p2pkh;
const signature = signMessage(
amendmentMessage({ act: "file", amendmentId: spec.id, revision: 1, contentHash, address }),
privateKeyHex,
);
console.log(`act ${spec.act}, version ${act.version.id}`);
for (const c of changes) if ("baseHash" in c) console.log(`${c.path} pinned to ${c.baseHash}`);
console.log(`content hash ${contentHash}, signed by ${address}`);
if (values["dry-run"]) return;
const filed = await fetch(`${base}/api/v1/law/amendments`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
id: spec.id,
act: spec.act,
lang: spec.lang,
title: spec.title,
rationale: spec.rationale,
changes,
address,
signature,
}),
});
console.log(filed.status, JSON.stringify(await filed.json(), null, 2));
if (filed.ok) console.log(`${base}/law/amendments/${spec.id}`);
else process.exitCode = 1;
}
main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exit(1);
});