Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

Guestbook daemon: v2 epoch mints + 15-min launch auto-roll

nyblnet2b047c790b40
diff --git a/server/guestbook-daemon/src/worker.js b/server/guestbook-daemon/src/worker.js
index f8f3381f..303f0a41 100644
--- a/server/guestbook-daemon/src/worker.js
+++ b/server/guestbook-daemon/src/worker.js
@@ -37,6 +37,40 @@ const b64u = {
const meta = async (env) => JSON.parse((await env.STORE.get(META, 'text')) ?? '{}')
const putMeta = (env, m) => env.STORE.put(META, JSON.stringify(m, null, 2))
+// ——— v2 room minting (mirrors scripts/build-guestbook.mjs) ————————————————
+// A v2 room commits its id to an OWNER pubkey and carries an owner-signed
+// writer INVITE, so anyone who opens the public deck mints their own device key
+// and writes — with the relay ENFORCING the signature chain. (A v1 `r` room is
+// permissive: anyone with the file writes, unverified.) The daemon mints
+// server-side, so the owner PRIVATE key is stashed in KV (owner/current) rather
+// than a local owner deck — recoverable if an epoch ever needs People→Remove
+// moderation; at the launch roll cadence spam auto-clears regardless.
+const EC = { name: 'ECDSA', namedCurve: 'P-256' }
+const SIG = { name: 'ECDSA', hash: 'SHA-256' }
+const rnd = (n) => { const b = new Uint8Array(n); crypto.getRandomValues(b); return b }
+async function keypair() {
+ const kp = await crypto.subtle.generateKey(EC, true, ['sign', 'verify'])
+ return {
+ pub: b64u.enc(new Uint8Array(await crypto.subtle.exportKey('raw', kp.publicKey))),
+ priv: b64u.enc(new Uint8Array(await crypto.subtle.exportKey('pkcs8', kp.privateKey))),
+ key: kp.privateKey,
+ }
+}
+async function mintV2Collab(host) {
+ const ownerKp = await keypair()
+ const inviteKp = await keypair()
+ const inviteSig = b64u.enc(new Uint8Array(await crypto.subtle.sign(
+ SIG, ownerKp.key, new TextEncoder().encode(`inv.${inviteKp.pub}.writer.0`))))
+ const commit = b64u.enc(new Uint8Array(await crypto.subtle.digest('SHA-256', b64u.dec(ownerKp.pub))))
+ const key = b64u.enc(rnd(32))
+ const collab = {
+ room: `${host}/d/w${commit}`, key, on: true, v: 2, owner: ownerKp.pub,
+ invite: { pub: inviteKp.pub, priv: inviteKp.priv, role: 'writer', sig: inviteSig },
+ }
+ const owner = { room: collab.room, key, owner: ownerKp.pub, ownerPriv: ownerKp.priv, invitePub: inviteKp.pub }
+ return { collab, owner }
+}
+
// ——— archivist: read-only join, replay, return the room's real doc ———————
async function captureRoom(env) {
const shellText = await env.STORE.get(CURRENT, 'text')
@@ -125,8 +159,7 @@ async function roll(env) {
const prevDoc = extractDoc(curText)
const epoch = (prevDoc.guestbookEpoch ?? 0) + 1
const fonts = { fraunces: prevDoc.assets['font-fraunces'], instrument: prevDoc.assets['font-instrument'] }
- const rnd = (n) => { const b = new Uint8Array(n); crypto.getRandomValues(b); return b }
- const collab = { room: `${env.SYNC_HOST}/d/r${b64u.enc(rnd(12))}`, key: b64u.enc(rnd(32)), on: true }
+ const { collab, owner } = await mintV2Collab(env.SYNC_HOST)
const doc = buildGuestbookDoc({ epoch, docId: crypto.randomUUID(), collab, fonts })
// 3 · splice into a FRESH shell (so epochs pick up app releases too)
@@ -135,12 +168,17 @@ async function roll(env) {
const spliced = spliceDoc(await shellResp.text(), doc)
await env.STORE.put(CURRENT, spliced)
+ // Stash the owner private key (this is a v2 room minted server-side with no
+ // local owner deck). Never served publicly; retrievable by an admin to
+ // reconstruct an owner deck for People→Remove moderation of this epoch.
+ await env.STORE.put('owner/current', JSON.stringify({ epoch, ...owner }, null, 2))
+
const m = await meta(env)
m.epoch = epoch
m.lastRoll = new Date().toISOString()
m.killed = false
await putMeta(env, m)
- return { epoch, archived, size: spliced.length }
+ return { epoch, room: collab.room, archived, size: spliced.length }
}
// ——— entrypoints ————————————————————————————————————————————————————
diff --git a/server/guestbook-daemon/wrangler.toml b/server/guestbook-daemon/wrangler.toml
index a2829393..c5cc25b1 100644
--- a/server/guestbook-daemon/wrangler.toml
+++ b/server/guestbook-daemon/wrangler.toml
@@ -10,9 +10,11 @@ routes = [
{ pattern = "bento.page/guestbook-admin/*", zone_name = "bento.page" },
]
-# Daily archivist snapshot. Cron is UTC: 01:00 UTC ≈ 09:00 HKT.
+# Archivist snapshot + (if due) epoch roll. LAUNCH PHASE: every 15 min so the
+# room time-shards under load and spam auto-clears. Revert to ["0 1 * * *"]
+# (daily) after launch. Cron is UTC.
[triggers]
-crons = ["0 1 * * *"]
+crons = ["*/15 * * * *"]
[[kv_namespaces]]
binding = "STORE"
@@ -26,10 +28,12 @@ ORIGIN_FALLBACK = "https://nyblnet.github.io/bento-site"
# relay host baked into newly minted epochs
SYNC_HOST = "wss://sync.bento.page"
# automatic roll cadence in hours; 0 = manual rolls only (admin endpoint).
-# 24 = daily roll (the archivist cron fires daily at 01:00 UTC, so sub-24 is a
-# no-op). Daily during launch to cap spam exposure on the unmoderated wall;
-# relax to 168 (weekly) once traffic settles.
-ROLL_HOURS = "0"
+# The scheduled handler can only roll as often as the cron above fires, so this
+# must be ≤ the cron interval to take effect. LAUNCH PHASE: 0.25 = every 15 min
+# (matches the */15 cron) to time-shard the room and auto-clear spam. Revert to
+# "0" (manual) after launch — and re-arm a v2 owner-moderated epoch locally
+# (build-guestbook + seed) to restore People→Remove moderation.
+ROLL_HOURS = "0.25"
# secrets (set with `npx wrangler secret put ADMIN_KEY`):
# ADMIN_KEY — bearer for /guestbook-admin/*