diff --git a/.env.example b/.env.example index dd7a6a4..bb3a672 100644 --- a/.env.example +++ b/.env.example @@ -10,7 +10,28 @@ 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= +# 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 > +# 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= 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/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..0a867f5 --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/EVIDENCE-LEDGER.md @@ -0,0 +1,89 @@ +# 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 | +| 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) + +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/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..b5c5589 --- /dev/null +++ b/docs/testnet-readiness-2026-10-31/MERGE-AND-IMPLEMENTATION-LOOP.md @@ -0,0 +1,91 @@ +# 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. **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. **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. **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. | +| 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. + +### 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. 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. 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..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", @@ -31,6 +32,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/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/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..12b2f59 100644 --- a/src/app/api/legislature/route.ts +++ b/src/app/api/legislature/route.ts @@ -1,130 +1,9 @@ import { NextRequest, NextResponse } from 'next/server'; +import { getLegislature } from '@/lib/civic/legislature'; -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(); - - const data = await res.json(); - if (!data.bills) return getFallbackUSBills(); - - 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 { - 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 []; - - const data = await res.json(); - if (!data.items) return []; - - return data.items.map((b: Record) => ({ - id: `uk-${b.billId}`, - 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}`, - })); - } catch { - console.error('UK Parliament fetch failed'); - return []; - } -} - -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 const runtime = 'nodejs'; export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); - const country = searchParams.get('country') || 'US'; - - let bills: Bill[] = []; - - 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(); - } - - return NextResponse.json({ - bills, - total: bills.length, - country, - timestamp: new Date().toISOString(), - }); + return NextResponse.json(await getLegislature(searchParams.get('country') || 'US')); } 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/api/propose/route.test.ts b/src/app/api/propose/route.test.ts new file mode 100644 index 0000000..4a68b6b --- /dev/null +++ b/src/app/api/propose/route.test.ts @@ -0,0 +1,138 @@ +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); +const select = vi.fn<(cols?: string) => unknown>(() => chain); +chain.select = select; +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(); + select.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); + }); + + 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 658f0c2..d74a5d8 100644 --- a/src/app/api/propose/route.ts +++ b/src/app/api/propose/route.ts @@ -1,79 +1,114 @@ 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; +// 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; + +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') - .select('*') + .select(PUBLIC_COLUMNS) .eq('status', status) .order('created_at', { ascending: false }) .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/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/app/api/wallet/challenge/route.test.ts b/src/app/api/wallet/challenge/route.test.ts new file mode 100644 index 0000000..ddc1f66 --- /dev/null +++ b/src/app/api/wallet/challenge/route.test.ts @@ -0,0 +1,59 @@ +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 }); + 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 () => { + 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..0a44b7a --- /dev/null +++ b/src/app/api/wallet/challenge/route.ts @@ -0,0 +1,38 @@ +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 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; + + 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, secret); + 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..9b0f8c8 --- /dev/null +++ b/src/app/api/wallet/link/route.test.ts @@ -0,0 +1,103 @@ +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, 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 }) }; +} + +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 () => { + 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, sending the HMAC attestation to link_wallet', 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); + expect(await res.json()).toEqual({ ok: true }); + 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(); + 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(); + 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(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..5382bae --- /dev/null +++ b/src/app/api/wallet/link/route.ts @@ -0,0 +1,6 @@ +import { handleWalletProof } from '@/lib/wallet/proofRoute'; +import { verifyWalletLink } from '@/lib/wallet/walletLink'; + +export async function POST(req: Request) { + 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/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/globals.css b/src/app/globals.css index b1101c8..0643a56 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -286,4 +286,13 @@ button{font-family:inherit;cursor:pointer;} .hero-content{padding:26px 22px;} .dhero-inner{padding:20px;} } -@media(prefers-reduced-motion:reduce){*{animation:none!important;transition:none!important;}} \ No newline at end of file +@media(prefers-reduced-motion:reduce){*{animation:none!important;transition:none!important;}} +/* ---- Data status badge (DataStatusBadge) ---- */ +.data-status{display:inline-flex;align-items:center;gap:4px;font-size:.72rem;font-weight:650;letter-spacing:.01em;white-space:nowrap; + padding:2px 8px;border-radius:999px;background:var(--primary-soft);color:var(--primary-deep);} +.mchip .data-status{background:transparent;color:inherit;padding:0;} +.data-status-live{background:var(--for-soft);color:var(--for);} +.data-status-stale{background:var(--accent-soft);color:var(--accent);} +.data-status-unavailable,.data-status-not_integrated{background:var(--unsure-soft);color:var(--muted);} +.data-status-sample{background:var(--primary-soft);color:var(--primary-deep);} +.data-status-time{font-weight:500;opacity:.8;} diff --git a/src/app/issue/[id]/page.test.tsx b/src/app/issue/[id]/page.test.tsx new file mode 100644 index 0000000..1d4eee0 --- /dev/null +++ b/src/app/issue/[id]/page.test.tsx @@ -0,0 +1,23 @@ +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); + const t = document.body.textContent || ''; + expect(t).not.toMatch(/verified|Live|live issues|Updated continuously/); + expect(t).toContain('Sample figures — illustrative only'); + }); +}); diff --git a/src/app/issue/[id]/page.tsx b/src/app/issue/[id]/page.tsx index 5d087b8..f2fef6a 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}
} @@ -277,10 +210,10 @@ 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}
@@ -296,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`}

@@ -310,7 +243,7 @@ export default function DetailView() {