# Tr3k agent drops

An agent that discovers lvlltd.com can claim **one** free coupon. That coupon lets tr3k.app create **one** drop for the wallet that signed. No USDC moves. The claim and the consume are `paid: false` and `source: "tr3k_agent_drop"`. They are not written to the confirmed revenue ledger, so `/api/proof`, `/api/revenue`, `/api/metrics`, and the sales monitor do not count them.

This is not advertised on `/.well-known/x402` and it is not added to any HTTP 402 `PAYMENT-REQUIRED` challenge. Prices, payTo, network, asset, and ERC-8004 registration files are unchanged.

Humans redeem starter packs with a different code (`TR3K-XXXX-XXXX`). See [TR3K-COUPONS.md](./TR3K-COUPONS.md). Agent-drop codes look like `TR3KDROP-XXXX-XXXX` and are not accepted by `/api/pay`.

## Caps

| Cap | Rule |
|---|---|
| Wallet | One code per EVM address. The same wallet always gets that code back (`idempotent: true`), including after it is consumed. |
| ERC-8004 agent id | Optional. If `agent_id` (or `agentId`) is sent, that id can be bound to only one code. A second wallet asking for the same id gets HTTP 409 `agent_already_claimed`. |
| Daily | `AGENT_DROP_DAILY_CAP` new codes per UTC day. Default **50**. A repeat claim for a wallet that already has a code does not use another slot. |
| IP | `AGENT_DROP_RATE_LIMIT` requests per minute per IP, separately for nonce, claim, and consume. Default **20**. |

## 1. Nonce

```
GET https://lvlltd.com/api/coupons/agent-drop/nonce?wallet=0x…
```

No auth. `security: []` on `/openapi.json`.

Success `200`:

```json
{
  "ok": true,
  "schema": "lvl-tr3k-agent-drop-v1",
  "wallet": "0xabc…",
  "message": "LVL LTD agent drop\nDomain: lvlltd.com\nWallet: 0xabc…\nNonce: …\nExpires: …\nSign this message to claim one free Tr3k drop coupon.\nThis is not a payment and does not move USDC.",
  "nonce": "…",
  "expires_at": "2026-11-19T15:10:00.000Z",
  "expires_in_sec": 600,
  "sign": "personal_sign",
  "paid": false,
  "source": "tr3k_agent_drop"
}
```

Sign `message` exactly, including newlines. The wallet inside it is lowercase. The message expires in 10 minutes. A new GET replaces the previous message for that wallet.

| Status | `error` | When |
|---|---|---|
| 400 | `invalid_wallet` | `wallet` is missing or not a 20-byte address. |
| 405 | `method_not_allowed` | Not GET. |
| 429 | `rate_limited` | More than the per-IP limit in the current minute. |
| 503 | `coupon_store_unavailable` | KV is not bound. |

## 2. Claim

```
POST https://lvlltd.com/api/coupons/agent-drop/claim
Content-Type: application/json
```

```json
{
  "wallet": "0xabc…",
  "signature": "0x…",
  "agent_id": "123"
}
```

`signature` is an EIP-191 `personal_sign` of the nonce `message` (65 bytes, `0x` + 130 hex chars). `agent_id` is optional. `agentId` is accepted as an alias.

Verification:

1. EOA: the server recovers the signer the same way as `POST /api/owner/verify` (`personal_sign` digest, then `ecrecover`). The recovered address must be `wallet`.
2. Smart accounts and EIP-7702 delegated accounts: if recovery does not match and the address has code (including the `0xef0100` delegation designator), the server calls ERC-1271 `isValidSignature(bytes32,bytes)` on that address with the same EIP-191 digest. Magic value `0x1626ba7e` accepts the signature.

Success `200`:

```json
{
  "ok": true,
  "schema": "lvl-tr3k-agent-drop-v1",
  "code": "TR3KDROP-XXXX-XXXX",
  "status": "issued",
  "idempotent": false,
  "wallet": "0xabc…",
  "agent_id": "123",
  "issued_at": "2026-11-19T15:00:00.000Z",
  "consumed_at": null,
  "drop_id": null,
  "paid": false,
  "source": "tr3k_agent_drop",
  "revenue_usd": 0,
  "gmv_usd": 0,
  "signature_method": "eip191"
}
```

`signature_method` is `eip191` or `eip1271`. A later claim with a new nonce and a valid signature returns the same `code` and `idempotent: true`. After consume, `status` is `consumed` and `drop_id` is the id tr3k.app sent.

| Status | `error` | When |
|---|---|---|
| 400 | `invalid_wallet` | `wallet` is not an address. |
| 400 | `invalid_signature` | Signature does not prove `wallet`. |
| 400 | `invalid_agent_id` | `agent_id` was sent and is not a decimal ERC-8004 id. |
| 400 | `nonce_required` | No live nonce for this wallet. GET the nonce first. |
| 400 | `nonce_expired` | The message is older than 10 minutes. |
| 400 | `nonce_already_used` | That message was already submitted. GET a new nonce. |
| 409 | `agent_already_claimed` | This `agent_id` is already bound to a different wallet. Omit `agent_id` to fetch the wallet's existing code. |
| 429 | `daily_cap` | Today's new-code cap is exhausted. |
| 429 | `rate_limited` | Per-IP limit. |
| 503 | `coupon_store_unavailable` | KV is not bound. Nothing is issued. |
| 503 | `signature_verifier_unavailable` | EOA recovery failed and the Base RPC needed for ERC-1271 could not be reached. |

## 3. Consume (tr3k.app worker)

Server-to-server only. The browser and the claiming agent do not call this.

```
POST https://lvlltd.com/api/coupons/agent-drop/consume
Authorization: Bearer <TR3K_COUPON_SECRET>
Content-Type: application/json
```

Request:

```json
{ "code": "TR3KDROP-XXXX-XXXX", "drop_id": "<tr3k drop id>" }
```

`drop_id` is 1–128 characters and is stored on the code. It is not a payment id.

Optional HMAC instead of the bearer (either one is enough; same construction as `POST /api/coupons/issue`):

- `X-Tr3k-Timestamp`: unix seconds
- `X-Tr3k-Signature`: hex HMAC-SHA256 of `TR3K_COUPON_SECRET` over `timestamp + "." + rawBody` (the timestamp header's exact text, a dot, then the raw request body)

The timestamp must be within 5 minutes.

Success `200`:

```json
{
  "ok": true,
  "wallet": "0xabc…",
  "agent_id": "123",
  "drop_id": "<tr3k drop id>",
  "paid": false
}
```

`agent_id` is JSON `null` when the claim did not include one. `wallet` is lowercase.

The write is single-use. A second consume of the same code, including a retry with the same `drop_id`, does not succeed.

Already consumed `409`:

```json
{
  "ok": false,
  "error": "already_consumed",
  "wallet": "0xabc…",
  "agent_id": "123",
  "drop_id": "<the drop id from the first consume>",
  "paid": false
}
```

Unknown code `404`:

```json
{ "ok": false, "error": "unknown_code", "paid": false, "source": "tr3k_agent_drop" }
```

| Status | `error` | When |
|---|---|---|
| 200 | — | First consume. Code is now consumed. |
| 400 | `drop_id_required` | Missing or invalid `drop_id`. The code stays issued. |
| 400 | `invalid_json` | Body is not JSON. |
| 401 | `unauthorized` | Missing or wrong bearer, or bad HMAC. |
| 404 | `unknown_code` | Code is malformed, was never issued, or is not the wallet's current code. |
| 409 | `already_consumed` | Code was already consumed. |
| 429 | `rate_limited` | Per-IP limit. |
| 503 | `coupon_issuer_unconfigured` | `TR3K_COUPON_SECRET` is unset. Nothing is consumed. |
| 503 | `coupon_store_unavailable` | KV is not bound. |
| 503 | `consume_in_progress` | Another consume of this code is in flight. Retry. |

Create the Tr3k drop only after `200`. On `409`, do not create a second drop; `drop_id` is the one that already consumed the code.

## Cloudflare Pages env

Project `lvlltd-skill-marketplace`.

| Name | Required | Notes |
|---|---|---|
| `TR3K_COUPON_SECRET` | Yes, for consume | Already used by `POST /api/coupons/issue`. Same value on the tr3k.app worker. If unset, consume returns 503 and writes nothing. Claim does not use this secret. |
| `AGENT_DROP_DAILY_CAP` | No | New codes per UTC day. Default 50. |
| `AGENT_DROP_RATE_LIMIT` | No | Requests per minute per IP per route. Default 20. |
| `KV` | Already bound | Namespace `8315cc0f6f634340814d34a3500dd97b`. No new binding. |

Do not put the secret in git. After saving a new secret, redeploy Pages so Functions see it.
