From e49108dd06ff2627da6cf1c7c0a151cf95d552a7 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:08:02 -0800 Subject: [PATCH 01/16] docs: import readiness docs and plan; add first-cycle evidence ledger Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- ...-utp-first-cycle-release-identity-civic.md | 231 ++++++++++++++++++ .../AUTH-CUSTODY-BITCOIN-STACK.md | 86 +++++++ .../CLAIM-AUDIT.md | 114 +++++++++ .../EVIDENCE-LEDGER.md | 15 ++ .../EVIDENCE.json | 77 ++++++ .../IDENTITY-AND-APP-RESEARCH.md | 74 ++++++ .../IMPLEMENTATION-PROMPT.md | 55 +++++ .../MERGE-AND-IMPLEMENTATION-LOOP.md | 78 ++++++ docs/testnet-readiness-2026-10-31/SPEC.md | 93 +++++++ 9 files changed, 823 insertions(+) create mode 100644 docs/superpowers/plans/2026-11-03-utp-first-cycle-release-identity-civic.md create mode 100644 docs/testnet-readiness-2026-10-31/AUTH-CUSTODY-BITCOIN-STACK.md create mode 100644 docs/testnet-readiness-2026-10-31/CLAIM-AUDIT.md create mode 100644 docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md create mode 100644 docs/testnet-readiness-2026-10-31/EVIDENCE.json create mode 100644 docs/testnet-readiness-2026-10-31/IDENTITY-AND-APP-RESEARCH.md create mode 100644 docs/testnet-readiness-2026-10-31/IMPLEMENTATION-PROMPT.md create mode 100644 docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md create mode 100644 docs/testnet-readiness-2026-10-31/SPEC.md diff --git a/docs/superpowers/plans/2026-11-03-utp-first-cycle-release-identity-civic.md b/docs/superpowers/plans/2026-11-03-utp-first-cycle-release-identity-civic.md new file mode 100644 index 0000000..5ca43e9 --- /dev/null +++ b/docs/superpowers/plans/2026-11-03-utp-first-cycle-release-identity-civic.md @@ -0,0 +1,231 @@ +# UTP First Cycle (R2 + I1 + C1 + vote hardening) Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Move UTP from "merged and deployed civic shell" to a tested civic slice: one canonical Supabase account, authenticated and race-safe shadow votes, server-verified wallet linking, and a federal source adapter that reports `unavailable` instead of serving fixtures. + +**Architecture:** Next.js 15 route handlers over Supabase Auth/Postgres/RLS. `auth.users.id` is the canonical civic account; Privy/Wagmi stays as the wallet connector only. Pure, injectable modules (`redirect`, `requireUser`, `walletLink`, `SourceAdapter`) carry the logic so Vitest covers them without network; DB behavior (RLS, aggregates, nonce consumption) is proven against a disposable local Supabase stack. + +**Tech Stack:** Next 15, React 19, TypeScript, Vitest 4 + Testing Library, `@supabase/ssr` + `supabase-js` 2.91, viem 2.52 (`viem/siwe`), Supabase CLI (already a devDependency), Hardhat (contracts, untouched this cycle). + +**Spec:** `docs/testnet-readiness-2026-10-31/SPEC.md` (criteria A, B-partial), `AUTH-CUSTODY-BITCOIN-STACK.md` (slices 1-2), `MERGE-AND-IMPLEMENTATION-LOOP.md` (backlog R2, I1, C1, first step of C2), `CLAIM-AUDIT.md` (R6). Executors read these first. + +## Global Constraints + +- Dell-only CI: `runs-on: [self-hosted, linux, utp]`; never a GitHub-hosted runner. +- Missing evidence is `NOT_RUN` or `BLOCKED`, never `PASS`. Record SHA, command, exit code per acceptance item. +- No production money or real BTC; testnet chain ID is 31; optional PQ mode stays off; no claim of private voting, quantum safety, or custom L2. +- Mainnet, provider/environment changes, migrations against remote databases, deployment, external messages, pushes and merges each need their own authorization. This plan prepares them and stops; nothing in it authorizes them. +- Secrets stay in approved stores. No secret, token or private ballot in logs, tests, fixtures or Brain. +- No new dependency, skill, plugin or MCP without required scanning. This plan adds none (viem, @supabase/ssr, vitest, supabase CLI already present). +- Preserve unrelated work: never reset, clean or blanket-stage `/Users/maysamtehranchi/Code/utp`. Implement in an isolated worktree. +- `npm run test:web` must stay nonempty (it exits 1 on zero test files; Task 0 proves this). +- Commit messages end with: + `Co-Authored-By: Claude Opus 5.5 ` and `Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV` + +## Review Focus + +Failure modes the spec implies but no existing test exercises, most likely first. Each has its pinning test in the owning task. + +1. `redirect` query on `/auth/callback` built as `` `${origin}${redirectTo}` `` — `@evil.com` or `.evil.com` yields an off-site redirect. Expected: always same-origin path or `/` (Task 2). +2. `/api/vote` trusting body fields and using a service-role client: non-UUID `billId`, vote outside `for|against|unsure`, or a body-supplied `user_id` must never write another user's row; missing Supabase env must fail closed, not run against `placeholder.supabase.co` (Task 4). +3. Concurrent vote changes leaving `vote_aggregates` different from a recount; no trigger or function in any repo migration updates it today (Task 4). +4. Provider outage or missing `CONGRESS_API_KEY` rendering hardcoded bills (`us-hr4763` etc.) as live (Task 7). +5. Wallet challenge replay across accounts, chains or origins, and address-case duplicates (`0xAbC…` vs `0xabc…`) bypassing uniqueness (Task 6). + +## Verified starting state (2026-11-03, read-only) + +- `origin/master` = `777658f` (last fetch known to the Mac; the cloud shell could not fetch, so Task 0 re-fetches). +- Canonical checkout: local `master` at `d81e60b`, ahead 2 (skills/gitignore commits), behind 1. Uncommitted: Supabase OAuth work (`src/middleware.ts`, `src/lib/supabaseAuth/*`, `src/app/{auth,login,oauth}`, `src/app/api/oauth`), `.env.example`, `AGENTS.md`, `CLAUDE.md`, `package.json` (+`@supabase/ssr`), lockfile, `docs/`. +- `ci:fast` = lint, typecheck, `test:web` (2 dedup tests), `test:contracts` (175 on the merged baseline per `MERGE-AND-IMPLEMENTATION-LOOP.md`). +- Stale and to be corrected in Task 9: `.gsd/STATE.md` (June: 128 passing / 14 failing) and the Project doc `claude/WORKSPACE-MAP.md` (lists #20/#21 as open). +- Code facts found: `src/app/api/vote/route.ts` uses `SUPABASE_SERVICE_ROLE_KEY` and unvalidated body; `src/app/api/propose/route.ts` has no auth, unbounded `limit`, and targets `voting_proposals`, which no repo migration creates; `src/app/api/legislature/route.ts` falls back to hardcoded bills and serves static EU/BR/IN entries; `src/lib/data.ts` issues are synthetic; the issue-page `VotePanel` gates on Privy and keeps votes in `localStorage('utp_votes')`; two conflicting schemas exist (`supabase/migration.sql` legacy vs `supabase/migrations/*`); `shadow_votes` has insert/select RLS but no update policy. +- Brain (Cloudflare D1 `cowork_sessions`) holds no UTP-specific engineering entries; this plan's source of truth is the repo docs above. + +## File Structure + +| Path | Responsibility | +|---|---| +| `src/lib/supabaseAuth/redirect.ts` (new) | `safeRedirectPath` | +| `src/lib/supabaseAuth/requireUser.ts` (new) | Resolve user from cookie session or bearer token | +| `src/lib/supabaseAuth/{client,server}.ts`, `src/middleware.ts`, `src/app/{auth,login,oauth}`, `src/app/api/oauth` | Imported from dirty checkout, then hardened | +| `src/lib/votes/parseVoteInput.ts` (new) | Validate vote payload | +| `src/app/api/vote/route.ts`, `src/app/api/propose/route.ts` | Auth + validation + user-scoped client | +| `src/lib/wallet/walletLink.ts`, `src/lib/wallet/store.ts` (new) | Challenge/verify logic and store interface | +| `src/app/api/wallet/{challenge,link}/route.ts` (new) | HTTP surface | +| `src/lib/civic/{types,freshness,congress}.ts` (new) | `SourceAdapter`, status model, federal adapter | +| `src/app/api/legislature/route.ts`, `src/app/legislature/page.tsx` | Honest status; no fixtures | +| `src/components/ui/DataStatusBadge.tsx` (new) | live / stale / unavailable / sample label | +| `supabase/migrations/20261104000000_vote_integrity.sql`, `…0100_wallet_links.sql`, `…0200_source_registry.sql`, `…0300_voting_proposals.sql` (new) | Schema, RLS, triggers | +| `scripts/test-db.sh`, `vitest.db.config.ts`, `test-db/*.test.ts` (new) | Disposable-DB suite (`npm run test:db`) | + +--- + +### Task 0: Preflight, isolated worktree, stale-doc inventory + +**Files:** Create `docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md` (in the worktree). No code. + +**Interfaces:** Produces: worktree path `~/Code/utp-wt/first-cycle` on branch `feat/first-cycle-auth-civic` from verified `origin/master`; ledger table with columns `id | status | sha | command | exit | note` used by every later task. + +- [ ] **Step 1: Fetch and record heads** — on the Mac run `git fetch origin && git rev-parse origin/master` and `git status -sb`. Expected: origin/master is `777658f` or a recorded successor; record both SHAs and the dirty-file list in the ledger. +- [ ] **Step 2: Create the worktree** — `git worktree add ~/Code/utp-wt/first-cycle -b feat/first-cycle-auth-civic origin/master`. Expected: clean `git status`; dirty checkout untouched (`git -C ~/Code/utp status -sb` unchanged). +- [ ] **Step 3: Baseline** — in the worktree `npm ci && npm run ci:fast`. Expected: exit 0, 2 web tests, 175 contract tests. Record SHA and counts as ledger row `A-baseline`. +- [ ] **Step 4: Prove the zero-test guard** — `npx vitest run src/__no_such_dir__`. Expected: exit 1 ("No test files found"). Record as `A-zero-test-guard`. +- [ ] **Step 5: Commit the ledger** — `git add docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md && git commit -m "docs: add first-cycle evidence ledger"` (with trailers). + +### Task 1: R2 — why Git integration built `master` as Preview (read-only finding) + +**Files:** Modify `docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md` (R2 row, evidence section). + +**Interfaces:** Consumes the Vercel MCP read tools (`get_project`, `list_deployments`, `get_deployment`). Produces: a dated finding stating the project's production branch setting and which deployments carry `target: production`; a reviewable proposed change if the setting is wrong. + +- [ ] **Step 1: Read project config** — `get_project` for the UTP project; record `link.productionBranch` (or equivalent), Git-connected repo, and whether auto-assign production domains is enabled. +- [ ] **Step 2: Compare deployments** — `list_deployments` filtered to the project; record `target` for `777658f` (Git-built, Preview) and `dpl_b2mSdYBpPc48gJGtTzxPjgeMryS9` (manual, production). Expected finding: a concrete cause (branch name mismatch, custom environment, or ignored-build step) or `NOT_DETERMINED` with what was checked. +- [ ] **Step 3: Write the finding** — add it to the R2 section with the exact setting and proposed change. **Do not change any Vercel setting**; changing project/environment settings needs Sam's authorization. Mark R2 `BLOCKED_ON_AUTH` if a change is required. +- [ ] **Step 4: Commit** — `git add docs/... && git commit -m "docs: record Vercel production-target finding (R2)"`. + +### Task 2: Import Supabase auth work and fix redirect handling (I1, slice 1) + +**Files:** +- Create: `src/lib/supabaseAuth/redirect.ts`, `src/lib/supabaseAuth/redirect.test.ts`, `src/app/auth/callback/route.test.ts` +- Import (copy from dirty checkout, record `git hash-object` of each in the ledger): `src/lib/supabaseAuth/{client,server}.ts`, `src/middleware.ts`, `src/app/auth/callback/route.ts`, `src/app/login/page.tsx`, `src/app/oauth/consent/page.tsx`, `src/app/api/oauth/decision/route.ts` +- Modify: `package.json` (+`"@supabase/ssr": "^0.8.0"`), `package-lock.json` via `npm install`, `.env.example` (take the dirty checkout's hunk) + +**Interfaces:** Produces `safeRedirectPath(raw: string | null | undefined): string` — returns a same-origin path (+search+hash) or `'/'`. Callers build URLs as `new URL(safe, origin)`. + +- [ ] **Step 1: Write failing tests** (`redirect.test.ts`): +```ts +import { describe, expect, it } from 'vitest'; +import { safeRedirectPath } from './redirect'; +describe('safeRedirectPath', () => { + it.each(['@evil.com', '.evil.com', '//evil.com', '/\\evil.com', 'https://evil.com', 'javascript:alert(1)', '/%2f%2fevil.com', '/ok\nSet-Cookie:x', '', null, undefined]) + ('rejects %s', (v) => expect(safeRedirectPath(v as string)).toBe('/')); + it('keeps same-origin path, query and hash', () => + expect(safeRedirectPath('/oauth/consent?authorization_id=a%20b#x')).toBe('/oauth/consent?authorization_id=a%20b#x')); +}); +``` +`route.test.ts`: mock `@/lib/supabaseAuth/server` so `exchangeCodeForSession` resolves `{ error: null }`; call `GET(new Request('https://utp.test/auth/callback?code=c&redirect=@evil.com'))`; assert `new URL(res.headers.get('location')!).origin === 'https://utp.test'`. Add a second case where exchange errors: location is `/login?error=…` on the same origin. +- [ ] **Step 2: Run to verify failure** — `npx vitest run src/lib/supabaseAuth src/app/auth` → FAIL (module missing / off-site redirect). +- [ ] **Step 3: Implement `safeRedirectPath`** in `redirect.ts`: accept only strings starting with a single `/`, reject `//`, backslashes, control characters, and anything where `new URL(raw, 'http://x.invalid').origin !== 'http://x.invalid'`; decode-once check so `/%2f%2f…` is rejected. +- [ ] **Step 4: Import files and apply the helper** — copy the listed files into the worktree; in `callback/route.ts` replace string concatenation with `NextResponse.redirect(new URL(safeRedirectPath(redirectParam), origin))`; in `login/page.tsx` run `safeRedirectPath` on the `redirect` search param before putting it in `callbackUrl`; in `api/oauth/decision/route.ts` wrap every interpolated `authorizationId` in `encodeURIComponent`. +- [ ] **Step 5: Run** — `npm run lint && npm run typecheck && npx vitest run` → exit 0, new tests pass. +- [ ] **Step 6: Commit** — `git add` the listed paths only; `git commit -m "feat(auth): import Supabase session/OAuth work with same-origin redirect guard"`. + +### Task 3: `requireUser` — one way to resolve the civic account + +**Files:** Create `src/lib/supabaseAuth/requireUser.ts`, `requireUser.test.ts`. + +**Interfaces:** Consumes `createClient()` from `server.ts`. Produces: +`requireUser(req: Request): Promise<{ user: { id: string }; accessToken: string } | { error: Response }>` — reads `Authorization: Bearer` first, else the cookie session; returns `{ error }` with status 401 (`{message:'Unauthorized'}`) when neither validates, 503 when `NEXT_PUBLIC_SUPABASE_URL`/`ANON_KEY` are unset (no `placeholder` fallback). + +- [ ] **Step 1: Failing tests** — (a) no header and no session → 401; (b) invalid bearer (mock `auth.getUser` returns error) → 401; (c) valid bearer → `user.id` equals mock id and `accessToken` echoed; (d) env unset → 503 and `getUser` never called. +- [ ] **Step 2: Run** `npx vitest run src/lib/supabaseAuth/requireUser.test.ts` → FAIL. +- [ ] **Step 3: Implement** with `createClient` from `@supabase/supabase-js` using the anon key for bearer validation (`auth.getUser(token)`); never accept a user id from the body. +- [ ] **Step 4: Run** → PASS; **Step 5: Commit** `feat(auth): add requireUser session resolver`. + +### Task 4: Vote API and integrity migration (C2 prerequisite) + +**Files:** +- Create: `src/lib/votes/parseVoteInput.ts`, `src/lib/votes/parseVoteInput.test.ts`, `src/app/api/vote/route.test.ts`, `supabase/migrations/20261104000000_vote_integrity.sql`, `scripts/test-db.sh`, `vitest.db.config.ts`, `test-db/vote-integrity.test.ts` +- Modify: `src/app/api/vote/route.ts`, `package.json` (`"test:db": "bash scripts/test-db.sh"`) + +**Interfaces:** Consumes `requireUser` (Task 3). Produces `parseVoteInput(body: unknown): { ok: true; billId: string; vote: 'for'|'against'|'unsure' } | { ok: false; message: string }`; SQL function `verify_vote_aggregates()` returning one row per bill whose `vote_aggregates` differs from a recount (empty = consistent). + +- [ ] **Step 1: Failing unit tests** — `parseVoteInput`: rejects non-object, non-UUID `billId`, vote `'maybe'`, and ignores any `user_id`/`userId` key (returned object has only `billId`, `vote`). Route tests (mock `requireUser` and `@supabase/supabase-js`): 401 passthrough; 400 for invalid input with no DB call; success path calls `upsert` once with `user_id` equal to the session user and `onConflict: 'bill_id,user_id'` even when body contains `user_id: 'someone-else'`; the client is constructed with the **anon** key and the caller's `Authorization` header, never `SUPABASE_SERVICE_ROLE_KEY`; production with `IP_SALT` unset → 503. +- [ ] **Step 2: Failing DB tests** (`test-db/vote-integrity.test.ts`, run against local stack): (a) user B cannot select, update or delete user A's vote; anon cannot insert; (b) 50 concurrent upserts by one user on one bill leave exactly one `shadow_votes` row; (c) 20 users voting concurrently with random choices, then 10 changing their vote: `verify_vote_aggregates()` returns zero rows and `total_votes` equals row count; (d) changing a vote moves one count between buckets; deleting a user's vote decrements. +- [ ] **Step 3: Run to verify failure** — `npx vitest run src/lib/votes src/app/api/vote` → FAIL; `npm run test:db` → FAIL (no trigger, no update policy). +- [ ] **Step 4: Implement migration** `vote_integrity.sql`: add `FOR UPDATE` and `FOR DELETE` policies on `shadow_votes` (`auth.uid() = user_id`); `AFTER INSERT OR UPDATE OR DELETE` trigger `apply_vote_aggregate()` (SECURITY DEFINER, `search_path` pinned) that first takes `pg_advisory_xact_lock(hashtext(bill_id::text))`, then recomputes counts for the affected bill(s) from `shadow_votes` and upserts `vote_aggregates` — recompute, not increment, and lock before counting so concurrent transactions cannot overwrite each other with stale counts; define `verify_vote_aggregates()`; revoke direct write on `vote_aggregates` from `anon, authenticated`. +- [ ] **Step 5: Implement** `parseVoteInput` and rewrite `route.ts`: `requireUser` → `parseVoteInput` → user-scoped client → upsert; remove the service-role client and the `placeholder` fallbacks; return generic 500 text (no raw DB error to caller). +- [ ] **Step 6: `scripts/test-db.sh`** — `supabase start`, `supabase db reset`, run `vitest run --config vitest.db.config.ts`, `supabase stop` (always, via trap). Requires Docker; not added to `ci:fast` until Dell's Docker availability is confirmed (open decision U1). +- [ ] **Step 7: Run** `npm run ci:fast && npm run test:db` → exit 0 (record `test:db` as `NOT_RUN` if Docker is unavailable; never PASS). +- [ ] **Step 8: Commit** — migration, route, helper, tests, scripts. **Remote migration is not applied**; prepare `supabase db push --dry-run` output in the ledger and stop for authorization. + +### Task 5: Propose API — auth, limits, and the missing table + +**Files:** Modify `src/app/api/propose/route.ts`; create `src/app/api/propose/route.test.ts`, `supabase/migrations/20261104000300_voting_proposals.sql`. + +**Interfaces:** Consumes `requireUser`. Produces table `voting_proposals(id uuid pk, title text not null, description text, source_url text unique, bias_score text, category text, status text default 'active', proposer_id uuid references auth.users, created_at)` with RLS: public select, insert only where `proposer_id = auth.uid()`. + +- [ ] **Step 1: Failing tests** — POST without auth → 401; empty/over-200-char title → 400; `sourceUrl` not `http(s)` → 400; duplicate `source_url` → 409; insert payload `proposer_id` equals session user and never `votes_for/votes_against` from the body; GET `limit=100000` is clamped to 50 and `limit=abc` falls back to 20. +- [ ] **Step 2: Run** → FAIL. **Step 3: Implement** route changes (user-scoped client as in Task 4) and the migration (no remote apply). **Step 4: Run** `npm run ci:fast` → PASS. **Step 5: Commit** `feat(api): authenticate and bound /api/propose`. + +### Task 6: Wallet link — server challenge and signature verification (I1, slice 2) + +**Files:** Create `src/lib/wallet/{walletLink.ts,store.ts,walletLink.test.ts}`, `src/app/api/wallet/challenge/route.ts`, `src/app/api/wallet/link/route.ts`, route tests, `supabase/migrations/20261104000100_wallet_links.sql`, `test-db/wallet-links.test.ts`. + +**Interfaces:** Consumes `requireUser`. Produces: +```ts +export const ALLOWED_LINK_CHAINS = [31] as const; +export function normalizeAddress(a: string): `0x${string}`; // throws on invalid; lowercases +export interface WalletLinkStore { + putChallenge(c: Challenge): Promise; + consumeChallenge(nonce: string, userId: string, now: Date): Promise; // atomic, single use + activateLink(l: { userId: string; chainId: number; address: string; assurance: 'aal1'|'aal2' }): Promise<'ok'|'address_taken'>; +} +export function createWalletChallenge(i: { userId: string; address: string; chainId: number; origin: string; now?: Date }, store: WalletLinkStore): Promise<{ message: string; nonce: string; expiresAt: string }>; +export function verifyWalletLink(i: { userId: string; message: string; signature: `0x${string}`; origin: string; now?: Date }, store: WalletLinkStore): Promise<{ ok: true } | { ok: false; reason: 'unknown_nonce'|'expired'|'replayed'|'wrong_origin'|'wrong_chain'|'wrong_user'|'bad_signature'|'address_taken' }>; +``` +Messages are ERC-4361 text built and parsed with `viem/siwe`; TTL 10 minutes. `assurance` is stored so tiered proofing (I2) can later filter; AAL2 enforcement is explicitly **not** part of this cycle. + +- [ ] **Step 1: Failing unit tests** (in-memory store, `privateKeyToAccount` test keys generated in the test): happy path links; replayed nonce → `replayed`; nonce issued to user A used by user B → `wrong_user`; signature by a different key → `bad_signature`; domain `evil.test` vs origin `utp.test` → `wrong_origin`; chain 1 → `wrong_chain` at challenge time (throws) and at verify time; expired by 11 minutes → `expired`; mixed-case address and its lowercase form both map to the same link, so a second account linking either casing → `address_taken`. +- [ ] **Step 2: Failing DB tests** — two users racing 20 parallel `consumeChallenge` calls on one nonce: exactly one non-null; two users racing `activateLink` for one `(chain_id, address)`: exactly one `ok`, one `address_taken`; unlinked wallet without a Supabase session cannot read `wallet_links` of others (RLS). +- [ ] **Step 3: Run** → FAIL. **Step 4: Implement migration**: `wallet_link_challenges(nonce pk, user_id, chain_id, address, domain, expires_at, consumed_at)`; `wallet_links(id, user_id, chain_id, address, assurance, status check in ('active','revoked'), verified_at)` with partial unique index on `(chain_id, address) where status='active'`; SQL function `consume_wallet_challenge(nonce, user_id)` using `UPDATE … WHERE consumed_at IS NULL AND expires_at > now() RETURNING …`; RLS select-own only; writes only through service-side functions. +- [ ] **Step 5: Implement** `walletLink.ts`, a Supabase-backed `store.ts`, and the two routes (challenge: authenticated, returns message; link: authenticated, verifies, returns `{ ok }` with 409 for `address_taken`, 400 otherwise, without echoing internals). +- [ ] **Step 6: Run** `npm run ci:fast && npm run test:db` → PASS (or `NOT_RUN` for DB with reason). **Step 7: Commit** `feat(auth): server-verified wallet linking`. No remote migration applied. + +### Task 7: Source registry and federal adapter; no fixtures in live mode (C1) + +**Files:** Create `src/lib/civic/{types.ts,freshness.ts,congress.ts}` and `*.test.ts`, `supabase/migrations/20261104000200_source_registry.sql`; modify `src/app/api/legislature/route.ts`, `src/app/legislature/page.tsx`. + +**Interfaces:** Produces +```ts +export type SourceStatus = 'live' | 'stale' | 'unavailable' | 'not_integrated'; +export interface CivicBill { externalId: string; jurisdictionCode: string; number: string; title: string; status: string; introducedDate: string | null; sourceUrl: string; recordHash: string } +export interface SourceResult { status: SourceStatus; items: T[]; fetchedAt: string | null; source: { name: string; url: string }; error?: string } +export interface SourceAdapter { id: string; jurisdictionCode: string; fetchBills(o: { limit: number; signal?: AbortSignal }): Promise> } +export function classifyFreshness(fetchedAt: string | null, now: Date, maxAgeMs: number): 'live' | 'stale' | 'unavailable'; +export function congressAdapter(d: { apiKey?: string; fetch?: typeof fetch; now?: () => Date }): SourceAdapter; +``` +`recordHash` = SHA-256 of canonical JSON `{externalId, updateDate, latestActionText}`; it identifies the fetched record state, **not** a bill-text version (text-version hashing is a later task and must not be advertised yet). + +- [ ] **Step 1: Failing tests** — `congressAdapter`: no `apiKey` → `status:'unavailable'`, `items:[]`; HTTP 500 → `unavailable`; thrown network error → `unavailable`; malformed JSON → `unavailable`; 200 with two bills → `status:'live'`, every item has non-empty `sourceUrl`, `fetchedAt` equals injected clock, and none of the ids `us-hr4763`, `us-s2281`, `us-hr5121` appear; identical input yields identical `recordHash`, changed `latestAction` yields a different one. `classifyFreshness`: null → `unavailable`; older than `maxAgeMs` → `stale`. Route test: `GET /api/legislature?country=EU` → `status:'not_integrated'`, `items:[]`; `country=US` with the adapter mocked unavailable → no hardcoded bill in the body. +- [ ] **Step 2: Run** → FAIL. **Step 3: Implement** the types, adapter and rewrite the route: delete `getFallbackUSBills` and the static EU/BR/IN arrays; UK keeps its live fetch but is wrapped in `SourceResult`; response adds `status`, `source`, `fetchedAt`; update `legislature/page.tsx` to read the new shape and show `DataStatusBadge` (Task 8). +- [ ] **Step 4: Migration** `source_registry.sql` — widen `jurisdictions.type` check to include `'local'`; add to `data_sources`: `jurisdiction_id`, `adapter_status text check in ('live','stale','blocked','not_integrated') default 'not_integrated'`, `last_attempt_at`, `last_error`; create `ingest_failures(id, source_id, external_id, error, payload_hash, created_at, resolved_at)` and `bill_versions(id, bill_id, record_hash, fetched_at, unique(bill_id, record_hash))`. Mark `supabase/migration.sql` as legacy in a header comment only; do not delete it. +- [ ] **Step 5: Run** `npm run ci:fast` → PASS. **Step 6: Commit** `feat(civic): source adapter model and federal slice; remove live-mode fixtures`. +- [ ] **Step 7 (state slice, adapter only):** add `openStatesAdapter` for California with the same tests and mocked fetch; a live run stays `BLOCKED` until an OpenStates key exists in an approved store. Local slice (Legistar) is `BLOCKED` on decision U2. + +### Task 8: UI honesty and Supabase-gated voting + +**Files:** Create `src/components/ui/DataStatusBadge.tsx` and test; modify `src/app/issue/[id]/page.tsx`, `src/app/legislature/page.tsx`, `src/lib/data.ts` (add `isSample: true` to every synthetic issue). + +**Interfaces:** Produces `` rendering the literal labels `Live`, `Stale`, `Unavailable`, `Not integrated`, `Sample data`. + +- [ ] **Step 1: Failing tests** (Testing Library): each status renders its label; `unavailable` never renders item counts; issue page for a synthetic issue shows `Sample data`; `VotePanel` with no Supabase session redirects to `/login?redirect=/issue/`; with a session it `fetch`es `POST /api/vote` with `Authorization: Bearer ` and body `{ billId, vote }`; a 401 shows an error and does not set the selected vote; `localStorage` is never written as a vote of record. +- [ ] **Step 2: Run** → FAIL. **Step 3: Implement** badge and `VotePanel` change: session from `createClient()` (`supabaseAuth/client`), not Privy `authenticated`; copy changes from "On-chain shadow vote · one voice per verified wallet" to "Civic test vote · signed-in account" (no verified-wallet claim until linking and eligibility are tested). +- [ ] **Step 4: Run** `npm run ci:fast && npm run build` → exit 0. **Step 5: Commit** `feat(ui): label sample data, gate votes on Supabase session`. + +### Task 9: Evidence, docs and handoff + +**Files:** Modify `.gsd/STATE.md`, `docs/testnet-readiness-2026-10-31/{EVIDENCE-LEDGER.md,MERGE-AND-IMPLEMENTATION-LOOP.md,IMPLEMENTATION-PROMPT.md}`; update Project doc `claude/WORKSPACE-MAP.md`; write Brain handoff. + +- [ ] **Step 1: Ledger** — fill every row (A-baseline, R2, I1-redirect, I1-wallet, C1-federal, C2-vote-integrity) with SHA, command, exit code; unrun items stay `NOT_RUN` or `BLOCKED` with reason. +- [ ] **Step 2: Fix stale docs** — `.gsd/STATE.md` to the merged baseline (175 contracts, protected master, Dell runner `dell-utp-1`); `WORKSPACE-MAP.md` to say #20/#21 merged at `777658f`, via `project_read` then `project_write`. +- [ ] **Step 3: Final gate** — in the worktree `npm ci && npm run ci:fast && npm run build && git diff --check`; expected exit 0. Run `npm run test:db` where Docker exists. +- [ ] **Step 4: Prepare, do not execute, external steps** — write into the ledger: the branch push + PR command, `supabase db push --dry-run` output for the four migrations, and any Vercel change from Task 1. Stop for Sam's authorization on each. +- [ ] **Step 5: Brain handoff** — insert one `cowork_sessions` row (`task_tag: 'utp-first-cycle'`) with summary, ledger statuses and next actions; no secrets. +- [ ] **Step 6: Commit** `docs: update evidence ledger, state and workspace map`. + +--- + +## Self-review + +- **Spec coverage:** A (isolated reconcile, nonempty web tests, DB tests for RLS/duplicates/concurrency) → Tasks 0, 2-6. B (source status/failed queue, no sample-as-live, vote tests: double/concurrent/forged auth/RLS, aggregate recount) → Tasks 4, 7, 8. R2 → Task 1. I1 → Tasks 2, 3, 6. **Not covered here, by design:** C (Rootstock lifecycle, G1/G2), I2 (TOTP/phone/camera), citizen drafting (C2 beyond votes), nationwide ledger (C3), anchoring, L2/PQ/ZK. Each needs its own plan. +- **Type consistency:** `requireUser` return shape is the one consumed by Tasks 4, 5, 6; `SourceStatus` is reused by `DataStatusBadge`; `recordHash` named consistently in adapter, migration (`bill_versions.record_hash`), and tests. +- **Proportion:** logic bodies are specified by signature and tests; only the trigger-locking approach and `recordHash` definition are spelled out because the signatures don't determine them. + +## Open decisions (block only dependent steps) + +- **U1:** Does the Dell runner have Docker for `supabase start`? Until answered, `test:db` is a local gate and `NOT_RUN` in CI. +- **U2:** Local-government denominator (Legistar clients, municipalities/counties) — blocks the local slice only. +- **U3:** Which Supabase project (local-only vs a staging project) receives the four migrations first — blocks remote application only. +- **U4:** Whether Privy sign-in stays visible in `TopBar` during the transition, or is hidden until wallet linking ships. diff --git a/docs/testnet-readiness-2026-10-31/AUTH-CUSTODY-BITCOIN-STACK.md b/docs/testnet-readiness-2026-10-31/AUTH-CUSTODY-BITCOIN-STACK.md new file mode 100644 index 0000000..64d341e --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/AUTH-CUSTODY-BITCOIN-STACK.md @@ -0,0 +1,86 @@ +# Implementation path: one UTP account, linked wallets, optional custodians + +Date: 2026-10-31 PT. Status: proposed stack and acceptance path, not implemented. This document extends `SPEC.md` and `IDENTITY-AND-APP-RESEARCH.md`. + +## Decision + +Use the existing Next.js 15 + Supabase Auth/Postgres/RLS application as UTP's account and civic data authority. `auth.users.id` is the stable UTP account ID. Keep Privy/Wagmi for the existing Web3 connector and signing UI while proving links to UTP accounts with a server challenge. UTP's Supabase OAuth 2.1 server serves *UTP as provider* to other applications; Google/GitHub, Coinbase and Gemini OAuth are *inbound* provider connections to UTP and do not replace UTP's identity or eligibility system. + +Avoid a third auth system. Do not silently equate a Privy DID, an exchange account and a Supabase user. The current local Supabase OAuth code is uncommitted and untested, and the existing Privy button signs users in independently. Reconcile those paths before making any binding vote or credential claim. + +## Components and ownership + +| Function | Proposed component | Boundary | +|---|---|---| +| App login and session | Supabase Auth + `@supabase/ssr` | Server validates session; `auth.users.id` for RLS and civic records | +| Social login | Existing Supabase Google/GitHub OAuth | Account access; provider email does not prove civic eligibility | +| Phone contact / authenticator | Supabase phone verification and TOTP MFA | Phone is contact tier; TOTP/AAL claim enforced in API and RLS for sensitive actions | +| Passkey | Supabase experimental passkey only after a compatibility spike | Current `@supabase/supabase-js` range is `^2.91.0`; official passkey docs require 2.105.0+, explicit opt-in. Pin/test upgrade or use existing TOTP first | +| Web3 account | Existing Privy + Wagmi + Viem | Sign challenge to link external/embedded wallet; privileged actions require fresh proof of possession and correct chain | +| Credential wallet | Evaluate Credo; local encrypted vault | Credential holder and private data stay user-controlled; separate credential/asset/encryption keys | +| Issuer/verifier | Evaluate Credo or walt.id | Signed, scoped eligibility with status/expiry; never infer identity from a wallet or exchange connection | +| UTP-to-app OAuth/OIDC | Supabase OAuth 2.1 server | Current consent UI is local only; enable/configure only after approval and exact-scope tests | +| Custodian connectors | Provider OAuth if available, otherwise least-privilege user API key in approved secret handling | Optional balances/history only in first version; no credential export to AI models or local plain storage | +| Bitcoin commitments | Self-operated Bitcoin testnet node plus a small anchoring worker, after design review | Commit Merkle root of public audit batch; no personal data, raw credentials or private ballot linkages on chain | + +## Link one wallet to one UTP user + +1. Authenticate Supabase user. Issue a single-use, short-expiry random nonce tied to user ID, origin, chain, action and intended wallet address. +2. Wallet signs a structured challenge following ERC-4361 for EVM accounts; verify signature on the server using Viem, including contract-wallet signatures if supported. Verify exact domain/URI, chain ID, nonce, issue/expiry and addressed account. Consume nonce atomically. +3. Persist only a unique `(chain_id, normalized_address, supabase_user_id, verified_at, status)` link. Reject the same address on another account; require fresh MFA plus wallet proof for link, unlink and transfer. Investigate account-recovery paths before enabling transfer. +4. Preserve Privy DID as provider metadata, not as the canonical civic identity. Verify Privy access tokens server-side if a Privy-authenticated endpoint is retained; never trust frontend `authenticated` alone. +5. Tests: two UTP accounts racing to link same wallet; replay, wrong origin/chain/address, expired challenge, contract-wallet signature, stolen session without wallet, and wallet without Supabase session. + +Supabase `linkIdentity()` supports its OAuth provider identities. It does not by itself prove or register arbitrary EVM wallet ownership. See source links below. + +## Custodian API decision + +| Provider | Official API finding | UTP first use | Limit | +|---|---|---|---| +| Coinbase App / Advanced Trade | OAuth and API-key account access exist; scopes and portfolio restrictions vary | Research read-only account/balance/history connection using the current OAuth flow and least scope | CDP wallet APIs are a different product. CDP server wallets are controlled by the app server; CDP smart-account support list does not include Rootstock. Do not assume existing `@coinbase/wallet-sdk` grants exchange access or local custody. | +| Gemini Exchange/Custody | OAuth authorization-code flow supports PKCE for public clients and `balances:read`; API keys use HMAC-SHA384. Production OAuth app activation requires Gemini review | Read-only OAuth sandbox balance test, then a reviewed connector | Custodian-held assets remain at Gemini; an API connection does not move them into UTP's wallet. No trading or withdrawal scopes. | +| BitGo / Fireblocks | Institutional wallet/custody APIs exist; product and access model differ | Research later only if a specific institutional custody need exists | Not needed for personal civic identity or initial testnet. Provider adoption is a separate product/security decision. | + +No provider account, API key, OAuth app or live balance was accessed in this review. Coinbase OAuth scope details and Rootstock support need exact endpoint validation before implementation; do not infer it from generic CDP wallet documentation. Gemini official OAuth docs explicitly document sandbox and production app review. + +## Private code boundary + +Provider tokens/keys stay in approved secret storage or a user-controlled encrypted local connector vault, depending on the provider flow and required server access. Never place them in source, build artifacts, Brain, browser logs, AI prompts or on Bitcoin. A server connector has one provider, one user, a small read-only scope and a revocation path. AI receives a filtered result such as a balance summary only after explicit user grant. It cannot call exchange trade/withdraw endpoints. + +For any provider requiring persistent server refresh tokens, state clearly that those credentials are *server-held*; do not market that integration as wholly local. Prefer local PKCE/public-client flows only if their token protection is acceptable on each target device. Test lost device, revoked consent, expired token, wrong account, provider outage, compromised refresh token and accidental scope expansion. + +## Bitcoin security through code: narrow, measurable claim + +Design candidate: collect public DAO deployment/governance audit events and document/version hashes into a deterministic Merkle tree; sign a manifest with the authorized UTP release key; publish its Merkle root in a Bitcoin **testnet** transaction. Record txid, block height/hash, proof path and required confirmations. A verifier independently checks the manifest/event hashes, Merkle path and inclusion in the Bitcoin chain through its own node. Reorgs invalidate provisional anchoring and trigger reconciliation. + +This yields tamper-evident evidence that a commitment existed by a chain block, subject to Bitcoin confirmation/reorg assumptions. It does **not** make UTP transactions Bitcoin-final, secure a custodian, verify a custom L2 state transition, protect private credentials, or confer post-quantum security. Those require separate bridge, execution, data-availability, custody and cryptographic designs in `SPEC.md`. + +Implementation tests: deterministic root across independent builds; altered leaf and altered path rejection; duplicate/missing event detection; wrong chain/genesis rejection; unconfirmed transaction and reorg handling; independent-node inclusion verification; public manifest reveals no private attributes. Expose `pending`, `confirmed`, `reorged`, and `unavailable` statuses, never a generic `secured by Bitcoin` badge. + +## Sequence and acceptance + +**Slice 1 — canonical account.** Isolate and test local Supabase auth/OAuth work. Fix callback redirect validation and provider configuration assumptions. Make Supabase session authoritative for civic APIs and RLS. Keep legacy Privy path until linked-wallet migration is tested. No production activation. + +**Slice 2 — wallet binding.** Server challenge + signature verification + unique link table, API/RLS tests, two-account race and recovery tests. Wallet-only actions remain possible only where they do not claim civic eligibility. + +**Slice 3 — tiered verification.** TOTP step-up first; phone as contact proof. Benchmark experimental Supabase passkey upgrade in isolation. Synthetic issuer/credential validation before any camera provider or real document processing. + +**Slice 4 — custodian interface.** Build a provider-neutral `AccountSummary` read-only contract and fake provider tests. Implement Gemini sandbox OAuth `balances:read` as first candidate if review/activation allows; validate Coinbase OAuth scopes separately. No trading or transfer APIs. + +**Slice 5 — Bitcoin anchor.** Local regtest deterministic commitment and verifier, then approved public testnet anchor. Tie it to the DAO acceptance manifest, with clear settlement status. + +Every slice needs an exact diff, tests and evidence record. External provider/environment changes, secret grants, migrations, wallet transfers, Bitcoin broadcasts and deployments follow their separate approval gates. Continue local implementation of independent slices while external access is pending. + +## Sources checked 2026-10-31 + +- https://supabase.com/docs/guides/auth/oauth-server/getting-started — OAuth server and OIDC; requires project enablement and client registration. +- https://supabase.com/docs/guides/auth/auth-mfa and https://supabase.com/docs/guides/auth/auth-mfa/totp — TOTP/phone, session `aal`; enforce server-side and RLS. +- https://supabase.com/docs/guides/auth/passkeys — experimental; SDK minimum 2.105.0. +- https://supabase.com/docs/guides/auth/auth-identity-linking — OAuth identity linking. +- https://eips.ethereum.org/EIPS/eip-4361 — signed EVM login challenge fields and nonce. +- https://docs.privy.io/authentication/user-authentication/access-tokens — server verification of Privy session tokens. +- https://docs.cdp.coinbase.com/coinbase-app/introduction/changelog — Coinbase App API/OAuth changes. +- https://docs.cdp.coinbase.com/server-wallets/v2/using-the-wallet-api/managing-accounts — CDP wallet types and supported smart-account networks. +- https://developer.gemini.com/authentication/oauth — Gemini OAuth, PKCE, sandbox, scopes and app review. +- https://developer.gemini.com/authentication/api-key — Gemini HMAC API keys. +- https://developer.bitcoin.org/devguide/operating_modes.html and https://developer.bitcoin.org/devguide/payment_processing.html — inclusion and confirmation/reorg tradeoffs. diff --git a/docs/testnet-readiness-2026-10-31/CLAIM-AUDIT.md b/docs/testnet-readiness-2026-10-31/CLAIM-AUDIT.md new file mode 100644 index 0000000..50fbb30 --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/CLAIM-AUDIT.md @@ -0,0 +1,114 @@ +# UTP claim audit and red-team review + +Review date: 2026-10-31 America/Los_Angeles. Scope: material claims in this chat's project review and proposed roadmap, remote candidate source, CI records, and relevant official documentation. This is a source/document review, not a penetration test, independent audit, or fresh execution of the test suite. + +## Verdict + +Proceed with bounded civic and conventional DAO testnet preparation. Do not describe the current candidate as quantum-secure, private voting, a new Bitcoin L2, or end-to-end testnet proven. The previous calendar and performance figures are unvalidated estimates and are withdrawn as delivery commitments. + +Strongest version of the plan: deliver useful civic workflows while validating conventional governance and researching the substantially different security requirements of a custom Bitcoin L2. + +Load-bearing assumptions: (1) coverage can be expanded through reusable data adapters, (2) existing DAO contracts are a useful application baseline, (3) a viable Bitcoin settlement and post-quantum security design can be selected. Only parts of (1) and (2) have supporting implementation evidence; (3) remains research. + +## Evidence map + +Remote source below is pinned to `e70fd24cc7ee69cda77528199fe5688183256cdb` (PR21). PR20 head is `5a10ca177c19470921a8c5a3159a9c585b8f3167`. Both were OPEN when checked. PR21 targets PR20's branch, not master. + +- E1: https://github.com/samtheloanman/utp/pull/20 +- E2: https://github.com/samtheloanman/utp/pull/21 +- E3: https://github.com/samtheloanman/utp/actions/runs/39207584829 — retrieved logs: contract job `175 passing (6s)`; frontend job `No test files found, exiting with code 0`. +- E4: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/scripts/smoke-governance.cjs +- E5: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/scripts/lib/deploy-core.cjs +- E6: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/contracts/plugins/GovernancePlugin.sol +- E7: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/test/plugins/GovernancePlugin.test.js +- E8: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/.github/workflows/deploy-rsk-testnet.yml +- E9: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/scripts/deploy.config.cjs +- E10: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/scripts/deploy.cjs +- E11: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/.gsd/STATE.md +- E12: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/package.json +- E13: https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/contracts/crypto/dilithium/ZKNOX_ethdilithium.sol +- E14: GitHub API `GET repos/samtheloanman/utp/actions/runners` returned `total_count:0,runners:[]` during this review. Dell host health and organization-runner access were not separately inspected. +- E15: Local `/Users/maysamtehranchi/Code/utp` source: `src/app/api/legislature/route.ts`, `src/app/api/propose/route.ts`, `src/app/api/vote/route.ts`, `supabase/migration.sql`. Local state is dirty and is not equivalent to PR21. + +## Claim ledger + +| Claim | Finding / correction | Evidence | +|---|---|---| +| 175 passing contracts | Verified historical CI result on the named head; not rerun here; not proof of absence of vulnerabilities | E3 | +| Frontend CI is green | Technically green but zero frontend tests ran; cannot support functional readiness | E3, E12 | +| Governance uses OZ Governor/Timelock | Verified source imports, inheritance, wiring; an upstream audit does not audit this integration | E5, E6 | +| Full testnet governance lifecycle works | Unsupported. Full lifecycle uses local mining/time travel; remote smoke returns after read-only checks | E4 | +| Testnet is not deployed | Branch documentation says so; listed Actions history contains only CI. No authoritative live deployment inventory or address/RPC validation obtained, so actual external deployment status is UNKNOWN | E11; run list | +| 11 invariants prove permissions safe | There are 11 named checks; they check selected actors, not a complete role/permission inventory | E5 | +| 8.9M gas and successful RPC rehearsal | Reported in branch state; not independently reproduced in this review. Do not use as a gas budget | E11 | +| Quantum verifier exists | Named Dilithium file is an interface; optional-mode tests use a mock. Real cryptographic integration is unverified | E7, E13 | +| Hybrid voting resists classical-key compromise | Unsupported: caller supplies PQ public key without a voter/key registration binding | E6 | +| Private ZK voting exists | Not established. Current civic vote route records user ID with vote; Governor records address-linked votes | E6, E15 | +| Deployer has no control after setup | Administrative roles are revoked, but initial 10M token supply is assigned to deployer by default; governance influence remains | E5, E9 | +| 4% quorum, 10K threshold, 2880 blocks, 3600-second timelock | Verified candidate config values, not a security endorsement or live-chain configuration | E9 | +| 2880 blocks is a day | Only approximate; vote ends by actual contract clock, not a promised wall-clock deadline | E9 | +| 0.01 tRBTC is enough to deploy | Merely a configured minimum balance, not a dynamic gas-price/estimate check | E9, E10 | +| Dell CI can run immediately | Not established; runner API returns none; both candidate CI and deploy workflow use ubuntu-latest contrary to current fleet policy | E8, E14; CI source | +| Existing APIs fully support civic participation | Prototype source exists; legislature falls back to hardcoded records; propose route has no explicit auth check; exploitability depends on DB policies not inspected live | E15 | +| Federal/state/local source options exist | Verified documentation below; live ingestion coverage, credentials, licensing, quotas and freshness are unverified | S1–S3 | +| Nationwide local coverage by a fixed date | Unsupported. No complete jurisdiction/source inventory or operating estimate | Scope gap | +| 14 weeks / 9–15 months | Planning guesses, not validated staffing, effort or critical-path estimates | Withdrawn as commitments | +| 100 TPS / p95 under 2 seconds | Proposed benchmark hypotheses only; no implementation, hardware definition or measurements | Withdrawn as achieved/promised performance | +| Rootstock app equals our own L2 | False equivalence. Rootstock has its own merged-mining/security system | S4 | +| ZK means zero trust / quantum safety | False generalization; security depends on full construction, setup, signatures, bridge, DA and operational assumptions | S5–S7 | + +## Red-team findings and required counter-tests + +### R1 — High: optional PQ key substitution + +`_castVote` decodes a public key from transaction parameters and verifies a signature over the voter-bound digest. It never verifies that this public key is the voter's enrolled key. Under the claimed classical-key-compromise threat model, an attacker controlling the classical account can generate a fresh PQ key and sign the same digest. Binding the address into a message does not bind key ownership. This is source-supported reasoning, not an executed exploit. Current remote deployment refuses hybrid mode, limiting immediate exposure. + +Required: real verifier known-answer tests, reject unregistered/attacker-generated keys, pre-compromise enrollment commitment, rotation/recovery rules that do not fall back to classical-key-only authority. Apply the threat model to token transfers/delegation too: protecting only a ballot path does not secure voting power or custody. Keep hybrid claims disabled until this passes. + +### R2 — High: successful smoke is not successful governance + +Remote smoke neither casts a vote nor waits an actual voting window/timelock. It prints network/config values without fully attesting expected chain, immutable token/verifier wiring, compiler artifacts, roles or bytecode. + +Required: exact chain assertion, manifest and bytecode verification accounting for immutable/link references; independent RPC readback; live lifecycle with independently controlled wallets and real chain time; negative lifecycle cases. + +### R3 — High: CI gives misleading application confidence + +`--passWithNoTests` allows zero frontend tests. Candidate workflows use hosted runners; no UTP repository runners are listed. Deploy key is job-wide, including dependency install/test steps, increasing credential exposure scope. This is exposure potential, not evidence of compromise. + +Required: nonempty frontend/API/DB suites; Dell runner access validated; untrusted PR jobs never share deployment credentials or persistent privileged runner state; isolate test/install from credential-bearing deployment, inject credentials only for the required step. + +### R4 — High: role renunciation is not decentralization + +Deployer initially owns all 10M UTP, versus configured quorum 400K and threshold 10K. The token holder can retain effective governance control after admin renunciation. A single-actor lifecycle demonstrates functionality, not distributed governance. + +Required: disclose allocation; independent tester control; test snapshot/transfer/delegation effects, minority veto/griefing, quorum shortfall, abstention, cancellation, and all DAO/timelock role holders. Do not describe locally generated wallets owned by one operator as independent governance. + +### R5 — High: governance root power can change the security model + +Timelock holds DAO ROOT. Authorized governance may grant future permissions, so 'only timelock executes' is a current-state assertion, not an immutable invariant. Inventory grant/revoke events from deployment and assert roles at the accepted block. Test malicious permission-grant proposals and make their effects clear in the UI. + +### R6 — High: civic trust and privacy can fail outside contracts + +Hardcoded fallback bills can be mistaken for live data. Local proposals lack an explicit route-level auth check; RLS effectiveness is not established. Stored user-linked ballots are not anonymous. Amendments, changing district boundaries, public IP logs and small voter sets can invalidate claims of accuracy/eligibility/privacy. + +Required: no silent fixtures in live modes; stable jurisdiction/session/type identifiers; cited version hashes; concurrent-vote/aggregate tests; RLS cross-user tests; clear sentiment vs binding modes; an eligibility and privacy threat model including issuer, recovery, logs and coercion limitations. + +### R7 — High: custom L2 security is a separate unresolved system + +The repository does not establish a working custom sequencer, DA, proof pipeline, Bitcoin verification/bridge, withdrawal mechanism or PQ-safe custody path. STARK execution proofs alone do not imply ballot confidentiality or a quantum-resistant Bitcoin bridge. BitVM's documented assumptions and any pairing-based wrapper must be evaluated end-to-end. + +Required: architecture decision, explicit trust/upgrade/custody model, valid and invalid proof tests, DA withholding, sequencer outage/censorship, reorg, bridge double-withdrawal and exit tests. No proprietary cryptography invented as an implementation shortcut. + +## Official research sources + +- S1 https://api.congress.gov/ and https://github.com/LibraryOfCongress/api.congress.gov — federal API exists; this audit did not run authenticated ingestion. +- S2 https://docs.openstates.org/ — provider documents 50 states, DC and Puerto Rico; this does not establish UTP coverage or completeness. +- S3 https://webapi.legistar.com/Help — client-specific legislative API exists; no nationwide municipal completeness claim follows. +- S4 https://dev.rootstock.io/concepts/foundations/merged-mining/ and https://dev.rootstock.io/dev-tools/wallets/metamask/ — separate Rootstock validation; testnet chain ID 31. +- S5 https://csrc.nist.gov/pubs/fips/204/final — ML-DSA signature standard, including current errata notice. Standardized algorithm is not proof of correct integration. +- S6 https://bitvm.org/bitvm2 — setup and operator assumptions; not assumption-free settlement. +- S7 https://www.starknet.io/roadmap/ — Bitcoin settlement/quantum items appear in a vendor roadmap; not evidence those capabilities are deployed in UTP or a drop-in solution. + +## Verification limits + +No fresh compile/test run, live DB/RLS audit, browser workflow, independent security review, contract deployment, chain address verification, latency/load benchmark or provider mutation occurred. Source conclusions and historical logs are explicitly separated from runtime proof. No complete-project security certification is implied. diff --git a/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md b/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md new file mode 100644 index 0000000..6a36593 --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md @@ -0,0 +1,15 @@ +# First-cycle evidence ledger + +Plan: `docs/superpowers/plans/2026-11-03-utp-first-cycle-release-identity-civic.md`. Statuses: PASS / FAIL / NOT_RUN / BLOCKED / BLOCKED_ON_AUTH. Missing evidence is never PASS. + +Environment: Claude cloud container (Linux, Node 22.23.4, npm 10.9.9, PostgreSQL 16 binaries, no Docker). Clone of `https://github.com/samtheloanman/utp` at `origin/master` `777658f`, branch `feat/first-cycle-auth-civic`. The canonical Mac checkout (`d81e60b`, dirty) was not modified; its uncommitted auth files and readiness docs were copied in read-only (provenance below). + +| id | status | sha | command | exit | note | +|---|---|---|---|---|---| +| A-baseline-web | PASS | 777658f | `npm run lint && npm run typecheck && npm run test:web` (inside `npm run ci:fast`) | 0 | lint clean, typecheck clean, 2/2 web tests | +| A-baseline-contracts | PASS | 777658f | `SOLC_JS_PATH= npm run test:contracts` | 0 | 175 passing. Plain `ci:fast` fails here only because the sandbox cannot download solc (HH502 ENOTFOUND binaries.soliditylang.org) | +| A-zero-test-guard | PASS | 777658f | `npx vitest run src/__no_such_dir__` | 1 | Vitest exits non-zero when no test files are found | + +## Provenance of imported files (from the Mac checkout, 2026-11-03) + +Readiness docs `docs/testnet-readiness-2026-10-31/*.md` and `EVIDENCE.json`, and the plan file, were untracked on the Mac and are committed here unchanged. Auth files are imported in Task 2 and listed there with `git hash-object` values. diff --git a/docs/testnet-readiness-2026-10-31/EVIDENCE.json b/docs/testnet-readiness-2026-10-31/EVIDENCE.json new file mode 100644 index 0000000..af86f1f --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/EVIDENCE.json @@ -0,0 +1,77 @@ +{ + "retrieved_at_utc": "2026-11-01T00:07:05.666931+00:00", + "candidate_sha": "e70fd24cc7ee69cda77528199fe5688183256cdb", + "sources": [ + { + "path": "contracts/plugins/GovernancePlugin.sol", + "url": "https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/contracts/plugins/GovernancePlugin.sol", + "git_blob_sha": "1cab0bcec9a85ab5f62ae869d7ca9eaf2599ce65", + "sha256": "e614f15e99794c403a6dd048a537cf8780fa0dfa4a95903063f4c595158474f1", + "content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\nimport {Governor} from \"@openzeppelin/contracts/governance/Governor.sol\";\nimport {GovernorSettings} from \"@openzeppelin/contracts/governance/extensions/GovernorSettings.sol\";\nimport {GovernorCountingSimple} from \"@openzeppelin/contracts/governance/extensions/GovernorCountingSimple.sol\";\nimport {GovernorVotes} from \"@openzeppelin/contracts/governance/extensions/GovernorVotes.sol\";\nimport {GovernorVotesQuorumFraction} from \"@openzeppelin/contracts/governance/extensions/GovernorVotesQuorumFraction.sol\";\nimport {GovernorTimelockControl} from \"@openzeppelin/contracts/governance/extensions/GovernorTimelockControl.sol\";\nimport {TimelockController} from \"@openzeppelin/contracts/governance/TimelockController.sol\";\nimport {IVotes} from \"@openzeppelin/contracts/governance/utils/IVotes.sol\";\n\n/// @dev Post-quantum signature verifier (e.g. ZKNOX Dilithium). Same shape as ZKNOX_ethdilithium.verify.\ninterface IPQVerifier {\n function verify(bytes calldata pk, bytes calldata m, bytes calldata sig, string calldata aux) external view returns (bool);\n}\n\n/**\n * @title GovernancePlugin\n * @notice Binding, token-weighted governance for the UtP DAO.\n * @dev Built on OpenZeppelin Governor (audited):\n * - For / Against / Abstain counting (GovernorCountingSimple)\n * - Voting delay, voting period, proposal threshold (GovernorSettings)\n * - Vote weight and quorum from UTP ERC20Votes snapshots (GovernorVotes, GovernorVotesQuorumFraction)\n * - Successful proposals are queued and executed ONLY through the timelock (GovernorTimelockControl)\n *\n * Optional post-quantum hybrid mode (off by default, see roadmap decision D1):\n * when `requireHybridAuth` is true, every vote must carry a PQ signature in `params`\n * (abi.encode(bytes pqPublicKey, bytes pqSignature)) over `hybridVoteDigest(...)`.\n * Enforced in `_castVote`, so castVote / castVoteBySig cannot bypass it (no downgrade).\n */\ncontract GovernancePlugin is\n Governor,\n GovernorSettings,\n GovernorCountingSimple,\n GovernorVotes,\n GovernorVotesQuorumFraction,\n GovernorTimelockControl\n{\n /// @notice PQ verifier used when hybrid auth is required. Zero when hybrid mode is off.\n IPQVerifier public immutable pqVerifier;\n\n /// @notice When true, every vote must include a valid PQ signature.\n bool public immutable requireHybridAuth;\n\n error HybridAuthRequired();\n error InvalidHybridAuth();\n error HybridVerifierMissing();\n\n struct Config {\n uint48 votingDelay; // blocks between proposal and vote start\n uint32 votingPeriod; // blocks the vote stays open\n uint256 proposalThreshold; // min UTP votes to propose\n uint256 quorumPercent; // % of past total supply (0-100)\n }\n\n constructor(\n IVotes token,\n TimelockController timelock,\n Config memory cfg,\n address pqVerifier_,\n bool requireHybridAuth_\n )\n Governor(\"UtP Governance\")\n GovernorSettings(cfg.votingDelay, cfg.votingPeriod, cfg.proposalThreshold)\n GovernorVotes(token)\n GovernorVotesQuorumFraction(cfg.quorumPercent)\n GovernorTimelockControl(timelock)\n {\n if (requireHybridAuth_ && pqVerifier_ == address(0)) revert HybridVerifierMissing();\n pqVerifier = IPQVerifier(pqVerifier_);\n requireHybridAuth = requireHybridAuth_;\n }\n\n // ------------------------------------------------------------------\n // Hybrid (post-quantum) voting\n // ------------------------------------------------------------------\n\n /// @notice Message a voter signs with their PQ key. Binds proposal, voter, choice, contract and chain (replay protection).\n function hybridVoteDigest(uint256 proposalId, address voter, uint8 support) public view returns (bytes32) {\n return keccak256(abi.encodePacked(proposalId, voter, support, address(this), block.chainid));\n }\n\n /// @notice Convenience wrapper: vote with a PQ signature.\n function castVoteHybrid(\n uint256 proposalId,\n uint8 support,\n bytes calldata pqPublicKey,\n bytes calldata pqSignature\n ) external returns (uint256) {\n return _castVote(proposalId, _msgSender(), support, \"\", abi.encode(pqPublicKey, pqSignature));\n }\n\n function _castVote(\n uint256 proposalId,\n address account,\n uint8 support,\n string memory reason,\n bytes memory params\n ) internal override returns (uint256) {\n if (requireHybridAuth) {\n if (params.length == 0) revert HybridAuthRequired();\n (bytes memory pk, bytes memory sig) = abi.decode(params, (bytes, bytes));\n bytes32 digest = hybridVoteDigest(proposalId, account, support);\n if (sig.length == 0 || !pqVerifier.verify(pk, abi.encodePacked(digest), sig, \"\")) {\n revert InvalidHybridAuth();\n }\n }\n return super._castVote(proposalId, account, support, reason, params);\n }\n\n // ------------------------------------------------------------------\n // Required overrides\n // ------------------------------------------------------------------\n\n function votingDelay() public view override(Governor, GovernorSettings) returns (uint256) {\n return super.votingDelay();\n }\n\n function votingPeriod() public view override(Governor, GovernorSettings) returns (uint256) {\n return super.votingPeriod();\n }\n\n function proposalThreshold() public view override(Governor, GovernorSettings) returns (uint256) {\n return super.proposalThreshold();\n }\n\n function quorum(uint256 blockNumber) public view override(Governor, GovernorVotesQuorumFraction) returns (uint256) {\n return super.quorum(blockNumber);\n }\n\n function state(uint256 proposalId) public view override(Governor, GovernorTimelockControl) returns (ProposalState) {\n return super.state(proposalId);\n }\n\n function proposalNeedsQueuing(uint256 proposalId) public view override(Governor, GovernorTimelockControl) returns (bool) {\n return super.proposalNeedsQueuing(proposalId);\n }\n\n function _queueOperations(\n uint256 proposalId,\n address[] memory targets,\n uint256[] memory values,\n bytes[] memory calldatas,\n bytes32 descriptionHash\n ) internal override(Governor, GovernorTimelockControl) returns (uint48) {\n return super._queueOperations(proposalId, targets, values, calldatas, descriptionHash);\n }\n\n function _executeOperations(\n uint256 proposalId,\n address[] memory targets,\n uint256[] memory values,\n bytes[] memory calldatas,\n bytes32 descriptionHash\n ) internal override(Governor, GovernorTimelockControl) {\n super._executeOperations(proposalId, targets, values, calldatas, descriptionHash);\n }\n\n function _cancel(\n address[] memory targets,\n uint256[] memory values,\n bytes[] memory calldatas,\n bytes32 descriptionHash\n ) internal override(Governor, GovernorTimelockControl) returns (uint256) {\n return super._cancel(targets, values, calldatas, descriptionHash);\n }\n\n function _executor() internal view override(Governor, GovernorTimelockControl) returns (address) {\n return super._executor();\n }\n}\n" + }, + { + "path": "scripts/lib/deploy-core.cjs", + "url": "https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/scripts/lib/deploy-core.cjs", + "git_blob_sha": "0a041721e6fcf9bd0c6a3457929c9f5a17915e54", + "sha256": "c2b6ec278044ff4cee2beb493511273e296ab1a13f968687a81e5b6ab5181904", + "content": "/**\n * Governance-core deployment, shared by scripts/deploy.cjs and the test suite\n * so tests exercise exactly the wiring that ships.\n *\n * Deploys: DAO, UTPToken, UtPTimelock, GovernancePlugin, Treasury, PluginRegistry.\n * Final permission state (verified by verifyCore):\n * - Timelock holds ROOT on the DAO and is the only executor of DAO actions.\n * - GovernancePlugin is the timelock's only proposer (+ canceller); it holds no DAO permissions.\n * - Timelock administers itself (no external admin).\n * - Deployer holds no ROOT and no timelock admin.\n * - Optional guardian may CANCEL queued operations (emergency veto), nothing else.\n * - PluginRegistry holds only UNINSTALL_PLUGIN_PERMISSION.\n */\n\nconst ZERO = \"0x0000000000000000000000000000000000000000\";\n\nfunction log(logger, msg) {\n if (logger) logger(msg);\n}\n\nasync function deployCore(ethers, deployer, params, logger) {\n const p = {\n initialHolder: deployer.address,\n initialSupply: ethers.parseEther(\"10000000\"),\n votingDelay: 1,\n votingPeriod: 50,\n proposalThreshold: 0n,\n quorumPercent: 4,\n timelockMinDelay: 3600,\n guardian: ZERO,\n pqVerifier: ZERO,\n requireHybridAuth: false,\n ...params,\n };\n\n const deployed = {};\n const deploy = async (name, args) => {\n const F = await ethers.getContractFactory(name, deployer);\n const c = await F.deploy(...args);\n await c.waitForDeployment();\n const addr = await c.getAddress();\n const rcpt = await c.deploymentTransaction().wait();\n deployed[name] = addr;\n log(logger, ` ${name.padEnd(17)} ${addr} (gas ${rcpt.gasUsed})`);\n return c;\n };\n const send = async (label, txPromise) => {\n const tx = await txPromise;\n const rcpt = await tx.wait();\n log(logger, ` \u2714 ${label} (gas ${rcpt.gasUsed})`);\n return rcpt;\n };\n\n log(logger, \"Deploying contracts\");\n const dao = await deploy(\"DAO\", []);\n const daoAddr = await dao.getAddress();\n const token = await deploy(\"UTPToken\", [daoAddr, p.initialHolder, p.initialSupply]);\n // admin = deployer only during setup; renounced below\n const timelock = await deploy(\"UtPTimelock\", [p.timelockMinDelay, [], [ZERO], deployer.address]);\n const governance = await deploy(\"GovernancePlugin\", [\n await token.getAddress(),\n await timelock.getAddress(),\n {\n votingDelay: p.votingDelay,\n votingPeriod: p.votingPeriod,\n proposalThreshold: p.proposalThreshold,\n quorumPercent: p.quorumPercent,\n },\n p.pqVerifier,\n p.requireHybridAuth,\n ]);\n const treasury = await deploy(\"Treasury\", [daoAddr]);\n const registry = await deploy(\"PluginRegistry\", [daoAddr]);\n\n const tlAddr = await timelock.getAddress();\n const govAddr = await governance.getAddress();\n const PROPOSER = await timelock.PROPOSER_ROLE();\n const CANCELLER = await timelock.CANCELLER_ROLE();\n const TL_ADMIN = await timelock.DEFAULT_ADMIN_ROLE();\n const ROOT = await dao.ROOT_PERMISSION_ID();\n const UNINSTALL = await dao.UNINSTALL_PLUGIN_PERMISSION_ID();\n\n log(logger, \"Wiring permissions\");\n await send(\"timelock: governance = proposer\", timelock.grantRole(PROPOSER, govAddr));\n await send(\"timelock: governance = canceller\", timelock.grantRole(CANCELLER, govAddr));\n if (p.guardian !== ZERO) {\n await send(`timelock: guardian ${p.guardian} = canceller`, timelock.grantRole(CANCELLER, p.guardian));\n }\n await send(\"dao: timelock = ROOT\", dao.grant(daoAddr, tlAddr, ROOT));\n await send(\"dao: registry = UNINSTALL_PLUGIN\", dao.grant(daoAddr, await registry.getAddress(), UNINSTALL));\n await send(\"registry: install governance plugin\", registry.installPlugin(govAddr));\n\n log(logger, \"Removing deployer privileges\");\n await send(\"dao: revoke deployer ROOT\", dao.revoke(daoAddr, deployer.address, ROOT));\n await send(\"timelock: deployer renounces admin\", timelock.renounceRole(TL_ADMIN, deployer.address));\n\n return {\n addresses: deployed,\n params: p,\n contracts: { dao, token, timelock, governance, treasury, registry },\n };\n}\n\n/** Asserts the post-deploy permission invariants. Throws on any violation. */\nasync function verifyCore(core, deployerAddress, logger) {\n const { dao, timelock, governance, registry } = core.contracts;\n const daoAddr = await dao.getAddress();\n const tlAddr = await timelock.getAddress();\n const govAddr = await governance.getAddress();\n const regAddr = await registry.getAddress();\n const ROOT = await dao.ROOT_PERMISSION_ID();\n const EXEC = await dao.EXECUTE_PERMISSION_ID();\n const TL_ADMIN = await timelock.DEFAULT_ADMIN_ROLE();\n\n const checks = [\n [\"timelock holds DAO ROOT\", await dao.hasPermission(daoAddr, tlAddr, ROOT), true],\n [\"deployer has no DAO ROOT\", await dao.hasPermission(daoAddr, deployerAddress, ROOT), false],\n [\"deployer cannot execute on DAO\", await dao.hasPermission(daoAddr, deployerAddress, EXEC), false],\n [\"governance cannot execute on DAO directly\", await dao.hasPermission(daoAddr, govAddr, EXEC), false],\n [\"registry has no DAO ROOT\", await dao.hasPermission(daoAddr, regAddr, ROOT), false],\n [\"timelock is self-administered\", await timelock.hasRole(TL_ADMIN, tlAddr), true],\n [\"deployer has no timelock admin\", await timelock.hasRole(TL_ADMIN, deployerAddress), false],\n [\"governance is timelock proposer\", await timelock.hasRole(await timelock.PROPOSER_ROLE(), govAddr), true],\n [\"deployer is not timelock proposer\", await timelock.hasRole(await timelock.PROPOSER_ROLE(), deployerAddress), false],\n [\"governance points at timelock\", (await governance.timelock()).toLowerCase(), tlAddr.toLowerCase()],\n [\"governance plugin installed\", (await registry.plugins(govAddr)).active, true],\n ];\n\n let failed = 0;\n for (const [label, actual, expected] of checks) {\n const ok = actual === expected;\n if (!ok) failed++;\n log(logger, ` ${ok ? \"\u2714\" : \"\u2718\"} ${label}${ok ? \"\" : ` (got ${actual}, want ${expected})`}`);\n }\n if (failed) throw new Error(`${failed} post-deploy invariant(s) failed`);\n return checks.length;\n}\n\nmodule.exports = { deployCore, verifyCore, ZERO };\n" + }, + { + "path": "scripts/smoke-governance.cjs", + "url": "https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/scripts/smoke-governance.cjs", + "git_blob_sha": "c4cd45bf9748c9179c05218fa2bc1a9c08d940ed", + "sha256": "ae03316b0f9f44402450a4616e032c9757eff1440908f707f4272c485c5dc550", + "content": "/**\n * Post-deploy smoke test against a live RPC, using deployments/.json.\n *\n * npx hardhat run scripts/smoke-governance.cjs --network localhost # full lifecycle (time travel)\n * npx hardhat run scripts/smoke-governance.cjs --network rskTestnet # read-only invariant checks\n *\n * Local: delegate -> propose -> vote -> queue -> wait -> execute a real proposal.\n * Remote: re-verify every permission invariant and print live governance settings.\n */\nconst hre = require(\"hardhat\");\nconst fs = require(\"fs\");\nconst path = require(\"path\");\nconst { verifyCore } = require(\"./lib/deploy-core.cjs\");\n\nconst LOCAL = new Set([\"hardhat\", \"localhost\"]);\n\nasync function main() {\n const network = hre.network.name;\n const file = path.join(__dirname, \"..\", \"deployments\", `${network}.json`);\n if (!fs.existsSync(file)) throw new Error(`No ${path.relative(process.cwd(), file)}; deploy first.`);\n const dep = JSON.parse(fs.readFileSync(file, \"utf8\"));\n const A = dep.contracts;\n const { ethers } = hre;\n\n const at = (name, addr) => ethers.getContractAt(name, addr);\n const contracts = {\n dao: await at(\"DAO\", A.DAO),\n token: await at(\"UTPToken\", A.UTPToken),\n timelock: await at(\"UtPTimelock\", A.UtPTimelock),\n governance: await at(\"GovernancePlugin\", A.GovernancePlugin),\n treasury: await at(\"Treasury\", A.Treasury),\n registry: await at(\"PluginRegistry\", A.PluginRegistry),\n };\n const g = contracts.governance;\n\n console.log(`=== Smoke test: ${network} (chainId ${(await ethers.provider.getNetwork()).chainId}) ===`);\n for (const [k, v] of Object.entries(A)) {\n const code = await ethers.provider.getCode(v);\n if (code === \"0x\") throw new Error(`${k} at ${v} has no code`);\n console.log(` \u2714 ${k.padEnd(17)} ${v} (${(code.length - 2) / 2} bytes)`);\n }\n console.log(\"Invariants\");\n await verifyCore({ contracts }, dep.deployer, console.log);\n console.log(\"Live settings\");\n const clock = await g.clock();\n console.log(` votingDelay=${await g.votingDelay()} votingPeriod=${await g.votingPeriod()} threshold=${ethers.formatEther(await g.proposalThreshold())} UTP`);\n console.log(` quorum now=${ethers.formatEther(await g.quorum(clock - 1n))} UTP timelockDelay=${await contracts.timelock.getMinDelay()}s hybrid=${await g.requireHybridAuth()}`);\n\n if (!LOCAL.has(network)) {\n console.log(\"=== SMOKE OK (read-only on remote network) ===\");\n return;\n }\n\n // ---- Full lifecycle on a local node ----\n const [deployer, voter, recipient] = await ethers.getSigners();\n const mine = async (n) => { for (let i = 0; i < n; i++) await ethers.provider.send(\"evm_mine\", []); };\n const step = (m) => console.log(` \u2192 ${m}`);\n\n console.log(\"Lifecycle\");\n step(\"distribute + delegate 1,000,000 UTP to voter\");\n await (await contracts.token.connect(deployer).transfer(voter.address, ethers.parseEther(\"1000000\"))).wait();\n await (await contracts.token.connect(voter).delegate(voter.address)).wait();\n await mine(1);\n\n step(\"fund treasury with 1 ETH\");\n await (await deployer.sendTransaction({ to: A.Treasury, value: ethers.parseEther(\"1\") })).wait();\n\n const withdraw = contracts.treasury.interface.encodeFunctionData(\"withdraw\", [recipient.address, ethers.parseEther(\"0.25\")]);\n const daoCall = contracts.dao.interface.encodeFunctionData(\"execute\", [[A.Treasury], [0], [withdraw]]);\n const targets = [A.DAO], values = [0], calldatas = [daoCall];\n const description = `Smoke test payout ${Date.now()}`;\n const descHash = ethers.id(description);\n\n step(\"propose treasury payout\");\n await (await g.connect(voter).propose(targets, values, calldatas, description)).wait();\n const id = await g.hashProposal(targets, values, calldatas, descHash);\n const stateName = async () => [\"Pending\", \"Active\", \"Canceled\", \"Defeated\", \"Succeeded\", \"Queued\", \"Expired\", \"Executed\"][Number(await g.state(id))];\n console.log(` proposal ${id.toString().slice(0, 12)}\u2026 state=${await stateName()}`);\n\n await mine(Number(await g.votingDelay()) + 1);\n step(`vote FOR (state=${await stateName()})`);\n await (await g.connect(voter).castVote(id, 1)).wait();\n\n await mine(Number(await g.votingPeriod()));\n step(`queue (state=${await stateName()})`);\n await (await g.queue(targets, values, calldatas, descHash)).wait();\n\n const delay = Number(await contracts.timelock.getMinDelay());\n step(`wait timelock ${delay}s (state=${await stateName()})`);\n await ethers.provider.send(\"evm_increaseTime\", [delay]);\n await mine(1);\n\n const before = await ethers.provider.getBalance(recipient.address);\n step(\"execute\");\n await (await g.execute(targets, values, calldatas, descHash)).wait();\n const delta = (await ethers.provider.getBalance(recipient.address)) - before;\n console.log(` state=${await stateName()} recipient +${ethers.formatEther(delta)} ETH`);\n if (delta !== ethers.parseEther(\"0.25\")) throw new Error(\"Payout amount mismatch\");\n console.log(\"=== SMOKE OK (full lifecycle) ===\");\n}\n\nmain().catch((e) => { console.error(`=== SMOKE FAILED === ${e.message}`); process.exitCode = 1; });\n" + }, + { + "path": "scripts/deploy.config.cjs", + "url": "https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/scripts/deploy.config.cjs", + "git_blob_sha": "abbaf5269d92e1cc72a12285a1c3a92fd93f979f", + "sha256": "53682d25792298946e16c6baa5c6bc5b3b2e5e81464487cd6725d6a347fed7ce", + "content": "/**\n * Governance parameters per network (roadmap decision D2).\n * RSK blocks average ~30s: 2880 blocks \u2248 1 day.\n * Change these, not the deploy script.\n */\nconst { ethers } = require(\"ethers\");\n\nconst ZERO = \"0x0000000000000000000000000000000000000000\";\n\nmodule.exports = {\n hardhat: {\n chainId: 31337,\n initialSupply: ethers.parseEther(\"10000000\"),\n votingDelay: 1,\n votingPeriod: 20,\n proposalThreshold: ethers.parseEther(\"1000\"),\n quorumPercent: 4,\n timelockMinDelay: 60,\n guardian: ZERO,\n minDeployerBalance: 0n,\n },\n localhost: {\n chainId: 31337,\n initialSupply: ethers.parseEther(\"10000000\"),\n votingDelay: 1,\n votingPeriod: 20,\n proposalThreshold: ethers.parseEther(\"1000\"),\n quorumPercent: 4,\n timelockMinDelay: 60,\n guardian: ZERO,\n minDeployerBalance: 0n,\n },\n rskTestnet: {\n chainId: 31,\n initialSupply: ethers.parseEther(\"10000000\"), // 10M UTP to deployer for tester distribution\n votingDelay: 1, // ~30s\n votingPeriod: 2880, // ~1 day\n proposalThreshold: ethers.parseEther(\"10000\"), // 0.1% of initial supply\n quorumPercent: 4,\n timelockMinDelay: 3600, // 1 hour\n guardian: process.env.GUARDIAN_ADDRESS || ZERO, // optional emergency canceller (multisig recommended)\n minDeployerBalance: ethers.parseEther(\"0.01\"), // tRBTC for gas\n },\n // rskMainnet intentionally absent: blocked until Phase 3 (audit) passes.\n};\n" + }, + { + "path": "scripts/deploy.cjs", + "url": "https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/scripts/deploy.cjs", + "git_blob_sha": "938dd57daf23bad7006d030871c364f87636eb35", + "sha256": "0b50d57dbb50552c61d2dec1d4d410a0f516488e7929f8fa742b3af1d9df8215", + "content": "/**\n * Deploy the UtP governance core.\n *\n * npx hardhat run scripts/deploy.cjs --network hardhat # in-process rehearsal\n * npx hardhat run scripts/deploy.cjs --network localhost # against `npx hardhat node`\n * npx hardhat run scripts/deploy.cjs --network rskTestnet # needs DEPLOYER_PRIVATE_KEY\n *\n * Outputs:\n * deployments/.json canonical addresses (read by the app)\n * deployments/logs/-.log full transcript of this run\n *\n * Financial modules (StablecoinController, UBTC, EventMarket) are NOT deployed here;\n * they stay gated until Roadmap Phase 4.\n */\nconst hre = require(\"hardhat\");\nconst fs = require(\"fs\");\nconst path = require(\"path\");\nconst { deployCore, verifyCore } = require(\"./lib/deploy-core.cjs\");\nconst config = require(\"./deploy.config.cjs\");\n\nconst LOCAL = new Set([\"hardhat\", \"localhost\"]);\n\nasync function main() {\n const network = hre.network.name;\n const ts = new Date().toISOString().replace(/[:.]/g, \"-\");\n const logDir = path.join(__dirname, \"..\", \"deployments\", \"logs\");\n fs.mkdirSync(logDir, { recursive: true });\n const logFile = path.join(logDir, `${network}-${ts}.log`);\n const lines = [];\n const log = (m) => { console.log(m); lines.push(m); };\n const flush = () => fs.writeFileSync(logFile, lines.join(\"\\n\") + \"\\n\");\n\n try {\n const params = config[network];\n if (!params) {\n throw new Error(`No deploy config for network \"${network}\". Mainnet is blocked until the Phase 3 audit gate.`);\n }\n\n const [deployer] = await hre.ethers.getSigners();\n if (!deployer) throw new Error(\"No deployer account. Set DEPLOYER_PRIVATE_KEY for non-local networks.\");\n\n // ---- Preflight ----\n const net = await hre.ethers.provider.getNetwork();\n const balance = await hre.ethers.provider.getBalance(deployer.address);\n log(\"=== UtP governance core deployment ===\");\n log(`network ${network} (chainId ${net.chainId})`);\n log(`deployer ${deployer.address}`);\n log(`balance ${hre.ethers.formatEther(balance)} native`);\n log(`block ${await hre.ethers.provider.getBlockNumber()}`);\n log(`params votingDelay=${params.votingDelay} votingPeriod=${params.votingPeriod} threshold=${hre.ethers.formatEther(params.proposalThreshold)} UTP quorum=${params.quorumPercent}% timelock=${params.timelockMinDelay}s guardian=${params.guardian}`);\n\n if (BigInt(net.chainId) !== BigInt(params.chainId)) {\n throw new Error(`chainId mismatch: RPC reports ${net.chainId}, config expects ${params.chainId}`);\n }\n if (balance < params.minDeployerBalance) {\n throw new Error(`Deployer balance ${hre.ethers.formatEther(balance)} below minimum ${hre.ethers.formatEther(params.minDeployerBalance)}. Fund it from a testnet faucet.`);\n }\n if (!LOCAL.has(network) && (params.pqVerifier || params.requireHybridAuth)) {\n throw new Error(\"Hybrid PQ voting has no production verifier yet (decision D1). Refusing to deploy with mocks.\");\n }\n\n // ---- Deploy + wire ----\n const started = Date.now();\n const core = await deployCore(hre.ethers, deployer, params, log);\n\n // ---- Verify invariants ----\n log(\"Verifying post-deploy invariants\");\n const n = await verifyCore(core, deployer.address, log);\n log(`All ${n} invariants hold. Took ${((Date.now() - started) / 1000).toFixed(1)}s.`);\n\n // ---- Persist ----\n const out = {\n network,\n chainId: Number(net.chainId),\n deployer: deployer.address,\n deployedAt: new Date().toISOString(),\n blockNumber: await hre.ethers.provider.getBlockNumber(),\n params: Object.fromEntries(Object.entries(params).map(([k, v]) => [k, typeof v === \"bigint\" ? v.toString() : v])),\n contracts: core.addresses,\n };\n if (network !== \"hardhat\") {\n const file = path.join(__dirname, \"..\", \"deployments\", `${network}.json`);\n fs.writeFileSync(file, JSON.stringify(out, null, 2) + \"\\n\");\n log(`Wrote ${path.relative(process.cwd(), file)}`);\n }\n log(`Log: ${path.relative(process.cwd(), logFile)}`);\n log(\"=== DEPLOYMENT OK ===\");\n } catch (err) {\n log(`=== DEPLOYMENT FAILED === ${err.message}`);\n flush();\n throw err;\n }\n flush();\n}\n\nmain().catch(() => { process.exitCode = 1; });\n" + }, + { + "path": ".github/workflows/ci.yml", + "url": "https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/.github/workflows/ci.yml", + "git_blob_sha": "1f8ff9639308dc3af127c598180b34563ff8beab", + "sha256": "594307fff6c6678bc09d1a8969f4ef384f6dfac174130701345e65eab23aa9b0", + "content": "name: CI\n\non:\n pull_request:\n push:\n branches: [master]\n\npermissions:\n contents: read\n\nconcurrency:\n group: ci-${{ github.ref }}\n cancel-in-progress: true\n\njobs:\n web:\n name: Lint, typecheck, build\n runs-on: ubuntu-latest\n steps:\n - uses: actions/checkout@v4\n - uses: actions/setup-node@v4\n with:\n node-version: 20\n cache: npm\n - run: npm ci --no-audit --no-fund\n - run: npm run lint\n - run: npm run typecheck\n - run: npm run build\n env:\n NEXT_TELEMETRY_DISABLED: \"1\"\n - run: npm run test:web\n\n contracts:\n name: Compile and test contracts\n runs-on: ubuntu-latest\n steps:\n - uses: actions/checkout@v4\n - uses: actions/setup-node@v4\n with:\n node-version: 20\n cache: npm\n - run: npm ci --no-audit --no-fund\n - run: npx hardhat compile\n - run: npm run test:contracts\n" + }, + { + "path": ".github/workflows/deploy-rsk-testnet.yml", + "url": "https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/.github/workflows/deploy-rsk-testnet.yml", + "git_blob_sha": "a87dda1a6cac5cec3b2ea5813d8b6c19818819cd", + "sha256": "9b05df53dbfcbcb4fa34e52a8467caf5cbc192c1c5ffcde480fa9903c3858051", + "content": "name: Deploy governance to RSK testnet\n\n# Manual only. Requires the \"rsk-testnet\" environment with:\n# secret DEPLOYER_PRIVATE_KEY (a dedicated testnet-only key funded with tRBTC)\n# variable GUARDIAN_ADDRESS (optional emergency canceller, e.g. a multisig)\n# variable RSK_TESTNET_RPC_URL (optional; defaults to the public RSK node)\non:\n workflow_dispatch:\n inputs:\n confirm:\n description: 'Type \"deploy-testnet\" to confirm'\n required: true\n\npermissions:\n contents: read\n\nconcurrency:\n group: deploy-rsk-testnet\n cancel-in-progress: false\n\njobs:\n deploy:\n if: ${{ github.event.inputs.confirm == 'deploy-testnet' }}\n runs-on: ubuntu-latest\n environment: rsk-testnet\n env:\n DEPLOYER_PRIVATE_KEY: ${{ secrets.DEPLOYER_PRIVATE_KEY }}\n GUARDIAN_ADDRESS: ${{ vars.GUARDIAN_ADDRESS }}\n RSK_TESTNET_RPC_URL: ${{ vars.RSK_TESTNET_RPC_URL || 'https://public-node.testnet.rsk.co' }}\n steps:\n - uses: actions/checkout@v4\n - uses: actions/setup-node@v4\n with:\n node-version: 20\n cache: npm\n - run: npm ci --no-audit --no-fund\n\n - name: Gate \u2014 full contract test suite must pass\n run: npm run test:contracts\n\n - name: Gate \u2014 rehearse the exact deploy in-process\n run: npx hardhat run scripts/deploy.cjs --network hardhat\n\n - name: Check secrets are present\n run: |\n if [ -z \"$DEPLOYER_PRIVATE_KEY\" ]; then\n echo \"::error::DEPLOYER_PRIVATE_KEY secret is not set on the rsk-testnet environment\"; exit 1\n fi\n\n - name: Deploy to RSK testnet\n run: npx hardhat run scripts/deploy.cjs --network rskTestnet\n\n - name: Smoke test (read-only) against RSK testnet\n run: npx hardhat run scripts/smoke-governance.cjs --network rskTestnet\n\n - name: Summary\n if: always()\n run: |\n {\n echo \"## RSK testnet deployment\"\n if [ -f deployments/rskTestnet.json ]; then\n echo '```json'; cat deployments/rskTestnet.json; echo '```'\n echo \"Commit \\`deployments/rskTestnet.json\\` from the artifact to record these addresses.\"\n else\n echo \"No deployment file written. See the log artifact.\"\n fi\n } >> \"$GITHUB_STEP_SUMMARY\"\n\n - name: Upload deployment record + logs\n if: always()\n uses: actions/upload-artifact@v4\n with:\n name: rsk-testnet-deployment\n path: |\n deployments/rskTestnet.json\n deployments/logs/\n if-no-files-found: warn\n" + }, + { + "path": "package.json", + "url": "https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/package.json", + "git_blob_sha": "3b2b52d3529da4ec3a61f02eb053816ae3f495d5", + "sha256": "bab16e1bc3ee6c195a92442709632e1746e695d449b6a9cecb3711f01b6a0702", + "content": "{\n \"name\": \"utp\",\n \"version\": \"1.0.0\",\n \"private\": true,\n \"type\": \"module\",\n \"scripts\": {\n \"dev\": \"next dev\",\n \"build\": \"next build\",\n \"start\": \"next start\",\n \"lint\": \"next lint\",\n \"compile\": \"npx hardhat compile\",\n \"postcompile\": \"node scripts/generate-abis.js\",\n \"test\": \"npm run test:contracts && npm run test:web\",\n \"typecheck\": \"tsc --noEmit\",\n \"test:contracts\": \"hardhat test\",\n \"test:web\": \"vitest run --passWithNoTests\",\n \"deploy:local\": \"hardhat run scripts/deploy.cjs --network localhost\",\n \"deploy:rskTestnet\": \"hardhat run scripts/deploy.cjs --network rskTestnet\"\n },\n \"dependencies\": {\n \"@base-org/account\": \"^2.5.3\",\n \"@coinbase/wallet-sdk\": \"^4.3.7\",\n \"@eslint/eslintrc\": \"^3.3.5\",\n \"@metamask/sdk\": \"^0.34.0\",\n \"@neynar/nodejs-sdk\": \"^3.176.0\",\n \"@privy-io/react-auth\": \"^3.30.0\",\n \"@privy-io/wagmi\": \"^4.0.12\",\n \"@react-native-async-storage/async-storage\": \"^3.0.1\",\n \"@react-three/drei\": \"^10.7.7\",\n \"@react-three/fiber\": \"^9.6.1\",\n \"@safe-global/safe-apps-provider\": \"^0.18.6\",\n \"@safe-global/safe-apps-sdk\": \"^9.1.0\",\n \"@supabase/supabase-js\": \"^2.91.0\",\n \"@tailwindcss/postcss\": \"^4.2.1\",\n \"@tanstack/react-query\": \"^5.90.21\",\n \"@types/three\": \"^0.184.1\",\n \"@walletconnect/ethereum-provider\": \"^2.23.8\",\n \"autoprefixer\": \"^10.4.27\",\n \"graphai\": \"^2.0.16\",\n \"gsap\": \"^3.15.0\",\n \"lenis\": \"^1.3.23\",\n \"lucide-react\": \"^0.563.0\",\n \"next\": \"^15.3.6\",\n \"pino-pretty\": \"^13.1.3\",\n \"porto\": \"^0.2.37\",\n \"ratelimit\": \"^0.0.4\",\n \"react\": \"^19.0.0\",\n \"react-dom\": \"^19.0.0\",\n \"three\": \"^0.184.0\",\n \"tinydb\": \"^0.1.0\",\n \"viem\": \"^2.52.2\",\n \"wagmi\": \"^2.19.5\",\n \"zustand\": \"^5.0.11\"\n },\n \"devDependencies\": {\n \"@nomicfoundation/hardhat-chai-matchers\": \"^2.1.0\",\n \"@nomicfoundation/hardhat-ethers\": \"^3.1.0\",\n \"@nomicfoundation/hardhat-ignition\": \"^0.15.16\",\n \"@nomicfoundation/hardhat-ignition-ethers\": \"^0.15.17\",\n \"@nomicfoundation/hardhat-network-helpers\": \"^1.1.0\",\n \"@nomicfoundation/hardhat-toolbox\": \"^5.0.0\",\n \"@nomicfoundation/hardhat-verify\": \"^2.1.3\",\n \"@nomicfoundation/ignition-core\": \"^0.15.15\",\n \"@openzeppelin/contracts\": \"^5.0.0\",\n \"@testing-library/dom\": \"^10.4.1\",\n \"@testing-library/jest-dom\": \"^6.9.1\",\n \"@testing-library/react\": \"^16.3.2\",\n \"@typechain/ethers-v6\": \"^0.5.1\",\n \"@typechain/hardhat\": \"^9.1.0\",\n \"@types/mocha\": \"^10.0.10\",\n \"@types/node\": \"^20.19.30\",\n \"@types/react\": \"^19.2.9\",\n \"@types/react-dom\": \"^19.2.3\",\n \"@vitejs/plugin-react\": \"^5.1.3\",\n \"chai\": \"^4.5.0\",\n \"dotenv\": \"^17.3.1\",\n \"eslint\": \"^9.39.2\",\n \"eslint-config-next\": \"^15.1.4\",\n \"ethers\": \"^6.16.0\",\n \"hardhat\": \"^2.22.6\",\n \"hardhat-gas-reporter\": \"^1.0.8\",\n \"hardhat-ignore-warnings\": \"^0.2.0\",\n \"jsdom\": \"^28.0.0\",\n \"postcss\": \"^8.5.6\",\n \"solc\": \"^0.8.33\",\n \"solidity-coverage\": \"^0.8.1\",\n \"supabase\": \"^2.72.8\",\n \"tailwindcss\": \"^3.4.19\",\n \"ts-node\": \"^10.9.2\",\n \"typechain\": \"^8.3.2\",\n \"typescript\": \"^5.9.3\",\n \"vitest\": \"^4.0.18\"\n }\n}\n" + }, + { + "path": "contracts/crypto/dilithium/ZKNOX_ethdilithium.sol", + "url": "https://github.com/samtheloanman/utp/blob/e70fd24cc7ee69cda77528199fe5688183256cdb/contracts/crypto/dilithium/ZKNOX_ethdilithium.sol", + "git_blob_sha": "33750f2f2964bb5e275af93fe4982fca4cb834a2", + "sha256": "a6c9888599a3bde549e2558652298552bb47185545022705557cfb66f1f2b73f", + "content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\n/**\n * @title ZKNOX_ethdilithium\n * @dev Dummy interface created to fix a blocking compilation error.\n * The original file was missing from the project.\n */\ninterface ZKNOX_ethdilithium {\n function verify(bytes calldata pk, bytes calldata m, bytes calldata sig, string calldata aux) external view returns (bool);\n}\n" + } + ], + "ci_log_excerpts": [ + "Compile and test contracts\tUNKNOWN STEP\t2026-10-19T06:03:42.5015503Z 175 passing (6s)", + "Lint, typecheck, build\tUNKNOWN STEP\t2026-10-19T06:05:21.5339114Z No test files found, exiting with code 0" + ], + "runner_snapshot": { + "total_count": 0, + "runners": [] + } +} diff --git a/docs/testnet-readiness-2026-10-31/IDENTITY-AND-APP-RESEARCH.md b/docs/testnet-readiness-2026-10-31/IDENTITY-AND-APP-RESEARCH.md new file mode 100644 index 0000000..8c2e5b1 --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/IDENTITY-AND-APP-RESEARCH.md @@ -0,0 +1,74 @@ +# Wallet-first identity and AI application research + +Date: 2026-10-31. User direction: phone, authenticator and camera credential validation in tiers; data stored locally in the user's wallet; AI-first everything-app direction. This updates the product target, not the status of implemented features. No packages, skills or services installed. + +## Recommended architecture (proposal) + +Keep UTP's civic/DAO application. Add a local encrypted personal vault and credential wallet, then an AI interface with narrow, revocable capabilities. Evaluate native/mobile wallet support before assuming a browser-only implementation can meet key-protection and camera-proofing requirements. A blockchain asset wallet does not automatically provide encrypted document storage or verifiable credentials. + +Separate authentication (control of an enrolled authenticator), identity proofing (who an issuer verified), eligibility (jurisdiction/age/membership), and transaction authorization (approval of a particular action). Product tiers below are not NIST assurance-level certifications and are not automatically additive factors. + +| Tier | Proposed evidence | Allowed use / limitation | +|---|---|---| +| Guest | None | Search/read public material; local drafts | +| Phone contact | Phone number possession, if SMS/voice used | Low-assurance contact badge/rate controls; not uniqueness, residency or sole high-risk recovery | +| Protected account | WebAuthn/passkey with required user verification; authenticator TOTP as explicitly lower-assurance alternative | Account control; fresh step-up for sensitive operations; device-bound vs synced passkeys disclosed | +| Proofed identity | Camera document capture/authenticity checks plus live holder matching when required by selected proofing method; trusted issuer validates evidence | Signed identity credential in local wallet; selfie alone does not prove identity; independent fallback for accessibility/failures | +| Eligible participant | Issuer-attested, scoped, expiring jurisdiction/age/membership claims and uniqueness policy | Only the specific eligibility established by evidence; not automatically citizenship, voter registration or one-human uniqueness | +| Sensitive action | Appropriate eligible credential plus fresh authenticator confirmation bound to exact action | Binding vote, credential recovery, transfer or admin action according to policy; AI cannot approve for user | + +Phone + authenticator + camera on one compromised handset is not three independent trust domains. Camera proofing differs from OS face unlock: device biometrics should activate the authenticator locally; a document/liveness verification process needs its own issuer, retention, injection-attack and false-rejection model. NIST treats PSTN-based out-of-band authentication as restricted and biometrics as part of an authenticator-based multifactor process, not an identity guarantee [S1]. + +## Local wallet data model + +- Local encrypted vault: signed credentials, private drafts, user-selected documents/preferences and private AI memory. Protect encryption keys with platform secure storage where supported; verify target-device behavior rather than promise hardware protection for every key type. +- Asset signing keys, credential keys and vault encryption keys have separate purposes and recovery policies. Passkey authentication does not automatically encrypt the vault or sign Bitcoin transactions. +- Optional user-controlled end-to-end encrypted backup/sync; opt-in metadata disclosure. Test recovery from device loss and corruption. Local-only mode honestly warns that no backup means permanent loss can occur. +- Public data: published legislative requests and intentionally public discussion must be distributed to servers/peers; they cannot remain exclusively local and simultaneously be publicly available. +- Server: minimal necessary public keys, session/abuse state, issuer trust/status information and public content. Do not put raw IDs, face images, phone numbers, private documents or stable cross-service identity hashes on chain. +- Proofing provider may process identity evidence; select and disclose its data flow/retention. Do not promise that all proofing is on-device before verifying it. Prefer short-lived processing and retaining a credential rather than raw images where feasible. +- Present scoped claims through challenge/domain-bound presentations with expiry and revocation checks. Selective disclosure is not automatically unlinkability, ZK voting or post-quantum security. Evaluate exact credential suites and issuer keys against the broader PQ goal. +- Private ballots require a separate issuance/nullifier/tally construction. Authentication session logs, IPs, timestamps and public token addresses must not silently relink the ballot. Contact verification is never the anti-Sybil proof. + +## AI permission boundary + +AI may search public legislation, query user-approved local documents, explain claims and draft requests. Give it typed operations and minimal results, not unrestricted wallet storage, OTP secrets, recovery material or signing keys. Keep untrusted retrieved text outside the authorization plane. + +Signing and credential presentation occur through a wallet-controlled approval surface with the exact recipient/domain, action, disclosed attributes and expiry. Default to local inference for private content; any cloud inference requires explicit data scope and consent. 'Local storage' alone does not mean 'local processing'. + +## Projects researched + +GitHub API/readme/license checks were performed on 2026-10-31. All six repositories below were non-archived and showed recent pushes; that does not establish quality, security, mobile compatibility or deploy readiness. No runtime evaluations performed. Fit rankings are engineering judgments, not measured results. + +| Project | Confirmed scope/license | Potential reuse | Important boundary | +|---|---|---|---| +| [Puter](https://github.com/HeyPuter/puter) | Self-hostable internet-computer/app platform; AI, storage and app SDK; AGPL-3.0 except identified components | Closest broad app-platform reference | Not a proven local credential wallet or Bitcoin/ZK platform; hosted/cloud behavior must be inventoried | +| [Goose](https://github.com/aaif-goose/goose) | Extensible AI agent; Apache-2.0 | Candidate AI execution/tool runtime | Not a consumer identity wallet; evaluate mobile/server boundary and containment | +| [OpenClaw](https://github.com/openclaw/openclaw) | Personal AI assistant across channels/platforms; MIT | Assistant UX and capability patterns | Broad tool access is inappropriate for a wallet without separate capability enforcement | +| [LobeHub](https://github.com/lobehub/lobehub) | Agent workspace; custom Community License | UI/product reference | License requires commercial licensing for developing/distributing commercial derivatives; not an unrestricted open-source fork candidate | +| [Credo](https://github.com/openwallet-foundation/credo-ts) | TypeScript identity framework; Node/React Native; credential/OpenID4VC support; Apache-2.0 | First credential SDK to evaluate because of UTP TypeScript stack | Not an AI app or complete secure wallet UX; exact credential versions and browser compatibility need testing | +| [walt.id](https://github.com/walt-id/waltid-identity) | Identity issuer/holder/verifier toolkit; Apache-2.0 Community Stack | Alternative issuance/verifier service stack | Server wallet deployments do not meet local-custody requirements by default; verify storage architecture | + +No reviewed project establishes the entire requested combination of AI app, local credential custody, camera proofing, private voting, Bitcoin L2 and PQ security. Reuse components rather than replacing UTP wholesale. Start with a Credo-based feasibility spike and compare walt.id for issuer/verifier services; evaluate Goose for scoped AI tools. Puter supplies product/platform ideas, not a required migration. + +## New acceptance gates before civic testnet sign-off + +1. Record accepted issuers, supported credentials, exact tier-to-action rules, privacy policy, recovery rules and mobile/browser target matrix. No 'verified' badge without a defined assertion. +2. Test passkey origin/RP challenge binding, user verification, expired/replayed challenges, TOTP replay/rate controls and session step-up. Phone-only recovery cannot regain higher-tier privileges. +3. Camera proofing: recorded/injected video, document tampering, stolen documents, failed liveness/matching, accessibility fallback and deletion checks. No accuracy/fairness claims without representative evaluation and stated error rates. +4. Present credentials with nonce/audience/holder binding; reject expired, revoked, forged and wrong-jurisdiction claims. Test issuer-key compromise/rotation and unavailable status service under defined fail-closed policy. +5. Vault: offline unlock/read, device theft, logout, browser XSS threat model, backup restore, lost device, corrupted backup and key rotation. Verify no plaintext credentials/keys in logs, telemetry, network traces or AI prompts. +6. AI: malicious document/tool output cannot trigger disclosure, signing, transfer or recovery. User approval binds exact action bytes/attributes; no silent substitution between preview and signature. +7. Test private eligibility proof separately from login; verify no unintended ballot linkage and no double voting after recovery or credential reissuance. +8. Existing conventional passkeys, credential suites and signatures are not labeled post-quantum. Quantum migration must include issuer, holder, recovery and action-signing paths; preserve the separate full-L2 gate. + +## Primary references + +- S1: https://pages.nist.gov/800-63-4/sp800-63b/authenticators/ — authenticator, biometric and restricted PSTN guidance. +- S2: https://www.w3.org/TR/vc-data-model/ — issuer/holder/verifier model; selecting a data model does not select a secure cryptographic suite. +- S3: Repository links in the comparison table, including current license files. +- S4: https://github.com/lobehub/lobehub/blob/main/LICENSE — commercial derivative restriction checked directly. + +## Immediate implementation sequence + +After acceptance of the revised spec: define tier/action policy and threat model; synthetic local credential issuance -> encrypted wallet storage -> passkey-authenticated presentation -> eligibility-gated civic action; adversarial tests; only then integrate a camera-proofing provider under separate approval. Keep real ID images out of research artifacts, Brain and general-purpose models. Expand the AI shell after its wallet capabilities are isolated. diff --git a/docs/testnet-readiness-2026-10-31/IMPLEMENTATION-PROMPT.md b/docs/testnet-readiness-2026-10-31/IMPLEMENTATION-PROMPT.md new file mode 100644 index 0000000..25d3826 --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/IMPLEMENTATION-PROMPT.md @@ -0,0 +1,55 @@ +# Proposed prompt for the next implementation session + +Release coordination update (2026-10-31): read [MERGE-AND-IMPLEMENTATION-LOOP.md](MERGE-AND-IMPLEMENTATION-LOOP.md) first. PR #21 merged into PR #20; the combined PR #20 merged to `master` as `777658f`. Post-merge Dell CI run `41264740648` passed at exact `777658f`; clean-source production deployment `dpl_b2mSdYBpPc48gJGtTzxPjgeMryS9` is READY. Reconfirm live state before the next batch. The custom Bitcoin L2 remains a later, separately evidenced workstream. + +Use after Sam validates SPEC.md. This document is not itself an authorization to deploy, commit, push, merge, migrate, purchase services, message people, install integrations or spawn agents. + +--- + +Implement UTP through bounded, evidence-driven iterations toward the acceptance contract in `/Users/maysamtehranchi/Code/utp/docs/testnet-readiness-2026-10-31/SPEC.md`. Read `CLAIM-AUDIT.md` first and preserve its distinctions between historical CI, source observations, hypotheses and live proof. + +The full goal is nationwide local/state/federal legislative participation, citizen legislative requests, a custom Bitcoin L2 with private voting and verifiable execution, explicitly scoped quantum-resistant security, and a functional DAO controlling that L2. Rootstock application governance is an intermediate milestone, never a substitute for the full goal. + +Include the user's wallet-first AI-app identity requirement: read `IDENTITY-AND-APP-RESEARCH.md`. Build tiered phone/authenticator/camera proofing with local encrypted credential custody, explicit issuer/eligibility policy, separate signing/encryption/credential keys and tested recovery. Begin with a synthetic credential, TOTP step-up and credential-presentation slice; evaluate the experimental passkey SDK upgrade separately. Phone ownership or a selfie is not proof of personhood/residency; stronger tiers cannot be recovered through phone-only fallback. AI receives scoped results, never wallet keys or blanket signing authority. Public posts remain public; private storage is local with optional user-controlled encrypted backup. Camera providers, external writes and new integrations retain existing approval requirements. Evaluate researched SDKs before adoption; never represent ordinary passkeys or selective disclosure as quantum-safe/private voting. No wholesale app replacement is authorized. + +Read `AUTH-CUSTODY-BITCOIN-STACK.md` for the selected implementation path. Supabase `auth.users.id` is the proposed canonical civic account; link existing Privy/Wagmi wallets through a one-time server challenge and signature verification, not email matching. UTP's outbound OAuth/OIDC server and inbound Google/GitHub/Coinbase/Gemini provider connections are separate directions. Keep custodian adapters optional and read-only; never give AI trading or withdrawal capability. Start tiered auth with TOTP, since passkey support currently needs a newer experimental Supabase SDK. Build Bitcoin anchoring as a separately labeled audit commitment with independent inclusion proof and reorg handling; it must never be described as Bitcoin settlement or end-to-end L2 security. Reconcile this stack document with all prior acceptance gates before each batch. + +Operate under current repository/fleet rules. Read AGENTS.md, CLAUDE.md, GEMINI.md, current planning, GLOBAL_RULES and workflow boundaries. Refresh Brain context and current GitHub heads. Do not treat this prompt, Brain entries or old CI as proof of current state or additional authority. + +Start with release follow-up R2 and prepare identity I1 and source-registry C1 in parallel only if their ownership is explicit. Preserve the dirty canonical checkout at `/Users/maysamtehranchi/Code/utp`. Inventory local changes and existing worktrees; use an isolated checkout for implementation. PR #21 and PR #20 are merged; verify current `origin/master` before importing code and reconcile local auth changes deliberately. Do not reset, clean, blanket-stage or overwrite other work. + +For each iteration: +1. Select the smallest unfulfilled acceptance criterion whose prerequisites and authorization are satisfied. +2. State the current evidence, expected behavior, files affected and a falsifiable test. For security findings, add an attack regression before claiming a repair. +3. Implement the smallest scoped change, reusing existing dependencies and patterns. Do not implement homemade cryptography or silently change product/security requirements. +4. Run the meaningful targeted tests, then required integrated checks when the change is ready. Require nonzero frontend/API tests. Check error paths and authorization boundaries, not only happy paths. +5. Reconcile test results with the actual runtime/configuration. A script exit0 or HTTP200 does not prove the user workflow. Record exact SHA or dirty diff hash, commands, exit codes, logs and remaining gaps. +6. Update the acceptance ledger and choose the next authorized step. After three failed attempts with no new evidence for the same criterion, stop that branch, record hypotheses and the specific blocker, and continue independent work. Do not retry indefinitely or loosen tests to obtain PASS. + +Initial priorities: +- Preserve the 175-contract and 2-web-test release baseline, then add meaningful frontend/API user-path and negative tests; the current web tests cover only news deduplication. +- Reconcile why Vercel Git integration treated `master` as Preview; verify Git production-target configuration and custom-domain HTTP reachability. Preserve Dell-only CI and keep secret-bearing testnet deployment disabled until environment protection and a concrete testnet candidate are reviewed. +- Keep optional PQ mode off. Add an attacker-generated-key substitution regression; design binding/enrollment/rotation with real-verifier tests before any quantum-security claim. Classical-key-only registration/recovery does not defeat classical-key compromise. +- Remove silent sample-data fallback in live legislation mode; add source/version identity, auth/RLS and concurrent tally tests. +- Implement one complete civic and citizen-drafting slice, then expand coverage with an explicit jurisdiction ledger. +- Implement complete DAO frontend flows and stronger manifest/role/bytecode checks. Preserve binding on-chain actions separate from civic sentiment. + +Before any externally visible mutation, prepare the exact reviewable candidate, target, payload/config, prechecks, cost or test-fund scope, rollback/recovery and validation steps. Obtain only the missing authorization required by current rules. Do not ask again for authorization already clearly granted. Continue independent local work while a dependent external step is blocked. + +For public testnet, use dedicated test-only accounts and test funds from approved secret stores. Verify chain31 for Rootstock. No mainnet deployment, real BTC custody, live financial products or messages to officials in this scope. Do not invent contract addresses or transaction receipts. + +Testnet C requires real wall-clock/chain progression, independently controlled participants, actual proposal/vote/queue/execute transactions, second-RPC readback and negative cases. Local time travel is only local evidence. Record allocation and effective governance control, not merely revoked admin roles. Use safe testnet actions and never expose secrets or private ballots in logs. + +For custom L2 D, first produce an evidence-backed architecture decision and benchmark prototypes. Do not claim quantum-safe Bitcoin settlement because an account uses ML-DSA, or private ballots because execution uses ZK. If dependencies cannot meet the threat model, report that honestly with options; do not silently substitute a sidechain deployment, trusted bridge or centralized operator for the agreed goal. + +Stop for missing credentials/401/403 according to fleet rules, unresolved authority, destructive steps, required external security review, or material product/cryptographic choices. A BLOCKED acceptance criterion is not completion. No background schedules or extra agents without applicable authorization. + +Return after each coherent batch: implemented behavior, evidence, acceptance status, blockers and next concrete action. Keep artifacts in this project; save a distilled Brain handoff under project `utp` when done. Commit/push/merge only if explicitly authorized and all applicable exact-head checks pass. + +Final labels: +- LOCAL_BASELINE_ACCEPTED: A passes. +- CIVIC_SCOPE_ACCEPTED: B passes for its explicitly declared coverage; incomplete national coverage stays open. +- ROOTSTOCK_DAO_TESTNET_ACCEPTED: C passes, without implying D. +- FULL_CUSTOM_L2_TESTNET_ACCEPTED: all full-scope required gates including D pass with receipts and independent review. + +Never claim the full goal complete because the budget/time window ends, a prototype works, an intermediate milestone passes, or a review model agrees. diff --git a/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md b/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md new file mode 100644 index 0000000..036dd9d --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md @@ -0,0 +1,78 @@ +# UTP branch integration and implementation loop + +Status: release integration completed 2026-10-31. This remains an implementation plan, not a claim of civic or custom-L2 testnet readiness. + +## Verification snapshot (2026-10-31) + +- PR #21 head `e70fd24`: fresh isolated `npm ci`, `npm run test:contracts` (175 passing), `npm run lint`, `npm run typecheck`, and `npm run build` exited 0. Build emitted optional Privy module and dynamic-dependency warnings. Fresh `npm run test:web` exited 0 with **zero tests** on the PR head. +- PR #20 head `5a10ca1`: fresh isolated `npm ci`, lint, typecheck and build exited 0. `npx hardhat test` exited 14 with 14 failing contract cases, including the absent `MockPriceOracle` artifact. Therefore #20 alone is not a releasable `master` candidate. +- PR #21 was updated from `e70fd24` to `6c570a4` with a dedicated `[self-hosted, linux, utp]` CI label, a disabled-by-default testnet workflow, deploy-key exposure limited to its deploy step, a nonempty Vitest suite and `ci:fast`. Fresh local `ci:fast` passed: 2 web utility tests and 175 contract tests. GitHub Dell run `41263088334` passed both jobs and Vercel preview was ready. PR #21 then merged into PR #20 as squash commit `0eb25eb`. +- A trailing-space fix on the combined PR #20 branch was `06116ee`. Its fresh local `ci:fast` passed (2 web utility tests, 175 contracts), `git diff --check` was clean, exact-head Dell run `41263936615` passed both jobs, and Vercel preview was ready. PR #20 merged to protected `master` as squash commit `777658f`. Post-merge Dell run `41264740648` completed successfully on exact master SHA `777658f`. A manual Vercel production deploy from a clean detached `777658f` worktree completed as `dpl_b2mSdYBpPc48gJGtTzxPjgeMryS9`. +- UTP runner `dell-utp-1` was registered on Dell under dedicated `utp-runner` OS identity, using GitHub runner v2.338.2 with the official release SHA-256 verified. GitHub reported it online and it completed the PR and post-merge checks. `master` branch protection now requires the named web and contract checks, strict up-to-date validation, PR merge, linear history, conversation resolution, and admin enforcement; force-push and deletion are disabled. UTP's GitHub environments are Preview and Production only; `rsk-testnet` does not exist and the testnet deployment switch remains off. +- The two added web tests exercise news deduplication, not the full voting or API user path. Those remain open coverage gates. + +## Current authority and release boundary + +- Canonical dirty checkout: `/Users/maysamtehranchi/Code/utp`, local `master` at `d81e60b`, two local commits ahead and one merge commit behind `origin/master` at `777658f`. Preserve its uncommitted OAuth/Supabase work and all other modifications. +- PR [#21](https://github.com/samtheloanman/utp/pull/21) merged into PR [#20](https://github.com/samtheloanman/utp/pull/20), and the combined PR #20 merged to `master` as `777658f`. This avoided releasing the 14-failure intermediate PR #20 baseline. +- Vercel Git integration built merge commit `777658f` as a ready **preview**. After post-merge Dell CI passed, the same clean source commit was manually deployed to production as `dpl_b2mSdYBpPc48gJGtTzxPjgeMryS9`. Vercel inspect reports READY and maps `utp-beta.vercel.app`, `unitingthepeople.org`, and `www.unitingthepeople.org` to that deployment. Public beta-alias smoke: `/`, `/how-it-works`, and `/governance` returned 200; `/launch` and `/vault` returned 307 to `/`. Direct HTTP verification of the custom domain from the Mac was blocked by Cloudflare 1010; alias mapping was verified through Vercel. Git integration production-target configuration still needs reconciliation. The prior production deployment `dpl_FMah8a3BGAkqLz4nKPLvDzqbRpQu` remains available for rollback. A source merge or web deployment is not a testnet contract deployment. +- `master` is now protected with required GitHub Actions checks on the UTP Dell runner. The PR #21 initial head had `ubuntu-latest` and no `ci:fast`; its merged finalization removed those defects. The manual testnet workflow is disabled by default; no `rsk-testnet` environment exists. + +## Branch finalization + +1. In isolated worktrees, audit PR #20 and PR #21 diffs for secrets, schema changes, unsafe claims, dead code, test coverage, and deployment effects. Reconcile local `master`'s two unpushed commits separately; never reset the dirty checkout. +2. Fix PR #21's CI and deployment workflows to use a **verified, authorized Dell runner**. Keep the deployment environment's secret isolated from PR jobs and dependency installation; require environment protection. If no eligible Dell runner exists, leave PR #21 unmerged and record the runner setup as a blocker. Do not switch to a GitHub-hosted runner. +3. Add real web/API tests and make `test:web` fail when zero tests are collected. Add `ci:fast` covering the meaningful local gates. Ensure contract tests include failed proposals, timelock-only execution, role revocation, PQ-key substitution, and rollback/negative paths. Do not turn on mock PQ/ZK modes outside local tests. +4. Run `npm ci`, `npm run ci:fast`, full contract tests, typecheck, lint, build, and a local governance lifecycle on the exact candidate. Record command exit codes and candidate SHA. Review PR #20 independently, then PR #21 against the post-#20 base. The old 175-test result is historical only. +5. Require exact-head Dell CI, test integrity, security review, Vercel preview smoke, no unresolved material finding, and a verified production-deployment/rollback boundary. Merge #21 into its existing #20 base first. Re-read the resulting #20 head, run Dell CI on that **combined** candidate, then merge #20 into `master` and verify the production deployment. Never release #20 alone, auto-merge a moving head, or rely on `MERGEABLE` alone. + +## Orchestrated implementation loop + +Each iteration selects the smallest unmet acceptance item in [SPEC.md](SPEC.md). Before edit: state owner, exact branch/base SHA, files, expected behavior, threat or failure mode, falsifiable test, affected data and services, and rollback. During edit: preserve other work, implement one vertical slice, and add a negative test where security or authorization changes. After edit: targeted tests, `ci:fast`, exact-head CI, runtime read-back, evidence ledger update, and a decision to continue, rework, or block. Stop a failing branch after three attempts without new evidence; do not weaken the gate. An orchestrator coordinates work but cannot grant deployment, provider, or credential authority. + +### Workstream A — release foundation + +- Resolve branch stack and Dell CI above; inventory the uncommitted Supabase OAuth work, choose one canonical civic identity, and link Privy wallets by server challenge and verified signature. +- Remove static/placeholder content labeled live, fabricated AI citations, silent sample-data fallback, and local Hardhat address from deployable UI paths. Require zero-test failure and source-aware API tests. +- Exit: exact-head CI and a production-safe release of the existing civic shell, with no claim of functional national voting. + +### Workstream B — civic participation + +- Build a source registry, immutable source/version identifiers, jurisdiction hierarchy, freshness and correction pipeline. Implement one local, one state, and federal vertical slice before scaling to nationwide coverage. Record coverage by jurisdiction/source in a ledger. +- Implement legislative request drafting, review, citation and jurisdiction checks, abuse controls, consent, publication states, and an explicit boundary before any message to an official. Keep civic sentiment and legally binding DAO votes distinct. +- Exit: users can authenticate, find a sourced bill, make a test suggestion/request, and cast a verified civic test vote; authorized read-back shows the right tally, correction and duplicate-vote behavior. Nationwide coverage remains open until its ledger is complete. + +### Workstream C — Rootstock DAO testnet + +- Harden Governor/Timelock and deployment manifest; distribute/delegate test governance tokens across independent accounts. Bind app flows to verified chain ID, contract addresses and bytecode. Add proposal/vote/queue/execute UI and event indexing. +- Deploy only with testnet-only identity/funds and recorded authorization. Verify actual wall-clock vote and timelock progression, at least two independently controlled participants, failure and cancellation cases, second-RPC read-back, and recovery. Local time travel and a read-only remote smoke do not satisfy this exit. +- Exit: `ROOTSTOCK_DAO_TESTNET_ACCEPTED` with transaction receipts and role/control inventory. This is application governance on Rootstock, not proof of a custom Bitcoin L2. + +### Workstream D — custom Bitcoin L2 and privacy + +- Specify trust, bridge, data availability, fraud/validity proof, finality, upgrade and reorg assumptions before selecting protocol. Benchmark candidate designs and require an independent cryptography/security review. +- Separately prove private ballots, eligible uniqueness, tally correctness, verifiable execution, Bitcoin anchoring/inclusion and reorg handling, and the actual scope of post-quantum protection. An on-chain Dilithium interface, mock verifier, wallet signature or Rootstock deployment proves none of these alone. +- Exit: `FULL_CUSTOM_L2_TESTNET_ACCEPTED` only after all [SPEC.md](SPEC.md) gates have independent evidence. Do not make real-BTC custody or quantum-safety claims from a prototype. + +## Evidence packet per batch + +Record base/head SHAs, dirty state, changed paths, relevant threat findings, test names and exit codes, CI run and exact head, deployment/chain IDs, transaction hashes, independent read-back, rollback result, unresolved failures, and next authorized action. Keep secrets and private ballots out of logs and Brain. + +## Ordered implementation backlog + +| ID | Dependency | Deliverable | Falsifiable exit test | +|---|---|---|---| +| R1 | None | Consolidate #21 into #20 and fix release checks | Combined head passes nonempty web tests, 175+ contracts, build and exact-head Dell CI; preview matches head. | +| R2 | R1 | Protect `master`, verify Vercel production target and rollback | Required checks reject an untested head; deploy the exact clean master SHA with production environment after post-merge CI, verify public alias and route smoke, and retain prior deployment for rollback. Reconcile why Git integration built `master` as Preview. | +| I1 | R1 | Canonical Supabase civic account and linked wallet | Wrong account, replayed challenge and mismatched signature cannot link a wallet; session refresh persists. | +| I2 | I1 | Tiered credential proofing and local encrypted wallet | Phone/TOTP/camera signals remain separate; lost-device and recovery tests do not silently lower assurance. Synthetic credentials only. | +| C1 | R1 | Source registry and one federal/state/local ingestion slice | Each record has source URL, jurisdiction, version and fetch time; bad key/provider failure never yields a fixture marked live. | +| C2 | I1,C1 | Proposal/request draft and civic vote | Unauthorized write fails; duplicate/concurrent votes have one authoritative tally; no message to officials occurs in tests. | +| C3 | C1,C2 | Nationwide coverage expansion | Per-jurisdiction ledger identifies provider, recency, gaps and correction path; no unverified locale is marked covered. | +| G1 | R1 | Governance manifest, roles and app UI | Contract bytecode/chain mismatch blocks actions; token allocation and all role holders are disclosed. | +| G2 | G1 | Rootstock testnet lifecycle | Independently controlled actors complete proposal, vote, queue, wait and execute; failed/cancelled cases and second-RPC read-back pass. | +| L1 | G2 | Bitcoin L2 threat model and benchmark | Architecture review specifies sequencer, DA, proof, bridge, reorg, exit and custody assumptions with measured alternatives. | +| L2 | L1,I2 | Private-vote and verifiable-execution prototype | Ballot/eligibility privacy, uniqueness and invalid proof rejection are independently tested; no mock verifier qualifies. | +| L3 | L1,L2 | Full custom L2 testnet | Independent review and testnet receipts pass all full-scope [SPEC.md](SPEC.md) gates, including Bitcoin anchoring and reorg handling. | + +Parallelism: I1 and C1 may proceed after R1; C3 and G1 may proceed after their own prerequisites. One integration owner maintains the acceptance ledger and refuses cross-workstream claims of completion until a vertical user path is tested. Every external mutation remains separately gated by the current fleet rules. diff --git a/docs/testnet-readiness-2026-10-31/SPEC.md b/docs/testnet-readiness-2026-10-31/SPEC.md new file mode 100644 index 0000000..0b68323 --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/SPEC.md @@ -0,0 +1,93 @@ +# UTP testnet acceptance specification + +Status: proposed acceptance contract, for Sam's validation. Date: 2026-10-31. +Evidence and findings: CLAIM-AUDIT.md. Execution handoff: IMPLEMENTATION-PROMPT.md. + +User-directed identity expansion (2026-10-31): phone, authenticator and camera validation in tiers, local wallet-held data, and an AI-first everything-app direction. `IDENTITY-AND-APP-RESEARCH.md` defines the proposed tier policy, component research, local-data boundary and additional identity acceptance gates. Those gates are prerequisites for identity/eligibility claims under B and D; the tiers themselves remain a proposed mapping, not implemented assurance. + +Preferred implementation path (2026-10-31): `AUTH-CUSTODY-BITCOIN-STACK.md` maps existing Supabase Auth, Privy/Web3, third-party custodian APIs and narrowly scoped Bitcoin commitments into five testable slices. It does not assert that a custodian API or Bitcoin anchoring completes the custom L2. + +## Objective + +Deliver nationwide civic participation and citizen legislative requests, conventional DAO governance on Rootstock testnet as an intermediate milestone, and eventually a custom Bitcoin L2 with private voting, verifiable execution, and explicitly scoped post-quantum protection. Do not substitute the intermediate milestone for the full goal. + +## Baseline and constraints + +Local master is dirty; PR21 is stacked on PR20. Historical CI proves 175 contracts passed, but zero frontend tests ran. Remote smoke is read-only. Quantum integration, ZK ballots, custom L2 and live testnet lifecycle remain unverified/unimplemented. See the claim audit for exact evidence. + +Preserve unrelated work. No production money or real BTC deposits. Mainnet, provider/environment changes, migrations, deployment, external messages and pushes/merges require their applicable authorization. Secrets remain in approved stores. No new skill/plugin/MCP without required scanning. Dell-only CI. No autonomous self-approval of gates. + +## Success levels + +All criteria below are proposed tests, not statements that they have passed. Missing evidence is NOT_RUN or BLOCKED, never PASS. Acceptance attaches to an exact commit, config and deployed address set. + +### A — Local integrated baseline + +- Reconcile the stacked PR candidate and local auth work in an isolated checkout; record provenance of every imported change. +- Run compile, lint, typecheck, build, contract tests and nonempty frontend/API suites. Define `ci:fast` if required by fleet rules; do not assume it exists. +- Test DB migrations on disposable local data: empty database and upgrade path, RLS ownership, duplicate IDs, concurrent vote updates and aggregate consistency. +- Reproduce the historical suite before interpreting new failures; retain all meaningful prior assertions, not merely a passing-test count. +- Cover wrong network, missing configuration, rejected signatures, session expiry and wallet disconnect in browser tests. + +### B — Civic workflow acceptance + +- Federal and all 50 states have explicit source-adapter status, freshness, failed-record queue and documented gaps. For testnet acceptance, validate representative records per enabled jurisdiction against official source text; label any incomplete jurisdiction and do not call national ingestion complete until each passes. +- Local scope is a versioned registry defining included government-body types and geographic denominator. Each locality is verified, stale, blocked or not integrated. A pilot subset may pass a scoped pilot only; full nationwide local success requires the agreed registry's entire denominator to pass. Inventory must precede a completeness/date promise. +- Ingestion is idempotent; versions/amendments are preserved; source outages show stale/unavailable, not sample legislation. Topic and ballot bind a specific version hash and voting window. +- Citizen flow: authenticate -> draft -> edit -> moderate -> publish -> endorse -> export. Unauthorized edits fail; official/citizen provenance stays distinct. Sending to officials is a separate authorized action. +- Vote tests: replay, concurrent requests, double voting, withdrawal/change policy, invalid choices, cross-jurisdiction collisions, missing/forged auth, RLS bypass attempts and independent aggregate recomputation. +- AI summaries carry citations that support their claims; unverifiable claims fail closed or display unverified. Test citation mismatches and injected instructions in source documents. +- Public sentiment, eligible-person polling and binding token governance remain separate. No verified-residency/one-person claim without a selected and tested eligibility system. + +### C — Live Rootstock DAO testnet acceptance (intermediate success) + +1. CI evidence from the exact candidate on an authorized Dell runner, including genuine frontend tests. Resolve current zero-runner finding before scheduling jobs. +2. Approved release manifest: git SHA; compiler/settings/lockfile hashes; chain ID 31; ABI/artifact hashes; expected immutable constructor values; contract addresses; deployment and wiring transaction hashes; block hashes; intended roles; allocation; verification depth policy. +3. Read back chain ID, bytecode and wiring through independent RPC sources. Check token, Governor, timelock, registry, DAO and treasury against the manifest. Reconstruct all relevant roles/permissions from deployment events and query effective state. Compare at a recorded block; detect reorg/replacement. +4. Distribute/delegate test tokens to independently controlled participants under a documented allocation. Three participants is a proposed minimum functional scenario, not a decentralization guarantee. +5. Through the actual UI on chain31: create a cited proposal, exercise For/Against/Abstain across scenarios, wait actual voting deadline, queue, reject execution before timelock, execute after delay, and verify recipient balance/state change and events from a second RPC. No `evm_mine`/time travel on public testnet. +6. Negative scenarios: insufficient threshold, insufficient quorum, defeated/cancelled proposal, repeat vote, repeat execution, replay/wrong-chain signature, unauthorized DAO call, stale frontend/indexer state. Verify optional guardian cancellation and lack of escalation if a guardian is configured. +7. Assert intended post-operation permissions and token distribution. Record that ROOT governance can change permissions; review grants rather than declaring permanent exclusivity. +8. Test RPC outage, browser refresh/reconnect, indexer restart/backfill, duplicate events and simulated reorg reconciliation locally; conduct non-disruptive public-testnet outage tests through isolated clients. +9. Proposed seven-day observation window after last material fix; daily and event-driven health evidence, no unresolved critical/high findings. This is an acceptance policy proposal, not proof that seven days establishes security. +10. Publish a reproducible acceptance report containing receipts and failures. Conventional DAO testnet completion must say PQ/ZK/custom-L2 completion remains outstanding. + +Configured public-testnet window is 2880 blocks plus 3600 seconds timelock and voting delay. Actual chain progress determines duration. Do not compress it silently to claim a quick end-to-end pass. Short devnet tests are a separate evidence class. + +### D — Full requested custom-L2 testnet success + +Requires B's agreed coverage, C's governance behavior adapted to the custom chain, and all of: + +- Approved architecture identifying execution, sequencing/consensus, DA, Bitcoin anchoring vs settlement, verifier/dispute mechanism, bridge, withdrawal rights, fees and emergency/upgrade authority. An anchor alone is not Bitcoin validation of execution. +- A functioning custom network with independently operated validating components; reproducible genesis and configuration; state recovery from public availability data; no hidden central administrator described as decentralized. +- Execution proof verifies valid transitions and rejects modified transactions, roots, witness/proof, wrong chain/domain and replay. Document all cryptographic wrappers and trust/setup assumptions. +- Private voting proves eligibility and one vote per election with unlinkable ballot data and a publicly verifiable tally. Test duplicate nullifiers, stale membership, key compromise/recovery, issuer abuse, small anonymity sets and metadata leakage. State explicitly whether receipt-freeness/coercion resistance is provided; do not imply it from ZK alone. +- Real standardized PQ implementation with known-answer and malformed-input tests; voter/key binding, revocation and recovery; no classical-only downgrade across accounts, token movement, operator/admin keys, custody or bridges within the claimed boundary. Any classical Bitcoin dependency remains disclosed; do not claim full end-to-end PQ security while it remains. +- Bitcoin test-network integration: deposits, valid exits, invalid exits, double withdrawal, operator/challenger outage, censorship, reorg and DA withholding exercises, with bounded behavior and actual receipts. No mainnet funds. +- DAO demonstrably changes an allowed protocol parameter or upgrade through a timelock; rejected/unauthorized proposals cannot alter protocol state. Treasury governance alone does not prove network governance. +- Workload-specific benchmark report: hardware, batch size, proof-generation time, verification time, cost, throughput, p50/p95/p99 acknowledgement and settlement delays, failure rate, and recovery. Pick SLOs after measurement and user validation. Earlier 100 TPS/2-second figures are not committed targets. +- Independent specialist cryptographic/bridge review plus resolved findings. Automated self-review cannot substitute for this. If the selected architecture cannot meet the required threat model, report infeasibility and propose options; never weaken the claim silently. + +Full goal status is the conjunction of required gates. Rootstock C passing does not satisfy D. + +## Dependency-driven schedule + +First cycle: baseline reconciliation -> regression tests for R1–R6 -> Dell CI eligibility -> local civic/DAO vertical slices -> deploy-ready packet. Remote action waits for its authorization while unrelated local work continues. + +Next: approved public testnet deployment -> actual voting/queue/execution window -> observation -> acceptance report. In parallel, read-only L2 research and credential-free benchmarks may proceed within authorization. + +Only after measured baseline effort, jurisdiction inventory, selected L2 architecture, staffing and audit availability should dates be estimated. No fixed 14-week or 9–15-month promise remains. + +## Evidence record + +For every acceptance ID record: status PASS/FAIL/NOT_RUN/BLOCKED, SHA/config hash, timestamp, command/tool, environment, expected/actual behavior, exit status, sanitized log/artifact path, transaction/block IDs where applicable, limitations and reviewer. Never store secrets or private ballots in evidence. + +## Unresolved decisions + +- Definition/denominator of nationwide local coverage (municipalities, counties, special districts and other bodies). +- Civic identity/eligibility and privacy requirements; token voting is a separate DAO mechanism. +- L2 architecture and achievable quantum protection boundary, including Bitcoin custody. +- Tester allocation, independent participants/operators/reviewer, budget and hardware. +- Deployment/environment/runner authorizations and observation/SLO policy. + +These block their dependent steps only; baseline testing and safe preparation can continue after the caller validates this specification. From 54aadea6e0d979e2bad09bec790f30e352e0e98c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:10:15 -0800 Subject: [PATCH 02/16] feat(auth): import Supabase session/OAuth work with same-origin redirect guard Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- .env.example | 18 ++++ .../EVIDENCE-LEDGER.md | 16 ++++ package-lock.json | 26 ++++++ package.json | 1 + src/app/api/oauth/decision/route.ts | 41 +++++++++ src/app/auth/callback/route.test.ts | 34 +++++++ src/app/auth/callback/route.ts | 19 ++++ src/app/login/page.tsx | 92 +++++++++++++++++++ src/app/oauth/consent/page.tsx | 86 +++++++++++++++++ src/lib/supabaseAuth/client.ts | 16 ++++ src/lib/supabaseAuth/redirect.test.ts | 12 +++ src/lib/supabaseAuth/redirect.ts | 33 +++++++ src/lib/supabaseAuth/server.ts | 34 +++++++ src/middleware.ts | 52 +++++++++++ 14 files changed, 480 insertions(+) create mode 100644 src/app/api/oauth/decision/route.ts create mode 100644 src/app/auth/callback/route.test.ts create mode 100644 src/app/auth/callback/route.ts create mode 100644 src/app/login/page.tsx create mode 100644 src/app/oauth/consent/page.tsx create mode 100644 src/lib/supabaseAuth/client.ts create mode 100644 src/lib/supabaseAuth/redirect.test.ts create mode 100644 src/lib/supabaseAuth/redirect.ts create mode 100644 src/lib/supabaseAuth/server.ts create mode 100644 src/middleware.ts diff --git a/.env.example b/.env.example index dd7a6a4..56a7412 100644 --- a/.env.example +++ b/.env.example @@ -10,8 +10,26 @@ REPORT_GAS=false # ---- Frontend (Next.js) ---- NEXT_PUBLIC_WC_PROJECT_ID= NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co +# Was missing from this file even though src/lib/supabase.ts and the new +# session-aware clients (src/lib/supabaseAuth/*, src/middleware.ts) both +# read it. Anon key is safe to expose to the browser. +NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key SUPABASE_SERVICE_ROLE_KEY= +# Google/GitHub sign-in (/login, src/app/auth/callback): provider client +# id/secret are configured in Supabase Dashboard > Authentication > +# Providers, not here. This is a Supabase-Auth-backed login path that runs +# alongside the existing Privy wallet "Sign In" button in TopBar -- the two +# are independent for now. It exists to activate the auth.users-keyed RLS +# already defined in supabase/migrations/20260711000000_add_bill_actions_ +# profiles_tracked.sql (user_profiles, tracked_bills), which nothing wired +# up until now. + +# Supabase OAuth 2.1 Server (this project as an OIDC identity provider for +# third-party apps -- see src/app/oauth/consent/page.tsx). Enabled +# per-project in Supabase Dashboard > Authentication > OAuth Server, with +# Authorization Path set to /oauth/consent. Nothing to set here today. + # ---- News API ---- NEWSDATA_API_KEY= # Get free key: https://newsdata.io/register diff --git a/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md b/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md index 6a36593..69fcb8f 100644 --- a/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md +++ b/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md @@ -9,7 +9,23 @@ Environment: Claude cloud container (Linux, Node 22.23.4, npm 10.9.9, PostgreSQL | A-baseline-web | PASS | 777658f | `npm run lint && npm run typecheck && npm run test:web` (inside `npm run ci:fast`) | 0 | lint clean, typecheck clean, 2/2 web tests | | A-baseline-contracts | PASS | 777658f | `SOLC_JS_PATH= npm run test:contracts` | 0 | 175 passing. Plain `ci:fast` fails here only because the sandbox cannot download solc (HH502 ENOTFOUND binaries.soliditylang.org) | | A-zero-test-guard | PASS | 777658f | `npx vitest run src/__no_such_dir__` | 1 | Vitest exits non-zero when no test files are found | +| R2-vercel-production-target | BLOCKED | 777658f | Vercel MCP read tools (`get_project`, `list_deployments`, `list_project_domains`, `get_deployment`) | 403 | `forbidden` for scope `samtheloanmans-projects` (`team_D2rNpq3e1jGfA7HanfuwMX3O`); productionBranch and deployment targets NOT_DETERMINED. Needs Sam to authorize team access | ## Provenance of imported files (from the Mac checkout, 2026-11-03) Readiness docs `docs/testnet-readiness-2026-10-31/*.md` and `EVIDENCE.json`, and the plan file, were untracked on the Mac and are committed here unchanged. Auth files are imported in Task 2 and listed there with `git hash-object` values. + +Task 2 imports (`git hash-object` of each SOURCE copy from the Mac checkout, before the redirect-guard edits; `.env.example` is copied verbatim): + +| path | git hash-object (source) | +|---|---| +| `src/middleware.ts` | `bd4218fcdba534e2c06c8b0313df83142d5987a7` | +| `src/lib/supabaseAuth/client.ts` | `e02448c6bf797f8e63b60458bfae6db157307713` | +| `src/lib/supabaseAuth/server.ts` | `552ad0313a65ad8d3229e07eb54bb6764c1f8c85` | +| `src/app/auth/callback/route.ts` | `152b07bf7f094b052bed33d55e60b1dc3668af4c` | +| `src/app/login/page.tsx` | `e23ea872d40013f90a34678f77ea9b45398b81bd` | +| `src/app/oauth/consent/page.tsx` | `4f65b60056a772890486321a031f1679f9d85c50` | +| `src/app/api/oauth/decision/route.ts` | `4d3daaedf017741e36f14af1a8714b9646e756e5` | +| `.env.example` | `56a74122b110429f8934f8228af44bd12cdd9e11` | + +Task 2 then modified `auth/callback/route.ts`, `login/page.tsx`, `oauth/consent/page.tsx` and `api/oauth/decision/route.ts` to use `safeRedirectPath` / `encodeURIComponent`, so committed blobs differ from these hashes for those four files. diff --git a/package-lock.json b/package-lock.json index b7e60b6..91d6950 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "@react-three/fiber": "^9.6.1", "@safe-global/safe-apps-provider": "^0.18.6", "@safe-global/safe-apps-sdk": "^9.1.0", + "@supabase/ssr": "^0.8.0", "@supabase/supabase-js": "^2.91.0", "@tailwindcss/postcss": "^4.2.1", "@tanstack/react-query": "^5.90.21", @@ -8460,6 +8461,31 @@ "node": ">=20.0.0" } }, + "node_modules/@supabase/ssr": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/@supabase/ssr/-/ssr-0.8.0.tgz", + "integrity": "sha512-/PKk8kNFSs8QvvJ2vOww1mF5/c5W8y42duYtXvkOSe+yZKRgTTZywYG2l41pjhNomqESZCpZtXuWmYjFRMV+dw==", + "license": "MIT", + "dependencies": { + "cookie": "^1.0.2" + }, + "peerDependencies": { + "@supabase/supabase-js": "^2.76.1" + } + }, + "node_modules/@supabase/ssr/node_modules/cookie": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-1.1.1.tgz", + "integrity": "sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, "node_modules/@supabase/storage-js": { "version": "2.103.3", "resolved": "https://registry.npmjs.org/@supabase/storage-js/-/storage-js-2.103.3.tgz", diff --git a/package.json b/package.json index 744d452..05ee81c 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "@react-three/fiber": "^9.6.1", "@safe-global/safe-apps-provider": "^0.18.6", "@safe-global/safe-apps-sdk": "^9.1.0", + "@supabase/ssr": "^0.8.0", "@supabase/supabase-js": "^2.91.0", "@tailwindcss/postcss": "^4.2.1", "@tanstack/react-query": "^5.90.21", diff --git a/src/app/api/oauth/decision/route.ts b/src/app/api/oauth/decision/route.ts new file mode 100644 index 0000000..d003535 --- /dev/null +++ b/src/app/api/oauth/decision/route.ts @@ -0,0 +1,41 @@ +import { NextResponse } from 'next/server'; +import { createClient } from '@/lib/supabaseAuth/server'; +import { safeRedirectPath } from '@/lib/supabaseAuth/redirect'; + +/** Handles Allow/Deny submissions from src/app/oauth/consent/page.tsx. */ +export async function POST(request: Request) { + const { origin } = new URL(request.url); + const formData = await request.formData(); + const authorizationId = formData.get('authorization_id') as string | null; + const decision = formData.get('decision') as string | null; + + if (!authorizationId || (decision !== 'approve' && decision !== 'deny')) { + return NextResponse.redirect(new URL(`/login?error=${encodeURIComponent('Malformed OAuth consent submission')}`, origin)); + } + + const supabase = await createClient(); + const { + data: { user }, + } = await supabase.auth.getUser(); + + if (!user) { + const consentPath = safeRedirectPath(`/oauth/consent?authorization_id=${encodeURIComponent(authorizationId)}`); + return NextResponse.redirect(new URL(`/login?redirect=${encodeURIComponent(consentPath)}`, origin)); + } + + const { data, error } = + decision === 'approve' + ? await supabase.auth.oauth.approveAuthorization(authorizationId) + : await supabase.auth.oauth.denyAuthorization(authorizationId); + + if (error || !data?.redirect_url) { + return NextResponse.redirect( + new URL( + `/oauth/consent?authorization_id=${encodeURIComponent(authorizationId)}&error=${encodeURIComponent(error?.message ?? 'Could not process decision')}`, + origin, + ), + ); + } + + return NextResponse.redirect(data.redirect_url); +} diff --git a/src/app/auth/callback/route.test.ts b/src/app/auth/callback/route.test.ts new file mode 100644 index 0000000..5c79485 --- /dev/null +++ b/src/app/auth/callback/route.test.ts @@ -0,0 +1,34 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +const exchangeCodeForSession = vi.fn(); + +vi.mock('@/lib/supabaseAuth/server', () => ({ + createClient: async () => ({ auth: { exchangeCodeForSession } }), +})); + +import { GET } from './route'; + +describe('GET /auth/callback', () => { + beforeEach(() => exchangeCodeForSession.mockReset()); + + it('never redirects off-site, even with a hostile redirect param', async () => { + exchangeCodeForSession.mockResolvedValue({ error: null }); + const res = await GET(new Request('https://utp.test/auth/callback?code=c&redirect=@evil.com')); + expect(new URL(res.headers.get('location')!).origin).toBe('https://utp.test'); + }); + + it('redirects to a safe same-origin redirect param', async () => { + exchangeCodeForSession.mockResolvedValue({ error: null }); + const res = await GET(new Request('https://utp.test/auth/callback?code=c&redirect=%2Foauth%2Fconsent%3Fauthorization_id%3Da')); + expect(res.headers.get('location')).toBe('https://utp.test/oauth/consent?authorization_id=a'); + }); + + it('sends exchange errors to same-origin /login?error=', async () => { + exchangeCodeForSession.mockResolvedValue({ error: { message: 'bad' } }); + const res = await GET(new Request('https://utp.test/auth/callback?code=c&redirect=@evil.com')); + const loc = new URL(res.headers.get('location')!); + expect(loc.origin).toBe('https://utp.test'); + expect(loc.pathname).toBe('/login'); + expect(loc.searchParams.get('error')).toBeTruthy(); + }); +}); diff --git a/src/app/auth/callback/route.ts b/src/app/auth/callback/route.ts new file mode 100644 index 0000000..6f1ef26 --- /dev/null +++ b/src/app/auth/callback/route.ts @@ -0,0 +1,19 @@ +import { NextResponse } from 'next/server'; +import { createClient } from '@/lib/supabaseAuth/server'; +import { safeRedirectPath } from '@/lib/supabaseAuth/redirect'; + +export async function GET(request: Request) { + const { searchParams, origin } = new URL(request.url); + const code = searchParams.get('code'); + const redirectTo = safeRedirectPath(searchParams.get('redirect')); + + if (code) { + const supabase = await createClient(); + const { error } = await supabase.auth.exchangeCodeForSession(code); + if (!error) { + return NextResponse.redirect(new URL(redirectTo, origin)); + } + } + + return NextResponse.redirect(new URL(`/login?error=${encodeURIComponent('Could not authenticate user')}`, origin)); +} diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx new file mode 100644 index 0000000..b127e93 --- /dev/null +++ b/src/app/login/page.tsx @@ -0,0 +1,92 @@ +'use client'; + +import { useState, Suspense } from 'react'; +import { useSearchParams } from 'next/navigation'; +import { createClient } from '@/lib/supabaseAuth/client'; +import { safeRedirectPath } from '@/lib/supabaseAuth/redirect'; + +/** + * Supabase-Auth-backed social login. Separate from the Privy "Sign In" + * button in TopBar (src/components/ui/Shared.tsx), which drives wallet / + * embedded-wallet auth and is unaffected by this page. This path exists + * because user_profiles and tracked_bills (supabase/migrations/ + * 20260711000000_add_bill_actions_profiles_tracked.sql) already key off + * auth.users + auth.uid() via RLS -- that only works for users who signed + * in through Supabase Auth, which nothing in the app did until now. + */ +export default function LoginPage() { + return ( + + + + ); +} + +function LoginForm() { + const searchParams = useSearchParams(); + const redirectTo = safeRedirectPath(searchParams.get('redirect')); + const callbackError = searchParams.get('error'); + const [error, setError] = useState(callbackError); + const [loading, setLoading] = useState<'google' | 'github' | null>(null); + + const handleLogin = async (provider: 'google' | 'github') => { + setError(null); + setLoading(provider); + try { + const supabase = createClient(); + const callbackUrl = new URL('/auth/callback', window.location.origin); + callbackUrl.searchParams.set('redirect', redirectTo); + + const { error } = await supabase.auth.signInWithOAuth({ + provider, + options: { redirectTo: callbackUrl.toString() }, + }); + if (error) { + setError(error.message); + setLoading(null); + } + // On success the browser navigates to the provider; nothing else to do. + } catch (err) { + setError(err instanceof Error ? err.message : 'Could not start sign-in'); + setLoading(null); + } + }; + + return ( +
+
+

Sign in

+

+ Sign in to track bills, vote, and manage your profile. +

+ + {error && ( +
+ {error} +
+ )} + +
+ + +
+
+
+ ); +} diff --git a/src/app/oauth/consent/page.tsx b/src/app/oauth/consent/page.tsx new file mode 100644 index 0000000..44c6079 --- /dev/null +++ b/src/app/oauth/consent/page.tsx @@ -0,0 +1,86 @@ +import { redirect } from 'next/navigation'; +import { createClient } from '@/lib/supabaseAuth/server'; + +/** + * Consent screen for Supabase's OAuth 2.1 Server feature, which lets this + * project act as an OAuth/OIDC identity provider for third-party apps + * ("Sign in with UTP"). Supabase Auth redirects the user's browser here + * with `?authorization_id=...`. Path must match the "Authorization Path" + * configured in Supabase Dashboard > Authentication > OAuth Server. + * Docs: https://supabase.com/docs/guides/auth/oauth-server + */ +export default async function OAuthConsentPage({ + searchParams, +}: { + searchParams: Promise<{ authorization_id?: string }>; +}) { + const { authorization_id: authorizationId } = await searchParams; + + if (!authorizationId) { + return

Missing authorization request. Close this tab and try again from the requesting app.

; + } + + const supabase = await createClient(); + const { + data: { user }, + } = await supabase.auth.getUser(); + + if (!user) { + redirect(`/login?redirect=${encodeURIComponent(`/oauth/consent?authorization_id=${encodeURIComponent(authorizationId)}`)}`); + } + + const { data: authorization, error } = await supabase.auth.oauth.getAuthorizationDetails(authorizationId); + + if (error || !authorization) { + return

{error?.message ?? 'This authorization request is invalid or has expired.'}

; + } + + // getAuthorizationDetails returns a union: full details when consent is + // still needed, or a bare redirect when this user already approved this + // client before. In the latter case skip the screen entirely. + if (!('authorization_id' in authorization)) { + redirect(authorization.redirect_url); + } + + const scopes: string[] = authorization.scope ? authorization.scope.split(' ').filter(Boolean) : []; + + return ( + +

+ {authorization.client.name || 'A third-party app'} wants to access your account +

+

Signed in as {user.email}

+ + {scopes.length > 0 && ( +
    + {scopes.map((scope) => ( +
  • • {scope}
  • + ))} +
+ )} + +
+
+ + + +
+
+ + + +
+
+
+ ); +} + +function Shell({ children }: { children: React.ReactNode }) { + return ( +
+
+ {children} +
+
+ ); +} diff --git a/src/lib/supabaseAuth/client.ts b/src/lib/supabaseAuth/client.ts new file mode 100644 index 0000000..e02448c --- /dev/null +++ b/src/lib/supabaseAuth/client.ts @@ -0,0 +1,16 @@ +'use client'; + +import { createBrowserClient } from '@supabase/ssr'; + +/** + * Session-aware (cookie-backed) Supabase client for browser components. + * Separate from src/lib/supabase.ts, which is a plain anon-key client used + * for public reads and predates any Supabase Auth session handling here. + * Use this one specifically for signInWithOAuth / getSession / onAuthStateChange. + */ +export function createClient() { + return createBrowserClient( + process.env.NEXT_PUBLIC_SUPABASE_URL!, + process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! + ); +} diff --git a/src/lib/supabaseAuth/redirect.test.ts b/src/lib/supabaseAuth/redirect.test.ts new file mode 100644 index 0000000..ee0f1f0 --- /dev/null +++ b/src/lib/supabaseAuth/redirect.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from 'vitest'; +import { safeRedirectPath } from './redirect'; + +describe('safeRedirectPath', () => { + it.each(['@evil.com', '.evil.com', '//evil.com', '/\\evil.com', 'https://evil.com', 'javascript:alert(1)', '/%2f%2fevil.com', '/ok\nSet-Cookie:x', '', null, undefined])( + 'rejects %s', + (v) => expect(safeRedirectPath(v as string)).toBe('/'), + ); + + it('keeps same-origin path, query and hash', () => + expect(safeRedirectPath('/oauth/consent?authorization_id=a%20b#x')).toBe('/oauth/consent?authorization_id=a%20b#x')); +}); diff --git a/src/lib/supabaseAuth/redirect.ts b/src/lib/supabaseAuth/redirect.ts new file mode 100644 index 0000000..eb867ae --- /dev/null +++ b/src/lib/supabaseAuth/redirect.ts @@ -0,0 +1,33 @@ +const PROBE_ORIGIN = 'http://x.invalid'; +const CONTROL_CHARS = /[\u0000-\u001f\u007f]/; + +function isSafe(value: string): boolean { + if (!value.startsWith('/') || value.startsWith('//')) return false; + if (value.includes('\\') || CONTROL_CHARS.test(value)) return false; + try { + return new URL(value, PROBE_ORIGIN).origin === PROBE_ORIGIN; + } catch { + return false; + } +} + +/** + * Returns a same-origin path (+search+hash) or '/'. Callers build URLs as + * `new URL(safe, origin)`. + */ +export function safeRedirectPath(raw: string | null | undefined): string { + if (typeof raw !== 'string' || raw === '') return '/'; + if (!isSafe(raw)) return '/'; + + // Decode once and re-check so encoded slashes/backslashes cannot smuggle + // a protocol-relative URL past the first check. + let decoded: string; + try { + decoded = decodeURIComponent(raw); + } catch { + return '/'; + } + if (!isSafe(decoded)) return '/'; + + return raw; +} diff --git a/src/lib/supabaseAuth/server.ts b/src/lib/supabaseAuth/server.ts new file mode 100644 index 0000000..552ad03 --- /dev/null +++ b/src/lib/supabaseAuth/server.ts @@ -0,0 +1,34 @@ +import { createServerClient } from '@supabase/ssr'; +import { cookies } from 'next/headers'; + +/** + * Session-aware (cookie-backed) Supabase client for Server Components, + * Server Actions, and Route Handlers. Pairs with client.ts's browser + * client and src/middleware.ts, which refreshes the session cookie on + * every request. + */ +export async function createClient() { + const cookieStore = await cookies(); + + return createServerClient( + process.env.NEXT_PUBLIC_SUPABASE_URL!, + process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, + { + cookies: { + getAll() { + return cookieStore.getAll(); + }, + setAll(cookiesToSet) { + try { + cookiesToSet.forEach(({ name, value, options }) => + cookieStore.set(name, value, options) + ); + } catch { + // Called from a Server Component -- safe to ignore since + // middleware.ts refreshes sessions on every matched request. + } + }, + }, + } + ); +} diff --git a/src/middleware.ts b/src/middleware.ts new file mode 100644 index 0000000..bd4218f --- /dev/null +++ b/src/middleware.ts @@ -0,0 +1,52 @@ +import { createServerClient } from '@supabase/ssr'; +import { NextResponse, type NextRequest } from 'next/server'; + +/** + * Refreshes the Supabase Auth session cookie on every request so + * getSession()/getUser() stay valid in Server Components without each one + * re-deriving it. Does not gate any routes -- UTP is a public-read app; + * the one new route that requires a session (/oauth/consent) checks + * getUser() itself and redirects to /login when absent. + */ +export async function middleware(request: NextRequest) { + const { pathname } = request.nextUrl; + + if ( + pathname.startsWith('/_next') || + pathname.startsWith('/favicon') || + pathname.match(/\.(ico|png|jpg|jpeg|svg|css|js|webmanifest)$/) + ) { + return NextResponse.next({ request }); + } + + const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; + const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; + if (!supabaseUrl || !supabaseAnonKey) { + return NextResponse.next({ request }); + } + + let response = NextResponse.next({ request }); + + const supabase = createServerClient(supabaseUrl, supabaseAnonKey, { + cookies: { + getAll() { + return request.cookies.getAll(); + }, + setAll(cookiesToSet) { + cookiesToSet.forEach(({ name, value }) => request.cookies.set(name, value)); + response = NextResponse.next({ request }); + cookiesToSet.forEach(({ name, value, options }) => + response.cookies.set(name, value, options) + ); + }, + }, + }); + + await supabase.auth.getUser(); + + return response; +} + +export const config = { + matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'], +}; From c8cb70048d2828064ae7ced0cfb4ed4ffa6baeb9 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:10:37 -0800 Subject: [PATCH 03/16] docs: record R2 Vercel production-target investigation (blocked on team access) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- .../MERGE-AND-IMPLEMENTATION-LOOP.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md b/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md index 036dd9d..ae4bf4f 100644 --- a/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md +++ b/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md @@ -63,7 +63,7 @@ Record base/head SHAs, dirty state, changed paths, relevant threat findings, tes | ID | Dependency | Deliverable | Falsifiable exit test | |---|---|---|---| | R1 | None | Consolidate #21 into #20 and fix release checks | Combined head passes nonempty web tests, 175+ contracts, build and exact-head Dell CI; preview matches head. | -| R2 | R1 | Protect `master`, verify Vercel production target and rollback | Required checks reject an untested head; deploy the exact clean master SHA with production environment after post-merge CI, verify public alias and route smoke, and retain prior deployment for rollback. Reconcile why Git integration built `master` as Preview. | +| R2 | R1 | Protect `master`, verify Vercel production target and rollback | Required checks reject an untested head; deploy the exact clean master SHA with production environment after post-merge CI, verify public alias and route smoke, and retain prior deployment for rollback. Reconcile why Git integration built `master` as Preview. **Status (2026-11-03): BLOCKED_ON_AUTH** (cause `NOT_DETERMINED`; see R2 finding below). | | I1 | R1 | Canonical Supabase civic account and linked wallet | Wrong account, replayed challenge and mismatched signature cannot link a wallet; session refresh persists. | | I2 | I1 | Tiered credential proofing and local encrypted wallet | Phone/TOTP/camera signals remain separate; lost-device and recovery tests do not silently lower assurance. Synthetic credentials only. | | C1 | R1 | Source registry and one federal/state/local ingestion slice | Each record has source URL, jurisdiction, version and fetch time; bad key/provider failure never yields a fixture marked live. | @@ -76,3 +76,16 @@ Record base/head SHAs, dirty state, changed paths, relevant threat findings, tes | L3 | L1,L2 | Full custom L2 testnet | Independent review and testnet receipts pass all full-scope [SPEC.md](SPEC.md) gates, including Bitcoin anchoring and reorg handling. | Parallelism: I1 and C1 may proceed after R1; C3 and G1 may proceed after their own prerequisites. One integration owner maintains the acceptance ledger and refuses cross-workstream claims of completion until a vertical user path is tested. Every external mutation remains separately gated by the current fleet rules. + +### R2 finding (2026-11-03) + +Status: `BLOCKED_ON_AUTH`. Cause of the Preview build of `master` 777658f: `NOT_DETERMINED`. + +Checked (read-only, Vercel MCP): +- `list_teams` and `get_git_deployment_context` returned no teams for the connected identity. +- `list_projects` (search `utp`) found project `utp` = `prj_PT3ORkBHyeUhSBU3z5s3qILiRCik`, account `team_D2rNpq3e1jGfA7HanfuwMX3O` (scope `samtheloanmans-projects`). +- `get_project`, `list_deployments`, `list_project_domains` and `get_deployment` (`dpl_b2mSdYBpPc48gJGtTzxPjgeMryS9`, `withGitRepoInfo`) all failed `403 forbidden`: the connection is not authorized for scope `samtheloanmans-projects`. + +Not read, so not known: `link.productionBranch`, the Git-connected repo, `target` for the 777658f deployment and for `dpl_b2mSdYBpPc48gJGtTzxPjgeMryS9` / `dpl_FMah8a3BGAkqLz4nKPLvDzqbRpQu`, and `meta.githubCommitRef`/`githubCommitSha`. Hypotheses to test once readable (unverified): production branch is not `master`; the Git link points at a different repo or branch; an ignored-build or custom-environment setting applies. + +Needed from Sam (authorization, not done here): approve Vercel team access for scope `samtheloanmans-projects` for this connection (`authorize_team_access`), or supply the project Settings > Git "Production Branch" value and the three deployments' target/branch/SHA. No Vercel setting was changed. Any change to the production branch setting stays gated on Sam's explicit approval. From 4e6eee26a49471c19116d603713126c5b9f8a75e Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:11:36 -0800 Subject: [PATCH 04/16] feat(auth): add requireUser session resolver Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- src/lib/supabaseAuth/requireUser.test.ts | 81 ++++++++++++++++++++++++ src/lib/supabaseAuth/requireUser.ts | 43 +++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/lib/supabaseAuth/requireUser.test.ts create mode 100644 src/lib/supabaseAuth/requireUser.ts diff --git a/src/lib/supabaseAuth/requireUser.test.ts b/src/lib/supabaseAuth/requireUser.test.ts new file mode 100644 index 0000000..017766b --- /dev/null +++ b/src/lib/supabaseAuth/requireUser.test.ts @@ -0,0 +1,81 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +const bearerGetUser = vi.fn(); +const cookieGetUser = vi.fn(); +const cookieGetSession = vi.fn(); + +vi.mock('@supabase/supabase-js', () => ({ + createClient: vi.fn(() => ({ auth: { getUser: bearerGetUser } })), +})); +vi.mock('./server', () => ({ + createClient: vi.fn(async () => ({ + auth: { getUser: cookieGetUser, getSession: cookieGetSession }, + })), +})); + +import { requireUser } from './requireUser'; + +const ENV_KEYS = ['NEXT_PUBLIC_SUPABASE_URL', 'NEXT_PUBLIC_SUPABASE_ANON_KEY'] as const; +const saved: Record = {}; + +function req(headers: Record = {}) { + return new Request('http://localhost/api/x', { headers }); +} + +describe('requireUser', () => { + beforeEach(() => { + for (const k of ENV_KEYS) saved[k] = process.env[k]; + process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://example.supabase.co'; + process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = 'anon-test-key'; + bearerGetUser.mockReset(); + cookieGetUser.mockReset(); + cookieGetSession.mockReset(); + }); + + afterEach(() => { + for (const k of ENV_KEYS) { + if (saved[k] === undefined) delete process.env[k]; + else process.env[k] = saved[k]; + } + }); + + it('returns 401 with no header and no session', async () => { + cookieGetUser.mockResolvedValue({ data: { user: null }, error: { message: 'no session' } }); + const res = await requireUser(req()); + expect('error' in res).toBe(true); + if ('error' in res) { + expect(res.error.status).toBe(401); + expect(await res.error.json()).toEqual({ message: 'Unauthorized' }); + } + }); + + it('returns 401 for an invalid bearer token', async () => { + bearerGetUser.mockResolvedValue({ data: { user: null }, error: { message: 'bad jwt' } }); + const res = await requireUser(req({ authorization: 'Bearer bad' })); + expect('error' in res && res.error.status).toBe(401); + expect(cookieGetUser).not.toHaveBeenCalled(); + }); + + it('returns user id and echoes token for a valid bearer', async () => { + bearerGetUser.mockResolvedValue({ data: { user: { id: 'user-1' } }, error: null }); + const res = await requireUser(req({ authorization: 'Bearer tok-123' })); + expect(res).toEqual({ user: { id: 'user-1' }, accessToken: 'tok-123' }); + expect(bearerGetUser).toHaveBeenCalledWith('tok-123'); + }); + + it('resolves the cookie session when no bearer is sent', async () => { + cookieGetUser.mockResolvedValue({ data: { user: { id: 'user-2' } }, error: null }); + cookieGetSession.mockResolvedValue({ data: { session: { access_token: 'cookie-tok' } } }); + const res = await requireUser(req()); + expect(res).toEqual({ user: { id: 'user-2' }, accessToken: 'cookie-tok' }); + }); + + it('returns 503 and never calls getUser when env is unset', async () => { + delete process.env.NEXT_PUBLIC_SUPABASE_URL; + delete process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; + const res = await requireUser(req({ authorization: 'Bearer tok' })); + expect('error' in res && res.error.status).toBe(503); + expect(bearerGetUser).not.toHaveBeenCalled(); + expect(cookieGetUser).not.toHaveBeenCalled(); + }); +}); diff --git a/src/lib/supabaseAuth/requireUser.ts b/src/lib/supabaseAuth/requireUser.ts new file mode 100644 index 0000000..42a90e7 --- /dev/null +++ b/src/lib/supabaseAuth/requireUser.ts @@ -0,0 +1,43 @@ +import { createClient as createSupabaseClient } from '@supabase/supabase-js'; +import { createClient as createCookieClient } from './server'; + +export type RequireUserResult = + | { user: { id: string }; accessToken: string } + | { error: Response }; + +function json(status: number, message: string): { error: Response } { + return { error: Response.json({ message }, { status }) }; +} + +/** + * The single way API routes resolve the canonical Supabase civic account. + * Reads `Authorization: Bearer ` first, otherwise the cookie session. + * The user id always comes from a validated Supabase session, never from + * the request body. + */ +export async function requireUser(req: Request): Promise { + const url = process.env.NEXT_PUBLIC_SUPABASE_URL; + const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; + if (!url || !anonKey) return json(503, 'Auth is not configured'); + + const header = req.headers.get('authorization'); + const match = header ? /^Bearer\s+(.+)$/i.exec(header.trim()) : null; + + if (match) { + const token = match[1].trim(); + const supabase = createSupabaseClient(url, anonKey, { + auth: { persistSession: false, autoRefreshToken: false }, + }); + const { data, error } = await supabase.auth.getUser(token); + if (error || !data?.user) return json(401, 'Unauthorized'); + return { user: { id: data.user.id }, accessToken: token }; + } + + const supabase = await createCookieClient(); + const { data, error } = await supabase.auth.getUser(); + if (error || !data?.user) return json(401, 'Unauthorized'); + const { data: sessionData } = await supabase.auth.getSession(); + const accessToken = sessionData?.session?.access_token; + if (!accessToken) return json(401, 'Unauthorized'); + return { user: { id: data.user.id }, accessToken }; +} From 13d425738287c06af6b8b395f775cbc86b3d2e20 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:14:52 -0800 Subject: [PATCH 05/16] feat(civic): source adapter model and federal slice; remove live-mode fixtures Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- src/app/api/legislature/route.test.ts | 32 ++++ src/app/api/legislature/route.ts | 147 +++++------------- src/app/legislature/page.tsx | 60 +++++-- src/lib/civic/congress.test.ts | 59 +++++++ src/lib/civic/congress.ts | 63 ++++++++ src/lib/civic/freshness.ts | 10 ++ src/lib/civic/hash.ts | 18 +++ src/lib/civic/openstates.test.ts | 38 +++++ src/lib/civic/openstates.ts | 59 +++++++ src/lib/civic/types.ts | 27 ++++ supabase/migration.sql | 1 + .../20261104000200_source_registry.sql | 39 +++++ 12 files changed, 432 insertions(+), 121 deletions(-) create mode 100644 src/app/api/legislature/route.test.ts create mode 100644 src/lib/civic/congress.test.ts create mode 100644 src/lib/civic/congress.ts create mode 100644 src/lib/civic/freshness.ts create mode 100644 src/lib/civic/hash.ts create mode 100644 src/lib/civic/openstates.test.ts create mode 100644 src/lib/civic/openstates.ts create mode 100644 src/lib/civic/types.ts create mode 100644 supabase/migrations/20261104000200_source_registry.sql diff --git a/src/app/api/legislature/route.test.ts b/src/app/api/legislature/route.test.ts new file mode 100644 index 0000000..86d9bd4 --- /dev/null +++ b/src/app/api/legislature/route.test.ts @@ -0,0 +1,32 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +const fetchBills = vi.fn(); +vi.mock('@/lib/civic/congress', () => ({ + congressAdapter: () => ({ id: 'congress-gov', jurisdictionCode: 'US', fetchBills }), +})); + +import { GET } from './route'; + +const call = (qs: string) => GET(new Request(`http://localhost/api/legislature${qs}`) as never); + +describe('GET /api/legislature', () => { + beforeEach(() => fetchBills.mockReset()); + + it('EU is not_integrated with no items', async () => { + const body = await (await call('?country=EU')).json(); + expect(body.status).toBe('not_integrated'); + expect(body.items).toEqual([]); + expect(body.bills).toEqual([]); + }); + + it('US unavailable returns no hardcoded bill', async () => { + fetchBills.mockResolvedValue({ + status: 'unavailable', items: [], fetchedAt: null, + source: { name: 'Congress.gov', url: 'https://www.congress.gov' }, error: 'no key', + }); + const text = JSON.stringify(await (await call('?country=US')).json()); + expect(JSON.parse(text).status).toBe('unavailable'); + expect(JSON.parse(text).bills).toEqual([]); + for (const s of ['hr4763', 's2281', 'hr5121', 'McHenry']) expect(text).not.toContain(s); + }); +}); diff --git a/src/app/api/legislature/route.ts b/src/app/api/legislature/route.ts index 27e2745..e1adeee 100644 --- a/src/app/api/legislature/route.ts +++ b/src/app/api/legislature/route.ts @@ -1,130 +1,67 @@ import { NextRequest, NextResponse } from 'next/server'; +import { congressAdapter } from '@/lib/civic/congress'; +import type { CivicBill, SourceResult } from '@/lib/civic/types'; -interface Bill { - id: string; - number: string; - title: string; - status: string; - chamber: string; - introduced: string; - sponsor: string; - country: string; - url: string; -} - -const CONGRESS_API = 'https://api.congress.gov/v3'; - -/** - * Fetches bills from Congress.gov (US legislature). - * Free tier: 5,000 requests/hour. - */ -async function fetchCongressBills(limit = 10): Promise { - const apiKey = process.env.CONGRESS_API_KEY; - if (!apiKey) return getFallbackUSBills(); - - try { - const res = await fetch( - `${CONGRESS_API}/bill?limit=${limit}&sort=updateDate+desc&api_key=${apiKey}`, - { next: { revalidate: 600 } } - ); - if (!res.ok) return getFallbackUSBills(); +export const runtime = 'nodejs'; - const data = await res.json(); - if (!data.bills) return getFallbackUSBills(); +const LIMIT = 10; - return data.bills.map((b: Record) => ({ - id: `us-${b.number}-${b.congress}`, - number: `${b.type}.${b.number}`, - title: (b.title as string) || 'Untitled', - status: (b.latestAction as Record)?.text?.substring(0, 50) || 'Unknown', - chamber: (b.originChamber as string) || 'Unknown', - introduced: (b.introducedDate as string) || '', - sponsor: 'Congress', - country: 'US', - url: (b.url as string) || '', - })); - } catch { - console.error('Congress.gov fetch failed'); - return getFallbackUSBills(); - } -} - -/** - * Fetches UK Parliament bills. - * Free, no API key required. - */ -async function fetchUKBills(limit = 10): Promise { +async function fetchUK(now: Date): Promise> { + const source = { name: 'UK Parliament Bills API', url: 'https://bills.parliament.uk' }; + const fail = (error: string): SourceResult => ({ + status: 'unavailable', items: [], fetchedAt: null, source, error, + }); try { const res = await fetch( - `https://bills-api.parliament.uk/api/v1/Bills?CurrentHouse=All&SortBy=DateUpdatedDesc&Take=${limit}`, - { next: { revalidate: 600 } } + `https://bills-api.parliament.uk/api/v1/Bills?CurrentHouse=All&SortBy=DateUpdatedDesc&Take=${LIMIT}`, + { next: { revalidate: 600 } }, ); - if (!res.ok) return []; - + if (!res.ok) return fail(`HTTP ${res.status}`); const data = await res.json(); - if (!data.items) return []; - - return data.items.map((b: Record) => ({ - id: `uk-${b.billId}`, + if (!Array.isArray(data.items)) return fail('response missing items'); + const items: CivicBill[] = data.items.map((b: Record) => ({ + externalId: `uk-${b.billId}`, + jurisdictionCode: 'UK', number: `Bill ${b.billId}`, title: (b.shortTitle as string) || (b.longTitle as string) || 'Untitled', status: (b.currentHouse as string) || 'Unknown', - chamber: (b.originatingHouse as string) || 'Unknown', - introduced: (b.introducedSessionId as string) || '', - sponsor: (b.sponsors as Array>)?.[0]?.name as string || 'UK Parliament', - country: 'UK', - url: `https://bills.parliament.uk/bills/${b.billId}`, + introducedDate: null, + sourceUrl: `https://bills.parliament.uk/bills/${b.billId}`, + recordHash: '', })); + return { status: 'live', items, fetchedAt: now.toISOString(), source }; } catch { - console.error('UK Parliament fetch failed'); - return []; + return fail('fetch failed'); } } -function getFallbackUSBills(): Bill[] { - return [ - { id: 'us-hr4763', number: 'H.R.4763', title: 'Financial Innovation and Technology for the 21st Century Act', status: 'Passed House', chamber: 'House', introduced: '2025-07-20', sponsor: 'Rep. McHenry', country: 'US', url: 'https://congress.gov/bill/118th-congress/house-bill/4763' }, - { id: 'us-s2281', number: 'S.2281', title: 'Stablecoin Transparency and Accountability Act', status: 'Committee', chamber: 'Senate', introduced: '2025-11-15', sponsor: 'Sen. Gillibrand', country: 'US', url: '' }, - { id: 'us-hr5121', number: 'H.R.5121', title: 'Blockchain Regulatory Certainty Act', status: 'Introduced', chamber: 'House', introduced: '2026-01-10', sponsor: 'Rep. Emmer', country: 'US', url: '' }, - ]; -} - export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); - const country = searchParams.get('country') || 'US'; - - let bills: Bill[] = []; + const country = (searchParams.get('country') || 'US').toUpperCase(); + const now = new Date(); - switch (country.toUpperCase()) { - case 'US': - bills = await fetchCongressBills(); - break; - case 'UK': - bills = await fetchUKBills(); - break; - case 'EU': - bills = [ - { id: 'eu-mica', number: 'COM/2024/0150', title: 'MiCA Amendment — Markets in Crypto-Assets', status: 'Trilogue', chamber: 'Parliament', introduced: '2025-03-01', sponsor: 'European Commission', country: 'EU', url: 'https://eur-lex.europa.eu' }, - ]; - break; - case 'BR': - bills = [ - { id: 'br-pl4401', number: 'PL 4401/2021', title: 'Marco Legal das Criptomoedas', status: 'Enacted', chamber: 'Câmara', introduced: '2021-08-12', sponsor: 'Dep. Aureo', country: 'BR', url: 'https://camara.leg.br' }, - ]; - break; - case 'IN': - bills = [ - { id: 'in-89', number: 'Bill No. 89', title: 'Digital India Act, 2026', status: 'Draft', chamber: 'Lok Sabha', introduced: '2026-02-01', sponsor: 'MeitY', country: 'IN', url: 'https://data.gov.in' }, - ]; - break; - default: - bills = await fetchCongressBills(); + let result: SourceResult; + if (country === 'US') { + result = await congressAdapter({ apiKey: process.env.CONGRESS_API_KEY }).fetchBills({ limit: LIMIT }); + } else if (country === 'UK') { + result = await fetchUK(now); + } else { + result = { + status: 'not_integrated', items: [], fetchedAt: null, + source: { name: 'Not integrated', url: '' }, + }; } return NextResponse.json({ - bills, - total: bills.length, + status: result.status, + source: result.source, + fetchedAt: result.fetchedAt, + error: result.error, + items: result.items, + // Legacy key kept for existing consumers; same array as `items`. + bills: result.items, + total: result.items.length, country, - timestamp: new Date().toISOString(), + timestamp: now.toISOString(), }); } diff --git a/src/app/legislature/page.tsx b/src/app/legislature/page.tsx index 4ed8741..c87aa51 100644 --- a/src/app/legislature/page.tsx +++ b/src/app/legislature/page.tsx @@ -1,20 +1,48 @@ -export default function LegislaturePage() { +import { headers } from 'next/headers'; + +interface LegislatureResponse { + status: 'live' | 'stale' | 'unavailable' | 'not_integrated'; + fetchedAt: string | null; + source: { name: string; url: string }; + items: Array<{ externalId: string; number: string; title: string; status: string; sourceUrl: string }>; +} + +const LABEL: Record = { + live: 'Live', stale: 'Stale', unavailable: 'Unavailable', not_integrated: 'Not integrated', +}; + +async function load(): Promise { + try { + const h = await headers(); + const host = h.get('host'); + if (!host) return null; + const proto = h.get('x-forwarded-proto') ?? 'http'; + const res = await fetch(`${proto}://${host}/api/legislature?country=US`, { cache: 'no-store' }); + return res.ok ? ((await res.json()) as LegislatureResponse) : null; + } catch { + return null; + } +} + +export default async function LegislaturePage() { + const data = await load(); + const status = data?.status ?? 'unavailable'; return (
-

- Legislature -

-

- This page will display tracked legislation from various countries. - The UI will allow users to view details, see AI-generated debates (pros/cons), - and participate in shadow voting. -

- {/* Placeholder for future components */} -
-

- Legislation content will be displayed here. -

-
+

Legislature

+

Status: {LABEL[status]}

+ {data?.fetchedAt &&

Fetched: {data.fetchedAt} from {data.source.name}

} + {data && data.items.length > 0 ? ( +
    + {data.items.map((b) => ( +
  • + {b.number} {b.title} ({b.status}) +
  • + ))} +
+ ) : ( +

No bills to display.

+ )}
); -} \ No newline at end of file +} diff --git a/src/lib/civic/congress.test.ts b/src/lib/civic/congress.test.ts new file mode 100644 index 0000000..c09451e --- /dev/null +++ b/src/lib/civic/congress.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it, vi } from 'vitest'; +import { congressAdapter } from './congress'; +import { classifyFreshness } from './freshness'; + +const NOW = new Date('2026-11-03T12:00:00.000Z'); +const bill = (n: string, text = 'Referred to committee') => ({ + congress: 119, type: 'HR', number: n, title: `Bill ${n}`, originChamber: 'House', + updateDate: '2026-11-02', latestAction: { actionDate: '2026-11-01', text }, + url: `https://api.congress.gov/v3/bill/119/hr/${n}?format=json`, +}); +const ok = (bills: unknown[]) => vi.fn(async () => new Response(JSON.stringify({ bills }), { status: 200 })); +const mk = (f: typeof fetch, apiKey: string | null = 'k') => + congressAdapter({ apiKey: apiKey ?? undefined, fetch: f, now: () => NOW }); + +describe('congressAdapter', () => { + it('is unavailable without an apiKey and does not fetch', async () => { + const f = ok([]); + const r = await mk(f as unknown as typeof fetch, null).fetchBills({ limit: 5 }); + expect(r.status).toBe('unavailable'); + expect(r.items).toEqual([]); + expect(f).not.toHaveBeenCalled(); + }); + it('is unavailable on HTTP 500', async () => { + const f = vi.fn(async () => new Response('x', { status: 500 })); + expect((await mk(f as unknown as typeof fetch).fetchBills({ limit: 5 })).status).toBe('unavailable'); + }); + it('is unavailable on network error', async () => { + const f = vi.fn(async () => { throw new Error('boom'); }); + const r = await mk(f as unknown as typeof fetch).fetchBills({ limit: 5 }); + expect(r.status).toBe('unavailable'); + expect(r.items).toEqual([]); + }); + it('is unavailable on malformed JSON', async () => { + const f = vi.fn(async () => new Response('{nope', { status: 200 })); + expect((await mk(f as unknown as typeof fetch).fetchBills({ limit: 5 })).status).toBe('unavailable'); + }); + it('returns live items with sourceUrl, injected clock, and no fixtures', async () => { + const r = await mk(ok([bill('100'), bill('200')]) as unknown as typeof fetch).fetchBills({ limit: 5 }); + expect(r.status).toBe('live'); + expect(r.items).toHaveLength(2); + expect(r.fetchedAt).toBe(NOW.toISOString()); + for (const i of r.items) expect(i.sourceUrl).not.toBe(''); + const ids = r.items.map((i) => i.externalId); + for (const bad of ['us-hr4763', 'us-s2281', 'us-hr5121']) expect(ids).not.toContain(bad); + }); + it('hashes deterministically and changes with latestAction', async () => { + const h = async (t?: string) => + (await mk(ok([bill('100', t)]) as unknown as typeof fetch).fetchBills({ limit: 1 })).items[0].recordHash; + expect(await h()).toBe(await h()); + expect(await h()).not.toBe(await h('Passed House')); + expect(await h()).toMatch(/^[0-9a-f]{64}$/); + }); +}); + +describe('classifyFreshness', () => { + it('null is unavailable', () => expect(classifyFreshness(null, NOW, 1000)).toBe('unavailable')); + it('old is stale', () => expect(classifyFreshness('2026-11-03T11:00:00.000Z', NOW, 1000)).toBe('stale')); + it('recent is live', () => expect(classifyFreshness('2026-11-03T11:59:59.500Z', NOW, 1000)).toBe('live')); +}); diff --git a/src/lib/civic/congress.ts b/src/lib/civic/congress.ts new file mode 100644 index 0000000..05aba0d --- /dev/null +++ b/src/lib/civic/congress.ts @@ -0,0 +1,63 @@ +import { recordHash } from './hash'; +import type { CivicBill, SourceAdapter, SourceResult } from './types'; + +const BASE = 'https://api.congress.gov/v3'; +const SOURCE = { name: 'Congress.gov', url: 'https://www.congress.gov' }; + +interface Deps { + apiKey?: string; + fetch?: typeof fetch; + now?: () => Date; +} + +const str = (v: unknown): string => (typeof v === 'string' ? v : ''); + +export function congressAdapter(d: Deps): SourceAdapter { + const doFetch = d.fetch ?? fetch; + const now = d.now ?? (() => new Date()); + const fail = (error: string): SourceResult => ({ + status: 'unavailable', items: [], fetchedAt: null, source: SOURCE, error, + }); + + return { + id: 'congress-gov', + jurisdictionCode: 'US', + async fetchBills({ limit, signal }) { + if (!d.apiKey) return fail('CONGRESS_API_KEY not configured'); + try { + const res = await doFetch( + `${BASE}/bill?format=json&limit=${limit}&sort=updateDate+desc`, + { signal, headers: { 'X-Api-Key': d.apiKey } }, + ); + if (!res.ok) return fail(`Congress.gov HTTP ${res.status}`); + const data = (await res.json()) as { bills?: unknown }; + if (!Array.isArray(data.bills)) return fail('Congress.gov response missing bills'); + const items: CivicBill[] = data.bills.map((raw) => { + const b = raw as Record; + const type = str(b.type); + const number = str(b.number) || String(b.number ?? ''); + const congress = String(b.congress ?? ''); + const latest = (b.latestAction ?? {}) as Record; + const externalId = `us-${congress}-${type.toLowerCase()}-${number}`; + return { + externalId, + jurisdictionCode: 'US', + number: `${type}.${number}`, + title: str(b.title) || 'Untitled', + status: str(latest.text) || 'Unknown', + introducedDate: str(b.introducedDate) || null, + sourceUrl: str(b.url) || `${SOURCE.url}/bill/${congress}th-congress/${type.toLowerCase()}/${number}`, + recordHash: recordHash({ + externalId, + updateDate: str(b.updateDate), + latestActionText: str(latest.text), + }), + }; + }); + return { status: 'live', items, fetchedAt: now().toISOString(), source: SOURCE }; + } catch (e) { + return fail(e instanceof Error ? e.message : 'fetch failed'); + } + }, + }; +} diff --git a/src/lib/civic/freshness.ts b/src/lib/civic/freshness.ts new file mode 100644 index 0000000..eec9edb --- /dev/null +++ b/src/lib/civic/freshness.ts @@ -0,0 +1,10 @@ +export function classifyFreshness( + fetchedAt: string | null, + now: Date, + maxAgeMs: number, +): 'live' | 'stale' | 'unavailable' { + if (!fetchedAt) return 'unavailable'; + const t = Date.parse(fetchedAt); + if (Number.isNaN(t)) return 'unavailable'; + return now.getTime() - t > maxAgeMs ? 'stale' : 'live'; +} diff --git a/src/lib/civic/hash.ts b/src/lib/civic/hash.ts new file mode 100644 index 0000000..a7312b0 --- /dev/null +++ b/src/lib/civic/hash.ts @@ -0,0 +1,18 @@ +import { createHash } from 'node:crypto'; + +function canonical(v: unknown): string { + if (Array.isArray(v)) return `[${v.map(canonical).join(',')}]`; + if (v && typeof v === 'object') { + const o = v as Record; + return `{${Object.keys(o) + .sort() + .map((k) => `${JSON.stringify(k)}:${canonical(o[k])}`) + .join(',')}}`; + } + return JSON.stringify(v ?? null); +} + +/** SHA-256 over canonical JSON (sorted keys, no whitespace). Node runtime only. */ +export function recordHash(value: Record): string { + return createHash('sha256').update(canonical(value)).digest('hex'); +} diff --git a/src/lib/civic/openstates.test.ts b/src/lib/civic/openstates.test.ts new file mode 100644 index 0000000..deebf1d --- /dev/null +++ b/src/lib/civic/openstates.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it, vi } from 'vitest'; +import { openStatesAdapter } from './openstates'; + +const NOW = new Date('2026-11-03T12:00:00.000Z'); +const row = (id: string, act = 'In committee') => ({ + id: `ocd-bill/${id}`, identifier: `AB ${id}`, title: `T${id}`, latest_action_description: act, + updated_at: '2026-11-02T00:00:00Z', first_action_date: '2026-01-05', openstates_url: `https://openstates.org/ca/bills/2025/AB${id}/`, +}); +const ok = (results: unknown[]) => vi.fn(async () => new Response(JSON.stringify({ results }), { status: 200 })) as unknown as typeof fetch; +const mk = (f: typeof fetch, apiKey: string | null = 'k') => openStatesAdapter({ apiKey: apiKey ?? undefined, fetch: f, now: () => NOW }); + +describe('openStatesAdapter (mocked)', () => { + it('unavailable without key', async () => { + const r = await mk(ok([]), null).fetchBills({ limit: 5 }); + expect(r.status).toBe('unavailable'); expect(r.items).toEqual([]); + }); + it('unavailable on 500, network error, malformed JSON', async () => { + for (const f of [ + vi.fn(async () => new Response('x', { status: 500 })), + vi.fn(async () => { throw new Error('boom'); }), + vi.fn(async () => new Response('{nope', { status: 200 })), + ]) { + const r = await mk(f as unknown as typeof fetch).fetchBills({ limit: 5 }); + expect(r.status).toBe('unavailable'); expect(r.items).toEqual([]); + } + }); + it('live with items, sourceUrl, clock', async () => { + const r = await mk(ok([row('1'), row('2')])).fetchBills({ limit: 5 }); + expect(r.status).toBe('live'); expect(r.fetchedAt).toBe(NOW.toISOString()); + expect(r.items).toHaveLength(2); + for (const i of r.items) { expect(i.sourceUrl).not.toBe(''); expect(i.jurisdictionCode).toBe('US-CA'); } + }); + it('hash stable and sensitive to latest action', async () => { + const h = async (a?: string) => (await mk(ok([row('1', a)])).fetchBills({ limit: 1 })).items[0].recordHash; + expect(await h()).toBe(await h()); + expect(await h()).not.toBe(await h('Passed')); + }); +}); diff --git a/src/lib/civic/openstates.ts b/src/lib/civic/openstates.ts new file mode 100644 index 0000000..b326e7c --- /dev/null +++ b/src/lib/civic/openstates.ts @@ -0,0 +1,59 @@ +import { recordHash } from './hash'; +import type { CivicBill, SourceAdapter, SourceResult } from './types'; + +const SOURCE = { name: 'Open States (California)', url: 'https://openstates.org/ca/bills/' }; + +interface Deps { + apiKey?: string; + fetch?: typeof fetch; + now?: () => Date; +} + +const str = (v: unknown): string => (typeof v === 'string' ? v : ''); + +/** California state adapter. Live run is BLOCKED until an OpenStates key exists in an approved store. */ +export function openStatesAdapter(d: Deps): SourceAdapter { + const doFetch = d.fetch ?? fetch; + const now = d.now ?? (() => new Date()); + const fail = (error: string): SourceResult => ({ + status: 'unavailable', items: [], fetchedAt: null, source: SOURCE, error, + }); + + return { + id: 'openstates-ca', + jurisdictionCode: 'US-CA', + async fetchBills({ limit, signal }) { + if (!d.apiKey) return fail('OPENSTATES_API_KEY not configured'); + try { + const res = await doFetch( + `https://v3.openstates.org/bills?jurisdiction=California&sort=updated_desc&per_page=${Math.min(limit, 20)}`, + { signal, headers: { 'X-API-KEY': d.apiKey } }, + ); + if (!res.ok) return fail(`Open States HTTP ${res.status}`); + const data = (await res.json()) as { results?: unknown }; + if (!Array.isArray(data.results)) return fail('Open States response missing results'); + const items: CivicBill[] = data.results.map((raw) => { + const b = raw as Record; + const externalId = `ca-${str(b.id)}`; + return { + externalId, + jurisdictionCode: 'US-CA', + number: str(b.identifier), + title: str(b.title) || 'Untitled', + status: str(b.latest_action_description) || 'Unknown', + introducedDate: str(b.first_action_date) || null, + sourceUrl: str(b.openstates_url) || SOURCE.url, + recordHash: recordHash({ + externalId, + updateDate: str(b.updated_at), + latestActionText: str(b.latest_action_description), + }), + }; + }); + return { status: 'live', items, fetchedAt: now().toISOString(), source: SOURCE }; + } catch (e) { + return fail(e instanceof Error ? e.message : 'fetch failed'); + } + }, + }; +} diff --git a/src/lib/civic/types.ts b/src/lib/civic/types.ts new file mode 100644 index 0000000..5a55454 --- /dev/null +++ b/src/lib/civic/types.ts @@ -0,0 +1,27 @@ +export type SourceStatus = 'live' | 'stale' | 'unavailable' | 'not_integrated'; + +export interface CivicBill { + externalId: string; + jurisdictionCode: string; + number: string; + title: string; + status: string; + introducedDate: string | null; + sourceUrl: string; + /** Identifies the fetched record state; NOT a bill-text version hash. */ + recordHash: string; +} + +export interface SourceResult { + status: SourceStatus; + items: T[]; + fetchedAt: string | null; + source: { name: string; url: string }; + error?: string; +} + +export interface SourceAdapter { + id: string; + jurisdictionCode: string; + fetchBills(o: { limit: number; signal?: AbortSignal }): Promise>; +} diff --git a/supabase/migration.sql b/supabase/migration.sql index bf2c8ee..5fbe47b 100644 --- a/supabase/migration.sql +++ b/supabase/migration.sql @@ -1,3 +1,4 @@ +-- LEGACY: superseded by supabase/migrations/*. Kept for reference only; do not apply. -- UTP Platform Supabase Schema -- Apply via Supabase SQL Editor or migration tool diff --git a/supabase/migrations/20261104000200_source_registry.sql b/supabase/migrations/20261104000200_source_registry.sql new file mode 100644 index 0000000..0d5f6e3 --- /dev/null +++ b/supabase/migrations/20261104000200_source_registry.sql @@ -0,0 +1,39 @@ +-- Source registry: adapter status tracking, ingest failures, bill record versions. +-- Not applied to any remote database by this task. + +ALTER TABLE jurisdictions DROP CONSTRAINT IF EXISTS jurisdictions_type_check; +ALTER TABLE jurisdictions ADD CONSTRAINT jurisdictions_type_check + CHECK (type IN ('federal', 'state', 'international', 'local')); + +ALTER TABLE data_sources + ADD COLUMN IF NOT EXISTS jurisdiction_id UUID REFERENCES jurisdictions(id), + ADD COLUMN IF NOT EXISTS adapter_status TEXT NOT NULL DEFAULT 'not_integrated' + CHECK (adapter_status IN ('live', 'stale', 'blocked', 'not_integrated')), + ADD COLUMN IF NOT EXISTS last_attempt_at TIMESTAMPTZ, + ADD COLUMN IF NOT EXISTS last_error TEXT; + +CREATE TABLE IF NOT EXISTS ingest_failures ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + source_id UUID REFERENCES data_sources(id) ON DELETE CASCADE, + external_id TEXT, + error TEXT NOT NULL, + payload_hash TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + resolved_at TIMESTAMPTZ +); +CREATE INDEX IF NOT EXISTS idx_ingest_failures_source ON ingest_failures(source_id, created_at DESC); + +-- record_hash identifies a fetched record state, not a bill-text version. +CREATE TABLE IF NOT EXISTS bill_versions ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + bill_id UUID NOT NULL REFERENCES bills(id) ON DELETE CASCADE, + record_hash TEXT NOT NULL, + fetched_at TIMESTAMPTZ NOT NULL, + UNIQUE (bill_id, record_hash) +); + +-- Service-role/ingest only: RLS on with no public policies. +ALTER TABLE ingest_failures ENABLE ROW LEVEL SECURITY; +ALTER TABLE bill_versions ENABLE ROW LEVEL SECURITY; +DROP POLICY IF EXISTS "Bill versions are publicly readable" ON bill_versions; +CREATE POLICY "Bill versions are publicly readable" ON bill_versions FOR SELECT USING (true); From 6892d49e921c1433d7b2b85ffa13c5848d202bf8 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:16:06 -0800 Subject: [PATCH 06/16] feat(vote): harden /api/vote and add vote integrity migration with disposable-DB tests Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- package.json | 3 +- scripts/test-db.sh | 44 ++++++ src/app/api/vote/route.test.ts | 96 ++++++++++++ src/app/api/vote/route.ts | 86 +++++------ src/lib/supabaseAuth/userClient.test.ts | 23 +++ src/lib/supabaseAuth/userClient.ts | 16 ++ src/lib/votes/parseVoteInput.test.ts | 23 +++ src/lib/votes/parseVoteInput.ts | 26 ++++ .../20261104000000_vote_integrity.sql | 137 ++++++++++++++++++ supabase/test/auth-shim.sql | 24 +++ test-db/psql.ts | 48 ++++++ test-db/vote-integrity.test.ts | 131 +++++++++++++++++ vitest.db.config.ts | 13 ++ 13 files changed, 623 insertions(+), 47 deletions(-) create mode 100644 scripts/test-db.sh create mode 100644 src/app/api/vote/route.test.ts create mode 100644 src/lib/supabaseAuth/userClient.test.ts create mode 100644 src/lib/supabaseAuth/userClient.ts create mode 100644 src/lib/votes/parseVoteInput.test.ts create mode 100644 src/lib/votes/parseVoteInput.ts create mode 100644 supabase/migrations/20261104000000_vote_integrity.sql create mode 100644 supabase/test/auth-shim.sql create mode 100644 test-db/psql.ts create mode 100644 test-db/vote-integrity.test.ts create mode 100644 vitest.db.config.ts diff --git a/package.json b/package.json index 05ee81c..45afa88 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "test:web": "vitest run", "ci:fast": "npm run lint && npm run typecheck && npm run test:web && npm run test:contracts", "deploy:local": "hardhat run scripts/deploy.cjs --network localhost", - "deploy:rskTestnet": "hardhat run scripts/deploy.cjs --network rskTestnet" + "deploy:rskTestnet": "hardhat run scripts/deploy.cjs --network rskTestnet", + "test:db": "bash scripts/test-db.sh" }, "dependencies": { "@base-org/account": "^2.5.3", diff --git a/scripts/test-db.sh b/scripts/test-db.sh new file mode 100644 index 0000000..7bbb947 --- /dev/null +++ b/scripts/test-db.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# Disposable PostgreSQL 16 cluster for DB integrity tests (no Docker). +# Creates a temp cluster on a random free port, loads the Supabase auth shim, +# applies supabase/migrations/*.sql in order, runs vitest.db.config.ts, and +# always tears the cluster down. +set -euo pipefail + +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +PGBIN="${PGBIN:-/usr/lib/postgresql/16/bin}" +TMP="$(mktemp -d -t utp-testdb-XXXXXX)" +DATA="$TMP/data" +PORT="$(python3 -c 'import socket;s=socket.socket();s.bind(("127.0.0.1",0));print(s.getsockname()[1]);s.close()')" + +AS=() +if [ "$(id -u)" = "0" ]; then + chown postgres "$TMP" + chmod 700 "$TMP" + AS=(runuser -u postgres --) +fi + +cleanup() { + "${AS[@]}" "$PGBIN/pg_ctl" -D "$DATA" -m immediate stop >/dev/null 2>&1 || true + rm -rf "$TMP" +} +trap cleanup EXIT INT TERM + +"${AS[@]}" "$PGBIN/initdb" -D "$DATA" -U postgres -A trust --no-sync >"$TMP/initdb.log" 2>&1 \ + || { cat "$TMP/initdb.log"; exit 1; } +"${AS[@]}" "$PGBIN/pg_ctl" -D "$DATA" -l "$TMP/pg.log" -w \ + -o "-p $PORT -k $TMP -c listen_addresses=127.0.0.1 -c max_connections=200 -c fsync=off" start >/dev/null \ + || { cat "$TMP/pg.log"; exit 1; } + +export TEST_DATABASE_URL="postgresql://postgres@127.0.0.1:$PORT/postgres" +PSQL=(psql "$TEST_DATABASE_URL" -X -q -v ON_ERROR_STOP=1) + +echo "test-db: cluster on port $PORT" +"${PSQL[@]}" -f "$ROOT/supabase/test/auth-shim.sql" +for f in $(ls "$ROOT"/supabase/migrations/*.sql | sort); do + echo "test-db: applying $(basename "$f")" + "${PSQL[@]}" -f "$f" +done + +cd "$ROOT" +npx vitest run --config vitest.db.config.ts diff --git a/src/app/api/vote/route.test.ts b/src/app/api/vote/route.test.ts new file mode 100644 index 0000000..8b7c468 --- /dev/null +++ b/src/app/api/vote/route.test.ts @@ -0,0 +1,96 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; + +const requireUser = vi.fn(); +vi.mock('@/lib/supabaseAuth/requireUser', () => ({ requireUser })); + +const upsert = vi.fn(); +const from = vi.fn(() => ({ upsert })); +const createClient = vi.fn(() => ({ from })); +vi.mock('@supabase/supabase-js', () => ({ createClient })); + +const BILL = '3f2b6c1e-8a4d-4c2b-9e1f-0a1b2c3d4e5f'; +const USER = '11111111-2222-4333-8444-555555555555'; + +function req(body: unknown) { + return new Request('http://localhost/api/vote', { + method: 'POST', + headers: { 'content-type': 'application/json', authorization: 'Bearer tok', 'x-forwarded-for': '1.2.3.4' }, + body: typeof body === 'string' ? body : JSON.stringify(body), + }); +} + +async function load() { + vi.resetModules(); + return (await import('./route')).POST; +} + +describe('POST /api/vote', () => { + beforeEach(() => { + vi.unstubAllEnvs(); + requireUser.mockReset(); + upsert.mockReset().mockResolvedValue({ error: null }); + from.mockClear(); + createClient.mockClear(); + vi.stubEnv('NEXT_PUBLIC_SUPABASE_URL', 'https://example.supabase.co'); + vi.stubEnv('NEXT_PUBLIC_SUPABASE_ANON_KEY', 'anon-key'); + vi.stubEnv('SUPABASE_SERVICE_ROLE_KEY', 'service-key'); + vi.stubEnv('IP_SALT', 'salt'); + }); + + it('passes through 401 from requireUser', async () => { + requireUser.mockResolvedValue({ error: Response.json({ message: 'Unauthorized' }, { status: 401 }) }); + const POST = await load(); + const res = await POST(req({ billId: BILL, vote: 'for' }) as never); + expect(res.status).toBe(401); + expect(createClient).not.toHaveBeenCalled(); + }); + + it('returns 400 for invalid input with no DB call', async () => { + requireUser.mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); + const POST = await load(); + for (const b of [{ billId: 'nope', vote: 'for' }, { billId: BILL, vote: 'maybe' }, '{bad json']) { + const res = await POST(req(b) as never); + expect(res.status).toBe(400); + } + expect(upsert).not.toHaveBeenCalled(); + }); + + it('upserts once with the session user, ignoring body user_id, using anon key + caller auth', async () => { + requireUser.mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); + const POST = await load(); + const res = await POST(req({ billId: BILL, vote: 'for', user_id: 'someone-else' }) as never); + expect(res.status).toBe(200); + expect(upsert).toHaveBeenCalledTimes(1); + const [row, opts] = upsert.mock.calls[0]; + expect(row.user_id).toBe(USER); + expect(row.bill_id).toBe(BILL); + expect(row.vote).toBe('for'); + expect(opts).toEqual({ onConflict: 'bill_id,user_id' }); + expect(createClient).toHaveBeenCalledTimes(1); + const [, key, cOpts] = createClient.mock.calls[0] as unknown as [string, string, { global: { headers: Record } }]; + expect(key).toBe('anon-key'); + expect(key).not.toBe('service-key'); + expect(cOpts.global.headers.Authorization).toBe('Bearer tok'); + }); + + it('returns generic 500 without leaking DB error text', async () => { + requireUser.mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); + upsert.mockResolvedValue({ error: { message: 'relation secret_table violates policy' } }); + const spy = vi.spyOn(console, 'error').mockImplementation(() => {}); + const POST = await load(); + const res = await POST(req({ billId: BILL, vote: 'for' }) as never); + expect(res.status).toBe(500); + expect(await res.text()).not.toContain('secret_table'); + spy.mockRestore(); + }); + + it('returns 503 in production when IP_SALT is unset', async () => { + vi.stubEnv('NODE_ENV', 'production'); + vi.stubEnv('IP_SALT', ''); + requireUser.mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); + const POST = await load(); + const res = await POST(req({ billId: BILL, vote: 'for' }) as never); + expect(res.status).toBe(503); + expect(upsert).not.toHaveBeenCalled(); + }); +}); diff --git a/src/app/api/vote/route.ts b/src/app/api/vote/route.ts index a4315f0..65f8ce3 100644 --- a/src/app/api/vote/route.ts +++ b/src/app/api/vote/route.ts @@ -1,61 +1,55 @@ import { NextRequest, NextResponse } from 'next/server'; import crypto from 'crypto'; -import { createClient } from '@supabase/supabase-js'; +import { requireUser } from '@/lib/supabaseAuth/requireUser'; +import { createUserScopedClient } from '@/lib/supabaseAuth/userClient'; +import { parseVoteInput } from '@/lib/votes/parseVoteInput'; -const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || 'https://placeholder.supabase.co'; -const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || 'placeholder'; - -const supabase = createClient(supabaseUrl, supabaseServiceKey); - -function hashIP(ip: string): string { - const salt = process.env.IP_SALT || 'default_salt'; - return crypto.createHash('sha256') - .update(ip + salt) - .digest('hex') - .substring(0, 16); +function hashIP(ip: string, salt: string): string { + return crypto.createHash('sha256').update(ip + salt).digest('hex').substring(0, 16); } export async function POST(req: NextRequest) { - try { - const { billId, vote } = await req.json(); - const forwarded = req.headers.get('x-forwarded-for'); - const ip = forwarded ? forwarded.split(',')[0] : '127.0.0.1'; - const ipHash = hashIP(ip); - - // Get user from auth header - const authHeader = req.headers.get('Authorization'); - if (!authHeader) { - return NextResponse.json({ message: 'Unauthorized' }, { status: 401 }); - } + const salt = process.env.IP_SALT; + if (!salt && process.env.NODE_ENV === 'production') { + return NextResponse.json({ message: 'Voting is not configured' }, { status: 503 }); + } - const { data: { user }, error: authError } = await supabase.auth.getUser(authHeader.replace('Bearer ', '')); + const auth = await requireUser(req); + if ('error' in auth) return auth.error; - if (authError || !user) { - return NextResponse.json({ message: 'Unauthorized' }, { status: 401 }); - } + let body: unknown; + try { + body = await req.json(); + } catch { + return NextResponse.json({ message: 'Invalid JSON' }, { status: 400 }); + } + const input = parseVoteInput(body); + if (!input.ok) return NextResponse.json({ message: input.message }, { status: 400 }); - // Upsert vote - const { error: voteError } = await supabase - .from('shadow_votes') - .upsert({ - bill_id: billId, - user_id: user.id, - vote, + try { + const forwarded = req.headers.get('x-forwarded-for'); + const ip = forwarded ? forwarded.split(',')[0].trim() : '127.0.0.1'; + // Outside production an unset salt stores no IP hash rather than a guessable one. + const ipHash = salt ? hashIP(ip, salt) : null; + + const supabase = createUserScopedClient(auth.accessToken); + const { error } = await supabase.from('shadow_votes').upsert( + { + bill_id: input.billId, + user_id: auth.user.id, + vote: input.vote, ip_hash: ipHash, created_at: new Date().toISOString(), - }, { onConflict: 'bill_id,user_id' }); - - if (voteError) { - throw voteError; + }, + { onConflict: 'bill_id,user_id' }, + ); + if (error) { + console.error('vote upsert failed', { code: (error as { code?: string }).code }); + return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 }); } - - // Update aggregates (In a real app, this might be a database trigger) - // For this prototype, we can do it manually or assume a trigger exists. - // Let's assume a RPC or trigger handles it for better consistency. - return NextResponse.json({ message: 'Vote recorded' }); - } catch (err: unknown) { - console.error('Error in vote API:', err); - return NextResponse.json({ message: err instanceof Error ? err.message : 'Internal Server Error' }, { status: 500 }); + } catch { + console.error('vote handler failed'); + return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 }); } } diff --git a/src/lib/supabaseAuth/userClient.test.ts b/src/lib/supabaseAuth/userClient.test.ts new file mode 100644 index 0000000..4a80bc7 --- /dev/null +++ b/src/lib/supabaseAuth/userClient.test.ts @@ -0,0 +1,23 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; + +const createClient = vi.fn(() => ({ tag: 'client' })); +vi.mock('@supabase/supabase-js', () => ({ createClient })); + +describe('createUserScopedClient', () => { + beforeEach(() => { + createClient.mockClear(); + vi.stubEnv('NEXT_PUBLIC_SUPABASE_URL', 'https://example.supabase.co'); + vi.stubEnv('NEXT_PUBLIC_SUPABASE_ANON_KEY', 'anon-key'); + vi.stubEnv('SUPABASE_SERVICE_ROLE_KEY', 'service-key'); + }); + it('uses the anon key and the caller bearer token', async () => { + const { createUserScopedClient } = await import('./userClient'); + createUserScopedClient('tok123'); + expect(createClient).toHaveBeenCalledTimes(1); + const [url, key, opts] = createClient.mock.calls[0] as unknown as [string, string, { global: { headers: Record }; auth: { persistSession: boolean } }]; + expect(url).toBe('https://example.supabase.co'); + expect(key).toBe('anon-key'); + expect(opts.global.headers.Authorization).toBe('Bearer tok123'); + expect(opts.auth.persistSession).toBe(false); + }); +}); diff --git a/src/lib/supabaseAuth/userClient.ts b/src/lib/supabaseAuth/userClient.ts new file mode 100644 index 0000000..7105fb9 --- /dev/null +++ b/src/lib/supabaseAuth/userClient.ts @@ -0,0 +1,16 @@ +import { createClient, type SupabaseClient } from '@supabase/supabase-js'; + +/** + * A Supabase client that acts as the calling user: anon key plus the + * caller's access token, so Row Level Security applies. Never uses the + * service-role key. + */ +export function createUserScopedClient(accessToken: string): SupabaseClient { + const url = process.env.NEXT_PUBLIC_SUPABASE_URL; + const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; + if (!url || !anonKey) throw new Error('Supabase is not configured'); + return createClient(url, anonKey, { + global: { headers: { Authorization: `Bearer ${accessToken}` } }, + auth: { persistSession: false, autoRefreshToken: false }, + }); +} diff --git a/src/lib/votes/parseVoteInput.test.ts b/src/lib/votes/parseVoteInput.test.ts new file mode 100644 index 0000000..f3cc448 --- /dev/null +++ b/src/lib/votes/parseVoteInput.test.ts @@ -0,0 +1,23 @@ +import { describe, it, expect } from 'vitest'; +import { parseVoteInput } from './parseVoteInput'; + +const BILL = '3f2b6c1e-8a4d-4c2b-9e1f-0a1b2c3d4e5f'; + +describe('parseVoteInput', () => { + it('rejects non-object bodies', () => { + for (const b of [null, undefined, 'x', 42, [BILL, 'for']]) { + expect(parseVoteInput(b).ok).toBe(false); + } + }); + it('rejects non-UUID billId', () => { + expect(parseVoteInput({ billId: 'abc', vote: 'for' }).ok).toBe(false); + expect(parseVoteInput({ billId: 123, vote: 'for' }).ok).toBe(false); + }); + it("rejects vote 'maybe'", () => { + expect(parseVoteInput({ billId: BILL, vote: 'maybe' }).ok).toBe(false); + }); + it('accepts valid input and ignores user_id/userId', () => { + const r = parseVoteInput({ billId: BILL, vote: 'against', user_id: 'x', userId: 'y' }); + expect(r).toEqual({ ok: true, billId: BILL, vote: 'against' }); + }); +}); diff --git a/src/lib/votes/parseVoteInput.ts b/src/lib/votes/parseVoteInput.ts new file mode 100644 index 0000000..df57965 --- /dev/null +++ b/src/lib/votes/parseVoteInput.ts @@ -0,0 +1,26 @@ +export type VoteChoice = 'for' | 'against' | 'unsure'; + +export type ParsedVoteInput = + | { ok: true; billId: string; vote: VoteChoice } + | { ok: false; message: string }; + +const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; +const VOTES: readonly VoteChoice[] = ['for', 'against', 'unsure']; + +/** + * Validate a vote request body. Only `billId` and `vote` are read; any + * client-supplied user id is ignored (identity comes from the session). + */ +export function parseVoteInput(body: unknown): ParsedVoteInput { + if (typeof body !== 'object' || body === null || Array.isArray(body)) { + return { ok: false, message: 'Request body must be a JSON object' }; + } + const { billId, vote } = body as Record; + if (typeof billId !== 'string' || !UUID_RE.test(billId)) { + return { ok: false, message: 'billId must be a UUID' }; + } + if (typeof vote !== 'string' || !VOTES.includes(vote as VoteChoice)) { + return { ok: false, message: "vote must be 'for', 'against' or 'unsure'" }; + } + return { ok: true, billId, vote: vote as VoteChoice }; +} diff --git a/supabase/migrations/20261104000000_vote_integrity.sql b/supabase/migrations/20261104000000_vote_integrity.sql new file mode 100644 index 0000000..3e6a68d --- /dev/null +++ b/supabase/migrations/20261104000000_vote_integrity.sql @@ -0,0 +1,137 @@ +-- Vote integrity: owner-only UPDATE/DELETE on shadow_votes, trigger-maintained +-- vote_aggregates (recompute under a per-bill advisory lock), a drift checker, +-- and no direct writes to vote_aggregates from API roles. + +-- 1. Owner-only update/delete (INSERT/SELECT policies already exist). +DROP POLICY IF EXISTS "Users can update their own votes" ON public.shadow_votes; +CREATE POLICY "Users can update their own votes" ON public.shadow_votes FOR UPDATE + USING (auth.uid() = user_id) + WITH CHECK (auth.uid() = user_id); + +DROP POLICY IF EXISTS "Users can delete their own votes" ON public.shadow_votes; +CREATE POLICY "Users can delete their own votes" ON public.shadow_votes FOR DELETE + USING (auth.uid() = user_id); + +-- 2. Recompute one bill's aggregate. Caller must hold the bill's advisory lock. +CREATE OR REPLACE FUNCTION public.recompute_vote_aggregate(p_bill_id uuid) +RETURNS void +LANGUAGE plpgsql +SECURITY DEFINER +SET search_path = pg_catalog, public +AS $$ +BEGIN + INSERT INTO public.vote_aggregates AS va + (bill_id, count_for, count_against, count_unsure, total_votes, last_updated) + SELECT p_bill_id, + count(*) FILTER (WHERE vote = 'for'), + count(*) FILTER (WHERE vote = 'against'), + count(*) FILTER (WHERE vote = 'unsure'), + count(*), + now() + FROM public.shadow_votes + WHERE bill_id = p_bill_id + ON CONFLICT (bill_id) DO UPDATE + SET count_for = EXCLUDED.count_for, + count_against = EXCLUDED.count_against, + count_unsure = EXCLUDED.count_unsure, + total_votes = EXCLUDED.total_votes, + last_updated = EXCLUDED.last_updated; +END; +$$; + +-- 3. Trigger: lock affected bill(s) (sorted, to avoid deadlocks), then recount. +-- Locking before counting means each recount runs after any concurrent writer +-- on the same bill has committed, so a stale count can never overwrite a newer one. +CREATE OR REPLACE FUNCTION public.apply_vote_aggregate() +RETURNS trigger +LANGUAGE plpgsql +SECURITY DEFINER +SET search_path = pg_catalog, public +AS $$ +DECLARE + b uuid; +BEGIN + FOR b IN + SELECT DISTINCT x FROM unnest(ARRAY[ + CASE WHEN TG_OP <> 'INSERT' THEN OLD.bill_id END, + CASE WHEN TG_OP <> 'DELETE' THEN NEW.bill_id END + ]) AS x + WHERE x IS NOT NULL + ORDER BY x + LOOP + PERFORM pg_advisory_xact_lock(hashtext(b::text)); + END LOOP; + + FOR b IN + SELECT DISTINCT x FROM unnest(ARRAY[ + CASE WHEN TG_OP <> 'INSERT' THEN OLD.bill_id END, + CASE WHEN TG_OP <> 'DELETE' THEN NEW.bill_id END + ]) AS x + WHERE x IS NOT NULL + LOOP + -- A bill being deleted cascades its aggregate away; skip it. + IF EXISTS (SELECT 1 FROM public.bills WHERE id = b) THEN + PERFORM public.recompute_vote_aggregate(b); + END IF; + END LOOP; + RETURN NULL; +END; +$$; + +DROP TRIGGER IF EXISTS shadow_votes_apply_aggregate ON public.shadow_votes; +CREATE TRIGGER shadow_votes_apply_aggregate + AFTER INSERT OR UPDATE OR DELETE ON public.shadow_votes + FOR EACH ROW EXECUTE FUNCTION public.apply_vote_aggregate(); + +-- 4. Drift checker: one row per bill whose stored aggregate differs from a recount. +CREATE OR REPLACE FUNCTION public.verify_vote_aggregates() +RETURNS TABLE ( + bill_id uuid, + stored_for integer, stored_against integer, stored_unsure integer, stored_total integer, + actual_for bigint, actual_against bigint, actual_unsure bigint, actual_total bigint +) +LANGUAGE sql +STABLE +SECURITY DEFINER +SET search_path = pg_catalog, public +AS $$ + WITH actual AS ( + SELECT sv.bill_id, + count(*) FILTER (WHERE sv.vote = 'for') AS f, + count(*) FILTER (WHERE sv.vote = 'against') AS a, + count(*) FILTER (WHERE sv.vote = 'unsure') AS u, + count(*) AS t + FROM public.shadow_votes sv + WHERE sv.bill_id IS NOT NULL + GROUP BY sv.bill_id + ) + SELECT coalesce(va.bill_id, ac.bill_id), + va.count_for, va.count_against, va.count_unsure, va.total_votes, + coalesce(ac.f, 0), coalesce(ac.a, 0), coalesce(ac.u, 0), coalesce(ac.t, 0) + FROM public.vote_aggregates va + FULL OUTER JOIN actual ac ON ac.bill_id = va.bill_id + WHERE coalesce(va.count_for, 0) IS DISTINCT FROM coalesce(ac.f, 0) + OR coalesce(va.count_against, 0) IS DISTINCT FROM coalesce(ac.a, 0) + OR coalesce(va.count_unsure, 0) IS DISTINCT FROM coalesce(ac.u, 0) + OR coalesce(va.total_votes, 0) IS DISTINCT FROM coalesce(ac.t, 0); +$$; + +-- 5. Privileges: functions are internal/operator-only; aggregates are read-only to API roles. +REVOKE ALL ON FUNCTION public.recompute_vote_aggregate(uuid) FROM PUBLIC, anon, authenticated; +REVOKE ALL ON FUNCTION public.apply_vote_aggregate() FROM PUBLIC, anon, authenticated; +REVOKE ALL ON FUNCTION public.verify_vote_aggregates() FROM PUBLIC, anon, authenticated; +GRANT EXECUTE ON FUNCTION public.verify_vote_aggregates() TO service_role; + +REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON public.vote_aggregates FROM anon, authenticated; +GRANT SELECT ON public.vote_aggregates TO anon, authenticated; + +-- 6. One-time backfill so pre-existing aggregates match the votes table. +DO $$ +DECLARE b uuid; +BEGIN + FOR b IN SELECT DISTINCT sv.bill_id FROM public.shadow_votes sv + JOIN public.bills bl ON bl.id = sv.bill_id ORDER BY 1 LOOP + PERFORM pg_advisory_xact_lock(hashtext(b::text)); + PERFORM public.recompute_vote_aggregate(b); + END LOOP; +END $$; diff --git a/supabase/test/auth-shim.sql b/supabase/test/auth-shim.sql new file mode 100644 index 0000000..105130a --- /dev/null +++ b/supabase/test/auth-shim.sql @@ -0,0 +1,24 @@ +-- Minimal Supabase auth shim for the disposable test cluster (scripts/test-db.sh). +-- NOT a migration; never applied to a real Supabase project. +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'anon') THEN CREATE ROLE anon NOLOGIN NOINHERIT; END IF; + IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'authenticated') THEN CREATE ROLE authenticated NOLOGIN NOINHERIT; END IF; + IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'service_role') THEN CREATE ROLE service_role NOLOGIN NOINHERIT BYPASSRLS; END IF; +END $$; + +CREATE SCHEMA IF NOT EXISTS auth; +CREATE TABLE IF NOT EXISTS auth.users (id uuid PRIMARY KEY); + +CREATE OR REPLACE FUNCTION auth.uid() RETURNS uuid +LANGUAGE sql STABLE AS $$ + SELECT nullif(current_setting('request.jwt.claims', true)::json->>'sub', '')::uuid +$$; + +GRANT USAGE ON SCHEMA auth TO anon, authenticated, service_role; +GRANT EXECUTE ON FUNCTION auth.uid() TO anon, authenticated, service_role; +GRANT USAGE ON SCHEMA public TO anon, authenticated, service_role; + +-- Mirror Supabase's default grants: API roles get table privileges, RLS decides rows. +ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO anon, authenticated, service_role; +ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO anon, authenticated, service_role; +ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO anon, authenticated, service_role; diff --git a/test-db/psql.ts b/test-db/psql.ts new file mode 100644 index 0000000..4075b6f --- /dev/null +++ b/test-db/psql.ts @@ -0,0 +1,48 @@ +import { spawn } from 'node:child_process'; +import { randomUUID } from 'node:crypto'; + +const URL = process.env.TEST_DATABASE_URL; + +/** Run SQL in a fresh psql process (one connection). Resolves stdout rows. */ +export function sql(text: string): Promise { + if (!URL) throw new Error('TEST_DATABASE_URL is not set; run via `npm run test:db`'); + return new Promise((resolve, reject) => { + const p = spawn('psql', [URL, '-X', '-q', '-A', '-t', '-v', 'ON_ERROR_STOP=1', '-f', '-']); + let out = ''; + let err = ''; + p.stdout.on('data', (d) => (out += d)); + p.stderr.on('data', (d) => (err += d)); + p.on('error', reject); + p.on('close', (code) => { + if (code === 0) resolve(out.split('\n').filter((l) => l.length > 0)); + else reject(new Error(err.trim() || `psql exited ${code}`)); + }); + p.stdin.end(text); + }); +} + +/** SQL prefix that makes the session act as an authenticated Supabase user. */ +export function asUser(userId: string): string { + return `SET ROLE authenticated; SELECT set_config('request.jwt.claims', '{"sub":"${userId}"}', false) \\g /dev/null\n`; +} + +export const asAnon = `SET ROLE anon; SELECT set_config('request.jwt.claims', '{}', false) \\g /dev/null\n`; + +export async function makeUsers(n: number): Promise { + const ids = Array.from({ length: n }, () => randomUUID()); + await sql(`INSERT INTO auth.users(id) VALUES ${ids.map((i) => `('${i}')`).join(',')};`); + return ids; +} + +export async function makeBill(): Promise { + const id = randomUUID(); + await sql( + `INSERT INTO bills(id, external_id, bill_number, title, source_url) VALUES ('${id}', '${id}', 'HB-1', 'Test bill', 'https://example.test');`, + ); + return id; +} + +export function upsertVote(billId: string, vote: string): string { + return `INSERT INTO shadow_votes(bill_id, user_id, vote) VALUES ('${billId}', auth.uid(), '${vote}') +ON CONFLICT (bill_id, user_id) DO UPDATE SET vote = EXCLUDED.vote;\n`; +} diff --git a/test-db/vote-integrity.test.ts b/test-db/vote-integrity.test.ts new file mode 100644 index 0000000..9870389 --- /dev/null +++ b/test-db/vote-integrity.test.ts @@ -0,0 +1,131 @@ +import { describe, it, expect } from 'vitest'; +import { sql, asUser, asAnon, makeUsers, makeBill, upsertVote } from './psql'; + +const CHOICES = ['for', 'against', 'unsure'] as const; +const pick = () => CHOICES[Math.floor(Math.random() * CHOICES.length)]; + +async function aggregate(billId: string) { + const [row] = await sql( + `SELECT count_for, count_against, count_unsure, total_votes FROM vote_aggregates WHERE bill_id = '${billId}';`, + ); + if (!row) return null; + const [f, a, u, t] = row.split('|').map(Number); + return { for: f, against: a, unsure: u, total: t }; +} + +async function drift(): Promise { + return sql('SELECT * FROM verify_vote_aggregates();'); +} + +describe('shadow_votes row level security', () => { + it("user B cannot select, update or delete user A's vote", async () => { + const [a, b] = await makeUsers(2); + const bill = await makeBill(); + await sql(asUser(a) + upsertVote(bill, 'for')); + + const seen = await sql(asUser(b) + `SELECT count(*) FROM shadow_votes WHERE bill_id = '${bill}';`); + expect(seen).toEqual(['0']); + + const upd = await sql( + asUser(b) + `WITH u AS (UPDATE shadow_votes SET vote = 'against' WHERE bill_id = '${bill}' RETURNING 1) SELECT count(*) FROM u;`, + ); + expect(upd).toEqual(['0']); + const del = await sql( + asUser(b) + `WITH d AS (DELETE FROM shadow_votes WHERE bill_id = '${bill}' RETURNING 1) SELECT count(*) FROM d;`, + ); + expect(del).toEqual(['0']); + + // B cannot insert a vote attributed to A either. + await expect( + sql(asUser(b) + `INSERT INTO shadow_votes(bill_id, user_id, vote) VALUES ('${bill}', '${a}', 'against');`), + ).rejects.toThrow(/row-level security/); + + expect(await sql(`SELECT vote FROM shadow_votes WHERE bill_id = '${bill}';`)).toEqual(['for']); + }); + + it('a user can update and delete their own vote', async () => { + const [a] = await makeUsers(1); + const bill = await makeBill(); + await sql(asUser(a) + upsertVote(bill, 'for')); + expect(await sql(asUser(a) + `UPDATE shadow_votes SET vote='unsure' WHERE bill_id='${bill}' RETURNING vote;`)).toEqual([ + 'unsure', + ]); + expect(await sql(asUser(a) + `DELETE FROM shadow_votes WHERE bill_id='${bill}' RETURNING vote;`)).toEqual(['unsure']); + }); + + it('anon cannot insert a vote', async () => { + const [a] = await makeUsers(1); + const bill = await makeBill(); + await expect( + sql(asAnon + `INSERT INTO shadow_votes(bill_id, user_id, vote) VALUES ('${bill}', '${a}', 'for');`), + ).rejects.toThrow(/row-level security|permission denied/); + }); + + it('API roles cannot write vote_aggregates directly', async () => { + const [a] = await makeUsers(1); + const bill = await makeBill(); + await sql(asUser(a) + upsertVote(bill, 'for')); + await expect( + sql(asUser(a) + `UPDATE vote_aggregates SET count_for = 999 WHERE bill_id = '${bill}';`), + ).rejects.toThrow(/permission denied/); + await expect( + sql(asAnon + `INSERT INTO vote_aggregates(bill_id, count_for) VALUES ('${bill}', 5);`), + ).rejects.toThrow(/permission denied/); + // Still publicly readable. + expect(await sql(asAnon + `SELECT count_for FROM vote_aggregates WHERE bill_id = '${bill}';`)).toEqual(['1']); + }); +}); + +describe('vote aggregate integrity under concurrency', () => { + it('50 concurrent upserts by one user on one bill leave exactly one row', async () => { + const [a] = await makeUsers(1); + const bill = await makeBill(); + await Promise.all(Array.from({ length: 50 }, () => sql(asUser(a) + upsertVote(bill, pick())))); + expect(await sql(`SELECT count(*) FROM shadow_votes WHERE bill_id = '${bill}';`)).toEqual(['1']); + expect((await aggregate(bill))?.total).toBe(1); + expect(await drift()).toEqual([]); + }); + + it('20 concurrent voters then 10 concurrent changes keep aggregates exact', async () => { + const users = await makeUsers(20); + const bill = await makeBill(); + await Promise.all(users.map((u) => sql(asUser(u) + upsertVote(bill, pick())))); + await Promise.all( + users.slice(0, 10).map((u) => + sql(asUser(u) + `UPDATE shadow_votes SET vote = '${pick()}' WHERE bill_id = '${bill}';`), + ), + ); + expect(await drift()).toEqual([]); + const [rows] = await sql(`SELECT count(*) FROM shadow_votes WHERE bill_id = '${bill}';`); + const agg = await aggregate(bill); + expect(agg?.total).toBe(Number(rows)); + expect(agg?.total).toBe(20); + expect(agg!.for + agg!.against + agg!.unsure).toBe(20); + }); + + it('changing a vote moves one count between buckets; deleting decrements', async () => { + const [a, b] = await makeUsers(2); + const bill = await makeBill(); + await sql(asUser(a) + upsertVote(bill, 'for')); + await sql(asUser(b) + upsertVote(bill, 'for')); + expect(await aggregate(bill)).toEqual({ for: 2, against: 0, unsure: 0, total: 2 }); + + await sql(asUser(a) + upsertVote(bill, 'against')); + expect(await aggregate(bill)).toEqual({ for: 1, against: 1, unsure: 0, total: 2 }); + + await sql(asUser(b) + `DELETE FROM shadow_votes WHERE bill_id = '${bill}';`); + expect(await aggregate(bill)).toEqual({ for: 0, against: 1, unsure: 0, total: 1 }); + expect(await drift()).toEqual([]); + }); + + it('verify_vote_aggregates reports a tampered aggregate', async () => { + const [a] = await makeUsers(1); + const bill = await makeBill(); + await sql(asUser(a) + upsertVote(bill, 'for')); + await sql(`UPDATE vote_aggregates SET count_for = 7 WHERE bill_id = '${bill}';`); + const rows = await drift(); + expect(rows.some((r) => r.startsWith(bill))).toBe(true); + // repair for subsequent tests + await sql(`UPDATE vote_aggregates SET count_for = 1 WHERE bill_id = '${bill}';`); + }); +}); diff --git a/vitest.db.config.ts b/vitest.db.config.ts new file mode 100644 index 0000000..77422a3 --- /dev/null +++ b/vitest.db.config.ts @@ -0,0 +1,13 @@ +import { defineConfig } from 'vitest/config'; + +// DB integrity tests; run only via `npm run test:db` (scripts/test-db.sh), +// which provides TEST_DATABASE_URL for a disposable cluster. +export default defineConfig({ + test: { + environment: 'node', + include: ['test-db/**/*.test.ts'], + testTimeout: 60_000, + hookTimeout: 60_000, + fileParallelism: false, + }, +}); From 64f94ef4bac07ed4ce03695d713940312e81a638 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:18:26 -0800 Subject: [PATCH 07/16] fix(civic): public bill URLs, fetch timeouts, shared getLegislature Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- src/app/api/legislature/route.ts | 62 +----------------------- src/app/legislature/page.tsx | 25 +++------- src/lib/civic/congress.test.ts | 29 +++++++++++ src/lib/civic/congress.ts | 33 +++++++++---- src/lib/civic/http.ts | 7 +++ src/lib/civic/legislature.test.ts | 35 ++++++++++++++ src/lib/civic/legislature.ts | 80 +++++++++++++++++++++++++++++++ src/lib/civic/openstates.test.ts | 14 ++++++ src/lib/civic/openstates.ts | 4 +- 9 files changed, 200 insertions(+), 89 deletions(-) create mode 100644 src/lib/civic/http.ts create mode 100644 src/lib/civic/legislature.test.ts create mode 100644 src/lib/civic/legislature.ts diff --git a/src/app/api/legislature/route.ts b/src/app/api/legislature/route.ts index e1adeee..12b2f59 100644 --- a/src/app/api/legislature/route.ts +++ b/src/app/api/legislature/route.ts @@ -1,67 +1,9 @@ import { NextRequest, NextResponse } from 'next/server'; -import { congressAdapter } from '@/lib/civic/congress'; -import type { CivicBill, SourceResult } from '@/lib/civic/types'; +import { getLegislature } from '@/lib/civic/legislature'; export const runtime = 'nodejs'; -const LIMIT = 10; - -async function fetchUK(now: Date): Promise> { - const source = { name: 'UK Parliament Bills API', url: 'https://bills.parliament.uk' }; - const fail = (error: string): SourceResult => ({ - status: 'unavailable', items: [], fetchedAt: null, source, error, - }); - try { - const res = await fetch( - `https://bills-api.parliament.uk/api/v1/Bills?CurrentHouse=All&SortBy=DateUpdatedDesc&Take=${LIMIT}`, - { next: { revalidate: 600 } }, - ); - if (!res.ok) return fail(`HTTP ${res.status}`); - const data = await res.json(); - if (!Array.isArray(data.items)) return fail('response missing items'); - const items: CivicBill[] = data.items.map((b: Record) => ({ - externalId: `uk-${b.billId}`, - jurisdictionCode: 'UK', - number: `Bill ${b.billId}`, - title: (b.shortTitle as string) || (b.longTitle as string) || 'Untitled', - status: (b.currentHouse as string) || 'Unknown', - introducedDate: null, - sourceUrl: `https://bills.parliament.uk/bills/${b.billId}`, - recordHash: '', - })); - return { status: 'live', items, fetchedAt: now.toISOString(), source }; - } catch { - return fail('fetch failed'); - } -} - export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); - const country = (searchParams.get('country') || 'US').toUpperCase(); - const now = new Date(); - - let result: SourceResult; - if (country === 'US') { - result = await congressAdapter({ apiKey: process.env.CONGRESS_API_KEY }).fetchBills({ limit: LIMIT }); - } else if (country === 'UK') { - result = await fetchUK(now); - } else { - result = { - status: 'not_integrated', items: [], fetchedAt: null, - source: { name: 'Not integrated', url: '' }, - }; - } - - return NextResponse.json({ - status: result.status, - source: result.source, - fetchedAt: result.fetchedAt, - error: result.error, - items: result.items, - // Legacy key kept for existing consumers; same array as `items`. - bills: result.items, - total: result.items.length, - country, - timestamp: now.toISOString(), - }); + return NextResponse.json(await getLegislature(searchParams.get('country') || 'US')); } diff --git a/src/app/legislature/page.tsx b/src/app/legislature/page.tsx index c87aa51..2ee1d73 100644 --- a/src/app/legislature/page.tsx +++ b/src/app/legislature/page.tsx @@ -1,4 +1,6 @@ -import { headers } from 'next/headers'; +import { getLegislature } from '@/lib/civic/legislature'; + +export const dynamic = 'force-dynamic'; interface LegislatureResponse { status: 'live' | 'stale' | 'unavailable' | 'not_integrated'; @@ -11,28 +13,15 @@ const LABEL: Record = { live: 'Live', stale: 'Stale', unavailable: 'Unavailable', not_integrated: 'Not integrated', }; -async function load(): Promise { - try { - const h = await headers(); - const host = h.get('host'); - if (!host) return null; - const proto = h.get('x-forwarded-proto') ?? 'http'; - const res = await fetch(`${proto}://${host}/api/legislature?country=US`, { cache: 'no-store' }); - return res.ok ? ((await res.json()) as LegislatureResponse) : null; - } catch { - return null; - } -} - export default async function LegislaturePage() { - const data = await load(); - const status = data?.status ?? 'unavailable'; + const data: LegislatureResponse = await getLegislature('US'); + const status = data.status; return (

Legislature

Status: {LABEL[status]}

- {data?.fetchedAt &&

Fetched: {data.fetchedAt} from {data.source.name}

} - {data && data.items.length > 0 ? ( + {data.fetchedAt &&

Fetched: {data.fetchedAt} from {data.source.name}

} + {data.items.length > 0 ? (
    {data.items.map((b) => (
  • diff --git a/src/lib/civic/congress.test.ts b/src/lib/civic/congress.test.ts index c09451e..59e2b83 100644 --- a/src/lib/civic/congress.test.ts +++ b/src/lib/civic/congress.test.ts @@ -52,6 +52,35 @@ describe('congressAdapter', () => { }); }); +describe('congressAdapter hardening', () => { + it('builds the public congress.gov URL and never leaks api_key', async () => { + const r = await mk(ok([bill('123'), { ...bill('9'), type: 'SJRES' }]) as unknown as typeof fetch).fetchBills({ limit: 5 }); + expect(r.items[0].sourceUrl).toBe('https://www.congress.gov/bill/119th-congress/house-bill/123'); + expect(r.items[1].sourceUrl).toBe('https://www.congress.gov/bill/119th-congress/senate-joint-resolution/9'); + expect(JSON.stringify(r)).not.toContain('api_key'); + }); + it('skips rows with missing fields or unknown type and counts them', async () => { + const rows = [bill('1'), { ...bill('2'), type: 'XYZ' }, { ...bill('3'), number: undefined }, { ...bill('4'), congress: undefined }]; + const r = await mk(ok(rows) as unknown as typeof fetch).fetchBills({ limit: 5 }); + expect(r.items).toHaveLength(1); + expect(r.error).toContain('3'); + }); + it('sends key by header and passes a signal', async () => { + const f = ok([bill('1')]); + await mk(f as unknown as typeof fetch).fetchBills({ limit: 5 }); + const [url, init] = (f.mock.calls[0] as unknown as [string, RequestInit]); + expect(url).not.toContain('api_key'); + expect((init.headers as Record)['X-Api-Key']).toBe('k'); + expect(init.signal).toBeDefined(); + }); + it('times out a never-resolving fetch as unavailable', async () => { + const f = ((_u: string, init: RequestInit) => + new Promise((_res, rej) => init.signal?.addEventListener('abort', () => rej(new Error('aborted'))))) as unknown as typeof fetch; + const r = await congressAdapter({ apiKey: 'k', fetch: f, now: () => NOW, timeoutMs: 20 }).fetchBills({ limit: 5 }); + expect(r.status).toBe('unavailable'); + }); +}); + describe('classifyFreshness', () => { it('null is unavailable', () => expect(classifyFreshness(null, NOW, 1000)).toBe('unavailable')); it('old is stale', () => expect(classifyFreshness('2026-11-03T11:00:00.000Z', NOW, 1000)).toBe('stale')); diff --git a/src/lib/civic/congress.ts b/src/lib/civic/congress.ts index 05aba0d..76bf578 100644 --- a/src/lib/civic/congress.ts +++ b/src/lib/civic/congress.ts @@ -1,4 +1,5 @@ import { recordHash } from './hash'; +import { withTimeout } from './http'; import type { CivicBill, SourceAdapter, SourceResult } from './types'; const BASE = 'https://api.congress.gov/v3'; @@ -8,8 +9,15 @@ interface Deps { apiKey?: string; fetch?: typeof fetch; now?: () => Date; + timeoutMs?: number; } +const TYPE_SLUG: Record = { + HR: 'house-bill', S: 'senate-bill', HJRES: 'house-joint-resolution', SJRES: 'senate-joint-resolution', + HCONRES: 'house-concurrent-resolution', SCONRES: 'senate-concurrent-resolution', + HRES: 'house-resolution', SRES: 'senate-resolution', +}; + const str = (v: unknown): string => (typeof v === 'string' ? v : ''); export function congressAdapter(d: Deps): SourceAdapter { @@ -27,34 +35,39 @@ export function congressAdapter(d: Deps): SourceAdapter { try { const res = await doFetch( `${BASE}/bill?format=json&limit=${limit}&sort=updateDate+desc`, - { signal, headers: { 'X-Api-Key': d.apiKey } }, + { signal: withTimeout(signal, d.timeoutMs), headers: { 'X-Api-Key': d.apiKey } }, ); if (!res.ok) return fail(`Congress.gov HTTP ${res.status}`); const data = (await res.json()) as { bills?: unknown }; if (!Array.isArray(data.bills)) return fail('Congress.gov response missing bills'); - const items: CivicBill[] = data.bills.map((raw) => { + const items: CivicBill[] = []; + let skipped = 0; + for (const raw of data.bills) { const b = raw as Record; - const type = str(b.type); - const number = str(b.number) || String(b.number ?? ''); - const congress = String(b.congress ?? ''); + const type = str(b.type).toUpperCase(); + const number = b.number == null ? '' : String(b.number); + const congress = b.congress == null ? '' : String(b.congress); + const slug = TYPE_SLUG[type]; + if (!type || !number || !/^\d+$/.test(congress) || !slug) { skipped++; continue; } const latest = (b.latestAction ?? {}) as Record; const externalId = `us-${congress}-${type.toLowerCase()}-${number}`; - return { + items.push({ externalId, jurisdictionCode: 'US', number: `${type}.${number}`, title: str(b.title) || 'Untitled', status: str(latest.text) || 'Unknown', introducedDate: str(b.introducedDate) || null, - sourceUrl: str(b.url) || `${SOURCE.url}/bill/${congress}th-congress/${type.toLowerCase()}/${number}`, + sourceUrl: `${SOURCE.url}/bill/${congress}th-congress/${slug}/${number}`, recordHash: recordHash({ externalId, updateDate: str(b.updateDate), latestActionText: str(latest.text), }), - }; - }); - return { status: 'live', items, fetchedAt: now().toISOString(), source: SOURCE }; + }); + } + return { status: 'live', items, fetchedAt: now().toISOString(), source: SOURCE, + ...(skipped ? { error: `skipped ${skipped} malformed rows` } : {}) }; } catch (e) { return fail(e instanceof Error ? e.message : 'fetch failed'); } diff --git a/src/lib/civic/http.ts b/src/lib/civic/http.ts new file mode 100644 index 0000000..3d52d83 --- /dev/null +++ b/src/lib/civic/http.ts @@ -0,0 +1,7 @@ +export const DEFAULT_TIMEOUT_MS = 8000; + +/** Combine an optional caller signal with a timeout signal. */ +export function withTimeout(signal: AbortSignal | undefined, timeoutMs = DEFAULT_TIMEOUT_MS): AbortSignal { + const t = AbortSignal.timeout(timeoutMs); + return signal ? AbortSignal.any([signal, t]) : t; +} diff --git a/src/lib/civic/legislature.test.ts b/src/lib/civic/legislature.test.ts new file mode 100644 index 0000000..f5ad116 --- /dev/null +++ b/src/lib/civic/legislature.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it, vi } from 'vitest'; +import { getLegislature } from './legislature'; + +const NOW = new Date('2026-11-03T12:00:00.000Z'); +const resp = (body: unknown, status = 200) => vi.fn(async () => new Response(JSON.stringify(body), { status })) as unknown as typeof fetch; + +describe('getLegislature', () => { + it('EU is not_integrated', async () => { + const r = await getLegislature('eu', { now: () => NOW }); + expect(r.status).toBe('not_integrated'); + expect(r.items).toEqual([]); + expect(r.country).toBe('EU'); + }); + it('US without key is unavailable', async () => { + const r = await getLegislature('US', { apiKey: undefined, now: () => NOW }); + expect(r.status).toBe('unavailable'); + expect(r.bills).toEqual([]); + }); + it('US live via injected fetch', async () => { + const f = resp({ bills: [{ congress: 119, type: 'S', number: '5', title: 'T', updateDate: '2026-11-01', latestAction: { text: 'x' } }] }); + const r = await getLegislature('US', { apiKey: 'k', fetch: f, now: () => NOW }); + expect(r.status).toBe('live'); + expect(r.items[0].sourceUrl).toBe('https://www.congress.gov/bill/119th-congress/senate-bill/5'); + }); + it('UK upstream error is unavailable, not live', async () => { + const r = await getLegislature('UK', { fetch: resp({}, 500), now: () => NOW }); + expect(r.status).toBe('unavailable'); + }); + it('UK success hashes items', async () => { + const f = resp({ items: [{ billId: 1, shortTitle: 'A', currentHouse: 'Commons', lastUpdate: '2026-11-01' }] }); + const r = await getLegislature('UK', { fetch: f, now: () => NOW }); + expect(r.status).toBe('live'); + expect(r.items[0].recordHash).toMatch(/^[0-9a-f]{64}$/); + }); +}); diff --git a/src/lib/civic/legislature.ts b/src/lib/civic/legislature.ts new file mode 100644 index 0000000..4acffe0 --- /dev/null +++ b/src/lib/civic/legislature.ts @@ -0,0 +1,80 @@ +import { congressAdapter } from './congress'; +import { recordHash } from './hash'; +import { withTimeout } from './http'; +import type { CivicBill, SourceResult } from './types'; + +export interface LegislatureDeps { + apiKey?: string; + fetch?: typeof fetch; + now?: () => Date; + timeoutMs?: number; +} + +const LIMIT = 10; + +async function fetchUK(d: LegislatureDeps, now: Date): Promise> { + const source = { name: 'UK Parliament Bills API', url: 'https://bills.parliament.uk' }; + const fail = (error: string): SourceResult => ({ + status: 'unavailable', items: [], fetchedAt: null, source, error, + }); + try { + const res = await (d.fetch ?? fetch)( + `https://bills-api.parliament.uk/api/v1/Bills?CurrentHouse=All&SortBy=DateUpdatedDesc&Take=${LIMIT}`, + { signal: withTimeout(undefined, d.timeoutMs), next: { revalidate: 600 } } as RequestInit, + ); + if (!res.ok) return fail(`HTTP ${res.status}`); + const data = await res.json(); + if (!Array.isArray(data.items)) return fail('response missing items'); + const items: CivicBill[] = data.items.map((b: Record) => { + const externalId = `uk-${b.billId}`; + return { + externalId, + jurisdictionCode: 'UK', + number: `Bill ${b.billId}`, + title: (b.shortTitle as string) || (b.longTitle as string) || 'Untitled', + status: (b.currentHouse as string) || 'Unknown', + introducedDate: null, + sourceUrl: `https://bills.parliament.uk/bills/${b.billId}`, + recordHash: recordHash({ + externalId, + updateDate: String(b.lastUpdate ?? ''), + latestActionText: String(b.currentHouse ?? ''), + }), + }; + }); + return { status: 'live', items, fetchedAt: now.toISOString(), source }; + } catch { + return fail('fetch failed'); + } +} + +/** Shared by the API route and the page; no loopback HTTP. */ +export async function getLegislature(countryParam: string, d: LegislatureDeps = {}) { + const country = (countryParam || 'US').toUpperCase(); + const now = d.now ? d.now() : new Date(); + const hasKey = 'apiKey' in d; + + let result: SourceResult; + if (country === 'US') { + result = await congressAdapter({ + apiKey: hasKey ? d.apiKey : process.env.CONGRESS_API_KEY, + fetch: d.fetch, now: d.now, timeoutMs: d.timeoutMs, + }).fetchBills({ limit: LIMIT }); + } else if (country === 'UK') { + result = await fetchUK(d, now); + } else { + result = { status: 'not_integrated', items: [], fetchedAt: null, source: { name: 'Not integrated', url: '' } }; + } + + return { + status: result.status, + source: result.source, + fetchedAt: result.fetchedAt, + error: result.error, + items: result.items, + bills: result.items, + total: result.items.length, + country, + timestamp: now.toISOString(), + }; +} diff --git a/src/lib/civic/openstates.test.ts b/src/lib/civic/openstates.test.ts index deebf1d..68a2405 100644 --- a/src/lib/civic/openstates.test.ts +++ b/src/lib/civic/openstates.test.ts @@ -36,3 +36,17 @@ describe('openStatesAdapter (mocked)', () => { expect(await h()).not.toBe(await h('Passed')); }); }); + +describe('openStatesAdapter hardening', () => { + it('sends header key, no key in URL, and times out', async () => { + const f = ok([row('1')]); + await mk(f).fetchBills({ limit: 5 }); + const [url, init] = (f as unknown as { mock: { calls: [string, RequestInit][] } }).mock.calls[0]; + expect(url).not.toContain('apikey'); + expect((init.headers as Record)['X-API-KEY']).toBe('k'); + const hang = ((_u: string, i: RequestInit) => + new Promise((_r, rej) => i.signal?.addEventListener('abort', () => rej(new Error('aborted'))))) as unknown as typeof fetch; + const r = await openStatesAdapter({ apiKey: 'k', fetch: hang, now: () => NOW, timeoutMs: 20 }).fetchBills({ limit: 5 }); + expect(r.status).toBe('unavailable'); + }); +}); diff --git a/src/lib/civic/openstates.ts b/src/lib/civic/openstates.ts index b326e7c..5aa2161 100644 --- a/src/lib/civic/openstates.ts +++ b/src/lib/civic/openstates.ts @@ -1,4 +1,5 @@ import { recordHash } from './hash'; +import { withTimeout } from './http'; import type { CivicBill, SourceAdapter, SourceResult } from './types'; const SOURCE = { name: 'Open States (California)', url: 'https://openstates.org/ca/bills/' }; @@ -7,6 +8,7 @@ interface Deps { apiKey?: string; fetch?: typeof fetch; now?: () => Date; + timeoutMs?: number; } const str = (v: unknown): string => (typeof v === 'string' ? v : ''); @@ -27,7 +29,7 @@ export function openStatesAdapter(d: Deps): SourceAdapter { try { const res = await doFetch( `https://v3.openstates.org/bills?jurisdiction=California&sort=updated_desc&per_page=${Math.min(limit, 20)}`, - { signal, headers: { 'X-API-KEY': d.apiKey } }, + { signal: withTimeout(signal, d.timeoutMs), headers: { 'X-API-KEY': d.apiKey } }, ); if (!res.ok) return fail(`Open States HTTP ${res.status}`); const data = (await res.json()) as { results?: unknown }; From b288fc8736d3fe128c30ad9295126c9dec3043e0 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:18:42 -0800 Subject: [PATCH 08/16] feat(api): authenticate and bound /api/propose Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- src/app/api/propose/route.test.ts | 126 ++++++++++++++++++ src/app/api/propose/route.ts | 103 +++++++++----- .../20261104000300_voting_proposals.sql | 43 ++++++ test-db/voting-proposals.test.ts | 55 ++++++++ 4 files changed, 292 insertions(+), 35 deletions(-) create mode 100644 src/app/api/propose/route.test.ts create mode 100644 supabase/migrations/20261104000300_voting_proposals.sql create mode 100644 test-db/voting-proposals.test.ts diff --git a/src/app/api/propose/route.test.ts b/src/app/api/propose/route.test.ts new file mode 100644 index 0000000..b328874 --- /dev/null +++ b/src/app/api/propose/route.test.ts @@ -0,0 +1,126 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; + +const requireUser = vi.fn(); +vi.mock('@/lib/supabaseAuth/requireUser', () => ({ requireUser })); + +const single = vi.fn(); +const limit = vi.fn(); +const insert = vi.fn(); +const chain: Record = {}; +chain.insert = insert.mockReturnValue(chain); +chain.select = vi.fn(() => chain); +chain.eq = vi.fn(() => chain); +chain.order = vi.fn(() => chain); +chain.single = single; +chain.limit = limit; +const from = vi.fn(() => chain); +const createClient = vi.fn(() => ({ from })); +vi.mock('@supabase/supabase-js', () => ({ createClient })); + +const USER = '11111111-2222-4333-8444-555555555555'; + +function post(body: unknown) { + return new Request('http://localhost/api/propose', { + method: 'POST', + headers: { 'content-type': 'application/json', authorization: 'Bearer tok' }, + body: typeof body === 'string' ? body : JSON.stringify(body), + }); +} + +async function load() { + vi.resetModules(); + return import('./route'); +} + +describe('/api/propose', () => { + beforeEach(() => { + vi.unstubAllEnvs(); + requireUser.mockReset().mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); + insert.mockClear(); + single.mockReset().mockResolvedValue({ data: { id: 'p1' }, error: null }); + limit.mockReset().mockResolvedValue({ data: [], error: null }); + from.mockClear(); + createClient.mockClear(); + vi.stubEnv('NEXT_PUBLIC_SUPABASE_URL', 'https://example.supabase.co'); + vi.stubEnv('NEXT_PUBLIC_SUPABASE_ANON_KEY', 'anon-key'); + vi.stubEnv('SUPABASE_SERVICE_ROLE_KEY', 'service-key'); + }); + + it('POST without auth returns 401 and touches no DB', async () => { + requireUser.mockResolvedValue({ error: Response.json({ message: 'Unauthorized' }, { status: 401 }) }); + const { POST } = await load(); + const res = await POST(post({ title: 'x' }) as never); + expect(res.status).toBe(401); + expect(insert).not.toHaveBeenCalled(); + }); + + it('rejects empty, blank, non-string and over-200-char titles with 400', async () => { + const { POST } = await load(); + for (const title of ['', ' ', 123, 'a'.repeat(201), undefined]) { + const res = await POST(post({ title }) as never); + expect(res.status).toBe(400); + } + expect((await POST(post('{bad') as never)).status).toBe(400); + expect(insert).not.toHaveBeenCalled(); + }); + + it('rejects a sourceUrl that is not http(s)', async () => { + const { POST } = await load(); + for (const sourceUrl of ['javascript:alert(1)', 'ftp://x.test/a', 'not a url', 'data:text/html,hi']) { + const res = await POST(post({ title: 'T', sourceUrl }) as never); + expect(res.status).toBe(400); + } + expect(insert).not.toHaveBeenCalled(); + }); + + it('maps a unique violation on source_url to 409', async () => { + single.mockResolvedValue({ data: null, error: { code: '23505', message: 'duplicate key' } }); + const { POST } = await load(); + const res = await POST(post({ title: 'T', sourceUrl: 'https://example.test/a' }) as never); + expect(res.status).toBe(409); + }); + + it('inserts proposer_id from the session and never votes_for/votes_against from the body', async () => { + const { POST } = await load(); + const res = await POST( + post({ + title: ' Hello ', + sourceUrl: 'https://example.test/a', + proposer_id: 'someone-else', + votes_for: 999, + votes_against: 5, + }) as never, + ); + expect(res.status).toBe(201); + const row = insert.mock.calls[0][0]; + expect(row.proposer_id).toBe(USER); + expect(row.title).toBe('Hello'); + expect(row).not.toHaveProperty('votes_for'); + expect(row).not.toHaveProperty('votes_against'); + const [, key, opts] = createClient.mock.calls.at(-1) as unknown as [string, string, { global: { headers: Record } }]; + expect(key).toBe('anon-key'); + expect(opts.global.headers.Authorization).toBe('Bearer tok'); + }); + + it('returns a generic 500 without leaking DB error text', async () => { + single.mockResolvedValue({ data: null, error: { code: 'XX000', message: 'secret_table exploded' } }); + const spy = vi.spyOn(console, 'error').mockImplementation(() => {}); + const { POST } = await load(); + const res = await POST(post({ title: 'T' }) as never); + expect(res.status).toBe(500); + expect(await res.text()).not.toContain('secret_table'); + spy.mockRestore(); + }); + + it('GET clamps limit=100000 to 50 and falls back to 20 for limit=abc', async () => { + const { GET } = await load(); + await GET(new Request('http://localhost/api/propose?limit=100000') as never); + expect(limit).toHaveBeenLastCalledWith(50); + await GET(new Request('http://localhost/api/propose?limit=abc') as never); + expect(limit).toHaveBeenLastCalledWith(20); + await GET(new Request('http://localhost/api/propose?limit=0') as never); + expect(limit).toHaveBeenLastCalledWith(20); + await GET(new Request('http://localhost/api/propose?limit=7') as never); + expect(limit).toHaveBeenLastCalledWith(7); + }); +}); diff --git a/src/app/api/propose/route.ts b/src/app/api/propose/route.ts index 658f0c2..e80af11 100644 --- a/src/app/api/propose/route.ts +++ b/src/app/api/propose/route.ts @@ -1,69 +1,102 @@ import { NextRequest, NextResponse } from 'next/server'; import { supabase } from '@/lib/supabase'; +import { requireUser } from '@/lib/supabaseAuth/requireUser'; +import { createUserScopedClient } from '@/lib/supabaseAuth/userClient'; + +const MAX_TITLE = 200; +const MAX_DESCRIPTION = 2000; +const MAX_URL = 2048; +const MAX_SHORT = 50; +const DEFAULT_LIMIT = 20; +const MAX_LIMIT = 50; + +function optionalString(v: unknown, max: number): string | null | undefined { + if (v === undefined || v === null || v === '') return null; + if (typeof v !== 'string' || v.length > max) return undefined; + return v.trim(); +} + +function isHttpUrl(v: string): boolean { + try { + const u = new URL(v); + return u.protocol === 'http:' || u.protocol === 'https:'; + } catch { + return false; + } +} /** * POST /api/propose - * Creates a voting proposal from a news article. - * Stores in Supabase `voting_proposals` table. + * Creates a voting proposal. Requires a signed-in user; the proposer is always + * the session user and vote counts are never accepted from the client. */ export async function POST(req: NextRequest) { - try { - const body = await req.json(); - const { title, sourceUrl, bias, category, description } = body; + const auth = await requireUser(req); + if ('error' in auth) return auth.error; - if (!title) { - return NextResponse.json({ error: 'Title is required' }, { status: 400 }); - } + let body: unknown; + try { + body = await req.json(); + } catch { + return NextResponse.json({ error: 'Invalid request body' }, { status: 400 }); + } + const b = (body && typeof body === 'object' ? body : {}) as Record; - // Check for duplicate proposals (same source URL) - if (sourceUrl) { - const { data: existing } = await supabase - .from('voting_proposals') - .select('id') - .eq('source_url', sourceUrl) - .single(); + const title = typeof b.title === 'string' ? b.title.trim() : ''; + if (!title || title.length > MAX_TITLE) { + return NextResponse.json({ error: `Title is required (1-${MAX_TITLE} characters)` }, { status: 400 }); + } - if (existing) { - return NextResponse.json({ error: 'This article has already been proposed', id: existing.id }, { status: 409 }); - } - } + const sourceUrl = optionalString(b.sourceUrl, MAX_URL); + if (sourceUrl === undefined || (sourceUrl !== null && !isHttpUrl(sourceUrl))) { + return NextResponse.json({ error: 'sourceUrl must be an http(s) URL' }, { status: 400 }); + } + const description = optionalString(b.description, MAX_DESCRIPTION); + const bias = optionalString(b.bias, MAX_SHORT); + const category = optionalString(b.category, MAX_SHORT); + if (description === undefined || bias === undefined || category === undefined) { + return NextResponse.json({ error: 'Invalid field length or type' }, { status: 400 }); + } - const { data, error } = await supabase + try { + const client = createUserScopedClient(auth.accessToken); + const { data, error } = await client .from('voting_proposals') .insert({ title, - description: description || '', - source_url: sourceUrl || '', - bias_score: bias || 'center', - category: category || 'General', + description: description ?? '', + source_url: sourceUrl, + bias_score: bias ?? 'center', + category: category ?? 'General', status: 'active', - votes_for: 0, - votes_against: 0, - created_at: new Date().toISOString(), + proposer_id: auth.user.id, }) .select() .single(); if (error) { - console.error('Supabase insert error:', error); + if ((error as { code?: string }).code === '23505') { + return NextResponse.json({ error: 'This article has already been proposed' }, { status: 409 }); + } + console.error('propose insert failed', { code: (error as { code?: string }).code }); return NextResponse.json({ error: 'Failed to create proposal' }, { status: 500 }); } - return NextResponse.json({ success: true, proposal: data }, { status: 201 }); - } catch (err) { - console.error('Propose error:', err); - return NextResponse.json({ error: 'Invalid request body' }, { status: 400 }); + } catch { + console.error('propose handler failed'); + return NextResponse.json({ error: 'Failed to create proposal' }, { status: 500 }); } } /** * GET /api/propose - * Lists all active voting proposals. + * Lists voting proposals (public read). limit defaults to 20, max 50. */ export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); const status = searchParams.get('status') || 'active'; - const limit = parseInt(searchParams.get('limit') || '20'); + const parsed = parseInt(searchParams.get('limit') || '', 10); + const limit = Number.isFinite(parsed) && parsed > 0 ? Math.min(parsed, MAX_LIMIT) : DEFAULT_LIMIT; const { data, error } = await supabase .from('voting_proposals') @@ -73,7 +106,7 @@ export async function GET(req: NextRequest) { .limit(limit); if (error) { - console.error('Supabase fetch error:', error); + console.error('propose fetch failed', { code: (error as { code?: string }).code }); return NextResponse.json({ proposals: [], error: 'Failed to fetch proposals' }, { status: 500 }); } diff --git a/supabase/migrations/20261104000300_voting_proposals.sql b/supabase/migrations/20261104000300_voting_proposals.sql new file mode 100644 index 0000000..ee39a71 --- /dev/null +++ b/supabase/migrations/20261104000300_voting_proposals.sql @@ -0,0 +1,43 @@ +-- voting_proposals: community-submitted proposals (POST /api/propose). +-- Written defensively: the remote table may already exist with a different +-- shape (NOT_RUN: remote shape unverified). Create if missing, then add the +-- columns/constraints this API relies on without dropping anything. + +CREATE TABLE IF NOT EXISTS public.voting_proposals ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + title text NOT NULL, + description text, + source_url text UNIQUE, + bias_score text, + category text, + status text DEFAULT 'active', + proposer_id uuid REFERENCES auth.users(id), + created_at timestamptz NOT NULL DEFAULT now() +); + +-- If the table pre-existed, make sure the proposer column is present. +ALTER TABLE public.voting_proposals + ADD COLUMN IF NOT EXISTS proposer_id uuid REFERENCES auth.users(id); + +-- If the table pre-existed without a unique source_url, add one (empty strings +-- from the old route are normalised to NULL first so they cannot collide). +UPDATE public.voting_proposals SET source_url = NULL WHERE source_url = ''; +CREATE UNIQUE INDEX IF NOT EXISTS voting_proposals_source_url_key + ON public.voting_proposals (source_url); + +ALTER TABLE public.voting_proposals ENABLE ROW LEVEL SECURITY; + +DROP POLICY IF EXISTS "Voting proposals are publicly readable" ON public.voting_proposals; +CREATE POLICY "Voting proposals are publicly readable" ON public.voting_proposals + FOR SELECT USING (true); + +DROP POLICY IF EXISTS "Users can propose as themselves" ON public.voting_proposals; +CREATE POLICY "Users can propose as themselves" ON public.voting_proposals + FOR INSERT TO authenticated + WITH CHECK (proposer_id = auth.uid()); + +-- Writes are insert-only for API roles; no client update/delete. +REVOKE UPDATE, DELETE, TRUNCATE ON public.voting_proposals FROM anon, authenticated; +REVOKE INSERT ON public.voting_proposals FROM anon; +GRANT SELECT ON public.voting_proposals TO anon, authenticated; +GRANT INSERT ON public.voting_proposals TO authenticated; diff --git a/test-db/voting-proposals.test.ts b/test-db/voting-proposals.test.ts new file mode 100644 index 0000000..4794ce7 --- /dev/null +++ b/test-db/voting-proposals.test.ts @@ -0,0 +1,55 @@ +import { describe, it, expect } from 'vitest'; +import { randomUUID } from 'node:crypto'; +import { sql, asUser, asAnon, makeUsers } from './psql'; + +const insertAs = (uid: string, url: string | null) => + `INSERT INTO voting_proposals(title, source_url, proposer_id) VALUES ('T', ${url ? `'${url}'` : 'NULL'}, '${uid}');`; + +describe('voting_proposals row level security', () => { + it('anon can select', async () => { + const [a] = await makeUsers(1); + const url = `https://example.test/${randomUUID()}`; + await sql(asUser(a) + insertAs(a, url)); + expect(await sql(asAnon + `SELECT count(*) FROM voting_proposals WHERE source_url = '${url}';`)).toEqual(['1']); + }); + + it('anon cannot insert', async () => { + const [a] = await makeUsers(1); + await expect(sql(asAnon + insertAs(a, `https://example.test/${randomUUID()}`))).rejects.toThrow( + /row-level security|permission denied/, + ); + }); + + it('authenticated user can insert with proposer_id = own uid', async () => { + const [a] = await makeUsers(1); + const url = `https://example.test/${randomUUID()}`; + await sql(asUser(a) + insertAs(a, url)); + expect(await sql(`SELECT proposer_id FROM voting_proposals WHERE source_url = '${url}';`)).toEqual([a]); + }); + + it('authenticated user cannot insert as another user', async () => { + const [a, b] = await makeUsers(2); + await expect(sql(asUser(b) + insertAs(a, `https://example.test/${randomUUID()}`))).rejects.toThrow( + /row-level security/, + ); + }); + + it('duplicate source_url is rejected', async () => { + const [a, b] = await makeUsers(2); + const url = `https://example.test/${randomUUID()}`; + await sql(asUser(a) + insertAs(a, url)); + await expect(sql(asUser(b) + insertAs(b, url))).rejects.toThrow(/duplicate key|unique/); + }); + + it('users cannot update or delete proposals', async () => { + const [a] = await makeUsers(1); + const url = `https://example.test/${randomUUID()}`; + await sql(asUser(a) + insertAs(a, url)); + await expect(sql(asUser(a) + `UPDATE voting_proposals SET title='x' WHERE source_url='${url}';`)).rejects.toThrow( + /permission denied/, + ); + await expect(sql(asUser(a) + `DELETE FROM voting_proposals WHERE source_url='${url}';`)).rejects.toThrow( + /permission denied/, + ); + }); +}); From 8aabb3b56d294007243b480f68c2d86fcee17349 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:22:28 -0800 Subject: [PATCH 09/16] feat(auth): server-verified wallet linking Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- src/app/api/wallet/challenge/route.test.ts | 51 ++++++ src/app/api/wallet/challenge/route.ts | 36 ++++ src/app/api/wallet/link/route.test.ts | 84 +++++++++ src/app/api/wallet/link/route.ts | 39 +++++ src/lib/wallet/store.ts | 61 +++++++ src/lib/wallet/walletLink.test.ts | 118 +++++++++++++ src/lib/wallet/walletLink.ts | 161 ++++++++++++++++++ .../20261104000100_wallet_links.sql | 150 ++++++++++++++++ test-db/wallet-links.test.ts | 93 ++++++++++ 9 files changed, 793 insertions(+) create mode 100644 src/app/api/wallet/challenge/route.test.ts create mode 100644 src/app/api/wallet/challenge/route.ts create mode 100644 src/app/api/wallet/link/route.test.ts create mode 100644 src/app/api/wallet/link/route.ts create mode 100644 src/lib/wallet/store.ts create mode 100644 src/lib/wallet/walletLink.test.ts create mode 100644 src/lib/wallet/walletLink.ts create mode 100644 supabase/migrations/20261104000100_wallet_links.sql create mode 100644 test-db/wallet-links.test.ts diff --git a/src/app/api/wallet/challenge/route.test.ts b/src/app/api/wallet/challenge/route.test.ts new file mode 100644 index 0000000..e138453 --- /dev/null +++ b/src/app/api/wallet/challenge/route.test.ts @@ -0,0 +1,51 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; + +const requireUser = vi.fn(); +vi.mock('@/lib/supabaseAuth/requireUser', () => ({ requireUser })); +const rpc = vi.fn(); +vi.mock('@/lib/supabaseAuth/userClient', () => ({ createUserScopedClient: vi.fn(() => ({ rpc })) })); + +const USER = '11111111-2222-4333-8444-555555555555'; +const ADDR = '0x52908400098527886E0F7030069857D2E4169EE7'; + +function req(body: unknown) { + return new Request('https://utp.test/api/wallet/challenge', { + method: 'POST', + headers: { 'content-type': 'application/json', authorization: 'Bearer tok' }, + body: JSON.stringify(body), + }); +} + +describe('POST /api/wallet/challenge', () => { + beforeEach(() => { + requireUser.mockReset().mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); + rpc.mockReset().mockResolvedValue({ data: null, error: null }); + }); + + it('passes through 401', async () => { + requireUser.mockResolvedValue({ error: Response.json({ message: 'Unauthorized' }, { status: 401 }) }); + const { POST } = await import('./route'); + expect((await POST(req({ address: ADDR, chainId: 31 }))).status).toBe(401); + }); + + it('returns an ERC-4361 message bound to the request host, ignoring client domain', async () => { + const { POST } = await import('./route'); + const res = await POST(req({ address: ADDR, chainId: 31, domain: 'evil.test' })); + expect(res.status).toBe(200); + const json = await res.json(); + expect(json.message).toMatch(/^utp\.test wants you to sign in/); + expect(json.message).toContain('URI: https://utp.test'); + expect(json.message).toContain('Chain ID: 31'); + expect(rpc).toHaveBeenCalledWith('create_wallet_challenge', expect.objectContaining({ + p_address: ADDR.toLowerCase(), p_chain_id: 31, p_domain: 'utp.test', p_nonce: json.nonce, + })); + expect(JSON.stringify(rpc.mock.calls)).not.toContain(USER); + }); + + it('rejects wrong chain and invalid address with 400', async () => { + const { POST } = await import('./route'); + expect((await POST(req({ address: ADDR, chainId: 1 }))).status).toBe(400); + expect((await POST(req({ address: '0x123', chainId: 31 }))).status).toBe(400); + expect(rpc).not.toHaveBeenCalled(); + }); +}); diff --git a/src/app/api/wallet/challenge/route.ts b/src/app/api/wallet/challenge/route.ts new file mode 100644 index 0000000..9550589 --- /dev/null +++ b/src/app/api/wallet/challenge/route.ts @@ -0,0 +1,36 @@ +import { NextResponse } from 'next/server'; +import { requireUser } from '@/lib/supabaseAuth/requireUser'; +import { createSupabaseWalletLinkStore } from '@/lib/wallet/store'; +import { createWalletChallenge, normalizeAddress, ALLOWED_LINK_CHAINS } from '@/lib/wallet/walletLink'; + +export async function POST(req: Request) { + const auth = await requireUser(req); + if ('error' in auth) return auth.error; + + let body: unknown; + try { + body = await req.json(); + } catch { + return NextResponse.json({ message: 'Invalid JSON' }, { status: 400 }); + } + const { address, chainId } = (body ?? {}) as { address?: unknown; chainId?: unknown }; + if (typeof address !== 'string') return NextResponse.json({ message: 'Invalid address' }, { status: 400 }); + try { + normalizeAddress(address); + } catch { + return NextResponse.json({ message: 'Invalid address' }, { status: 400 }); + } + if (typeof chainId !== 'number' || !(ALLOWED_LINK_CHAINS as readonly number[]).includes(chainId)) { + return NextResponse.json({ message: 'Unsupported chain' }, { status: 400 }); + } + + // Domain and URI come from the request URL, never from the client body. + const origin = new URL(req.url).origin; + try { + const store = createSupabaseWalletLinkStore(auth.accessToken, auth.user.id); + const c = await createWalletChallenge({ userId: auth.user.id, address, chainId, origin }, store); + return NextResponse.json(c); + } catch { + return NextResponse.json({ message: 'Could not create challenge' }, { status: 500 }); + } +} diff --git a/src/app/api/wallet/link/route.test.ts b/src/app/api/wallet/link/route.test.ts new file mode 100644 index 0000000..df857e7 --- /dev/null +++ b/src/app/api/wallet/link/route.test.ts @@ -0,0 +1,84 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; +import { createSiweMessage } from 'viem/siwe'; + +const requireUser = vi.fn(); +vi.mock('@/lib/supabaseAuth/requireUser', () => ({ requireUser })); +const rpc = vi.fn(); +vi.mock('@/lib/supabaseAuth/userClient', () => ({ createUserScopedClient: vi.fn(() => ({ rpc })) })); + +const USER = '11111111-2222-4333-8444-555555555555'; +const NONCE = 'abcdef0123456789'; + +function req(body: unknown, url = 'https://utp.test/api/wallet/link') { + return new Request(url, { + method: 'POST', + headers: { 'content-type': 'application/json', authorization: 'Bearer tok' }, + body: JSON.stringify(body), + }); +} + +async function signed() { + const acct = privateKeyToAccount(generatePrivateKey()); + const message = createSiweMessage({ + domain: 'utp.test', uri: 'https://utp.test', address: acct.address, chainId: 31, nonce: NONCE, version: '1', + }); + return { acct, message, signature: await acct.signMessage({ message }) }; +} + +function stubRpc(address: string, activate: string) { + rpc.mockImplementation(async (fn: string) => { + if (fn === 'consume_wallet_challenge') + return { data: [{ nonce: NONCE, user_id: USER, chain_id: 31, address: address.toLowerCase(), domain: 'utp.test', expires_at: new Date(Date.now() + 60_000).toISOString() }], error: null }; + if (fn === 'activate_wallet_link') return { data: activate, error: null }; + if (fn === 'wallet_challenge_status') return { data: 'consumed', error: null }; + return { data: null, error: null }; + }); +} + +describe('POST /api/wallet/link', () => { + beforeEach(() => { + requireUser.mockReset().mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); + rpc.mockReset(); + }); + + it('passes through 401', async () => { + requireUser.mockResolvedValue({ error: Response.json({ message: 'Unauthorized' }, { status: 401 }) }); + const { POST } = await import('./route'); + expect((await POST(req({}))).status).toBe(401); + }); + + it('links on a valid signature', async () => { + const s = await signed(); + stubRpc(s.acct.address, 'ok'); + const { POST } = await import('./route'); + const res = await POST(req({ message: s.message, signature: s.signature })); + expect(res.status).toBe(200); + expect(await res.json()).toEqual({ ok: true }); + expect(rpc).toHaveBeenCalledWith('activate_wallet_link', { p_chain_id: 31, p_address: s.acct.address.toLowerCase(), p_assurance: 'aal1' }); + }); + + it('409 for address_taken without internals', async () => { + const s = await signed(); + stubRpc(s.acct.address, 'address_taken'); + const { POST } = await import('./route'); + const res = await POST(req({ message: s.message, signature: s.signature })); + expect(res.status).toBe(409); + expect(await res.json()).toEqual({ ok: false }); + }); + + it('400 when the request host differs from the signed domain', async () => { + const s = await signed(); + stubRpc(s.acct.address, 'ok'); + const { POST } = await import('./route'); + const res = await POST(req({ message: s.message, signature: s.signature }, 'https://other.test/api/wallet/link')); + expect(res.status).toBe(400); + expect(await res.json()).toEqual({ ok: false }); + expect(rpc).not.toHaveBeenCalled(); + }); + + it('400 on malformed body', async () => { + const { POST } = await import('./route'); + expect((await POST(req({ message: 1, signature: 'zz' }))).status).toBe(400); + }); +}); diff --git a/src/app/api/wallet/link/route.ts b/src/app/api/wallet/link/route.ts new file mode 100644 index 0000000..7e742ef --- /dev/null +++ b/src/app/api/wallet/link/route.ts @@ -0,0 +1,39 @@ +import { NextResponse } from 'next/server'; +import { requireUser } from '@/lib/supabaseAuth/requireUser'; +import { createSupabaseWalletLinkStore } from '@/lib/wallet/store'; +import { verifyWalletLink } from '@/lib/wallet/walletLink'; + +export async function POST(req: Request) { + const auth = await requireUser(req); + if ('error' in auth) return auth.error; + + let body: unknown; + try { + body = await req.json(); + } catch { + return NextResponse.json({ ok: false }, { status: 400 }); + } + const { message, signature } = (body ?? {}) as { message?: unknown; signature?: unknown }; + if ( + typeof message !== 'string' || + message.length > 4096 || + typeof signature !== 'string' || + !/^0x[0-9a-fA-F]+$/.test(signature) + ) { + return NextResponse.json({ ok: false }, { status: 400 }); + } + + const origin = new URL(req.url).origin; + try { + const store = createSupabaseWalletLinkStore(auth.accessToken, auth.user.id); + const r = await verifyWalletLink( + { userId: auth.user.id, message, signature: signature as `0x${string}`, origin }, + store, + ); + if (r.ok) return NextResponse.json({ ok: true }); + // Do not echo internal reasons beyond the conflict case. + return NextResponse.json({ ok: false }, { status: r.reason === 'address_taken' ? 409 : 400 }); + } catch { + return NextResponse.json({ ok: false }, { status: 500 }); + } +} diff --git a/src/lib/wallet/store.ts b/src/lib/wallet/store.ts new file mode 100644 index 0000000..50b4ea6 --- /dev/null +++ b/src/lib/wallet/store.ts @@ -0,0 +1,61 @@ +import type { SupabaseClient } from '@supabase/supabase-js'; +import { createUserScopedClient } from '@/lib/supabaseAuth/userClient'; +import type { Challenge, ChallengeStatus, WalletLinkStore } from './walletLink'; + +/** + * Supabase-backed WalletLinkStore acting as the calling user (anon key + + * access token; never the service-role key). Every write goes through a + * SECURITY DEFINER function that derives the user from auth.uid(); the + * userId arguments of the interface are cross-checked here and otherwise + * ignored, so a caller cannot act for someone else. `now` is ignored too: + * expiry is judged by the database clock. + */ +export function createSupabaseWalletLinkStore(accessToken: string, expectedUserId: string): WalletLinkStore { + const db: SupabaseClient = createUserScopedClient(accessToken); + const check = (userId: string) => { + if (userId !== expectedUserId) throw new Error('wallet store user mismatch'); + }; + return { + async putChallenge(c: Challenge) { + check(c.userId); + const { error } = await db.rpc('create_wallet_challenge', { + p_nonce: c.nonce, + p_chain_id: c.chainId, + p_address: c.address, + p_domain: c.domain, + }); + if (error) throw new Error('could not store challenge'); + }, + async consumeChallenge(nonce, userId) { + check(userId); + const { data, error } = await db.rpc('consume_wallet_challenge', { p_nonce: nonce }); + if (error) throw new Error('could not consume challenge'); + const row = Array.isArray(data) ? data[0] : null; + if (!row || row.user_id !== expectedUserId) return null; + return { + nonce: row.nonce, + userId: row.user_id, + chainId: row.chain_id, + address: row.address, + domain: row.domain, + expiresAt: new Date(row.expires_at), + }; + }, + async challengeStatus(nonce, userId) { + check(userId); + const { data, error } = await db.rpc('wallet_challenge_status', { p_nonce: nonce }); + if (error) throw new Error('could not read challenge status'); + return data as ChallengeStatus; + }, + async activateLink(l) { + check(l.userId); + const { data, error } = await db.rpc('activate_wallet_link', { + p_chain_id: l.chainId, + p_address: l.address, + p_assurance: l.assurance, + }); + if (error) throw new Error('could not activate link'); + return data === 'ok' ? 'ok' : 'address_taken'; + }, + }; +} diff --git a/src/lib/wallet/walletLink.test.ts b/src/lib/wallet/walletLink.test.ts new file mode 100644 index 0000000..20aef5d --- /dev/null +++ b/src/lib/wallet/walletLink.test.ts @@ -0,0 +1,118 @@ +import { describe, it, expect } from 'vitest'; +import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; +import { createSiweMessage, generateSiweNonce } from 'viem/siwe'; +import { + createWalletChallenge, + verifyWalletLink, + normalizeAddress, + createMemoryWalletLinkStore, +} from './walletLink'; + +const ORIGIN = 'https://utp.test'; +const A = '11111111-1111-4111-8111-111111111111'; +const B = '22222222-2222-4222-8222-222222222222'; +const T0 = new Date('2026-11-04T12:00:00Z'); + +function wallet() { + return privateKeyToAccount(generatePrivateKey()); +} + +async function challengeAndSign(store = createMemoryWalletLinkStore(), userId = A, acct = wallet(), address?: string) { + const c = await createWalletChallenge({ userId, address: address ?? acct.address, chainId: 31, origin: ORIGIN, now: T0 }, store); + const signature = await acct.signMessage({ message: c.message }); + return { store, acct, c, signature }; +} + +describe('normalizeAddress', () => { + it('lowercases valid addresses and rejects invalid', () => { + const a = wallet().address; + expect(normalizeAddress(a)).toBe(a.toLowerCase()); + expect(normalizeAddress(a.toLowerCase())).toBe(a.toLowerCase()); + expect(() => normalizeAddress('0x123')).toThrow(); + expect(() => normalizeAddress('not-an-address')).toThrow(); + }); +}); + +describe('wallet link challenge/verify', () => { + it('happy path links', async () => { + const { store, c, signature } = await challengeAndSign(); + expect(c.expiresAt).toBe(new Date(T0.getTime() + 10 * 60_000).toISOString()); + const r = await verifyWalletLink({ userId: A, message: c.message, signature, origin: ORIGIN, now: T0 }, store); + expect(r).toEqual({ ok: true }); + }); + + it('replayed nonce -> replayed', async () => { + const { store, c, signature } = await challengeAndSign(); + await verifyWalletLink({ userId: A, message: c.message, signature, origin: ORIGIN, now: T0 }, store); + const r = await verifyWalletLink({ userId: A, message: c.message, signature, origin: ORIGIN, now: T0 }, store); + expect(r).toEqual({ ok: false, reason: 'replayed' }); + }); + + it('nonce issued to A used by B -> wrong_user', async () => { + const { store, c, signature } = await challengeAndSign(); + const r = await verifyWalletLink({ userId: B, message: c.message, signature, origin: ORIGIN, now: T0 }, store); + expect(r).toEqual({ ok: false, reason: 'wrong_user' }); + // A can still use it: B's attempt did not burn the nonce. + const ok = await verifyWalletLink({ userId: A, message: c.message, signature, origin: ORIGIN, now: T0 }, store); + expect(ok).toEqual({ ok: true }); + }); + + it('signature by a different key -> bad_signature', async () => { + const { store, c } = await challengeAndSign(); + const signature = await wallet().signMessage({ message: c.message }); + const r = await verifyWalletLink({ userId: A, message: c.message, signature, origin: ORIGIN, now: T0 }, store); + expect(r).toEqual({ ok: false, reason: 'bad_signature' }); + }); + + it('unknown nonce -> unknown_nonce', async () => { + const store = createMemoryWalletLinkStore(); + const acct = wallet(); + const message = createSiweMessage({ + domain: 'utp.test', uri: ORIGIN, address: acct.address, chainId: 31, nonce: generateSiweNonce(), version: '1', issuedAt: T0, + }); + const signature = await acct.signMessage({ message }); + const r = await verifyWalletLink({ userId: A, message, signature, origin: ORIGIN, now: T0 }, store); + expect(r).toEqual({ ok: false, reason: 'unknown_nonce' }); + }); + + it('domain evil.test vs origin utp.test -> wrong_origin', async () => { + const store = createMemoryWalletLinkStore(); + const acct = wallet(); + const c = await createWalletChallenge({ userId: A, address: acct.address, chainId: 31, origin: 'https://evil.test', now: T0 }, store); + const signature = await acct.signMessage({ message: c.message }); + const r = await verifyWalletLink({ userId: A, message: c.message, signature, origin: ORIGIN, now: T0 }, store); + expect(r).toEqual({ ok: false, reason: 'wrong_origin' }); + }); + + it('chain 1 -> wrong_chain at challenge time and at verify time', async () => { + const store = createMemoryWalletLinkStore(); + const acct = wallet(); + await expect( + createWalletChallenge({ userId: A, address: acct.address, chainId: 1, origin: ORIGIN, now: T0 }, store), + ).rejects.toThrow(/chain/); + const c = await createWalletChallenge({ userId: A, address: acct.address, chainId: 31, origin: ORIGIN, now: T0 }, store); + const nonce = /Nonce: (\S+)/.exec(c.message)![1]; + const message = createSiweMessage({ + domain: 'utp.test', uri: ORIGIN, address: acct.address, chainId: 1, nonce, version: '1', issuedAt: T0, + }); + const signature = await acct.signMessage({ message }); + const r = await verifyWalletLink({ userId: A, message, signature, origin: ORIGIN, now: T0 }, store); + expect(r).toEqual({ ok: false, reason: 'wrong_chain' }); + }); + + it('expired by 11 minutes -> expired', async () => { + const { store, c, signature } = await challengeAndSign(); + const later = new Date(T0.getTime() + 11 * 60_000); + const r = await verifyWalletLink({ userId: A, message: c.message, signature, origin: ORIGIN, now: later }, store); + expect(r).toEqual({ ok: false, reason: 'expired' }); + }); + + it('mixed-case and lowercase forms map to one link -> address_taken', async () => { + const store = createMemoryWalletLinkStore(); + const acct = wallet(); + const first = await challengeAndSign(store, A, acct, acct.address); + expect(await verifyWalletLink({ userId: A, message: first.c.message, signature: first.signature, origin: ORIGIN, now: T0 }, store)).toEqual({ ok: true }); + const second = await challengeAndSign(store, B, acct, acct.address.toLowerCase()); + expect(await verifyWalletLink({ userId: B, message: second.c.message, signature: second.signature, origin: ORIGIN, now: T0 }, store)).toEqual({ ok: false, reason: 'address_taken' }); + }); +}); diff --git a/src/lib/wallet/walletLink.ts b/src/lib/wallet/walletLink.ts new file mode 100644 index 0000000..c88bba0 --- /dev/null +++ b/src/lib/wallet/walletLink.ts @@ -0,0 +1,161 @@ +import { getAddress, isAddress, verifyMessage } from 'viem'; +import { createSiweMessage, generateSiweNonce, parseSiweMessage } from 'viem/siwe'; + +export const ALLOWED_LINK_CHAINS = [31] as const; +export const CHALLENGE_TTL_MS = 10 * 60_000; + +export type Challenge = { + nonce: string; + userId: string; + chainId: number; + address: `0x${string}`; // lowercase + domain: string; + expiresAt: Date; +}; + +/** Why a nonce could not be consumed, from the caller's point of view. */ +export type ChallengeStatus = 'unknown' | 'expired' | 'consumed' | 'other_user' | 'available'; + +export interface WalletLinkStore { + putChallenge(c: Challenge): Promise; + consumeChallenge(nonce: string, userId: string, now: Date): Promise; // atomic, single use + activateLink(l: { userId: string; chainId: number; address: string; assurance: 'aal1' | 'aal2' }): Promise<'ok' | 'address_taken'>; + /** Diagnostic only, used after a failed consume to choose an error reason. */ + challengeStatus(nonce: string, userId: string, now: Date): Promise; +} + +export type VerifyFailure = + | 'unknown_nonce' | 'expired' | 'replayed' | 'wrong_origin' | 'wrong_chain' | 'wrong_user' | 'bad_signature' | 'address_taken'; + +export function normalizeAddress(a: string): `0x${string}` { + if (typeof a !== 'string' || !isAddress(a, { strict: false })) throw new Error('Invalid address'); + return a.toLowerCase() as `0x${string}`; +} + +function isAllowedChain(chainId: number): boolean { + return (ALLOWED_LINK_CHAINS as readonly number[]).includes(chainId); +} + +export async function createWalletChallenge( + i: { userId: string; address: string; chainId: number; origin: string; now?: Date }, + store: WalletLinkStore, +): Promise<{ message: string; nonce: string; expiresAt: string }> { + if (!isAllowedChain(i.chainId)) throw new Error('Unsupported chain'); + const address = normalizeAddress(i.address); + const origin = new URL(i.origin); + const now = i.now ?? new Date(); + const expiresAt = new Date(now.getTime() + CHALLENGE_TTL_MS); + const nonce = generateSiweNonce(); + const message = createSiweMessage({ + domain: origin.host, + uri: origin.origin, + address: getAddress(address), + chainId: i.chainId, + nonce, + version: '1', + issuedAt: now, + expirationTime: expiresAt, + statement: 'Link this wallet to your UTP account.', + }); + await store.putChallenge({ nonce, userId: i.userId, chainId: i.chainId, address, domain: origin.host, expiresAt }); + return { message, nonce, expiresAt: expiresAt.toISOString() }; +} + +export async function verifyWalletLink( + i: { userId: string; message: string; signature: `0x${string}`; origin: string; now?: Date }, + store: WalletLinkStore, +): Promise<{ ok: true } | { ok: false; reason: VerifyFailure }> { + const fail = (reason: VerifyFailure) => ({ ok: false as const, reason }); + const now = i.now ?? new Date(); + let expected: URL; + try { + expected = new URL(i.origin); + } catch { + return fail('wrong_origin'); + } + + let parsed: ReturnType; + try { + parsed = parseSiweMessage(i.message); + } catch { + return fail('unknown_nonce'); + } + if (!parsed.nonce || !parsed.address) return fail('unknown_nonce'); + if (parsed.domain !== expected.host) return fail('wrong_origin'); + if (parsed.uri !== undefined) { + try { + if (new URL(parsed.uri).origin !== expected.origin) return fail('wrong_origin'); + } catch { + return fail('wrong_origin'); + } + } else return fail('wrong_origin'); + if (parsed.chainId === undefined || !isAllowedChain(parsed.chainId)) return fail('wrong_chain'); + + let address: `0x${string}`; + try { + address = normalizeAddress(parsed.address); + } catch { + return fail('bad_signature'); + } + + // Check the signature before consuming, so a forged attempt cannot burn + // the legitimate user's nonce. + let validSig = false; + try { + validSig = await verifyMessage({ address: getAddress(address), message: i.message, signature: i.signature }); + } catch { + validSig = false; + } + if (!validSig) return fail('bad_signature'); + + const challenge = await store.consumeChallenge(parsed.nonce, i.userId, now); + if (!challenge) { + const status = await store.challengeStatus(parsed.nonce, i.userId, now); + if (status === 'other_user') return fail('wrong_user'); + if (status === 'expired') return fail('expired'); + if (status === 'consumed') return fail('replayed'); + return fail('unknown_nonce'); + } + // The signed message must match exactly what this server issued for the nonce. + if (challenge.domain !== expected.host) return fail('wrong_origin'); + if (challenge.chainId !== parsed.chainId) return fail('wrong_chain'); + if (challenge.address !== address) return fail('bad_signature'); + + const res = await store.activateLink({ userId: i.userId, chainId: challenge.chainId, address, assurance: 'aal1' }); + return res === 'ok' ? { ok: true } : fail('address_taken'); +} + +/** In-memory store for tests and local development. Not for production. */ +export function createMemoryWalletLinkStore(): WalletLinkStore { + const challenges = new Map(); + const links: { userId: string; chainId: number; address: string; assurance: string; status: 'active' | 'revoked' }[] = []; + return { + async putChallenge(c) { + if (challenges.has(c.nonce)) throw new Error('duplicate nonce'); + challenges.set(c.nonce, { ...c, consumedAt: null }); + }, + async consumeChallenge(nonce, userId, now) { + const c = challenges.get(nonce); + if (!c || c.userId !== userId || c.consumedAt || c.expiresAt.getTime() <= now.getTime()) return null; + c.consumedAt = now; + const { consumedAt: _ignored, ...rest } = c; + void _ignored; + return rest; + }, + async challengeStatus(nonce, userId, now) { + const c = challenges.get(nonce); + if (!c) return 'unknown'; + if (c.userId !== userId) return 'other_user'; + if (c.consumedAt) return 'consumed'; + if (c.expiresAt.getTime() <= now.getTime()) return 'expired'; + return 'available'; + }, + async activateLink(l) { + const address = normalizeAddress(l.address); + const existing = links.find((x) => x.status === 'active' && x.chainId === l.chainId && x.address === address); + if (existing) return existing.userId === l.userId ? 'ok' : 'address_taken'; + links.push({ userId: l.userId, chainId: l.chainId, address, assurance: l.assurance, status: 'active' }); + return 'ok'; + }, + }; +} diff --git a/supabase/migrations/20261104000100_wallet_links.sql b/supabase/migrations/20261104000100_wallet_links.sql new file mode 100644 index 0000000..8b8bc6c --- /dev/null +++ b/supabase/migrations/20261104000100_wallet_links.sql @@ -0,0 +1,150 @@ +-- Task 6: server-challenge wallet linking (ERC-4361). +-- Depends only on auth.users / auth.uid(); independent of 0200/0300. +-- All writes go through SECURITY DEFINER functions that take the user from +-- auth.uid(), never from a parameter. API roles get SELECT-own only. + +CREATE TABLE IF NOT EXISTS public.wallet_link_challenges ( + nonce text PRIMARY KEY CHECK (nonce ~ '^[A-Za-z0-9]{8,96}$'), + user_id uuid NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE, + chain_id integer NOT NULL CHECK (chain_id IN (31)), + address text NOT NULL CHECK (address ~ '^0x[0-9a-f]{40}$'), + domain text NOT NULL CHECK (length(domain) BETWEEN 1 AND 255), + created_at timestamptz NOT NULL DEFAULT now(), + expires_at timestamptz NOT NULL, + consumed_at timestamptz, + activated_at timestamptz +); +CREATE INDEX IF NOT EXISTS wallet_link_challenges_user_idx ON public.wallet_link_challenges(user_id); + +CREATE TABLE IF NOT EXISTS public.wallet_links ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + user_id uuid NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE, + chain_id integer NOT NULL CHECK (chain_id IN (31)), + address text NOT NULL CHECK (address ~ '^0x[0-9a-f]{40}$'), + assurance text NOT NULL CHECK (assurance IN ('aal1', 'aal2')), + status text NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'revoked')), + verified_at timestamptz NOT NULL DEFAULT now() +); +CREATE UNIQUE INDEX IF NOT EXISTS wallet_links_one_active_per_address + ON public.wallet_links(chain_id, address) WHERE status = 'active'; +CREATE INDEX IF NOT EXISTS wallet_links_user_idx ON public.wallet_links(user_id); + +ALTER TABLE public.wallet_link_challenges ENABLE ROW LEVEL SECURITY; +ALTER TABLE public.wallet_links ENABLE ROW LEVEL SECURITY; + +DROP POLICY IF EXISTS "wallet challenges select own" ON public.wallet_link_challenges; +CREATE POLICY "wallet challenges select own" ON public.wallet_link_challenges + FOR SELECT TO authenticated USING (auth.uid() = user_id); +DROP POLICY IF EXISTS "wallet links select own" ON public.wallet_links; +CREATE POLICY "wallet links select own" ON public.wallet_links + FOR SELECT TO authenticated USING (auth.uid() = user_id); + +REVOKE ALL ON public.wallet_link_challenges FROM PUBLIC, anon, authenticated; +REVOKE ALL ON public.wallet_links FROM PUBLIC, anon, authenticated; +GRANT SELECT ON public.wallet_link_challenges TO authenticated; +GRANT SELECT ON public.wallet_links TO authenticated; + +-- Issue a challenge for the calling user. Expiry is set here (10 minutes), +-- not by the caller. +CREATE OR REPLACE FUNCTION public.create_wallet_challenge( + p_nonce text, p_chain_id integer, p_address text, p_domain text +) RETURNS timestamptz +LANGUAGE plpgsql SECURITY DEFINER SET search_path = public, pg_temp AS $$ +DECLARE + v_uid uuid := auth.uid(); + v_expires timestamptz := now() + interval '10 minutes'; +BEGIN + IF v_uid IS NULL THEN RAISE EXCEPTION 'not authenticated' USING ERRCODE = '42501'; END IF; + INSERT INTO public.wallet_link_challenges(nonce, user_id, chain_id, address, domain, expires_at) + VALUES (p_nonce, v_uid, p_chain_id, p_address, p_domain, v_expires); + RETURN v_expires; +END $$; + +-- Atomic single use: the row-level UPDATE guard means only one caller wins. +CREATE OR REPLACE FUNCTION public.consume_wallet_challenge(p_nonce text) +RETURNS TABLE(nonce text, user_id uuid, chain_id integer, address text, domain text, expires_at timestamptz) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = public, pg_temp AS $$ +BEGIN + IF auth.uid() IS NULL THEN RAISE EXCEPTION 'not authenticated' USING ERRCODE = '42501'; END IF; + RETURN QUERY + UPDATE public.wallet_link_challenges c + SET consumed_at = now() + WHERE c.nonce = p_nonce + AND c.user_id = auth.uid() + AND c.consumed_at IS NULL + AND c.expires_at > now() + RETURNING c.nonce, c.user_id, c.chain_id, c.address, c.domain, c.expires_at; +END $$; + +-- Diagnostic status of a nonce from the caller's point of view. Reveals only +-- that a nonce belongs to someone else, never whose or which wallet. +CREATE OR REPLACE FUNCTION public.wallet_challenge_status(p_nonce text) +RETURNS text +LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = public, pg_temp AS $$ +DECLARE r public.wallet_link_challenges%ROWTYPE; +BEGIN + IF auth.uid() IS NULL THEN RAISE EXCEPTION 'not authenticated' USING ERRCODE = '42501'; END IF; + SELECT * INTO r FROM public.wallet_link_challenges WHERE nonce = p_nonce; + IF NOT FOUND THEN RETURN 'unknown'; END IF; + IF r.user_id <> auth.uid() THEN RETURN 'other_user'; END IF; + IF r.consumed_at IS NOT NULL THEN RETURN 'consumed'; END IF; + IF r.expires_at <= now() THEN RETURN 'expired'; END IF; + RETURN 'available'; +END $$; + +-- Activate a link for the calling user. Requires a challenge this user +-- consumed in the last 10 minutes for the same chain/address and not yet +-- used for activation. Assurance is capped by the session's aal claim. +CREATE OR REPLACE FUNCTION public.activate_wallet_link(p_chain_id integer, p_address text, p_assurance text) +RETURNS text +LANGUAGE plpgsql SECURITY DEFINER SET search_path = public, pg_temp AS $$ +DECLARE + v_uid uuid := auth.uid(); + v_nonce text; + v_claim_aal text := coalesce(nullif(current_setting('request.jwt.claims', true), '')::json->>'aal', 'aal1'); + v_assurance text; + v_owner uuid; +BEGIN + IF v_uid IS NULL THEN RAISE EXCEPTION 'not authenticated' USING ERRCODE = '42501'; END IF; + IF p_assurance NOT IN ('aal1', 'aal2') THEN RAISE EXCEPTION 'invalid assurance' USING ERRCODE = '22023'; END IF; + v_assurance := CASE WHEN p_assurance = 'aal2' AND v_claim_aal = 'aal2' THEN 'aal2' ELSE 'aal1' END; + + UPDATE public.wallet_link_challenges c SET activated_at = now() + WHERE c.nonce = ( + SELECT c2.nonce FROM public.wallet_link_challenges c2 + WHERE c2.user_id = v_uid AND c2.chain_id = p_chain_id AND c2.address = p_address + AND c2.consumed_at IS NOT NULL AND c2.activated_at IS NULL + AND c2.consumed_at > now() - interval '10 minutes' + ORDER BY c2.consumed_at DESC LIMIT 1 + FOR UPDATE SKIP LOCKED) + RETURNING c.nonce INTO v_nonce; + IF v_nonce IS NULL THEN RAISE EXCEPTION 'no verified challenge' USING ERRCODE = '42501'; END IF; + + SELECT user_id INTO v_owner FROM public.wallet_links + WHERE chain_id = p_chain_id AND address = p_address AND status = 'active'; + IF FOUND THEN + IF v_owner = v_uid THEN + UPDATE public.wallet_links SET verified_at = now(), assurance = v_assurance + WHERE chain_id = p_chain_id AND address = p_address AND status = 'active'; + RETURN 'ok'; + END IF; + RETURN 'address_taken'; + END IF; + + BEGIN + INSERT INTO public.wallet_links(user_id, chain_id, address, assurance) + VALUES (v_uid, p_chain_id, p_address, v_assurance); + EXCEPTION WHEN unique_violation THEN + RETURN 'address_taken'; + END; + RETURN 'ok'; +END $$; + +REVOKE ALL ON FUNCTION public.create_wallet_challenge(text, integer, text, text) FROM PUBLIC, anon; +REVOKE ALL ON FUNCTION public.consume_wallet_challenge(text) FROM PUBLIC, anon; +REVOKE ALL ON FUNCTION public.wallet_challenge_status(text) FROM PUBLIC, anon; +REVOKE ALL ON FUNCTION public.activate_wallet_link(integer, text, text) FROM PUBLIC, anon; +GRANT EXECUTE ON FUNCTION public.create_wallet_challenge(text, integer, text, text) TO authenticated; +GRANT EXECUTE ON FUNCTION public.consume_wallet_challenge(text) TO authenticated; +GRANT EXECUTE ON FUNCTION public.wallet_challenge_status(text) TO authenticated; +GRANT EXECUTE ON FUNCTION public.activate_wallet_link(integer, text, text) TO authenticated; diff --git a/test-db/wallet-links.test.ts b/test-db/wallet-links.test.ts new file mode 100644 index 0000000..9b44647 --- /dev/null +++ b/test-db/wallet-links.test.ts @@ -0,0 +1,93 @@ +import { describe, it, expect } from 'vitest'; +import { randomBytes } from 'node:crypto'; +import { sql, asUser, asAnon, makeUsers } from './psql'; + +const addr = () => '0x' + randomBytes(20).toString('hex'); +const nonce = () => randomBytes(12).toString('hex'); + +function createChallenge(n: string, a: string, chain = 31) { + return `SELECT public.create_wallet_challenge('${n}', ${chain}, '${a}', 'utp.test') \\g /dev/null\n`; +} + +describe('wallet link challenges', () => { + it('20 parallel consumes of one nonce by two users: exactly one non-null', async () => { + const [a, b] = await makeUsers(2); + const n = nonce(); + await sql(asUser(a) + createChallenge(n, addr())); + const results = await Promise.all( + Array.from({ length: 20 }, (_, i) => + sql(asUser(i % 2 === 0 ? a : b) + `SELECT count(*) FROM public.consume_wallet_challenge('${n}');`), + ), + ); + const winners = results.map((r) => Number(r[0])).reduce((x, y) => x + y, 0); + expect(winners).toBe(1); + expect(await sql(asUser(a) + `SELECT public.wallet_challenge_status('${n}');`)).toEqual(['consumed']); + expect(await sql(asUser(b) + `SELECT public.wallet_challenge_status('${n}');`)).toEqual(['other_user']); + }); + + it('expired challenges cannot be consumed', async () => { + const [a] = await makeUsers(1); + const n = nonce(); + await sql(asUser(a) + createChallenge(n, addr())); + await sql(`UPDATE wallet_link_challenges SET expires_at = now() - interval '1 minute' WHERE nonce = '${n}';`); + expect(await sql(asUser(a) + `SELECT count(*) FROM public.consume_wallet_challenge('${n}');`)).toEqual(['0']); + expect(await sql(asUser(a) + `SELECT public.wallet_challenge_status('${n}');`)).toEqual(['expired']); + }); + + it('rejects disallowed chains and non-normalized addresses', async () => { + const [a] = await makeUsers(1); + await expect(sql(asUser(a) + createChallenge(nonce(), addr(), 1))).rejects.toThrow(); + await expect(sql(asUser(a) + createChallenge(nonce(), '0xABC'))).rejects.toThrow(); + }); +}); + +describe('wallet links', () => { + async function prepared(user: string, a: string) { + const n = nonce(); + await sql(asUser(user) + createChallenge(n, a)); + await sql(asUser(user) + `SELECT count(*) FROM public.consume_wallet_challenge('${n}');`); + } + + it('two users racing activateLink for one (chain, address): one ok, one address_taken', async () => { + const [a, b] = await makeUsers(2); + const w = addr(); + await prepared(a, w); + await prepared(b, w); + const results = await Promise.all( + [a, b].map((u) => sql(asUser(u) + `SELECT public.activate_wallet_link(31, '${w}', 'aal1');`)), + ); + expect(results.map((r) => r[0]).sort()).toEqual(['address_taken', 'ok']); + expect(await sql(`SELECT count(*) FROM wallet_links WHERE address = '${w}' AND status = 'active';`)).toEqual(['1']); + }); + + it('activation without a consumed challenge for that address is refused', async () => { + const [a] = await makeUsers(1); + await expect(sql(asUser(a) + `SELECT public.activate_wallet_link(31, '${addr()}', 'aal1');`)).rejects.toThrow( + /no verified challenge/, + ); + }); + + it('assurance is capped by the session aal claim', async () => { + const [a] = await makeUsers(1); + const w = addr(); + await prepared(a, w); + await sql(asUser(a) + `SELECT public.activate_wallet_link(31, '${w}', 'aal2');`); + expect(await sql(`SELECT assurance FROM wallet_links WHERE address = '${w}';`)).toEqual(['aal1']); + }); + + it('RLS: users and anon cannot read others links; no direct writes', async () => { + const [a, b] = await makeUsers(2); + const w = addr(); + await prepared(a, w); + await sql(asUser(a) + `SELECT public.activate_wallet_link(31, '${w}', 'aal1');`); + expect(await sql(asUser(a) + `SELECT count(*) FROM wallet_links WHERE address = '${w}';`)).toEqual(['1']); + expect(await sql(asUser(b) + `SELECT count(*) FROM wallet_links WHERE address = '${w}';`)).toEqual(['0']); + await expect(sql(asAnon + `SELECT count(*) FROM wallet_links;`)).rejects.toThrow(/permission denied/); + await expect( + sql(asUser(b) + `INSERT INTO wallet_links(user_id, chain_id, address, assurance) VALUES ('${b}', 31, '${addr()}', 'aal1');`), + ).rejects.toThrow(/permission denied/); + await expect(sql(asUser(a) + `UPDATE wallet_links SET status = 'revoked';`)).rejects.toThrow(/permission denied/); + await expect(sql(asUser(b) + `SELECT count(*) FROM wallet_link_challenges;`)).resolves.toEqual(['0']); + await expect(sql(asAnon + `SELECT public.consume_wallet_challenge('x');`)).rejects.toThrow(/permission denied/); + }); +}); From 346828c518f334f3ec5df78bca6cce0df104c8a9 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:23:27 -0800 Subject: [PATCH 10/16] fix(api): harden voting_proposals migration over legacy tables; hide proposer_id on GET Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- src/app/api/propose/route.test.ts | 14 +++- src/app/api/propose/route.ts | 4 +- .../20261104000300_voting_proposals.sql | 42 +++++++++-- test-db/voting-proposals-legacy.test.ts | 72 +++++++++++++++++++ 4 files changed, 125 insertions(+), 7 deletions(-) create mode 100644 test-db/voting-proposals-legacy.test.ts diff --git a/src/app/api/propose/route.test.ts b/src/app/api/propose/route.test.ts index b328874..47c747e 100644 --- a/src/app/api/propose/route.test.ts +++ b/src/app/api/propose/route.test.ts @@ -8,7 +8,8 @@ const limit = vi.fn(); const insert = vi.fn(); const chain: Record = {}; chain.insert = insert.mockReturnValue(chain); -chain.select = vi.fn(() => chain); +const select = vi.fn((_cols?: string) => chain); +chain.select = select; chain.eq = vi.fn(() => chain); chain.order = vi.fn(() => chain); chain.single = single; @@ -37,6 +38,7 @@ describe('/api/propose', () => { vi.unstubAllEnvs(); requireUser.mockReset().mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); insert.mockClear(); + select.mockClear(); single.mockReset().mockResolvedValue({ data: { id: 'p1' }, error: null }); limit.mockReset().mockResolvedValue({ data: [], error: null }); from.mockClear(); @@ -123,4 +125,14 @@ describe('/api/propose', () => { await GET(new Request('http://localhost/api/propose?limit=7') as never); expect(limit).toHaveBeenLastCalledWith(7); }); + + it('GET selects an explicit column list that excludes proposer_id', async () => { + const { GET } = await load(); + await GET(new Request('http://localhost/api/propose') as never); + const cols = select.mock.calls.at(-1)?.[0] as string; + expect(cols).not.toBe('*'); + expect(cols).not.toContain('proposer_id'); + expect(cols).toContain('title'); + expect(cols).toContain('source_url'); + }); }); diff --git a/src/app/api/propose/route.ts b/src/app/api/propose/route.ts index e80af11..d74a5d8 100644 --- a/src/app/api/propose/route.ts +++ b/src/app/api/propose/route.ts @@ -7,6 +7,8 @@ const MAX_TITLE = 200; const MAX_DESCRIPTION = 2000; const MAX_URL = 2048; const MAX_SHORT = 50; +// Explicit list: proposer_id (an auth user UUID) is never exposed to anonymous callers. +const PUBLIC_COLUMNS = 'id,title,description,source_url,bias_score,category,status,created_at'; const DEFAULT_LIMIT = 20; const MAX_LIMIT = 50; @@ -100,7 +102,7 @@ export async function GET(req: NextRequest) { const { data, error } = await supabase .from('voting_proposals') - .select('*') + .select(PUBLIC_COLUMNS) .eq('status', status) .order('created_at', { ascending: false }) .limit(limit); diff --git a/supabase/migrations/20261104000300_voting_proposals.sql b/supabase/migrations/20261104000300_voting_proposals.sql index ee39a71..254d8d3 100644 --- a/supabase/migrations/20261104000300_voting_proposals.sql +++ b/supabase/migrations/20261104000300_voting_proposals.sql @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS public.voting_proposals ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), title text NOT NULL, description text, - source_url text UNIQUE, + source_url text, bias_score text, category text, status text DEFAULT 'active', @@ -19,19 +19,51 @@ CREATE TABLE IF NOT EXISTS public.voting_proposals ( ALTER TABLE public.voting_proposals ADD COLUMN IF NOT EXISTS proposer_id uuid REFERENCES auth.users(id); --- If the table pre-existed without a unique source_url, add one (empty strings --- from the old route are normalised to NULL first so they cannot collide). +-- Legacy-shape compatibility: the old route inserted votes_for/votes_against/ +-- created_at/status explicitly and source_url as ''. The new route sends none +-- of the vote columns, so give them defaults (only where the column exists). +DO $$ +DECLARE r record; +BEGIN + FOR r IN SELECT * FROM (VALUES + ('votes_for', '0'), ('votes_against', '0'), + ('created_at', 'now()'), ('status', '''active''') + ) AS d(col, def) LOOP + IF EXISTS (SELECT 1 FROM information_schema.columns + WHERE table_schema = 'public' AND table_name = 'voting_proposals' + AND column_name = r.col) THEN + EXECUTE format('ALTER TABLE public.voting_proposals ALTER COLUMN %I SET DEFAULT %s', r.col, r.def); + END IF; + END LOOP; + IF EXISTS (SELECT 1 FROM information_schema.columns + WHERE table_schema = 'public' AND table_name = 'voting_proposals' + AND column_name = 'source_url') THEN + ALTER TABLE public.voting_proposals ALTER COLUMN source_url DROP NOT NULL; + END IF; +END $$; + +-- Empty strings from the old route are normalised to NULL so they cannot +-- collide under the unique index. UPDATE public.voting_proposals SET source_url = NULL WHERE source_url = ''; CREATE UNIQUE INDEX IF NOT EXISTS voting_proposals_source_url_key ON public.voting_proposals (source_url); ALTER TABLE public.voting_proposals ENABLE ROW LEVEL SECURITY; -DROP POLICY IF EXISTS "Voting proposals are publicly readable" ON public.voting_proposals; +-- Drop EVERY existing policy first: a legacy permissive INSERT policy would +-- otherwise be OR-ed with ours and bypass proposer_id = auth.uid(). +DO $$ +DECLARE p record; +BEGIN + FOR p IN SELECT policyname FROM pg_policies + WHERE schemaname = 'public' AND tablename = 'voting_proposals' LOOP + EXECUTE format('DROP POLICY %I ON public.voting_proposals', p.policyname); + END LOOP; +END $$; + CREATE POLICY "Voting proposals are publicly readable" ON public.voting_proposals FOR SELECT USING (true); -DROP POLICY IF EXISTS "Users can propose as themselves" ON public.voting_proposals; CREATE POLICY "Users can propose as themselves" ON public.voting_proposals FOR INSERT TO authenticated WITH CHECK (proposer_id = auth.uid()); diff --git a/test-db/voting-proposals-legacy.test.ts b/test-db/voting-proposals-legacy.test.ts new file mode 100644 index 0000000..3ca199c --- /dev/null +++ b/test-db/voting-proposals-legacy.test.ts @@ -0,0 +1,72 @@ +import { describe, it, expect, beforeAll } from 'vitest'; +import { spawn } from 'node:child_process'; +import { readFileSync } from 'node:fs'; +import { randomUUID } from 'node:crypto'; +import path from 'node:path'; + +const BASE = process.env.TEST_DATABASE_URL!; +const DB = `legacy_${randomUUID().replace(/-/g, '').slice(0, 12)}`; +const URL2 = BASE.replace(/\/[^/]*$/, `/${DB}`); +const ROOT = path.resolve(__dirname, '..'); + +function psql(url: string, text: string, file = false): Promise { + return new Promise((resolve, reject) => { + const args = [url, '-X', '-q', '-A', '-t', '-v', 'ON_ERROR_STOP=1', '-f', file ? text : '-']; + const p = spawn('psql', args); + let out = ''; + let err = ''; + p.stdout.on('data', (d) => (out += d)); + p.stderr.on('data', (d) => (err += d)); + p.on('close', (c) => (c === 0 ? resolve(out.split('\n').filter(Boolean)) : reject(new Error(err.trim() || `psql ${c}`)))); + if (!file) p.stdin.end(text); + }); +} +const q = (t: string) => psql(URL2, t); +const asUser = (id: string) => + `SET ROLE authenticated; SELECT set_config('request.jwt.claims', '{"sub":"${id}"}', false) \\g /dev/null\n`; + +describe('voting_proposals migration over a legacy-shaped table', () => { + let a: string; + let b: string; + beforeAll(async () => { + await psql(BASE, `CREATE DATABASE ${DB};`); + await psql(URL2, path.join(ROOT, 'supabase/test/auth-shim.sql'), true); + [a, b] = [randomUUID(), randomUUID()]; + await q(`INSERT INTO auth.users(id) VALUES ('${a}'),('${b}'); + CREATE TABLE public.voting_proposals ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + title text NOT NULL, + description text, + source_url text NOT NULL, + bias_score text, category text, + status text, + votes_for integer NOT NULL, + votes_against integer NOT NULL, + created_at timestamptz NOT NULL + ); + INSERT INTO public.voting_proposals(title, source_url, votes_for, votes_against, created_at) + VALUES ('old', '', 0, 0, now()), ('old2', '', 0, 0, now()); + ALTER TABLE public.voting_proposals ENABLE ROW LEVEL SECURITY; + CREATE POLICY legacy_any_insert ON public.voting_proposals FOR INSERT WITH CHECK (true); + CREATE POLICY legacy_any_select ON public.voting_proposals FOR SELECT USING (true);`); + await psql(URL2, path.join(ROOT, 'supabase/migrations/20261104000300_voting_proposals.sql'), true); + expect(readFileSync(path.join(ROOT, 'supabase/migrations/20261104000300_voting_proposals.sql'), 'utf8')).toContain('pg_policies'); + }); + + it('removes the legacy permissive policies', async () => { + const rows = await q(`SELECT policyname FROM pg_policies WHERE tablename='voting_proposals' ORDER BY 1;`); + expect(rows.some((r) => r.startsWith('legacy_'))).toBe(false); + }); + + it('denies insert as another user', async () => { + await expect( + q(asUser(b) + `INSERT INTO voting_proposals(title, source_url, proposer_id) VALUES ('x','https://e.test/1','${a}');`), + ).rejects.toThrow(/row-level security/); + }); + + it('allows a normal insert (defaults fill votes/created_at, NULL source_url ok)', async () => { + await q(asUser(a) + `INSERT INTO voting_proposals(title, proposer_id) VALUES ('ok','${a}');`); + await q(asUser(a) + `INSERT INTO voting_proposals(title, source_url, proposer_id) VALUES ('ok2','https://e.test/2','${a}');`); + expect(await q(`SELECT count(*) FROM voting_proposals WHERE source_url = '';`)).toEqual(['0']); + }); +}); From b38491cd659c6050d7b252d7fe63e1eed629298e Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:23:55 -0800 Subject: [PATCH 11/16] test(api): fix lint warning in propose test Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- src/app/api/propose/route.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/api/propose/route.test.ts b/src/app/api/propose/route.test.ts index 47c747e..4a68b6b 100644 --- a/src/app/api/propose/route.test.ts +++ b/src/app/api/propose/route.test.ts @@ -8,7 +8,7 @@ const limit = vi.fn(); const insert = vi.fn(); const chain: Record = {}; chain.insert = insert.mockReturnValue(chain); -const select = vi.fn((_cols?: string) => chain); +const select = vi.fn<(cols?: string) => unknown>(() => chain); chain.select = select; chain.eq = vi.fn(() => chain); chain.order = vi.fn(() => chain); From 81ed28e6a20b9480e4749915624716e6656f750d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:24:56 -0800 Subject: [PATCH 12/16] feat(ui): label sample data, gate votes on Supabase session Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- src/app/issue/[id]/page.test.tsx | 20 ++++++ src/app/issue/[id]/page.tsx | 77 ++-------------------- src/app/legislature/page.test.tsx | 17 +++++ src/app/legislature/page.tsx | 7 +- src/app/page.tsx | 45 +------------ src/components/issue/VotePanel.test.tsx | 76 +++++++++++++++++++++ src/components/issue/VotePanel.tsx | 76 +++++++++++++++++++++ src/components/ui/DataStatusBadge.test.tsx | 27 ++++++++ src/components/ui/DataStatusBadge.tsx | 23 +++++++ src/lib/data.ts | 14 ++++ 10 files changed, 263 insertions(+), 119 deletions(-) create mode 100644 src/app/issue/[id]/page.test.tsx create mode 100644 src/app/legislature/page.test.tsx create mode 100644 src/components/issue/VotePanel.test.tsx create mode 100644 src/components/issue/VotePanel.tsx create mode 100644 src/components/ui/DataStatusBadge.test.tsx create mode 100644 src/components/ui/DataStatusBadge.tsx diff --git a/src/app/issue/[id]/page.test.tsx b/src/app/issue/[id]/page.test.tsx new file mode 100644 index 0000000..7a7055a --- /dev/null +++ b/src/app/issue/[id]/page.test.tsx @@ -0,0 +1,20 @@ +import { describe, expect, it, vi } from 'vitest'; +import { render, screen } from '@testing-library/react'; + +vi.mock('next/navigation', () => ({ + useParams: () => ({ id: 'natl-insulin' }), + useRouter: () => ({ push: vi.fn() }), +})); +vi.mock('next/link', () => ({ default: ({ children }: { children: React.ReactNode }) => {children} })); +vi.mock('@privy-io/react-auth', () => ({ usePrivy: () => ({ login: vi.fn(), logout: vi.fn(), authenticated: false }) })); +vi.mock('@/lib/supabaseAuth/client', () => ({ createClient: () => ({ auth: { getSession: vi.fn() } }) })); + +import DetailView from './page'; + +describe('issue page', () => { + it('labels a synthetic issue as Sample data', () => { + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false })); + render(); + expect(screen.getAllByText('Sample data').length).toBeGreaterThan(0); + }); +}); diff --git a/src/app/issue/[id]/page.tsx b/src/app/issue/[id]/page.tsx index 5d087b8..4feea3a 100644 --- a/src/app/issue/[id]/page.tsx +++ b/src/app/issue/[id]/page.tsx @@ -5,40 +5,8 @@ import { useParams, useRouter } from 'next/navigation'; import { ISSUES, Issue, makeActivity } from '@/lib/data'; import { scaleMeta, Media, SentimentBar, TrendChart, TopBar, fmt } from '@/components/ui/Shared'; import { Icon, Flag } from '@/components/ui/icons'; -import { useWriteContract } from 'wagmi'; -import { usePrivy } from '@privy-io/react-auth'; -import { UTP_POLLING_ADDRESS, UTP_POLLING_ABI } from '@/lib/contracts'; - -function VotePanel({ issue, vote, onVote }: { issue: Issue, vote: string | null, onVote: (id: string, vote: string) => void }) { - const { authenticated, login } = usePrivy(); - - const opts = [ - { k: 'for', cls: 'for-opt', label: 'For', pct: issue.for, ico: 'up' }, - { k: 'against', cls: 'against-opt', label: 'Against', pct: issue.against, ico: 'down' }, - { k: 'unsure', cls: 'unsure-opt', label: 'Unsure', pct: issue.unsure, ico: 'question' }, - ]; - return ( -
    -

    Cast your voice

    -

    On-chain shadow vote · one voice per verified wallet

    -
    - {opts.map(o => ( - - ))} -
    - {vote - ?
    Your voice is counted as {vote} · change anytime
    - : authenticated - ?

    Select an option above to add your voice.

    - :

    Connect wallet or email to vote.

    } -
    - ); -} +import { VotePanel } from '@/components/issue/VotePanel'; +import { DataStatusBadge } from '@/components/ui/DataStatusBadge'; function AiDebate({ issue }: { issue: Issue }) { // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -176,7 +144,7 @@ function ActivityWidget({ issue }: { issue: Issue }) { return (
    -

    Live activity

    +

    {issue.isSample ? "Sample activity" : "Live activity"}

    {acts.map((a, i) => (
    @@ -195,47 +163,13 @@ export default function DetailView() { const issue = ISSUES.find(i => i.id === id); - const [votes, setVotes] = useState>({}); - const { writeContract } = useWriteContract(); - useEffect(() => { - try { - const saved = localStorage.getItem('utp_votes'); - if (saved) setVotes(JSON.parse(saved)); - } catch {} - const r = document.documentElement; r.setAttribute('data-palette', 'civic'); r.setAttribute('data-density', 'comfortable'); r.setAttribute('data-display', 'grotesk'); }, []); - const onVote = (id: string, v: string) => { - // 1=for, 2=against, 3=unsure - const voteMap: Record = { 'for': 1, 'against': 2, 'unsure': 3 }; - const voteType = voteMap[v]; - - writeContract({ - address: UTP_POLLING_ADDRESS, - abi: UTP_POLLING_ABI, - functionName: 'castVote', - args: [id, voteType], - }, { - onSuccess: () => { - setVotes(prev => { - const next = { ...prev, [id]: prev[id] === v ? undefined : v }; - if (next[id] === undefined) delete next[id]; - try { localStorage.setItem('utp_votes', JSON.stringify(next)); } catch {} - return next as Record; - }); - }, - onError: (err) => { - console.error('Failed to vote:', err); - alert('Failed to vote on-chain. Make sure you are connected to the right network.'); - } - }); - }; - if (!issue) { return (
    @@ -248,7 +182,6 @@ export default function DetailView() { } const sm = scaleMeta(issue.scale); - const vote = votes[issue.id] || null; const related = ISSUES.filter(i => i.id !== issue.id && (i.category === issue.category || i.scale === issue.scale)).slice(0, 4); const delta = issue.deltaWeek; @@ -268,7 +201,7 @@ export default function DetailView() { {issue.category} {issue.region} {issue.scope && {issue.scope}} - Live + {issue.isSample && }

    {issue.question}

    {issue.billNo &&
    {issue.billNo}
    } @@ -310,7 +243,7 @@ export default function DetailView() {
    -
    Voices{fmt(issue.voices)}
    +
    {issue.isSample ? 'Sample voices' : 'Voices'}{fmt(issue.voices)}
    Lean For{issue.for}%
    7-day shift= 0 ? 'trend-up' : 'trend-down')}>= 0 ? 'up' : 'down'} size={14} stroke={2.4} />{Math.abs(delta)} pts
    -
    Status{issue.deadline}
    +
    Status{issue.isSample ? 'Sample' : issue.deadline}
    @@ -229,13 +229,13 @@ export default function DetailView() {
    Share voting For over time - Updated continuously + {issue.isSample ? 'Sample figures — illustrative only' : 'Updated continuously'}

    Where people stand

    -

    {fmt(issue.voices)} verified voices so far

    +

    {issue.isSample ? 'Sample figures — illustrative only' : `${fmt(issue.voices)} verified voices so far`}

    diff --git a/src/app/page.test.tsx b/src/app/page.test.tsx new file mode 100644 index 0000000..e67e25d --- /dev/null +++ b/src/app/page.test.tsx @@ -0,0 +1,26 @@ +import { describe, expect, it, vi } from 'vitest'; +import { render } from '@testing-library/react'; + +vi.mock('next/navigation', () => ({ useRouter: () => ({ push: vi.fn() }) })); +vi.mock('next/link', () => ({ default: ({ children }: { children: React.ReactNode }) => {children} })); +vi.mock('@privy-io/react-auth', () => ({ usePrivy: () => ({ login: vi.fn(), logout: vi.fn(), authenticated: false }) })); +vi.mock('@/components/ui/HeroCanvas', () => ({ HeroCanvas: () => null })); +vi.mock('gsap', () => ({ default: { context: () => ({ revert: () => {} }), fromTo: vi.fn(), from: vi.fn(), to: vi.fn() } })); + +import FeedView from './page'; +import { ISSUES } from '@/lib/data'; + +describe('home feed', () => { + it('does not present sample content as live or verified', () => { + const { container } = render(); + const t = container.textContent || ''; + expect(t).not.toMatch(/verified|Live|live issues|Updated continuously/); + expect(t).toContain(`${ISSUES.length} sample issues`); + }); + it('feed cards show Sample data', () => { + const { container } = render(); + const cards = container.querySelectorAll('article.icard'); + expect(cards.length).toBeGreaterThan(0); + cards.forEach(c => expect(c.textContent).toContain('Sample data')); + }); +}); diff --git a/src/app/page.tsx b/src/app/page.tsx index 0a60891..4d2e2d4 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -102,14 +102,14 @@ export default function FeedView() {
    - {fmt(totalVoices)} voices · {ISSUES.length} live issues + {ISSUES.length} sample issues · illustrative figures

    Vote on what shapes your world

    From your city council to the United Nations, weigh in on the decisions that matter — every claim backed by neutral, citation-grounded evidence.

    - {fmt(totalVoices)} Voices cast - {ISSUES.length} Open issues + {fmt(totalVoices)} Sample voices + {ISSUES.length} Sample issues 4 Local → global - 100% Sources cited + Sample Illustrative data
    diff --git a/src/components/ui/Shared.tsx b/src/components/ui/Shared.tsx index befb045..dd3663e 100644 --- a/src/components/ui/Shared.tsx +++ b/src/components/ui/Shared.tsx @@ -5,6 +5,7 @@ import { SCALES, Issue, photoUrl } from '@/lib/data'; import { Icon, BrandMark, Flag, CAT_COLOR } from './icons'; import { usePrivy } from '@privy-io/react-auth'; import Link from 'next/link'; +import { DataStatusBadge } from './DataStatusBadge'; export function fmt(n: number) { if (n >= 1000000) return (n / 1000000).toFixed(1).replace(/\.0$/, '') + 'M'; @@ -137,6 +138,7 @@ export function IssueCard({ issue, vote, onVote, onOpen }: { issue: Issue, vote:
    {sm.label} {issue.category} + {issue.isSample && }
    {issue.region} @@ -156,8 +158,8 @@ export function IssueCard({ issue, vote, onVote, onOpen }: { issue: Issue, vote:
    - {fmt(issue.voices)} voices - {issue.deadline} + {fmt(issue.voices)} {issue.isSample ? 'sample voices' : 'voices'} + {!issue.isSample && {issue.deadline}}
    From fbe56741e3b898702f7d94c6425c9a4d86446f0f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:41:34 -0800 Subject: [PATCH 14/16] fix(auth): require server HMAC attestation for wallet link/unlink in the database Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- src/app/api/wallet/challenge/route.test.ts | 8 + src/app/api/wallet/challenge/route.ts | 4 +- src/app/api/wallet/link/route.test.ts | 51 ++++-- src/app/api/wallet/link/route.ts | 37 +---- src/app/api/wallet/unlink/route.test.ts | 63 ++++++++ src/app/api/wallet/unlink/route.ts | 8 + src/lib/wallet/proofRoute.ts | 49 ++++++ src/lib/wallet/store.ts | 67 ++++---- src/lib/wallet/walletLink.test.ts | 16 ++ src/lib/wallet/walletLink.ts | 148 ++++++++++-------- .../20261104000100_wallet_links.sql | 121 ++++++++++---- test-db/wallet-links.test.ts | 139 +++++++++++----- 12 files changed, 495 insertions(+), 216 deletions(-) create mode 100644 src/app/api/wallet/unlink/route.test.ts create mode 100644 src/app/api/wallet/unlink/route.ts create mode 100644 src/lib/wallet/proofRoute.ts diff --git a/src/app/api/wallet/challenge/route.test.ts b/src/app/api/wallet/challenge/route.test.ts index e138453..ddc1f66 100644 --- a/src/app/api/wallet/challenge/route.test.ts +++ b/src/app/api/wallet/challenge/route.test.ts @@ -20,6 +20,14 @@ describe('POST /api/wallet/challenge', () => { beforeEach(() => { requireUser.mockReset().mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); rpc.mockReset().mockResolvedValue({ data: null, error: null }); + vi.stubEnv('WALLET_LINK_SECRET', 'k'.repeat(64)); + }); + + it('503 when WALLET_LINK_SECRET is unset', async () => { + vi.stubEnv('WALLET_LINK_SECRET', ''); + const { POST } = await import('./route'); + expect((await POST(req({ address: ADDR, chainId: 31 }))).status).toBe(503); + expect(rpc).not.toHaveBeenCalled(); }); it('passes through 401', async () => { diff --git a/src/app/api/wallet/challenge/route.ts b/src/app/api/wallet/challenge/route.ts index 9550589..0a44b7a 100644 --- a/src/app/api/wallet/challenge/route.ts +++ b/src/app/api/wallet/challenge/route.ts @@ -4,6 +4,8 @@ import { createSupabaseWalletLinkStore } from '@/lib/wallet/store'; import { createWalletChallenge, normalizeAddress, ALLOWED_LINK_CHAINS } from '@/lib/wallet/walletLink'; export async function POST(req: Request) { + const secret = process.env.WALLET_LINK_SECRET; + if (!secret) return NextResponse.json({ message: 'Wallet linking is not configured' }, { status: 503 }); const auth = await requireUser(req); if ('error' in auth) return auth.error; @@ -27,7 +29,7 @@ export async function POST(req: Request) { // Domain and URI come from the request URL, never from the client body. const origin = new URL(req.url).origin; try { - const store = createSupabaseWalletLinkStore(auth.accessToken, auth.user.id); + const store = createSupabaseWalletLinkStore(auth.accessToken, auth.user.id, secret); const c = await createWalletChallenge({ userId: auth.user.id, address, chainId, origin }, store); return NextResponse.json(c); } catch { diff --git a/src/app/api/wallet/link/route.test.ts b/src/app/api/wallet/link/route.test.ts index df857e7..9b0f8c8 100644 --- a/src/app/api/wallet/link/route.test.ts +++ b/src/app/api/wallet/link/route.test.ts @@ -1,4 +1,5 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { createHmac } from 'node:crypto'; import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; import { createSiweMessage } from 'viem/siwe'; @@ -9,6 +10,7 @@ vi.mock('@/lib/supabaseAuth/userClient', () => ({ createUserScopedClient: vi.fn( const USER = '11111111-2222-4333-8444-555555555555'; const NONCE = 'abcdef0123456789'; +const SECRET = 'k'.repeat(64); function req(body: unknown, url = 'https://utp.test/api/wallet/link') { return new Request(url, { @@ -26,20 +28,19 @@ async function signed() { return { acct, message, signature: await acct.signMessage({ message }) }; } -function stubRpc(address: string, activate: string) { - rpc.mockImplementation(async (fn: string) => { - if (fn === 'consume_wallet_challenge') - return { data: [{ nonce: NONCE, user_id: USER, chain_id: 31, address: address.toLowerCase(), domain: 'utp.test', expires_at: new Date(Date.now() + 60_000).toISOString() }], error: null }; - if (fn === 'activate_wallet_link') return { data: activate, error: null }; - if (fn === 'wallet_challenge_status') return { data: 'consumed', error: null }; - return { data: null, error: null }; - }); -} - describe('POST /api/wallet/link', () => { beforeEach(() => { requireUser.mockReset().mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); rpc.mockReset(); + vi.stubEnv('WALLET_LINK_SECRET', SECRET); + }); + + it('503 fail-closed when WALLET_LINK_SECRET is unset', async () => { + vi.stubEnv('WALLET_LINK_SECRET', ''); + const s = await signed(); + const { POST } = await import('./route'); + expect((await POST(req({ message: s.message, signature: s.signature }))).status).toBe(503); + expect(rpc).not.toHaveBeenCalled(); }); it('passes through 401', async () => { @@ -48,32 +49,50 @@ describe('POST /api/wallet/link', () => { expect((await POST(req({}))).status).toBe(401); }); - it('links on a valid signature', async () => { + it('links on a valid signature, sending the HMAC attestation to link_wallet', async () => { const s = await signed(); - stubRpc(s.acct.address, 'ok'); + rpc.mockResolvedValue({ data: 'ok', error: null }); const { POST } = await import('./route'); const res = await POST(req({ message: s.message, signature: s.signature })); expect(res.status).toBe(200); expect(await res.json()).toEqual({ ok: true }); - expect(rpc).toHaveBeenCalledWith('activate_wallet_link', { p_chain_id: 31, p_address: s.acct.address.toLowerCase(), p_assurance: 'aal1' }); + const a = s.acct.address.toLowerCase(); + const att = createHmac('sha256', SECRET).update(`${USER}|31|${a}|${NONCE}`).digest('hex'); + expect(rpc).toHaveBeenCalledWith('link_wallet', { p_nonce: NONCE, p_att: att, p_assurance: 'aal1' }); + expect(rpc).toHaveBeenCalledTimes(1); }); it('409 for address_taken without internals', async () => { const s = await signed(); - stubRpc(s.acct.address, 'address_taken'); + rpc.mockResolvedValue({ data: 'address_taken', error: null }); const { POST } = await import('./route'); const res = await POST(req({ message: s.message, signature: s.signature })); expect(res.status).toBe(409); expect(await res.json()).toEqual({ ok: false }); }); + it('400 for replayed, 500 for bad_attestation (key mismatch)', async () => { + const s = await signed(); + const { POST } = await import('./route'); + rpc.mockResolvedValue({ data: 'replayed', error: null }); + expect((await POST(req({ message: s.message, signature: s.signature }))).status).toBe(400); + rpc.mockResolvedValue({ data: 'bad_attestation', error: null }); + expect((await POST(req({ message: s.message, signature: s.signature }))).status).toBe(500); + }); + + it('400 for a bad signature without calling the database', async () => { + const s = await signed(); + const other = await privateKeyToAccount(generatePrivateKey()).signMessage({ message: s.message }); + const { POST } = await import('./route'); + expect((await POST(req({ message: s.message, signature: other }))).status).toBe(400); + expect(rpc).not.toHaveBeenCalled(); + }); + it('400 when the request host differs from the signed domain', async () => { const s = await signed(); - stubRpc(s.acct.address, 'ok'); const { POST } = await import('./route'); const res = await POST(req({ message: s.message, signature: s.signature }, 'https://other.test/api/wallet/link')); expect(res.status).toBe(400); - expect(await res.json()).toEqual({ ok: false }); expect(rpc).not.toHaveBeenCalled(); }); diff --git a/src/app/api/wallet/link/route.ts b/src/app/api/wallet/link/route.ts index 7e742ef..5382bae 100644 --- a/src/app/api/wallet/link/route.ts +++ b/src/app/api/wallet/link/route.ts @@ -1,39 +1,6 @@ -import { NextResponse } from 'next/server'; -import { requireUser } from '@/lib/supabaseAuth/requireUser'; -import { createSupabaseWalletLinkStore } from '@/lib/wallet/store'; +import { handleWalletProof } from '@/lib/wallet/proofRoute'; import { verifyWalletLink } from '@/lib/wallet/walletLink'; export async function POST(req: Request) { - const auth = await requireUser(req); - if ('error' in auth) return auth.error; - - let body: unknown; - try { - body = await req.json(); - } catch { - return NextResponse.json({ ok: false }, { status: 400 }); - } - const { message, signature } = (body ?? {}) as { message?: unknown; signature?: unknown }; - if ( - typeof message !== 'string' || - message.length > 4096 || - typeof signature !== 'string' || - !/^0x[0-9a-fA-F]+$/.test(signature) - ) { - return NextResponse.json({ ok: false }, { status: 400 }); - } - - const origin = new URL(req.url).origin; - try { - const store = createSupabaseWalletLinkStore(auth.accessToken, auth.user.id); - const r = await verifyWalletLink( - { userId: auth.user.id, message, signature: signature as `0x${string}`, origin }, - store, - ); - if (r.ok) return NextResponse.json({ ok: true }); - // Do not echo internal reasons beyond the conflict case. - return NextResponse.json({ ok: false }, { status: r.reason === 'address_taken' ? 409 : 400 }); - } catch { - return NextResponse.json({ ok: false }, { status: 500 }); - } + return handleWalletProof(req, verifyWalletLink, 'address_taken'); } diff --git a/src/app/api/wallet/unlink/route.test.ts b/src/app/api/wallet/unlink/route.test.ts new file mode 100644 index 0000000..b9cb696 --- /dev/null +++ b/src/app/api/wallet/unlink/route.test.ts @@ -0,0 +1,63 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { createHmac } from 'node:crypto'; +import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; +import { createSiweMessage } from 'viem/siwe'; + +const requireUser = vi.fn(); +vi.mock('@/lib/supabaseAuth/requireUser', () => ({ requireUser })); +const rpc = vi.fn(); +vi.mock('@/lib/supabaseAuth/userClient', () => ({ createUserScopedClient: vi.fn(() => ({ rpc })) })); + +const USER = '11111111-2222-4333-8444-555555555555'; +const NONCE = 'abcdef0123456789'; +const SECRET = 'k'.repeat(64); + +function req(body: unknown) { + return new Request('https://utp.test/api/wallet/unlink', { + method: 'POST', + headers: { 'content-type': 'application/json', authorization: 'Bearer tok' }, + body: JSON.stringify(body), + }); +} + +async function signed() { + const acct = privateKeyToAccount(generatePrivateKey()); + const message = createSiweMessage({ + domain: 'utp.test', uri: 'https://utp.test', address: acct.address, chainId: 31, nonce: NONCE, version: '1', + }); + return { acct, message, signature: await acct.signMessage({ message }) }; +} + +describe('POST /api/wallet/unlink', () => { + beforeEach(() => { + requireUser.mockReset().mockResolvedValue({ user: { id: USER }, accessToken: 'tok' }); + rpc.mockReset(); + vi.stubEnv('WALLET_LINK_SECRET', SECRET); + }); + + it('503 when WALLET_LINK_SECRET is unset', async () => { + vi.stubEnv('WALLET_LINK_SECRET', ''); + const { POST } = await import('./route'); + expect((await POST(req({}))).status).toBe(503); + }); + + it('revokes with an attested fresh proof', async () => { + const s = await signed(); + rpc.mockResolvedValue({ data: 'ok', error: null }); + const { POST } = await import('./route'); + const res = await POST(req({ message: s.message, signature: s.signature })); + expect(res.status).toBe(200); + const a = s.acct.address.toLowerCase(); + const att = createHmac('sha256', SECRET).update(`${USER}|31|${a}|${NONCE}`).digest('hex'); + expect(rpc).toHaveBeenCalledWith('revoke_wallet_link', { p_chain_id: 31, p_address: a, p_nonce: NONCE, p_att: att }); + }); + + it('400 when not linked', async () => { + const s = await signed(); + rpc.mockResolvedValue({ data: 'not_linked', error: null }); + const { POST } = await import('./route'); + const res = await POST(req({ message: s.message, signature: s.signature })); + expect(res.status).toBe(400); + expect(await res.json()).toEqual({ ok: false }); + }); +}); diff --git a/src/app/api/wallet/unlink/route.ts b/src/app/api/wallet/unlink/route.ts new file mode 100644 index 0000000..606619d --- /dev/null +++ b/src/app/api/wallet/unlink/route.ts @@ -0,0 +1,8 @@ +import { handleWalletProof } from '@/lib/wallet/proofRoute'; +import { verifyWalletUnlink } from '@/lib/wallet/walletLink'; + +// Requires a fresh wallet proof for an address the caller has linked. +// Fresh-MFA for unlink is deferred to I2. +export async function POST(req: Request) { + return handleWalletProof(req, verifyWalletUnlink); +} diff --git a/src/lib/wallet/proofRoute.ts b/src/lib/wallet/proofRoute.ts new file mode 100644 index 0000000..754ae24 --- /dev/null +++ b/src/lib/wallet/proofRoute.ts @@ -0,0 +1,49 @@ +import { NextResponse } from 'next/server'; +import { requireUser } from '@/lib/supabaseAuth/requireUser'; +import { createSupabaseWalletLinkStore } from './store'; +import type { WalletLinkStore } from './walletLink'; + +type Action = ( + i: { userId: string; message: string; signature: `0x${string}`; origin: string }, + store: WalletLinkStore, +) => Promise<{ ok: true } | { ok: false; reason: string }>; + +/** + * Shared POST handler for wallet-proof routes (link, unlink). Fails closed + * with 503 when WALLET_LINK_SECRET is unset; never echoes failure reasons. + * `conflict` maps one reason to 409; every other failure is 400. + */ +export async function handleWalletProof(req: Request, action: Action, conflict?: string): Promise { + const secret = process.env.WALLET_LINK_SECRET; + if (!secret) return NextResponse.json({ ok: false }, { status: 503 }); + + const auth = await requireUser(req); + if ('error' in auth) return auth.error; + + let body: unknown; + try { + body = await req.json(); + } catch { + return NextResponse.json({ ok: false }, { status: 400 }); + } + const { message, signature } = (body ?? {}) as { message?: unknown; signature?: unknown }; + if ( + typeof message !== 'string' || + message.length > 4096 || + typeof signature !== 'string' || + !/^0x[0-9a-fA-F]+$/.test(signature) + ) { + return NextResponse.json({ ok: false }, { status: 400 }); + } + + // Domain and URI come from the request URL, never from the client. + const origin = new URL(req.url).origin; + try { + const store = createSupabaseWalletLinkStore(auth.accessToken, auth.user.id, secret); + const r = await action({ userId: auth.user.id, message, signature: signature as `0x${string}`, origin }, store); + if (r.ok) return NextResponse.json({ ok: true }); + return NextResponse.json({ ok: false }, { status: conflict && r.reason === conflict ? 409 : 400 }); + } catch { + return NextResponse.json({ ok: false }, { status: 500 }); + } +} diff --git a/src/lib/wallet/store.ts b/src/lib/wallet/store.ts index 50b4ea6..0b017f9 100644 --- a/src/lib/wallet/store.ts +++ b/src/lib/wallet/store.ts @@ -1,16 +1,29 @@ +import { createHmac } from 'node:crypto'; import type { SupabaseClient } from '@supabase/supabase-js'; import { createUserScopedClient } from '@/lib/supabaseAuth/userClient'; -import type { Challenge, ChallengeStatus, WalletLinkStore } from './walletLink'; +import type { Challenge, LinkStatus, RevokeStatus, WalletLinkStore } from './walletLink'; + +/** + * Server attestation that this server verified the wallet signature for + * (user, chain, address, nonce). The database recomputes it with the same + * key (private.wallet_link_settings) before changing link state. + */ +export function walletAttestation(secret: string, userId: string, chainId: number, address: string, nonce: string): string { + return createHmac('sha256', secret).update(`${userId}|${chainId}|${address}|${nonce}`).digest('hex'); +} + +const LINK: readonly string[] = ['ok', 'address_taken', 'bad_attestation', 'unknown_nonce', 'expired', 'replayed', 'wrong_user']; +const REVOKE: readonly string[] = ['ok', 'not_linked', 'bad_attestation', 'unknown_nonce', 'expired', 'replayed', 'wrong_user']; /** * Supabase-backed WalletLinkStore acting as the calling user (anon key + - * access token; never the service-role key). Every write goes through a - * SECURITY DEFINER function that derives the user from auth.uid(); the - * userId arguments of the interface are cross-checked here and otherwise - * ignored, so a caller cannot act for someone else. `now` is ignored too: - * expiry is judged by the database clock. + * access token; never the service-role key). SQL derives the user from + * auth.uid(); the interface's userId arguments are cross-checked against + * the session user and otherwise ignored. `now` is ignored: the database + * clock decides expiry. */ -export function createSupabaseWalletLinkStore(accessToken: string, expectedUserId: string): WalletLinkStore { +export function createSupabaseWalletLinkStore(accessToken: string, expectedUserId: string, secret: string): WalletLinkStore { + if (!secret) throw new Error('WALLET_LINK_SECRET is not configured'); const db: SupabaseClient = createUserScopedClient(accessToken); const check = (userId: string) => { if (userId !== expectedUserId) throw new Error('wallet store user mismatch'); @@ -26,36 +39,26 @@ export function createSupabaseWalletLinkStore(accessToken: string, expectedUserI }); if (error) throw new Error('could not store challenge'); }, - async consumeChallenge(nonce, userId) { - check(userId); - const { data, error } = await db.rpc('consume_wallet_challenge', { p_nonce: nonce }); - if (error) throw new Error('could not consume challenge'); - const row = Array.isArray(data) ? data[0] : null; - if (!row || row.user_id !== expectedUserId) return null; - return { - nonce: row.nonce, - userId: row.user_id, - chainId: row.chain_id, - address: row.address, - domain: row.domain, - expiresAt: new Date(row.expires_at), - }; - }, - async challengeStatus(nonce, userId) { - check(userId); - const { data, error } = await db.rpc('wallet_challenge_status', { p_nonce: nonce }); - if (error) throw new Error('could not read challenge status'); - return data as ChallengeStatus; + async linkWallet(l) { + check(l.userId); + const { data, error } = await db.rpc('link_wallet', { + p_nonce: l.nonce, + p_att: walletAttestation(secret, expectedUserId, l.chainId, l.address, l.nonce), + p_assurance: l.assurance, + }); + if (error || typeof data !== 'string' || !LINK.includes(data)) throw new Error('could not link wallet'); + return data as LinkStatus; }, - async activateLink(l) { + async revokeLink(l) { check(l.userId); - const { data, error } = await db.rpc('activate_wallet_link', { + const { data, error } = await db.rpc('revoke_wallet_link', { p_chain_id: l.chainId, p_address: l.address, - p_assurance: l.assurance, + p_nonce: l.nonce, + p_att: walletAttestation(secret, expectedUserId, l.chainId, l.address, l.nonce), }); - if (error) throw new Error('could not activate link'); - return data === 'ok' ? 'ok' : 'address_taken'; + if (error || typeof data !== 'string' || !REVOKE.includes(data)) throw new Error('could not revoke wallet link'); + return data as RevokeStatus; }, }; } diff --git a/src/lib/wallet/walletLink.test.ts b/src/lib/wallet/walletLink.test.ts index 20aef5d..b952a62 100644 --- a/src/lib/wallet/walletLink.test.ts +++ b/src/lib/wallet/walletLink.test.ts @@ -6,6 +6,7 @@ import { verifyWalletLink, normalizeAddress, createMemoryWalletLinkStore, + verifyWalletUnlink, } from './walletLink'; const ORIGIN = 'https://utp.test'; @@ -115,4 +116,19 @@ describe('wallet link challenge/verify', () => { const second = await challengeAndSign(store, B, acct, acct.address.toLowerCase()); expect(await verifyWalletLink({ userId: B, message: second.c.message, signature: second.signature, origin: ORIGIN, now: T0 }, store)).toEqual({ ok: false, reason: 'address_taken' }); }); + + it('unlink needs a fresh proof by the owner; address can then be relinked', async () => { + const store = createMemoryWalletLinkStore(); + const acct = wallet(); + const l = await challengeAndSign(store, A, acct); + expect(await verifyWalletLink({ userId: A, message: l.c.message, signature: l.signature, origin: ORIGIN, now: T0 }, store)).toEqual({ ok: true }); + // reusing the link proof fails + expect(await verifyWalletUnlink({ userId: A, message: l.c.message, signature: l.signature, origin: ORIGIN, now: T0 }, store)).toEqual({ ok: false, reason: 'replayed' }); + const bProof = await challengeAndSign(store, B, acct); + expect(await verifyWalletUnlink({ userId: B, message: bProof.c.message, signature: bProof.signature, origin: ORIGIN, now: T0 }, store)).toEqual({ ok: false, reason: 'not_linked' }); + const u = await challengeAndSign(store, A, acct); + expect(await verifyWalletUnlink({ userId: A, message: u.c.message, signature: u.signature, origin: ORIGIN, now: T0 }, store)).toEqual({ ok: true }); + const b2 = await challengeAndSign(store, B, acct); + expect(await verifyWalletLink({ userId: B, message: b2.c.message, signature: b2.signature, origin: ORIGIN, now: T0 }, store)).toEqual({ ok: true }); + }); }); diff --git a/src/lib/wallet/walletLink.ts b/src/lib/wallet/walletLink.ts index c88bba0..d11d68f 100644 --- a/src/lib/wallet/walletLink.ts +++ b/src/lib/wallet/walletLink.ts @@ -13,15 +13,19 @@ export type Challenge = { expiresAt: Date; }; -/** Why a nonce could not be consumed, from the caller's point of view. */ -export type ChallengeStatus = 'unknown' | 'expired' | 'consumed' | 'other_user' | 'available'; +export type NonceFailure = 'unknown_nonce' | 'expired' | 'replayed' | 'wrong_user'; +export type LinkStatus = 'ok' | 'address_taken' | 'bad_attestation' | NonceFailure; +export type RevokeStatus = 'ok' | 'not_linked' | 'bad_attestation' | NonceFailure; +/** + * Link state changes only through linkWallet / revokeLink, each of which + * atomically consumes the caller's nonce (single use) and, in the Supabase + * store, carries a server HMAC attestation the database re-checks. + */ export interface WalletLinkStore { putChallenge(c: Challenge): Promise; - consumeChallenge(nonce: string, userId: string, now: Date): Promise; // atomic, single use - activateLink(l: { userId: string; chainId: number; address: string; assurance: 'aal1' | 'aal2' }): Promise<'ok' | 'address_taken'>; - /** Diagnostic only, used after a failed consume to choose an error reason. */ - challengeStatus(nonce: string, userId: string, now: Date): Promise; + linkWallet(l: { userId: string; nonce: string; chainId: number; address: `0x${string}`; assurance: 'aal1' | 'aal2'; now: Date }): Promise; + revokeLink(l: { userId: string; nonce: string; chainId: number; address: `0x${string}`; now: Date }): Promise; } export type VerifyFailure = @@ -61,19 +65,19 @@ export async function createWalletChallenge( return { message, nonce, expiresAt: expiresAt.toISOString() }; } -export async function verifyWalletLink( - i: { userId: string; message: string; signature: `0x${string}`; origin: string; now?: Date }, - store: WalletLinkStore, -): Promise<{ ok: true } | { ok: false; reason: VerifyFailure }> { +type Proof = { nonce: string; chainId: number; address: `0x${string}` }; + +/** Checks an ERC-4361 message + signature against the request origin. */ +async function verifyProof( + i: { message: string; signature: `0x${string}`; origin: string }, +): Promise<{ ok: true; proof: Proof } | { ok: false; reason: VerifyFailure }> { const fail = (reason: VerifyFailure) => ({ ok: false as const, reason }); - const now = i.now ?? new Date(); let expected: URL; try { expected = new URL(i.origin); } catch { return fail('wrong_origin'); } - let parsed: ReturnType; try { parsed = parseSiweMessage(i.message); @@ -81,80 +85,100 @@ export async function verifyWalletLink( return fail('unknown_nonce'); } if (!parsed.nonce || !parsed.address) return fail('unknown_nonce'); - if (parsed.domain !== expected.host) return fail('wrong_origin'); - if (parsed.uri !== undefined) { - try { - if (new URL(parsed.uri).origin !== expected.origin) return fail('wrong_origin'); - } catch { - return fail('wrong_origin'); - } - } else return fail('wrong_origin'); + if (parsed.domain !== expected.host || parsed.uri === undefined) return fail('wrong_origin'); + try { + if (new URL(parsed.uri).origin !== expected.origin) return fail('wrong_origin'); + } catch { + return fail('wrong_origin'); + } if (parsed.chainId === undefined || !isAllowedChain(parsed.chainId)) return fail('wrong_chain'); - let address: `0x${string}`; try { address = normalizeAddress(parsed.address); } catch { return fail('bad_signature'); } - - // Check the signature before consuming, so a forged attempt cannot burn - // the legitimate user's nonce. - let validSig = false; + // Signature is checked before any nonce is consumed, so a forged attempt + // cannot burn the legitimate user's nonce. EOA only (no ERC-1271 yet). + let valid = false; try { - validSig = await verifyMessage({ address: getAddress(address), message: i.message, signature: i.signature }); + valid = await verifyMessage({ address: getAddress(address), message: i.message, signature: i.signature }); } catch { - validSig = false; + valid = false; } - if (!validSig) return fail('bad_signature'); + if (!valid) return fail('bad_signature'); + return { ok: true, proof: { nonce: parsed.nonce, chainId: parsed.chainId, address } }; +} - const challenge = await store.consumeChallenge(parsed.nonce, i.userId, now); - if (!challenge) { - const status = await store.challengeStatus(parsed.nonce, i.userId, now); - if (status === 'other_user') return fail('wrong_user'); - if (status === 'expired') return fail('expired'); - if (status === 'consumed') return fail('replayed'); - return fail('unknown_nonce'); - } - // The signed message must match exactly what this server issued for the nonce. - if (challenge.domain !== expected.host) return fail('wrong_origin'); - if (challenge.chainId !== parsed.chainId) return fail('wrong_chain'); - if (challenge.address !== address) return fail('bad_signature'); +function nonceOrThrow(status: LinkStatus | RevokeStatus): NonceFailure { + // bad_attestation means the server and database keys disagree: a + // configuration fault, not a user error. + if (status === 'bad_attestation') throw new Error('wallet attestation rejected'); + return status as NonceFailure; +} - const res = await store.activateLink({ userId: i.userId, chainId: challenge.chainId, address, assurance: 'aal1' }); - return res === 'ok' ? { ok: true } : fail('address_taken'); +export async function verifyWalletLink( + i: { userId: string; message: string; signature: `0x${string}`; origin: string; now?: Date }, + store: WalletLinkStore, +): Promise<{ ok: true } | { ok: false; reason: VerifyFailure }> { + const v = await verifyProof(i); + if (!v.ok) return v; + const status = await store.linkWallet({ userId: i.userId, ...v.proof, assurance: 'aal1', now: i.now ?? new Date() }); + if (status === 'ok') return { ok: true }; + if (status === 'address_taken') return { ok: false, reason: 'address_taken' }; + return { ok: false, reason: nonceOrThrow(status) }; +} + +/** Unlink requires a fresh wallet proof. Fresh-MFA is deferred to I2. */ +export async function verifyWalletUnlink( + i: { userId: string; message: string; signature: `0x${string}`; origin: string; now?: Date }, + store: WalletLinkStore, +): Promise<{ ok: true } | { ok: false; reason: Exclude | 'not_linked' }> { + const v = await verifyProof(i); + if (!v.ok) { + if (v.reason === 'address_taken') throw new Error('unreachable'); + return { ok: false, reason: v.reason }; + } + const status = await store.revokeLink({ userId: i.userId, ...v.proof, now: i.now ?? new Date() }); + if (status === 'ok') return { ok: true }; + if (status === 'not_linked') return { ok: false, reason: 'not_linked' }; + return { ok: false, reason: nonceOrThrow(status) }; } /** In-memory store for tests and local development. Not for production. */ export function createMemoryWalletLinkStore(): WalletLinkStore { const challenges = new Map(); const links: { userId: string; chainId: number; address: string; assurance: string; status: 'active' | 'revoked' }[] = []; + function consume(nonce: string, userId: string, now: Date): Challenge | NonceFailure { + const c = challenges.get(nonce); + if (!c) return 'unknown_nonce'; + if (c.userId !== userId) return 'wrong_user'; + if (c.consumedAt) return 'replayed'; + if (c.expiresAt.getTime() <= now.getTime()) return 'expired'; + c.consumedAt = now; + return c; + } return { async putChallenge(c) { if (challenges.has(c.nonce)) throw new Error('duplicate nonce'); challenges.set(c.nonce, { ...c, consumedAt: null }); }, - async consumeChallenge(nonce, userId, now) { - const c = challenges.get(nonce); - if (!c || c.userId !== userId || c.consumedAt || c.expiresAt.getTime() <= now.getTime()) return null; - c.consumedAt = now; - const { consumedAt: _ignored, ...rest } = c; - void _ignored; - return rest; - }, - async challengeStatus(nonce, userId, now) { - const c = challenges.get(nonce); - if (!c) return 'unknown'; - if (c.userId !== userId) return 'other_user'; - if (c.consumedAt) return 'consumed'; - if (c.expiresAt.getTime() <= now.getTime()) return 'expired'; - return 'available'; - }, - async activateLink(l) { - const address = normalizeAddress(l.address); - const existing = links.find((x) => x.status === 'active' && x.chainId === l.chainId && x.address === address); + async linkWallet(l) { + const c = consume(l.nonce, l.userId, l.now); + if (typeof c === 'string') return c; + if (c.chainId !== l.chainId || c.address !== l.address) return 'bad_attestation'; + const existing = links.find((x) => x.status === 'active' && x.chainId === c.chainId && x.address === c.address); if (existing) return existing.userId === l.userId ? 'ok' : 'address_taken'; - links.push({ userId: l.userId, chainId: l.chainId, address, assurance: l.assurance, status: 'active' }); + links.push({ userId: l.userId, chainId: c.chainId, address: c.address, assurance: l.assurance, status: 'active' }); + return 'ok'; + }, + async revokeLink(l) { + const c = consume(l.nonce, l.userId, l.now); + if (typeof c === 'string') return c; + if (c.chainId !== l.chainId || c.address !== l.address) return 'bad_attestation'; + const existing = links.find((x) => x.status === 'active' && x.userId === l.userId && x.chainId === c.chainId && x.address === c.address); + if (!existing) return 'not_linked'; + existing.status = 'revoked'; return 'ok'; }, }; diff --git a/supabase/migrations/20261104000100_wallet_links.sql b/supabase/migrations/20261104000100_wallet_links.sql index 8b8bc6c..7072b36 100644 --- a/supabase/migrations/20261104000100_wallet_links.sql +++ b/supabase/migrations/20261104000100_wallet_links.sql @@ -1,5 +1,23 @@ -- Task 6: server-challenge wallet linking (ERC-4361). -- Depends only on auth.users / auth.uid(); independent of 0200/0300. +-- Trust boundary: the database cannot verify secp256k1 signatures, so the +-- app server verifies the SIWE signature and passes an HMAC attestation +-- (key WALLET_LINK_SECRET == private.wallet_link_settings.hmac_key). Only +-- link_wallet / revoke_wallet_link change link state, and both require it. + +CREATE SCHEMA IF NOT EXISTS extensions; +CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA extensions; + +CREATE SCHEMA IF NOT EXISTS private; +REVOKE ALL ON SCHEMA private FROM PUBLIC, anon, authenticated; +CREATE TABLE IF NOT EXISTS private.wallet_link_settings ( + id integer PRIMARY KEY CHECK (id = 1), + hmac_key text NOT NULL CHECK (length(hmac_key) >= 32) +); +REVOKE ALL ON private.wallet_link_settings FROM PUBLIC, anon, authenticated; +-- Set the key out of band (never in a migration): +-- INSERT INTO private.wallet_link_settings(id, hmac_key) VALUES (1, '') +-- ON CONFLICT (id) DO UPDATE SET hmac_key = EXCLUDED.hmac_key; -- All writes go through SECURITY DEFINER functions that take the user from -- auth.uid(), never from a parameter. API roles get SELECT-own only. @@ -11,8 +29,7 @@ CREATE TABLE IF NOT EXISTS public.wallet_link_challenges ( domain text NOT NULL CHECK (length(domain) BETWEEN 1 AND 255), created_at timestamptz NOT NULL DEFAULT now(), expires_at timestamptz NOT NULL, - consumed_at timestamptz, - activated_at timestamptz + consumed_at timestamptz ); CREATE INDEX IF NOT EXISTS wallet_link_challenges_user_idx ON public.wallet_link_challenges(user_id); @@ -60,7 +77,7 @@ BEGIN RETURN v_expires; END $$; --- Atomic single use: the row-level UPDATE guard means only one caller wins. +-- Internal: atomic single use. The guarded UPDATE means only one caller wins. CREATE OR REPLACE FUNCTION public.consume_wallet_challenge(p_nonce text) RETURNS TABLE(nonce text, user_id uuid, chain_id integer, address text, domain text, expires_at timestamptz) LANGUAGE plpgsql SECURITY DEFINER SET search_path = public, pg_temp AS $$ @@ -76,8 +93,8 @@ BEGIN RETURNING c.nonce, c.user_id, c.chain_id, c.address, c.domain, c.expires_at; END $$; --- Diagnostic status of a nonce from the caller's point of view. Reveals only --- that a nonce belongs to someone else, never whose or which wallet. +-- Status of a nonce from the caller's point of view. Reveals only that a +-- nonce belongs to someone else, never whose or which wallet. CREATE OR REPLACE FUNCTION public.wallet_challenge_status(p_nonce text) RETURNS text LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = public, pg_temp AS $$ @@ -92,59 +109,105 @@ BEGIN RETURN 'available'; END $$; --- Activate a link for the calling user. Requires a challenge this user --- consumed in the last 10 minutes for the same chain/address and not yet --- used for activation. Assurance is capped by the session's aal claim. -CREATE OR REPLACE FUNCTION public.activate_wallet_link(p_chain_id integer, p_address text, p_assurance text) +-- Internal: true when p_att is the server HMAC of uid|chain|address|nonce. +-- Compares SHA-256 of both sides so timing does not track a prefix match. +CREATE OR REPLACE FUNCTION public.wallet_attestation_ok( + p_uid uuid, p_chain_id integer, p_address text, p_nonce text, p_att text +) RETURNS boolean +LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = public, pg_temp AS $$ +DECLARE v_key text; v_expected bytea; v_given bytea; +BEGIN + SELECT hmac_key INTO v_key FROM private.wallet_link_settings WHERE id = 1; + IF v_key IS NULL OR p_att IS NULL OR p_att !~ '^[0-9a-f]{64}$' THEN RETURN false; END IF; + v_expected := extensions.hmac(convert_to(p_uid::text || '|' || p_chain_id || '|' || p_address || '|' || p_nonce, 'UTF8'), + convert_to(v_key, 'UTF8'), 'sha256'); + v_given := decode(p_att, 'hex'); + RETURN extensions.digest(v_expected, 'sha256') = extensions.digest(v_given, 'sha256'); +END $$; + +CREATE OR REPLACE FUNCTION public.wallet_nonce_failure(p_nonce text) RETURNS text +LANGUAGE sql STABLE SECURITY DEFINER SET search_path = public, pg_temp AS $$ + SELECT CASE public.wallet_challenge_status(p_nonce) + WHEN 'other_user' THEN 'wrong_user' WHEN 'consumed' THEN 'replayed' + WHEN 'expired' THEN 'expired' ELSE 'unknown_nonce' END +$$; + +-- Link: consume the caller's nonce, check the server attestation, activate. +-- Returns ok | address_taken | bad_attestation | unknown_nonce | expired | +-- replayed | wrong_user. Assurance is capped by the session's aal claim. +-- Fresh-MFA for linking is deferred to I2. +CREATE OR REPLACE FUNCTION public.link_wallet(p_nonce text, p_att text, p_assurance text) RETURNS text LANGUAGE plpgsql SECURITY DEFINER SET search_path = public, pg_temp AS $$ DECLARE v_uid uuid := auth.uid(); - v_nonce text; + c record; v_claim_aal text := coalesce(nullif(current_setting('request.jwt.claims', true), '')::json->>'aal', 'aal1'); v_assurance text; v_owner uuid; BEGIN IF v_uid IS NULL THEN RAISE EXCEPTION 'not authenticated' USING ERRCODE = '42501'; END IF; - IF p_assurance NOT IN ('aal1', 'aal2') THEN RAISE EXCEPTION 'invalid assurance' USING ERRCODE = '22023'; END IF; + IF p_assurance IS NULL OR p_assurance NOT IN ('aal1', 'aal2') THEN RAISE EXCEPTION 'invalid assurance' USING ERRCODE = '22023'; END IF; v_assurance := CASE WHEN p_assurance = 'aal2' AND v_claim_aal = 'aal2' THEN 'aal2' ELSE 'aal1' END; - UPDATE public.wallet_link_challenges c SET activated_at = now() - WHERE c.nonce = ( - SELECT c2.nonce FROM public.wallet_link_challenges c2 - WHERE c2.user_id = v_uid AND c2.chain_id = p_chain_id AND c2.address = p_address - AND c2.consumed_at IS NOT NULL AND c2.activated_at IS NULL - AND c2.consumed_at > now() - interval '10 minutes' - ORDER BY c2.consumed_at DESC LIMIT 1 - FOR UPDATE SKIP LOCKED) - RETURNING c.nonce INTO v_nonce; - IF v_nonce IS NULL THEN RAISE EXCEPTION 'no verified challenge' USING ERRCODE = '42501'; END IF; + SELECT * INTO c FROM public.consume_wallet_challenge(p_nonce); + IF NOT FOUND THEN RETURN public.wallet_nonce_failure(p_nonce); END IF; + IF NOT public.wallet_attestation_ok(v_uid, c.chain_id, c.address, c.nonce, p_att) THEN + RETURN 'bad_attestation'; + END IF; SELECT user_id INTO v_owner FROM public.wallet_links - WHERE chain_id = p_chain_id AND address = p_address AND status = 'active'; + WHERE chain_id = c.chain_id AND address = c.address AND status = 'active'; IF FOUND THEN IF v_owner = v_uid THEN UPDATE public.wallet_links SET verified_at = now(), assurance = v_assurance - WHERE chain_id = p_chain_id AND address = p_address AND status = 'active'; + WHERE chain_id = c.chain_id AND address = c.address AND status = 'active'; RETURN 'ok'; END IF; RETURN 'address_taken'; END IF; - BEGIN INSERT INTO public.wallet_links(user_id, chain_id, address, assurance) - VALUES (v_uid, p_chain_id, p_address, v_assurance); + VALUES (v_uid, c.chain_id, c.address, v_assurance); EXCEPTION WHEN unique_violation THEN RETURN 'address_taken'; END; RETURN 'ok'; END $$; +-- Revoke: needs a fresh challenge for that exact chain/address, consumed now, +-- plus a valid attestation, and an active link owned by the caller. +-- Returns ok | not_linked | bad_attestation | unknown_nonce | expired | +-- replayed | wrong_user. Fresh-MFA for unlink is deferred to I2. +CREATE OR REPLACE FUNCTION public.revoke_wallet_link(p_chain_id integer, p_address text, p_nonce text, p_att text) +RETURNS text +LANGUAGE plpgsql SECURITY DEFINER SET search_path = public, pg_temp AS $$ +DECLARE v_uid uuid := auth.uid(); c record; v_n integer; +BEGIN + IF v_uid IS NULL THEN RAISE EXCEPTION 'not authenticated' USING ERRCODE = '42501'; END IF; + SELECT * INTO c FROM public.consume_wallet_challenge(p_nonce); + IF NOT FOUND THEN RETURN public.wallet_nonce_failure(p_nonce); END IF; + IF c.chain_id <> p_chain_id OR c.address <> p_address + OR NOT public.wallet_attestation_ok(v_uid, c.chain_id, c.address, c.nonce, p_att) THEN + RETURN 'bad_attestation'; + END IF; + UPDATE public.wallet_links SET status = 'revoked' + WHERE user_id = v_uid AND chain_id = c.chain_id AND address = c.address AND status = 'active'; + GET DIAGNOSTICS v_n = ROW_COUNT; + RETURN CASE WHEN v_n > 0 THEN 'ok' ELSE 'not_linked' END; +END $$; + +-- Remove the earlier, unattested activation path if present. +DROP FUNCTION IF EXISTS public.activate_wallet_link(integer, text, text); + REVOKE ALL ON FUNCTION public.create_wallet_challenge(text, integer, text, text) FROM PUBLIC, anon; -REVOKE ALL ON FUNCTION public.consume_wallet_challenge(text) FROM PUBLIC, anon; REVOKE ALL ON FUNCTION public.wallet_challenge_status(text) FROM PUBLIC, anon; -REVOKE ALL ON FUNCTION public.activate_wallet_link(integer, text, text) FROM PUBLIC, anon; +REVOKE ALL ON FUNCTION public.link_wallet(text, text, text) FROM PUBLIC, anon; +REVOKE ALL ON FUNCTION public.revoke_wallet_link(integer, text, text, text) FROM PUBLIC, anon; +REVOKE ALL ON FUNCTION public.consume_wallet_challenge(text) FROM PUBLIC, anon, authenticated; +REVOKE ALL ON FUNCTION public.wallet_attestation_ok(uuid, integer, text, text, text) FROM PUBLIC, anon, authenticated; +REVOKE ALL ON FUNCTION public.wallet_nonce_failure(text) FROM PUBLIC, anon, authenticated; GRANT EXECUTE ON FUNCTION public.create_wallet_challenge(text, integer, text, text) TO authenticated; -GRANT EXECUTE ON FUNCTION public.consume_wallet_challenge(text) TO authenticated; GRANT EXECUTE ON FUNCTION public.wallet_challenge_status(text) TO authenticated; -GRANT EXECUTE ON FUNCTION public.activate_wallet_link(integer, text, text) TO authenticated; +GRANT EXECUTE ON FUNCTION public.link_wallet(text, text, text) TO authenticated; +GRANT EXECUTE ON FUNCTION public.revoke_wallet_link(integer, text, text, text) TO authenticated; diff --git a/test-db/wallet-links.test.ts b/test-db/wallet-links.test.ts index 9b44647..6784a4d 100644 --- a/test-db/wallet-links.test.ts +++ b/test-db/wallet-links.test.ts @@ -1,37 +1,59 @@ -import { describe, it, expect } from 'vitest'; -import { randomBytes } from 'node:crypto'; +import { describe, it, expect, beforeAll } from 'vitest'; +import { createHmac, randomBytes } from 'node:crypto'; import { sql, asUser, asAnon, makeUsers } from './psql'; +// Random per-run key; never a fixture. +const KEY = randomBytes(32).toString('hex'); const addr = () => '0x' + randomBytes(20).toString('hex'); const nonce = () => randomBytes(12).toString('hex'); +const att = (uid: string, a: string, n: string, chain = 31) => + createHmac('sha256', KEY).update(`${uid}|${chain}|${a}|${n}`).digest('hex'); function createChallenge(n: string, a: string, chain = 31) { return `SELECT public.create_wallet_challenge('${n}', ${chain}, '${a}', 'utp.test') \\g /dev/null\n`; } +async function challenge(user: string, a: string) { + const n = nonce(); + await sql(asUser(user) + createChallenge(n, a)); + return n; +} +const link = (user: string, n: string, at: string | null, assurance = 'aal1') => + sql(asUser(user) + `SELECT public.link_wallet('${n}', ${at === null ? 'NULL' : `'${at}'`}, '${assurance}');`).then((r) => r[0]); +const revoke = (user: string, a: string, n: string, at: string) => + sql(asUser(user) + `SELECT public.revoke_wallet_link(31, '${a}', '${n}', '${at}');`).then((r) => r[0]); +const activeCount = (a: string) => sql(`SELECT count(*) FROM wallet_links WHERE address = '${a}' AND status = 'active';`).then((r) => r[0]); + +beforeAll(async () => { + await sql(`INSERT INTO private.wallet_link_settings(id, hmac_key) VALUES (1, '${KEY}') + ON CONFLICT (id) DO UPDATE SET hmac_key = EXCLUDED.hmac_key;`); +}); describe('wallet link challenges', () => { - it('20 parallel consumes of one nonce by two users: exactly one non-null', async () => { + it('20 parallel link attempts on one nonce by two users: exactly one consumes it', async () => { const [a, b] = await makeUsers(2); - const n = nonce(); - await sql(asUser(a) + createChallenge(n, addr())); + const w = addr(); + const n = await challenge(a, w); const results = await Promise.all( - Array.from({ length: 20 }, (_, i) => - sql(asUser(i % 2 === 0 ? a : b) + `SELECT count(*) FROM public.consume_wallet_challenge('${n}');`), - ), + Array.from({ length: 20 }, (_, i) => (i % 2 === 0 ? link(a, n, att(a, w, n)) : link(b, n, att(b, w, n)))), ); - const winners = results.map((r) => Number(r[0])).reduce((x, y) => x + y, 0); - expect(winners).toBe(1); - expect(await sql(asUser(a) + `SELECT public.wallet_challenge_status('${n}');`)).toEqual(['consumed']); - expect(await sql(asUser(b) + `SELECT public.wallet_challenge_status('${n}');`)).toEqual(['other_user']); + expect(results.filter((r) => r === 'ok')).toHaveLength(1); + expect(results.filter((r) => r === 'wrong_user')).toHaveLength(10); + expect(results.filter((r) => r === 'replayed')).toHaveLength(9); + expect(await activeCount(w)).toBe('1'); }); - it('expired challenges cannot be consumed', async () => { + it('expired challenges cannot be used', async () => { const [a] = await makeUsers(1); - const n = nonce(); - await sql(asUser(a) + createChallenge(n, addr())); + const w = addr(); + const n = await challenge(a, w); await sql(`UPDATE wallet_link_challenges SET expires_at = now() - interval '1 minute' WHERE nonce = '${n}';`); - expect(await sql(asUser(a) + `SELECT count(*) FROM public.consume_wallet_challenge('${n}');`)).toEqual(['0']); - expect(await sql(asUser(a) + `SELECT public.wallet_challenge_status('${n}');`)).toEqual(['expired']); + expect(await link(a, n, att(a, w, n))).toBe('expired'); + expect(await activeCount(w)).toBe('0'); + }); + + it('unknown nonce', async () => { + const [a] = await makeUsers(1); + expect(await link(a, nonce(), 'ab'.repeat(32))).toBe('unknown_nonce'); }); it('rejects disallowed chains and non-normalized addresses', async () => { @@ -41,45 +63,80 @@ describe('wallet link challenges', () => { }); }); -describe('wallet links', () => { - async function prepared(user: string, a: string) { - const n = nonce(); - await sql(asUser(user) + createChallenge(n, a)); - await sql(asUser(user) + `SELECT count(*) FROM public.consume_wallet_challenge('${n}');`); - } - - it('two users racing activateLink for one (chain, address): one ok, one address_taken', async () => { +describe('wallet links (attested)', () => { + it('missing or forged attestation -> bad_attestation, no active row', async () => { const [a, b] = await makeUsers(2); const w = addr(); - await prepared(a, w); - await prepared(b, w); - const results = await Promise.all( - [a, b].map((u) => sql(asUser(u) + `SELECT public.activate_wallet_link(31, '${w}', 'aal1');`)), - ); - expect(results.map((r) => r[0]).sort()).toEqual(['address_taken', 'ok']); - expect(await sql(`SELECT count(*) FROM wallet_links WHERE address = '${w}' AND status = 'active';`)).toEqual(['1']); + expect(await link(a, await challenge(a, w), null)).toBe('bad_attestation'); + expect(await link(a, await challenge(a, w), 'not-hex')).toBe('bad_attestation'); + // HMAC with a wrong key + const n1 = await challenge(a, w); + const forged = createHmac('sha256', 'x'.repeat(64)).update(`${a}|31|${w}|${n1}`).digest('hex'); + expect(await link(a, n1, forged)).toBe('bad_attestation'); + // valid HMAC but for another user / another address + const n2 = await challenge(a, w); + expect(await link(a, n2, att(b, w, n2))).toBe('bad_attestation'); + const n3 = await challenge(a, w); + expect(await link(a, n3, att(a, addr(), n3))).toBe('bad_attestation'); + expect(await activeCount(w)).toBe('0'); }); - it('activation without a consumed challenge for that address is refused', async () => { + it('direct consume / activate / attestation helpers are not callable by authenticated', async () => { const [a] = await makeUsers(1); - await expect(sql(asUser(a) + `SELECT public.activate_wallet_link(31, '${addr()}', 'aal1');`)).rejects.toThrow( - /no verified challenge/, + const w = addr(); + const n = await challenge(a, w); + await expect(sql(asUser(a) + `SELECT * FROM public.consume_wallet_challenge('${n}');`)).rejects.toThrow(/permission denied/); + await expect( + sql(asUser(a) + `SELECT public.wallet_attestation_ok('${a}', 31, '${w}', '${n}', 'x');`), + ).rejects.toThrow(/permission denied/); + await expect(sql(asUser(a) + `SELECT public.activate_wallet_link(31, '${w}', 'aal1');`)).rejects.toThrow( + /does not exist|permission denied/, ); + await expect(sql(asUser(a) + `SELECT hmac_key FROM private.wallet_link_settings;`)).rejects.toThrow(/permission denied/); + expect(await activeCount(w)).toBe('0'); + }); + + it('two users racing for one (chain, address): one ok, one address_taken', async () => { + const [a, b] = await makeUsers(2); + const w = addr(); + const na = await challenge(a, w); + const nb = await challenge(b, w); + const results = await Promise.all([link(a, na, att(a, w, na)), link(b, nb, att(b, w, nb))]); + expect(results.sort()).toEqual(['address_taken', 'ok']); + expect(await activeCount(w)).toBe('1'); }); it('assurance is capped by the session aal claim', async () => { const [a] = await makeUsers(1); const w = addr(); - await prepared(a, w); - await sql(asUser(a) + `SELECT public.activate_wallet_link(31, '${w}', 'aal2');`); + const n = await challenge(a, w); + expect(await link(a, n, att(a, w, n), 'aal2')).toBe('ok'); expect(await sql(`SELECT assurance FROM wallet_links WHERE address = '${w}';`)).toEqual(['aal1']); }); + it('revoke works only for the owner with a valid attestation; address can then be relinked', async () => { + const [a, b] = await makeUsers(2); + const w = addr(); + const n = await challenge(a, w); + expect(await link(a, n, att(a, w, n))).toBe('ok'); + + const nb = await challenge(b, w); + expect(await revoke(b, w, nb, att(b, w, nb))).toBe('not_linked'); + const na1 = await challenge(a, w); + expect(await revoke(a, w, na1, 'ab'.repeat(32))).toBe('bad_attestation'); + const na2 = await challenge(a, w); + expect(await revoke(a, w, na2, att(a, w, na2))).toBe('ok'); + expect(await activeCount(w)).toBe('0'); + + const nb2 = await challenge(b, w); + expect(await link(b, nb2, att(b, w, nb2))).toBe('ok'); + }); + it('RLS: users and anon cannot read others links; no direct writes', async () => { const [a, b] = await makeUsers(2); const w = addr(); - await prepared(a, w); - await sql(asUser(a) + `SELECT public.activate_wallet_link(31, '${w}', 'aal1');`); + const n = await challenge(a, w); + await link(a, n, att(a, w, n)); expect(await sql(asUser(a) + `SELECT count(*) FROM wallet_links WHERE address = '${w}';`)).toEqual(['1']); expect(await sql(asUser(b) + `SELECT count(*) FROM wallet_links WHERE address = '${w}';`)).toEqual(['0']); await expect(sql(asAnon + `SELECT count(*) FROM wallet_links;`)).rejects.toThrow(/permission denied/); @@ -87,7 +144,7 @@ describe('wallet links', () => { sql(asUser(b) + `INSERT INTO wallet_links(user_id, chain_id, address, assurance) VALUES ('${b}', 31, '${addr()}', 'aal1');`), ).rejects.toThrow(/permission denied/); await expect(sql(asUser(a) + `UPDATE wallet_links SET status = 'revoked';`)).rejects.toThrow(/permission denied/); - await expect(sql(asUser(b) + `SELECT count(*) FROM wallet_link_challenges;`)).resolves.toEqual(['0']); - await expect(sql(asAnon + `SELECT public.consume_wallet_challenge('x');`)).rejects.toThrow(/permission denied/); + expect(await sql(asUser(b) + `SELECT count(*) FROM wallet_link_challenges;`)).toEqual(['0']); + await expect(sql(asAnon + `SELECT public.link_wallet('x', 'y', 'aal1');`)).rejects.toThrow(/permission denied/); }); }); From 03cee7033bb1c3ff14ed3a4b5c06a89b7084202f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:41:47 -0800 Subject: [PATCH 15/16] chore(auth): document WALLET_LINK_SECRET in .env.example Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- .env.example | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.env.example b/.env.example index 56a7412..bb3a672 100644 --- a/.env.example +++ b/.env.example @@ -15,6 +15,9 @@ NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co # read it. Anon key is safe to expose to the browser. NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key SUPABASE_SERVICE_ROLE_KEY= +# Wallet linking attestation key: server-only, generate 32+ random bytes; +# must match private.wallet_link_settings in the DB. +WALLET_LINK_SECRET= # Google/GitHub sign-in (/login, src/app/auth/callback): provider client # id/secret are configured in Supabase Dashboard > Authentication > From 23f70bf7ab45bff283597dd32d799bf707a19a01 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 21:46:30 -0800 Subject: [PATCH 16/16] docs: update evidence ledger, state and readiness docs Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SSjN64Ve8j6CJk4iVbbJJV --- .gsd/STATE.md | 39 ++++++------ .../EVIDENCE-LEDGER.md | 60 ++++++++++++++++++- .../MERGE-AND-IMPLEMENTATION-LOOP.md | 6 +- 3 files changed, 80 insertions(+), 25 deletions(-) diff --git a/.gsd/STATE.md b/.gsd/STATE.md index fcf680b..328e58d 100644 --- a/.gsd/STATE.md +++ b/.gsd/STATE.md @@ -1,31 +1,28 @@ # Project State -Last updated: 2026-10-18 PT +Last updated: 2026-11-03 ## References - Roadmap: `.gsd/ROADMAP.md` (only live roadmap) - Product: `brain/prd.md`, `brain/02_TECHNICAL_SPEC.md` -- Brand: `DESIGN.md` (civic glass-teal). Dark uBTC system archived in `archive/design-dark-v1/`. +- Plan: `docs/superpowers/plans/2026-11-03-utp-first-cycle-release-identity-civic.md` +- Evidence: `docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md` (statuses live there; missing evidence is never PASS) +- Brand: `DESIGN.md`. -## Branch -Work branches off `master` (default). `sync/freedom-stack-master` is fully merged and can be deleted. +## Baseline +`master` is protected. PRs #20 and #21 are merged at `777658f` (175 contract tests). CI runs only on the self-hosted Dell runner `dell-utp-1`. R2 (Vercel production target) is BLOCKED_ON_AUTH. -## Current phase -Phase 2: RSK testnet deployment (Phases 0 and 1 done 2026-10-18). +## Branch `feat/first-cycle-auth-civic` (NOT pushed, NOT merged; awaiting Sam's authorization) +Head `03cee70` on top of `777658f`: +- Supabase session/OAuth auth with same-origin redirect guard; `requireUser` (cookie and Bearer). +- Source adapter model with federal (Congress.gov) slice and OpenStates adapter; no fixture is ever marked live; sample content is labelled "Sample data". +- Authenticated `/api/vote` with DB-enforced one vote per user per bill and recounted aggregates; authenticated bounded `/api/propose`. +- Server-verified wallet linking (SIWE plus HMAC attestation checked in a SECURITY DEFINER RPC). +- Four migrations: `20261104000000_vote_integrity`, `0100_wallet_links`, `0200_source_registry`, `0300_voting_proposals`. -## Verified (2026-10-18, branch feat/phase0-1-governance) -- lint, typecheck, next build, hardhat compile: PASS -- `npm test`: 175 contract tests passing, 0 failing (was 128/14); no frontend unit tests yet -- Deploy rehearsal: in-process + local node over RPC, 11/11 invariants, ~8.9M gas total -- Smoke lifecycle on local node: propose → vote → queue → timelock → execute OK; 0 reverts -- Failure paths: mainnet refused; smoke refuses without a deployment record -- RSK testnet: NOT deployed. The sandbox cannot reach RSK RPC; deploy runs from the GitHub Actions workflow with the owner's secret. -- Frontend: real UI on `/`, `/issue/[id]`, `/how-it-works`, `/us/california/[bill]`; `/governance`, `/token`, `/stablecoin`, `/events`, `/news`, `/legislature` are placeholders +Final gate at head `03cee70`: lint, typecheck, vitest (107), build, `test:db` (27, shim PostgreSQL 16, not real Supabase), contracts (175, via SOLC_JS_PATH) and `git diff --check` all exit 0. Not verified: Dell CI on this head, real Supabase, live providers. -## Decisions -- D1: v1 = standard wallet voting; PQ hybrid optional/off; ZK deferred. -- D2: testnet params in `scripts/deploy.config.cjs`. - -## Next action -1. Owner: create the `rsk-testnet` GitHub environment with `DEPLOYER_PRIVATE_KEY` (testnet-only key, funded from the RSK faucet), then run "Deploy governance to RSK testnet". -2. Build `/governance` + `/token` UI against `deployments/rskTestnet.json`. +## Next actions +1. Prepared external steps in the ledger, each needs Sam's authorization: push and PR; `supabase db push --dry-run` then apply the four migrations (with the preflights); secrets (`WALLET_LINK_SECRET`, `CONGRESS_API_KEY`, OpenStates key, `IP_SALT`); Vercel team access for R2. +2. Open decisions: U1 Docker on the Dell runner (until then `test:db` is a local gate); U2 local-government denominator (blocks C1 local); U3 target Supabase project for the migrations; U4 Privy sign-in visibility in `TopBar`. +3. Deferred: TOTP/phone/camera proofing (I2); ERC-1271 contract-wallet signatures; G1/G2 Rootstock governance lifecycle; RSK testnet deploy (owner creates the `rsk-testnet` environment secret). diff --git a/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md b/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md index 69fcb8f..0a867f5 100644 --- a/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md +++ b/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md @@ -9,7 +9,65 @@ Environment: Claude cloud container (Linux, Node 22.23.4, npm 10.9.9, PostgreSQL | A-baseline-web | PASS | 777658f | `npm run lint && npm run typecheck && npm run test:web` (inside `npm run ci:fast`) | 0 | lint clean, typecheck clean, 2/2 web tests | | A-baseline-contracts | PASS | 777658f | `SOLC_JS_PATH= npm run test:contracts` | 0 | 175 passing. Plain `ci:fast` fails here only because the sandbox cannot download solc (HH502 ENOTFOUND binaries.soliditylang.org) | | A-zero-test-guard | PASS | 777658f | `npx vitest run src/__no_such_dir__` | 1 | Vitest exits non-zero when no test files are found | -| R2-vercel-production-target | BLOCKED | 777658f | Vercel MCP read tools (`get_project`, `list_deployments`, `list_project_domains`, `get_deployment`) | 403 | `forbidden` for scope `samtheloanmans-projects` (`team_D2rNpq3e1jGfA7HanfuwMX3O`); productionBranch and deployment targets NOT_DETERMINED. Needs Sam to authorize team access | +| R2-vercel-production-target | BLOCKED_ON_AUTH | 777658f | Vercel MCP read tools (`get_project`, `list_deployments`, `list_project_domains`, `get_deployment`) | 403 | `forbidden` for scope `samtheloanmans-projects` (`team_D2rNpq3e1jGfA7HanfuwMX3O`); productionBranch and deployment targets NOT_DETERMINED. Needs Sam to authorize team access | +| I1-redirect | PASS | 54aadea | `npx vitest run src/lib/supabaseAuth` (redirect guard, callback, login, consent, decision tests; in final gate vitest) | 0 | same-origin `safeRedirectPath`; deferred: `/%5c`, double-encoded `/%252f%252f`, missing-code callback tests. Current-head: final gate below | +| I1-requireUser | PASS | 4e6eee2 | `npx vitest run` (requireUser tests; final gate) | 0 | cookie and Bearer paths via `getUser()`; deferred: thrown client => unhandled 500 (not fail-closed), transient network error reported 401 not 503 | +| C2-vote-integrity | PASS (shim DB) | 6892d49 | `npm run test:db` + `npx vitest run` (final gate) | 0 | authenticated `/api/vote`, one-vote-per-user-per-bill, advisory-locked aggregate recount, tamper test, concurrency on a throwaway PG16 cluster with an auth shim (Ruling P2). NOT real Supabase, see DB-on-real-Supabase | +| C2-propose | PASS (shim DB) | b38491c | `npm run test:db` + `npx vitest run` (final gate) | 0 | authenticated, bounded `/api/propose`; unique `source_url` index; GET hides `proposer_id`; legacy-table migration test. Needs preflights before remote apply (see Prepared external steps) | +| I1-wallet-link | PASS (shim DB) | 03cee70 | `npm run test:db` + `npx vitest run` (final gate) | 0 | SIWE-verified link/unlink; server HMAC attestation (`WALLET_LINK_SECRET`) checked in a SECURITY DEFINER RPC; wrong account / replayed nonce / forged signature rejected. EOA only: ERC-1271 unsupported; unlink has no fresh MFA (I2) | +| C1-federal | PASS (mocked) | 64f94ef | `npx vitest run` (adapter tests, final gate) | 0 | Congress.gov adapter, source URL, jurisdiction, version/`recordHash`, fetch time; provider failure never returns fixture as live. Live fetch NOT_RUN (no key) | +| C1-state | PASS (adapter on mocks) / BLOCKED (live) | 64f94ef | `npx vitest run` (adapter tests, final gate) | 0 / n/a | OpenStates adapter passes on mocks. Live ingestion BLOCKED: no OPENSTATES key provisioned | +| C1-local | BLOCKED | 64f94ef | none | n/a | Local-government denominator (Legistar clients, municipalities/counties) undecided: plan decision U2 | +| UI-honesty | PASS | 2b14387 | `npx vitest run` (UI tests, final gate) | 0 | sample content labelled "Sample data", votes gated on Supabase session, no sample-as-live; removed Privy gate and on-chain castVote from home feed (Ruling, Task 8) | +| DB-on-real-Supabase | NOT_RUN | n/a | `supabase start` / `supabase db reset` | n/a | no Docker (U1); `test:db` used a shim PG16 cluster (Ruling P2). Real-Supabase RLS/auth behaviour is unverified | + +### Final gate, head `03cee70` (2026-11-03, this container) + +| step | command | exit | result | +|---|---|---|---| +| lint | `npm run lint` | 0 | clean | +| typecheck | `npm run typecheck` | 0 | clean | +| web tests | `npx vitest run` | 0 | 21 files, 107 tests passed | +| build | `npm run build` | 0 | Next production build OK | +| db tests | `npm run test:db` | 0 | 4 files, 27 tests passed (shim PG16, not real Supabase) | +| contracts | `SOLC_JS_PATH=/tmp/solc824/node_modules/solc/soljson.js npm run test:contracts` | 0 | 175 passing | +| whitespace | `git diff --check` | 0 | clean | + +Not covered by this gate: CI on the Dell runner for this head (branch not pushed), Vercel preview, any remote database, live provider calls, R2. + +## Prepared external steps (awaiting Sam's authorization) + +Nothing below has been executed. This session has read-only GitHub access and no linked Supabase project. + +### a. Push and PR +``` +git push -u origin feat/first-cycle-auth-civic +gh pr create --base master --head feat/first-cycle-auth-civic --title "First cycle: auth, civic ingestion, vote integrity, wallet link, UI honesty" +``` +Sam or an authorized session runs these. `master` is protected: required checks must pass on the exact head (Dell runner `dell-utp-1`). Status: NOT_RUN. + +### b. Migrations (apply in this order) +1. `20261104000000_vote_integrity.sql` +2. `20261104000100_wallet_links.sql` +3. `20261104000200_source_registry.sql` +4. `20261104000300_voting_proposals.sql` + +`supabase db push --dry-run`: NOT_RUN (no linked project; target project undecided, plan decision U3). Run it before any real push, and only after Sam picks the target. + +Preflights against the target database (read-only SQL): +- Duplicate non-empty source URLs, which would make the new unique index fail: + `select source_url, count(*) from public.voting_proposals where coalesce(source_url,'') <> '' group by source_url having count(*) > 1;` must return no rows. +- Remote `voting_proposals` column shape: confirm via `information_schema.columns` that `description`, `bias_score`, `category` and the other columns the GET route selects exist (the explicit-column GET returns 500 if they do not). + +### c. Secret provisioning (values never in the repo) +- `WALLET_LINK_SECRET`: Vercel environment variable, and the same key stored in `private.wallet_link_settings` via the SQL editor or Vault. +- `CONGRESS_API_KEY` and the OpenStates API key: in the approved secret stores. Until set, C1 live ingestion stays BLOCKED. +- `IP_SALT`: set in production (vote IP hashing). + +### d. Vercel +Authorize team access for scope `samtheloanmans-projects` so the R2 investigation can read `productionBranch` and deployment targets (403 today). Sam's decision. + +Stop after each step and wait for authorization. ## Provenance of imported files (from the Mac checkout, 2026-11-03) diff --git a/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md b/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md index ae4bf4f..b5c5589 100644 --- a/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md +++ b/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md @@ -64,10 +64,10 @@ Record base/head SHAs, dirty state, changed paths, relevant threat findings, tes |---|---|---|---| | R1 | None | Consolidate #21 into #20 and fix release checks | Combined head passes nonempty web tests, 175+ contracts, build and exact-head Dell CI; preview matches head. | | R2 | R1 | Protect `master`, verify Vercel production target and rollback | Required checks reject an untested head; deploy the exact clean master SHA with production environment after post-merge CI, verify public alias and route smoke, and retain prior deployment for rollback. Reconcile why Git integration built `master` as Preview. **Status (2026-11-03): BLOCKED_ON_AUTH** (cause `NOT_DETERMINED`; see R2 finding below). | -| I1 | R1 | Canonical Supabase civic account and linked wallet | Wrong account, replayed challenge and mismatched signature cannot link a wallet; session refresh persists. | +| I1 | R1 | Canonical Supabase civic account and linked wallet | Wrong account, replayed challenge and mismatched signature cannot link a wallet; session refresh persists. **Status 2026-11-03:** implemented on `feat/first-cycle-auth-civic` (head `03cee70`), not merged, awaiting authorization; wallet-link tests pass on a shim DB only, real Supabase NOT_RUN. See EVIDENCE-LEDGER. | | I2 | I1 | Tiered credential proofing and local encrypted wallet | Phone/TOTP/camera signals remain separate; lost-device and recovery tests do not silently lower assurance. Synthetic credentials only. | -| C1 | R1 | Source registry and one federal/state/local ingestion slice | Each record has source URL, jurisdiction, version and fetch time; bad key/provider failure never yields a fixture marked live. | -| C2 | I1,C1 | Proposal/request draft and civic vote | Unauthorized write fails; duplicate/concurrent votes have one authoritative tally; no message to officials occurs in tests. | +| C1 | R1 | Source registry and one federal/state/local ingestion slice | Each record has source URL, jurisdiction, version and fetch time; bad key/provider failure never yields a fixture marked live. **Status 2026-11-03:** federal slice and state adapter implemented on `feat/first-cycle-auth-civic`, mock-tested only, not merged, awaiting authorization; live fetch BLOCKED (no keys); local slice BLOCKED (decision U2). | +| C2 | I1,C1 | Proposal/request draft and civic vote | Unauthorized write fails; duplicate/concurrent votes have one authoritative tally; no message to officials occurs in tests. **Status 2026-11-03:** authenticated vote and propose implemented on `feat/first-cycle-auth-civic`, tested on a shim DB only, not merged, awaiting authorization; migrations not applied remotely. | | C3 | C1,C2 | Nationwide coverage expansion | Per-jurisdiction ledger identifies provider, recency, gaps and correction path; no unverified locale is marked covered. | | G1 | R1 | Governance manifest, roles and app UI | Contract bytecode/chain mismatch blocks actions; token allocation and all role holders are disclosed. | | G2 | G1 | Rootstock testnet lifecycle | Independently controlled actors complete proposal, vote, queue, wait and execute; failed/cancelled cases and second-RPC read-back pass. |