Files
CatoandClaude Opus 5.5 b261787c2d fix(person-seat): decide on fresh person tags; one key is one seat in every encoding (#574)
* fix(membership): one key cannot hold a second seat under its uncompressed address

verifyMessage named only the addresses of the serialization the header
chose. The header's compression flag is not signed, so the holder of a key
seated under its compressed address could lower the header by 4 and claim
the uncompressed P2PKH address with the same r||s. keyAddresses now always
holds every standard address of the recovered point: P2PKH of the
uncompressed key, and P2PKH, P2WPKH and P2SH-P2WPKH of the compressed key.
Validity stays tied to the claimed address and the header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmubu4vMeqp2pdae1Zy

* fix(person-seat): decide on fresh person tags; a stored tag is only a cache

After an OrangeCat identity is handed over its tag changes, so the tag
Solon stored at admission was wrong both ways: the former holder was
refused (and the operator told the two were once linked), and the new
holder could take a second seat.

- personTagsFor asks OrangeCat for many accounts in one request (chunked
  at 100); personTagFor uses it.
- No OrangeCat client configured is now "could not ask", not "cannot be
  told apart", so add-member refuses instead of seating unchecked and the
  backfill exits 1.
- seatHeldByPerson asks for the candidate and every non-retired human seat
  at once, decides on those answers only, and rewrites stored tags that
  changed (null for one OrangeCat now leaves out). planPersonTags never
  writes one tag to two seats; a clash between existing seats is reported
  instead of thrown, and add-member prints it.
- backfill-person-tags asks once per organization per 100 seats, has a
  --refresh mode, and its dry run finds clashes between untagged seats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmubu4vMeqp2pdae1Zy

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 08:37:28 +01:00

169 lines
6.4 KiB
TypeScript

/**
* Operator bootstrap: register a member (human or agent) in an organization.
*
* Membership changes are humans-only by default, but the genesis
* roster has to come from somewhere — this script is that documented,
* operator-run bootstrap. It runs on the box with DATABASE_URL set and writes
* a MEMBER_ADDED audit event like every other roster change.
*
* Usage:
* pnpm exec tsx scripts/add-member.ts --org orangecat --name "George" \
* --type HUMAN --address <bitcoin-address> [--pubkey <hex>] \
* [--system orangecat:cat] [--weight 1] [--mint-key] \
* [--oc-actor <orangecat-actor-uuid>]
*
* --oc-actor (humans only) links the member to an OrangeCat identity so
* "Sign in with OrangeCat" recognizes them. The actor id is shown on
* /account after the person signs in — recognition only; voting authority
* stays with the Bitcoin key regardless.
*
* --mint-key (agents only) generates a transport API key, stores its sha256,
* and prints the plaintext ONCE — it is never stored or shown again.
*/
import { parseArgs } from "node:util";
import { randomBytes } from "node:crypto";
import { and, eq } from "drizzle-orm";
import { db } from "../src/lib/db/client";
import { agentApiKeys, auditEvents, members, organizations } from "../src/lib/db/schema";
import { sha256Hex } from "../src/lib/domain/canonical";
import { seatHeldByPerson } from "../src/lib/domain/person-seat";
const { values } = parseArgs({
options: {
org: { type: "string" },
name: { type: "string" },
type: { type: "string" },
address: { type: "string" },
pubkey: { type: "string" },
system: { type: "string" },
weight: { type: "string", default: "1" },
"mint-key": { type: "boolean", default: false },
"oc-actor": { type: "string" },
// Seat an account without asking OrangeCat whether its person already
// sits here. Only for when OrangeCat cannot be reached and the operator
// knows the answer another way.
"skip-person-check": { type: "boolean", default: false },
},
});
async function main() {
const { org, name, type, address } = values;
if (!org || !name || !type || !address) {
console.error("required: --org --name --type HUMAN|AGENT --address");
process.exit(1);
}
if (type !== "HUMAN" && type !== "AGENT") {
console.error(`--type must be HUMAN or AGENT, got ${type}`);
process.exit(1);
}
if (type === "AGENT" && !values.system) {
console.error(
"agents need --system (e.g. orangecat:cat) — which system attests this member's votes",
);
process.exit(1);
}
if (type === "AGENT" && values["oc-actor"]) {
console.error(
"--oc-actor is for HUMAN members only — agents are recognized by their API key, not a login",
);
process.exit(1);
}
const organization = await db.query.organizations.findFirst({
where: eq(organizations.slug, org),
});
if (!organization) {
console.error(`organization "${org}" not found`);
process.exit(1);
}
const existing = await db.query.members.findFirst({
where: and(eq(members.organizationId, organization.id), eq(members.bitcoinAddress, address)),
});
if (existing) {
console.log(`member already registered: ${existing.id} (${existing.displayName})`);
process.exit(0);
}
// One person, one human seat: an OrangeCat person can hold several accounts.
let personTag: string | null = null;
if (type === "HUMAN" && values["oc-actor"] && !values["skip-person-check"]) {
const check = await seatHeldByPerson(organization.id, values["oc-actor"]);
// Seats already here that OrangeCat now says are one person's (say, after
// an identity was handed over). Not this admission's to decide; shown so
// the organization can.
for (const clash of "clashes" in check ? (check.clashes ?? []) : []) {
console.error(
`note: these seats in ${org} are one person's: ${clash.seats
.map((s) => `"${s.displayName}" (${s.memberId})`)
.join(", ")}. Only the first keeps the person tag; the organization decides.`,
);
}
if (!check.ok && check.reason === "already_seated") {
console.error(
`refused: the person behind --oc-actor already holds a seat in ${org} as "${check.displayName}" (${check.memberId}), under another of their OrangeCat identities. One person, one seat.`,
);
process.exit(1);
}
if (!check.ok) {
console.error(
`refused: could not ask OrangeCat whether this person already sits in ${org} (${check.detail}). Set ORANGECAT_OAUTH_CLIENT_ID and ORANGECAT_OAUTH_CLIENT_SECRET if they are missing and try again, or pass --skip-person-check if you know they do not.`,
);
process.exit(1);
}
personTag = check.tag;
}
const member = await db.transaction(async (tx) => {
const [m] = await tx
.insert(members)
.values({
organizationId: organization.id,
displayName: name,
memberType: type,
// Humans hold their own keys; an agent's key lives in its system's env.
keyCustody: type === "HUMAN" ? "SELF" : "SERVICE",
bitcoinAddress: address,
publicKeyHex: values.pubkey ?? null,
votingWeight: values.weight,
system: values.system ?? null,
ocActorId: values["oc-actor"] ?? null,
personTag,
})
.returning();
await tx.insert(auditEvents).values({
organizationId: organization.id,
eventType: "MEMBER_ADDED",
subjectType: "member",
subjectId: m.id,
payload: {
displayName: name,
memberType: type,
bitcoinAddress: address,
...(values.system ? { system: values.system } : {}),
...(values["oc-actor"] ? { ocActorId: values["oc-actor"] } : {}),
note: "operator bootstrap — roster changes after genesis go through MEMBERSHIP votes",
},
});
return m;
});
console.log(`member created: ${member.id} (${member.displayName}, ${member.memberType})`);
if (values["mint-key"]) {
if (type !== "AGENT") {
console.error("--mint-key is for AGENT members only");
process.exit(1);
}
const plaintext = `sk_solon_${randomBytes(24).toString("hex")}`;
await db.insert(agentApiKeys).values({ memberId: member.id, keyHash: sha256Hex(plaintext) });
console.log(`API key (shown once, store it in the agent's env now):\n${plaintext}`);
}
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(() => db.$client.end());