Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Build output & caches
.next/
out/
coverage/

# Dependency lockfiles & generated TS artifacts
package-lock.json
next-env.d.ts
tsconfig.tsbuildinfo

# Drizzle-generated migration metadata (machine-written; not hand-edited)
drizzle/meta/
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,45 @@ compare links at the bottom are pointed at the `v1.0.0` tag.
wraps long lines onto a second line, so both stay fully readable (desktop is
unchanged).

#### Gameplay

- **Food is no longer clipped from an adjacent lane mid-merge:** food now
resolves strictly against the runner's nearest lane, so sliding toward a
booster can't accidentally eat the junk in the lane you're leaving — booster
gauntlets stay dodgeable as designed.

#### Internals

- **Per-match track length** is read from the active track instead of the
shared default, removing a latent mismatch if a future seed changes length.
- **Lobby host/track** are read from the host's own presence rather than
whichever seat happened to arrive first.

#### Tooling & docs

- **Repo is Prettier-clean** and CI now runs `format:check` (added a
`.prettierignore` for build output and generated migration metadata), so
formatting drift can't land again.
- **Integration tests run locally and in CI:** `test:integration` auto-loads
`.env.test` (via `--env-file-if-exists`), and a new
`tests/integration/nonce-store.test.ts` covers the durable `auth_nonces`
replay guard against a real Neon test branch.
- **Docs corrected to match the implementation:** rolling 7-day session (not a
60-minute timeout), auth methods are NIP-07 / NIP-46 / nsec (not `nostr-login`),
`.env.local` setup, the scoring table (booster bonus added, the unimplemented
sprint/overtake row removed), the ±10s NIP-98 window, and the junk-food copy
(bounded bathroom setback, not "back to the start").

### Security

- **Host-only race start:** the `control` (start) event is now verified against
the match host (signed by the host's identity), so a rogue peer subscribed to
the channel can no longer force-start the race for everyone.
- **Durable NIP-98 replay protection:** single-use login nonces moved from
in-process memory to a Postgres `auth_nonces` table, so a captured
`Authorization` header can't be replayed by landing on a different Vercel
serverless instance.

<!--
At release time, replace the line below with:
[1.0.0]: https://github.com/bitbybit-ar/bitbybit-run/releases/tag/v1.0.0
Expand Down
24 changes: 13 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,30 @@ Out of scope:
Lightning wallets, hosting).
- Theoretical issues with no demonstrated impact.

### Hardening already in place
### Security hardening

- HTTPS-only via Vercel.
- Auth is Nostr-based (NIP-07 / NIP-46) — no passwords are ever stored.
The session is a JWT signed with `AUTH_SECRET`, which is required in
production.
- The session cookie uses the `__Host-` prefix in production (Secure,
`Path=/`, no `Domain`), blocking subdomain cookie injection from any
`*.bitbybit.com.ar` service. Sessions use a sliding 60-minute
inactivity timeout.
`*.bitbybit.com.ar` service. Sessions use a rolling 7-day window,
refreshed on activity (see [`docs/AUTH.md`](docs/AUTH.md)).
- Secrets (`AUTH_SECRET`, the `DATABASE_URL` Postgres connection
string) live in environment variables and never reach the client;
server-only modules are kept out of client bundles.
- All external links open in a new tab with `rel="noopener noreferrer"`.

### Hardening still in progress

- A Content-Security-Policy and the related security headers (HSTS,
X-Frame-Options, X-Content-Type-Options, Referrer-Policy,
Permissions-Policy) are not yet configured. A CSP must allow `wss:`
and `https:` in `connect-src` so the NIP-46 relay handshake keeps
working (see the TODO in `next.config.ts`).
- **Security response headers** on every response (`next.config.ts`): a
Content-Security-Policy (`frame-ancestors 'none'`, `object-src 'none'`,
`base-uri 'self'`), `X-Frame-Options: DENY`, `X-Content-Type-Options: nosniff`,
a `Referrer-Policy`, and a `Permissions-Policy`. The CSP is kept to
framing/plugin/base directives only (no `connect-src`) so the many origins the
app talks to — Nostr relays over `wss:`, arbitrary avatar/LNURL hosts over
`https:` — keep working.
- **NIP-98 login is single-use** (durable `auth_nonces` table) and the realtime
layer rejects spoofed frames + non-host race starts (see
[`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) §4.5).

## Open source license

Expand Down
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

Four runners line up on an athletics track and sprint for the finish. Fuel up at
the stations along the way, but choose wisely: good food keeps your pace, while
**junk sends you straight back to the start**. First across the line wins — and
rivals can **zap** the winner real sats over Lightning.
**too much junk sends you to the bathroom and costs you hard-won ground**. First
across the line wins — and rivals can **zap** the winner real sats over
Lightning.

The twist under the hood: **there's no game server**. Real-time multiplayer runs
entirely over public **Nostr relays**, and you sign in with your **Nostr**
identity — no email, no password.

> 🎮 **Play it live:** **[run.bitbybit.com.ar](https://run.bitbybit.com.ar)** —
> try the free demo with no sign-in.

## 🌱 Mission

BitByBit RUN is fun first — the learning rides along for free. The same race
Expand All @@ -30,9 +34,13 @@ a click. Learning through play, one bit at a time.
- **i18n:** `next-intl` (Spanish default, English) with `[locale]` routing
- **Theme:** `next-themes` light/dark via `data-theme`
- **Realtime multiplayer:** Nostr ephemeral events as the transport (no server)
- **Auth:** Nostr login (NIP-07)
- **Auth:** Nostr login (NIP-07 browser extension, NIP-46 remote signer, or nsec)
- **Database:** Neon (serverless Postgres) + Drizzle ORM
- **Payments:** zap the winner via their Lightning address (WebLN)
- **PWA:** installable app via `serwist` (web manifest, generated icons, mobile
install prompt) — single-player **practice keeps working offline**
- **Security:** CSP + security headers, single-use NIP-98 login (anti-replay),
host-only race start, `__Host-` session cookie

## 🚀 Quick start

Expand All @@ -41,7 +49,7 @@ npm install
npm run dev # http://localhost:3000 (ES default; /en for English)
```

Open **`/play`** to jump into the prototype.
Open **`/play`** to jump straight into a race.

> 🧭 **New here, or judging?** Follow the [Guided Tour](docs/TOUR.md) to try every
> feature in ~10 minutes.
Expand All @@ -55,6 +63,7 @@ npm run typecheck # tsc --noEmit
npm test # full vitest suite
npm run test:unit # unit tests only
npm run test:integration # integration tests (needs a test database)
npm run format:check # Prettier (CI-enforced); `npm run format` to fix
npm run db:migrate # apply Drizzle migrations
```

Expand All @@ -76,6 +85,7 @@ Everything runs on free tiers — Vercel (hosting), public Nostr relays
- [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) — how it works, free & serverless
- [`docs/AUTH.md`](docs/AUTH.md) — the Nostr login flow
- [`docs/CHARACTERS.md`](docs/CHARACTERS.md) — the character/sprite pipeline
- [`docs/PWA.md`](docs/PWA.md) — install as an app: manifest, icons, install prompt
- [`docs/TESTING.md`](docs/TESTING.md) — automated tests & CI
- [`CHANGELOG.md`](CHANGELOG.md) — release notes (per version)

Expand All @@ -84,7 +94,7 @@ Everything runs on free tiers — Vercel (hosting), public Nostr relays
- **[`bitbybit-cursats`](https://github.com/bitbybit-ar/bitbybit-cursats)** —
sibling project in the same family.
- **[`bitbybit-arena`](https://github.com/bitbybit-ar/bitbybit-arena)** —
public Nostr challenges with badges and zaps.
public Nostr challenges with badges and zaps.
- **[`bitbybit-habits`](https://github.com/bitbybit-ar/bitbybit-habits)** —
habit tracker with Lightning rewards.

Expand Down
90 changes: 44 additions & 46 deletions app/[locale]/opengraph-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,57 +32,55 @@ export default async function OpengraphImage({
const sub = tagline(m.metadata.siteTitle, m.metadata.description);

return new ImageResponse(
(
<div
style={{
width: "100%",
height: "100%",
display: "flex",
alignItems: "center",
gap: 72,
padding: "0 96px",
background: BG,
}}
>
{/* Brand blocks */}
<div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
{BLOCKS.map((color) => (
<div
key={color}
style={{
width: 132,
height: 132,
background: color,
border: `6px solid ${EDGE}`,
borderRadius: 10,
}}
/>
))}
</div>

{/* Wordmark + tagline */}
<div style={{ display: "flex", flexDirection: "column" }}>
<div style={{ display: "flex", flexWrap: "wrap", gap: 22 }}>
<span style={{ fontSize: 104, fontWeight: 800, color: "#ffffff" }}>
BitByBit
</span>
<span style={{ fontSize: 104, fontWeight: 800, color: "#ff5d8f" }}>
RUN
</span>
</div>
<span
<div
style={{
width: "100%",
height: "100%",
display: "flex",
alignItems: "center",
gap: 72,
padding: "0 96px",
background: BG,
}}
>
{/* Brand blocks */}
<div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
{BLOCKS.map((color) => (
<div
key={color}
style={{
fontSize: 40,
fontWeight: 600,
color: "#cfc7ea",
marginTop: 28,
width: 132,
height: 132,
background: color,
border: `6px solid ${EDGE}`,
borderRadius: 10,
}}
>
{sub}
/>
))}
</div>

{/* Wordmark + tagline */}
<div style={{ display: "flex", flexDirection: "column" }}>
<div style={{ display: "flex", flexWrap: "wrap", gap: 22 }}>
<span style={{ fontSize: 104, fontWeight: 800, color: "#ffffff" }}>
BitByBit
</span>
<span style={{ fontSize: 104, fontWeight: 800, color: "#ff5d8f" }}>
RUN
</span>
</div>
<span
style={{
fontSize: 40,
fontWeight: 600,
color: "#cfc7ea",
marginTop: 28,
}}
>
{sub}
</span>
</div>
),
</div>,
{ ...size }
);
}
7 changes: 4 additions & 3 deletions app/api/auth/nostr/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { claimNonce } from "@/lib/nostr/nonce-store";
* from:
* - the `u` tag binding the event to this exact URL
* - the `method` tag binding it to POST
* - the ±30s `created_at` window (validateNip98AuthEvent)
* - the ±10s `created_at` window (validateNip98AuthEvent)
*
* The signer method (extension / nsec / nip46) and the user's
* locale travel in custom `["bbr_signer", ...]` and
Expand Down Expand Up @@ -104,8 +104,9 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
const event = validation.event;

// Single-use: a valid event is honored once. Replaying the captured header
// within the ±10s window (to mint a second session) is rejected here.
if (!claimNonce(event.id)) {
// within the ±10s window (to mint a second session) is rejected here. Backed
// by Postgres so the guard holds across serverless instances/cold starts.
if (!(await claimNonce(event.id))) {
return NextResponse.json({ error: "auth_replayed" }, { status: 400 });
}

Expand Down
4 changes: 3 additions & 1 deletion app/api/matches/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
const match = parsed.data;

// The submitter must be one of the match's players (host or any participant).
const isParticipant = match.standings.some((s) => s.pubkey === session.pubkey);
const isParticipant = match.standings.some(
(s) => s.pubkey === session.pubkey
);
if (!isParticipant) {
return NextResponse.json({ error: "forbidden" }, { status: 403 });
}
Expand Down
52 changes: 25 additions & 27 deletions app/apple-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,31 @@ const BLOCKS = ["#ff4d85", "#16a06b", "#f5b500"]; // primary / secondary / highl

export default function AppleIcon() {
return new ImageResponse(
(
<div
style={{
width: "100%",
height: "100%",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
gap: 10,
background: EDGE,
}}
>
{BLOCKS.map((color) => (
<div
key={color}
style={{
width: 47,
height: 47,
background: color,
border: `3px solid ${EDGE}`,
borderRadius: 4,
}}
/>
))}
</div>
),
<div
style={{
width: "100%",
height: "100%",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
gap: 10,
background: EDGE,
}}
>
{BLOCKS.map((color) => (
<div
key={color}
style={{
width: 47,
height: 47,
background: color,
border: `3px solid ${EDGE}`,
borderRadius: 4,
}}
/>
))}
</div>,
{ ...size }
);
}
Loading
Loading