From 480232838fa3e7f19e46e08a80ee27e19825b4c3 Mon Sep 17 00:00:00 2001 From: OmO Orchestrator Date: Fri, 20 Nov 2026 03:49:44 -0500 Subject: [PATCH] feat(fairdraw): surface the validateReveal integrity verdict in the UI RevealDraw claimed a re-roll after a published commit is auditable+rejected, but validateReveal had zero production callers. The reveal flow now replays the signed log and renders a Draw integrity: valid/rejected badge; a duplicate or pre-commit reveal flips it to rejected. --- src/miniapps/fairdraw/RevealDraw.module.css | 19 +++++ src/miniapps/fairdraw/RevealDraw.test.tsx | 80 ++++++++++++++++++++- src/miniapps/fairdraw/RevealDraw.tsx | 45 +++++++++++- 3 files changed, 142 insertions(+), 2 deletions(-) diff --git a/src/miniapps/fairdraw/RevealDraw.module.css b/src/miniapps/fairdraw/RevealDraw.module.css index 87d76ff..e579c81 100644 --- a/src/miniapps/fairdraw/RevealDraw.module.css +++ b/src/miniapps/fairdraw/RevealDraw.module.css @@ -146,6 +146,25 @@ color: var(--color-alert); } +.integrityValid, +.integrityRejected { + align-self: flex-start; + padding: 0.35rem 0.8rem; + border-radius: 999px; + font-size: 0.78rem; + font-weight: 700; +} + +.integrityValid { + background: rgba(46, 204, 113, 0.12); + color: #2ecc71; +} + +.integrityRejected { + background: rgba(229, 72, 77, 0.12); + color: var(--color-alert); +} + .winner { font-size: 0.9rem; font-weight: 700; diff --git a/src/miniapps/fairdraw/RevealDraw.test.tsx b/src/miniapps/fairdraw/RevealDraw.test.tsx index d16a1ac..eeab451 100644 --- a/src/miniapps/fairdraw/RevealDraw.test.tsx +++ b/src/miniapps/fairdraw/RevealDraw.test.tsx @@ -4,7 +4,7 @@ import { act, cleanup, render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { db } from "../../lib/db"; -import { readAll } from "../../lib/eventlog"; +import { append, readAll } from "../../lib/eventlog"; import { commit, drawOutcome, @@ -351,6 +351,84 @@ describe("RevealDraw append-to-log commit requirement (plan todo 8, require a pu }); }); +describe("RevealDraw replay-derived integrity badge (re-roll is auditable and rejected)", () => { + const PARTICIPANTS = ["Resident A", "Resident B"]; + + it("renders a replay-derived 'valid' integrity badge for a clean single commit+reveal draw", async () => { + await init(); + const user = userEvent.setup(); + render(); + + await act(async () => { + await user.click(screen.getByRole("button", { name: "Start Draw" })); + }); + await waitFor(() => + expect(screen.getByTestId("reveal-draw-commit-hash")).toBeInTheDocument(), + ); + await act(async () => { + await user.click(screen.getByRole("button", { name: "Reveal" })); + }); + await waitFor(() => expect(screen.getByTestId("reveal-draw-revealed")).toBeInTheDocument()); + + const badge = screen.getByTestId("reveal-draw-integrity-badge"); + expect(badge).toHaveAttribute("data-state", "valid"); + expect(badge).toHaveTextContent("Draw integrity: valid"); + + // The badge must reflect the SAME validateReveal() verdict an external + // auditor would compute over the real, cold-replayed signed log - one + // published commit strictly before exactly one verifying reveal. + const log = await readAll(); + const commitEntry = log.find((e) => isFairDrawCommitPayload(e.payload)); + expect(commitEntry).toBeDefined(); + const { drawId } = commitEntry!.payload as { drawId: string }; + expect(log.filter((e) => isFairDrawRevealPayload(e.payload))).toHaveLength(1); + expect(await validateReveal(log, drawId)).toBe(true); + }); + + it("flips the integrity badge to 'rejected' when a second fairdraw.reveal (a re-roll) is appended for the same drawId", async () => { + await init(); + const user = userEvent.setup(); + + // Simulate an auditable re-roll: the drawer re-broadcasts a SECOND reveal + // for the same published drawId. Both reveals go through the real signed + // append() path, so the component's own validateReveal() replay over the + // real log (readAll) sees two reveals for one drawId and rejects it. + const appendDrawEvent: typeof append = async (payload) => { + const result = await append(payload); + if ((payload as { type?: string }).type === "fairdraw.reveal") { + await append(payload); + } + return result; + }; + + render(); + + await act(async () => { + await user.click(screen.getByRole("button", { name: "Start Draw" })); + }); + await waitFor(() => + expect(screen.getByTestId("reveal-draw-commit-hash")).toBeInTheDocument(), + ); + await act(async () => { + await user.click(screen.getByRole("button", { name: "Reveal" })); + }); + await waitFor(() => expect(screen.getByTestId("reveal-draw-revealed")).toBeInTheDocument()); + + const badge = screen.getByTestId("reveal-draw-integrity-badge"); + expect(badge).toHaveAttribute("data-state", "rejected"); + expect(badge).toHaveTextContent("rejected"); + + // Prove the log really holds the duplicate reveal the badge rejected - + // the verdict is replay-derived, not faked. + const log = await readAll(); + const reveals = log.filter((e) => isFairDrawRevealPayload(e.payload)); + expect(reveals).toHaveLength(2); + const commitEntry = log.find((e) => isFairDrawCommitPayload(e.payload)); + const { drawId } = commitEntry!.payload as { drawId: string }; + expect(await validateReveal(log, drawId)).toBe(false); + }); +}); + describe("RevealDraw empty-participants guard", () => { it("shows an empty-state note and no Start Draw button when participants is empty - never commits an unresolvable draw", async () => { const appendDrawEvent = vi.fn().mockResolvedValue({ id: 1 }); diff --git a/src/miniapps/fairdraw/RevealDraw.tsx b/src/miniapps/fairdraw/RevealDraw.tsx index 1e4e5d5..3d00a9d 100644 --- a/src/miniapps/fairdraw/RevealDraw.tsx +++ b/src/miniapps/fairdraw/RevealDraw.tsx @@ -6,6 +6,7 @@ import { hashParticipants, isFairDrawCommitPayload, reveal, + validateReveal, verify, type Revealed, } from "../../lib/fairdraw"; @@ -49,6 +50,17 @@ type Phase = revealed: Revealed; verified: boolean; winner?: string; + /** + * Replay-derived audit verdict for THIS draw, computed from + * `validateReveal()` (src/lib/fairdraw.ts) over the whole signed Event + * Log - `true` only if the log holds exactly one published commit + * strictly before exactly one verifying reveal for this `drawId`. A + * re-roll (a second `fairdraw.reveal` for the same `drawId`) or a + * reveal with no prior published commit flips this to `false`, which + * is what makes the screen's "re-rolling ... is auditable and + * rejected" claim real rather than aspirational. + */ + integrity: boolean; }; export interface RevealDrawProps { @@ -236,6 +248,18 @@ export function RevealDraw({ } } + // Replay the signed Event Log and compute THIS draw's audit verdict with + // the SAME `validateReveal()` an external log auditor would run - the + // function was previously called only from tests, so the screen's + // "re-rolling ... is auditable and rejected" claim had no in-UI teeth. + // It is a pure read over `{ payload, lamportTs }` entries, identical on a + // live log or a cold-replayed one, and flips to `false` the moment a + // second `fairdraw.reveal` (a re-roll) or a pre-commit reveal exists for + // this `drawId`. Read through the same `readDrawLog` seam the commit gate + // uses so it stays the real `readAll()` in production. + const auditLog = await (readDrawLog ?? readAll)(); + const integrity = await validateReveal(auditLog, committed.drawId); + // Best-effort cross-device sync (plan todo 3): fire-and-forget, never // blocks or affects the local-first reveal/credit writes that just // completed above. @@ -248,8 +272,17 @@ export function RevealDraw({ revealed: published, verified, winner, + integrity, }); - }, [phase, participants, participantAccounts, tamperReveal, appendWalletCredit, appendDrawEvent]); + }, [ + phase, + participants, + participantAccounts, + tamperReveal, + appendWalletCredit, + appendDrawEvent, + readDrawLog, + ]); return (
@@ -319,6 +352,16 @@ export function RevealDraw({ {phase.verified ? "\u2713 Verified" : "\u2717 Invalid"} +
+ {phase.integrity + ? "Draw integrity: valid" + : "Draw integrity: rejected (duplicate or pre-commit reveal)"} +
+ {phase.verified ? (
Winner: {phase.winner}