# POLLA — Permissionless Open Lightning Lottery Audit

[![license: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
![node ≥ 20](https://img.shields.io/badge/node-%E2%89%A5%2020-339933)
![runtime dependencies: 0](https://img.shields.io/badge/runtime%20dependencies-0-brightgreen)

🇪🇸 [Leer en español](README.es.md) · 🌐 **Live: [polla21.org](https://polla21.org)** · ⚡ Bitcoin mainnet

Open-source Lightning lottery: no owner, non-profit, verifiable by anyone. The website is static HTML/JS; the server in `server/` (Node, no dependencies) creates invoices, locks numbers and signs every sale, commitment and result with the lottery's Nostr key.

> **Status: young project.** The first numbered draws run in December 2026. Everything that decides a draw is public and signed so that you do not have to trust the operator: read the code, [verify the draws](docs/VERIFY-DRAW.md) and only play what you can afford to lose. Found a problem? See [Security](#security).

## Architecture

```mermaid
flowchart LR
  buyer["Buyer's browser<br/>polla21.org"] -- "POST /api/invoice" --> api["server/<br/>Node, 0 deps"]
  api -- "NWC IN (receive only)" --> wallet[("Lightning wallet")]
  api -- "signed sales, commitment,<br/>anchor and results" --> nostr[("Nostr relays")]
  chain[("Bitcoin block")] -- "block hash decides" --> api
  owner["Owner panel /admin/<br/>NIP-98 signature"] -- "admin API" --> api
  owner -- "NWC OUT, one-time,<br/>never stored" --> prize[("Prize payment")]
  auditor["Anyone<br/>scripts/verify-draw.mjs"] -. "recomputes" .-> nostr
  auditor -. "recomputes" .-> chain
```

- The public website **never** holds a key that can spend; the server's wallet connection can only receive.
- Prizes are paid by hand by the owner; a claim never triggers a payment.
- Admin requests are authorised only by a Nostr signature (NIP-98) from a key on the server's allowlist, with a single-use nonce.

## Repository layout

| Path | What it is |
| --- | --- |
| `index.html`, `*.html`, `*.js`, `site.css`, `themes.css` | Public website (static, no build step). `common.js` holds the logic shared by the website and the server. |
| `admin/` | Owner panel (served at `/admin/`, every write is signed). |
| `server/` | Node HTTP API: invoices, number locks, claims, signed Nostr events. `server/lib/` has one module per concern. |
| `server/deploy/` | systemd unit, Caddy snippet, `polla.env.example` and the deploy script. No secrets. |
| `server/strfry/` | Write policy for the project's own Nostr relay. |
| `scripts/` | Independent draw verifier, key generation, i18n checks and other tooling. |
| `tests/` | `node:test` suite (unit, HTTP end-to-end, security invariants). Runs in CI on every pull request. |
| `docs/` | Protocol and operations documentation. |
| `locales/`, `i18n.js` | Translations (English is the source language). |
| `vendor/` | Pinned third-party code, checked by SHA-384 in the tests. |

## Quick start

```sh
# from a clone of this repository:
node --test 'tests/*.test.mjs'                   # Node ≥ 20, nothing to install
POLLA_TEST_MODE=1 node server/test-mode.mjs      # http://127.0.0.1:8788, simulated sats
```

## How a draw works

- **Sale.** Each purchase gets a secret code (`POLLA-…`, or `LOTTO-…` in LOTTO21) that only the buyer knows; the invoice, Nostr and the ledger only carry its hash.
- **Draw.** At the draw time the server records the latest Bitcoin block seen by mempool.space, blockstream.info and blockchain.info (the highest of the three) and publishes it, signed, on Nostr within 60 seconds. The first block after that one decides: the last decimal digits of its hash (numbers game) or a SHA-256 chain with rejection sampling (lotto game). If that record is missing or late, the first block whose timestamp is at least 2 hours after the draw time decides: Bitcoin does not let it appear before the draw time. Final after 6 confirmations (a draw may ask for more). See [docs/VERIFY-DRAW.md](docs/VERIFY-DRAW.md).
- **Pre-draw commitment.** When sales close, the server signs and publishes the list of sold numbers before the block exists, so the operator can be audited. See [docs/PRE-DRAW-COMMITMENT.md](docs/PRE-DRAW-COMMITMENT.md).
- **Claim.** The winner signs with the key derived from their code and is paid by Lightning invoice (or Lightning address, if the server allows it). Unclaimed prizes go back to the pot after the claim period.
- **Independent check.** `node scripts/verify-draw.mjs --results results/<id>.json --sold <list>` recomputes the winner from the chain. See [docs/VERIFY-DRAW.md](docs/VERIFY-DRAW.md).

## Collecting and paying

The public website **never** holds a key that can spend. There is a single payment backend, our own server (`server/`):

| | What it is | Where it lives |
| --- | --- | --- |
| ⚡ **NWC IN** (collect) | A Nostr Wallet Connect connection that can **only receive** (`make_invoice`, `lookup_invoice`, `get_info`). The server creates the invoices with the amount it computes, locks the numbers and signs the sales with the lottery key. | On the server, in a 600 file. Never in `config.json`. |
| ⚡ **NWC OUT** (pay) | A **one-time** connection the owner creates in their own wallet for each prize payment, pastes in the panel and deletes afterwards. | Only in the owner's browser, during that payment. It never reaches the server. |
| 🧪 **Simulation** | Simulated invoices and payments. | Test mode only, never in production. |

Prizes are always paid by hand from the panel; a claim never triggers a payment. See [docs/BACKEND-NWC.md](docs/BACKEND-NWC.md), [docs/MANUAL-PAYOUTS.md](docs/MANUAL-PAYOUTS.md) and [docs/INSTALL-VPS.md](docs/INSTALL-VPS.md).

## Which draws are on sale: `GET /api/draws`

Agents and scripts: the public API is described in [`/openapi.json`](openapi.json); buying, verifying and claiming without a browser in [docs/AGENT-BUY.md](docs/AGENT-BUY.md); verification in [docs/VERIFY-DRAW.md](docs/VERIFY-DRAW.md). For agents: [`/llms.txt`](llms.txt) says what POLLA is, what can be read, verified and bought, and what not to touch. The home page answers `Accept: text/markdown` with [index.md](index.md); the FAQ is also in [faq.md](faq.md). An address that does not exist is a plain 404, not an outage.

`GET /api/draws` is the public, stable answer to «which draws exist and which can be bought right now». Each draw
carries `status` (`onSale` · `announced` · `paused` · `closed` · `drawn`), `onSale`, the reason in the request's
language, the public rules (game, price, numbers, maximum per purchase, invoice expiry, dates, block rule) and whether
its signed commitment, anchor and result exist. `onSale` is true exactly when `POST /api/invoice` would sell that
draw: do not infer it from the dates or from `enabledDraws`. The website reads the same catalog for its labels.

## Test mode (no sats)

```sh
POLLA_TEST_MODE=1 node server/test-mode.mjs     # http://127.0.0.1:8788
node --test 'tests/*.test.mjs'                  # Node ≥ 20
```

Simulated invoices and payments, a separate key and no production relay. Run a draw with `POST /api/test/draw {"id":"NAVIDAD26","n":"007"}`. Details: [docs/TEST-MODE.md](docs/TEST-MODE.md).

## Languages, time zones and addresses

English by default and Spanish (browser or system language, or a Spanish-speaking time zone). Every time is shown in the user's time zone with its abbreviation, plus UTC where it matters. Each page has one address per language; the files are the English ones and the Spanish addresses are rewritten to them (`vercel.json`, Caddy and the test server, all from `PAGES` in `i18n.js`):

| Page | English | Spanish |
| --- | --- | --- |
| Draws | `/` | `/es` |
| Calendar | `calendar.html` | `calendario.html` |
| Treasury | `treasury.html` | `tesoreria.html` |
| What is POLLA? | `what-is-polla.html` | `que-es-polla.html` |
| My tickets | `my-tickets.html` | `mis-boletos.html` |
| Sample bearer tickets | `ticket.html` | `participaciones.html` (old: `decimo.html`) |
| Admin panel | `/admin/` | `/admin/` |

Published data (results, commitments, Nostr events) is signed JSON and is never duplicated per language; official text communications are in English only. Details and how to add a language: [docs/I18N.md](docs/I18N.md).

## Pages and features

- **Draws** (`/`): buy numbers or lotto combinations, for one or several announced draws with a single invoice ([docs/multi-draw.md](docs/multi-draw.md), [docs/lotto.md](docs/lotto.md)).
- **Calendar**: ongoing, upcoming and past draws with all their rules; the panel creates draws from templates ([docs/calendar.md](docs/calendar.md)).
- **Treasury**: pot, carry-overs, revenue, paid, pending, costs collected and reserve, computed from public data ([docs/treasury.md](docs/treasury.md)).
- **My tickets**: purchases encrypted in the browser (AES-GCM, non-exportable key in IndexedDB, optional passphrase) ([docs/my-tickets.md](docs/my-tickets.md)).
- **Bearer tickets**: a PDF with a summary sheet (master code) and one sheet per number (POLLA) or per bet (LOTTO21), each with its own code that claims only that number or bet; the codes are real text, on one line ([docs/ticket.md](docs/ticket.md), [docs/BEARER-TICKET.md](docs/BEARER-TICKET.md)).
- **Purchase ledger**: one CSV line per number and draw ([docs/claim-log.md](docs/claim-log.md)).

## Admin panel (`/admin/`)

- The repository's `config.json` ships with `"vault": null`. The first time you open the panel it asks you to create a password: it generates the contact key, encrypts it (PBKDF2 600,000 + AES-GCM) and stores it in `vault`.
- With the server installed, the panel saves events and settings to it, signed with your Nostr extension ([docs/ADMIN-VPS-AUTH.md](docs/ADMIN-VPS-AUTH.md)). Without it, download `config.json` from Settings and upload it to the web root by hand.


## Contributing

Issues and pull requests are welcome, in English or Spanish: see [CONTRIBUTING.md](CONTRIBUTING.md). Translations are especially welcome ([docs/I18N.md](docs/I18N.md)).

## Security

Please **do not open a public issue** for a vulnerability. Report it privately as described in [SECURITY.md](SECURITY.md), which also explains what is secret, what is public on purpose and why.

## License

[MIT](LICENSE).
