/** * 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= pnpm exec tsx scripts/amendment-file.ts \ * --spec data/law/amendments/.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 "); 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); });