# Pre-draw commitment (A5)

Closes audit item **A5**: making the operator auditable.

## The problem

In the numbers game, if the number the block gives was not sold, the next block is used.
The operator decides what «was sold». Without a prior commitment, a dishonest operator could
wait for the block and then omit a sale to skip a number, or slip one in to keep the prize.

## The solution

When a draw's sales close, the server:

1. Waits until no invoice of that draw can still be paid in time
   (all paid or expired more than 120 s ago).
2. Computes the sold list with the same function the draw uses (`buildState`).
3. Stores it on disk and **signs it with the lottery key** in a Nostr event
   **kind 1** (regular event, not replaceable; tags `t = lottobtc-<id>`, `k = commitment`,
   `d = commitment:<id>`), published to the relays.
4. Publishes it until at least `POLLA_MIN_COMMIT_RELAYS` relays accept it (2 by default; our own
   counts, but is not enough alone). By default they are our own and free third-party relays (damus, nos.lol,
   primal, nostr.band).
5. Anchors the **event id** with OpenTimestamps (public calendars, free) and stores the `.ots`.

Event content:

```json
{ "v": 2, "type": "commitment", "d": "NAVIDAD26", "game": "numbers", "blockRule": "anchor-v1",
  "closeAt": 1797930000, "drawAt": 1797933600, "at": 1797930420,
  "n": 3, "revenue": 15000, "pot": 14550,
  "hash": "<sha256 of the sold numbers, one per line>", "sold": ["007", "123", "900"],
  "hashPurchases": "<sha256 of the purchases, one per line>" }
```

- **`blockRule`:** which block decides (see [VERIFY-DRAW.md](VERIFY-DRAW.md)), fixed before the block. A commitment
  without it was signed before the rule existed: that event keeps the old rule (`timestamp-v0`).
- **`hashPurchases` (v2):** the hash of the list «number|purchase|claim hash», one per line. Without this,
  whoever controls the server could change the owner of a winning number after the block. **Only the hash is
  published** (since 26/11/2026): the list itself (`purchases`) stays on the server, which checks every claim against
  it and against this signed hash before paying. The Nostr event, the API and the OpenTimestamps stamp only carry the
  list of tickets (`sold`) and the hashes; never purchase ids, claim hashes or contacts. Events signed before that
  date did include `purchases` (no contacts: only the hashes of the claim codes, which do not reveal the codes).
- **Immutable:** it is fixed only once. Later rounds, a payment arriving afterwards or a restart
  do not change it; publication retries resend exactly the same signed event.
- **Long lists:** if the event would exceed ~30 kB, it only carries `hash`, `hashPurchases` and `n`; the list of
  tickets is served at `GET /api/commitment/<id>` (which also returns the `.ots` in base64).
- **What OpenTimestamps receives:** only the 32-byte event id (a hash). The calendars never see the list.
- **Late:** if it could not be fixed before `drawAt`, it carries `"late": true` and is not valid as proof.
- Manual test result events do not come from a block and carry no commitment.
- Lotto commits to the combinations played (with repeats).

## The server does not pay what is not in the commitment

With `POLLA_REQUIRE_COMMITMENT=1` (default in production; off in test mode), before paying a
prize the server requires, for each draw in the claim:

- that a signed commitment exists and is not late;
- that it is v2 (with `purchases` in the copy stored on the server) and that the stored copy matches its hashes **and** the signed event;
- that the prize's `number|purchase|claim hash` line is in that stored `purchases`.

If anything fails, it answers 409 and locks or pays nothing. Manual test results carry no
commitment and are not checked.

Limit: whoever holds the lottery key **and** the server could rewrite and re-sign the stored
commitment. The server no longer stops that: the copies on third-party relays (kind 1,
not replaceable) and the OpenTimestamps anchor give it away.

## Close notice on Nostr and where the stamp is

Once the commitment is on the relays and its id is stamped with OpenTimestamps, the server signs **one more** message
with the lottery key (kind 1, tags `k=close`, `d=close:<id>`, `e=<commitment event id>`). It says that sales of the draw
closed, which numbers or combinations take part (`sold`, when the list is short) and carries the stamp itself:

```json
{ "v": 1, "type": "close", "d": "<id>", "n": 3, "commitment": "<event id>", "hash": "<sha256 of the list>",
  "sold": ["007", "123", "456"], "ots": { "file": "<.ots in base64>", "calendars": ["https://…"], "digest": "<event id>" } }
```

Where to find the stamp:

- **Draw page**, in the result box: «Sales closed X hours ago · signed list on Nostr · OpenTimestamps stamp (.ots) · close notice on Nostr».
- **Audit** (`treasury.html`), first card «Results»: the winner of each draw with the same three links; the relay board shows the close notice as a memorandum entry with a «⬇ .ots» link.
- **API**: `GET /api/commitment/<id>` → `{ commitment, event, ots, close }`.

Check it: save the `.ots`, then `ots upgrade file.ots && ots verify -d <commitment event id> file.ots` (or drop it on opentimestamps.org). The stamp is pending until a calendar puts it in a Bitcoin block (a few hours).

## How anyone can check it

1. Ask a relay for the event (`#t = lottobtc-<id>`, `k = commitment`) or `/api/commitment/<id>`.
2. Verify the signature with the lottery public key (any Nostr client).
3. Check that `created_at` and `at` are **earlier** than the time of the deciding block.
4. Pass `sold` to `scripts/verify-draw.mjs --sold` (see [VERIFY-DRAW.md](VERIFY-DRAW.md)).
5. Anchor: save the `ots` field of `/api/commitment/<id>` as `commitment.ots` (base64 → binary) and,
   a few hours later, with the official OpenTimestamps client:
   `ots upgrade commitment.ots` and `ots verify -d <event id> commitment.ots`. The block it gives
   must be **earlier** than the draw block.
6. Anyone can resend the signed event to their own relays: the signature stays valid. The more third-party
   copies, the harder it is to deny.

## Zero sales (since 28/11/2026)

A draw this server never charged gets, after the close and before the draw time, a commitment with `n: 0`,
`sold: []`, `purchases: []` and the hashes of the empty list, signed, published and stamped like any other; at the draw
time the server signs a `no-sales` result that cites it. «0 tickets, signed» (200 with `n: 0`) is no longer the same
as «never fixed» (404). Draws already held without a commitment are not committed afterwards. No anchor is recorded
for an `n = 0` draw (nothing is drawn). Details and the relay rule: [VERIFY-DRAW.md](VERIFY-DRAW.md).

## Honest limits

- Up to v1 the commitment was kind 30078 (replaceable). Since v2 it is kind 1: relays keep
  all of them, and two different commitments signed by the same key are in themselves the evidence of fraud.
- `created_at` is set by the signer: it proves nothing by itself. What proves the date is the
  OpenTimestamps anchor and that third parties (other relays, buyers) stored it before the block.
- **OpenTimestamps takes time.** The calendars aggregate and write to Bitcoin every so often; it can take
  a few hours to confirm. **Decided 23/11/2026: sales close at least 4 h before the draw**
  (`STAMP_MARGIN_HOURS` in `common.js`). The server refuses an event, new or with changed dates, that leaves less
  (`server/lib/admin-config.mjs`), and the templates close 240 minutes before. If even so the anchor lands
  **after** the draw block, the proof that the commitment came first rests on the copies on third-party relays
  (weaker), and it has to be said so publicly («late seal»).
- Free public relays may rate-limit or delete events. That is why it is published to several.
- The commitment does not stop the operator from buying numbers for themselves: that shows in the signed sales.

## Pending

- Publish the upgraded `.ots` (with the Bitcoin attestation) as a NIP-03 event (kind 1040).
- Show it on the website next to the results.
- ~~Signature verification inside the independent script.~~ Done: `scripts/verify-draw.mjs --commitment` / `--server` (28/11/2026).
