> ## Documentation Index
> Fetch the complete documentation index at: https://docs.leverine.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Leverine Licensing — Plan of Record

> Leverine Licensing — licence keys, activation, and verification for Leverine products, signed with ML-DSA.

# Leverine Licensing — Plan of Record

Filed 2026-12-03, on Kev’s approval to build (“Okay lets start building it. keep the repos neat and organized.” / “you have cloudflare access.”). This document supersedes the phase sketch in the README where the two differ; the settled decisions below do not change.

## The goal, in two parts

1. **Implement the licensing service** — the hosted engine that issues, activates, refreshes, and revokes licence keys for Leverine’s own products first (PVEZ-v2 / PowerDesk, Hoistmark, File Handler desktop tier) and, on the same engine, for external indie vendors as the paid service described in the monetization plan.
2. **Give it predictable, modular hooks** — in code, in domains, and in workers — so a future app integrates the same way the first one did: by registration and configuration, not by forking the engine or inventing a new protocol.

## Shape (settled 2026-11-27, reaffirmed)

* **Stack:** TypeScript on Cloudflare Workers + D1.
* **Deployments:** one deployment per customer for V1 — each vendor gets their own Worker, D1 database, secrets, and issuer host. No tenancy layer in the engine; the bottleneck that would change this is deployment count, not features.
* **Products inside a deployment:** the engine is product-aware from the first migration. A deployment holds a `products` table; Leverine’s own deployment registers PVEZ-v2, Hoistmark, and File Handler as products in one Worker, separated by product id. A vendor deployment registers that vendor’s apps the same way. One engine, one Worker per deployment, many products per Worker.
* **Issuer:** `licence.leverine.com` for Leverine’s own deployment. **Key prefix:** `LVN-`.

## Part 1 — Build phases

### L0 — Foundations (this phase)

* Repo layout (below), TypeScript Worker scaffold, Wrangler config with dev / staging / prod environments.
* D1 migration `0001`: `products`, `licences`, `deployments`, `licence_events` (append-only audit) with indexes.
* `GET /health` route reporting service, environment, and D1 reachability.
* Tests proving the migration applies to a local D1 and the health route responds, run in CI-style locally (Vitest + Miniflare).
* Real dev resources provisioned on Cloudflare through the API: a dev D1 database and the dev Worker on the account’s workers.dev host, health verified over the internet. Staging/prod stay as config until needed.

### L1 — Issuance core (port of the Ferret core, audit fixes applied)

The donor core (\~700 tested lines: Ed25519-signed tokens, HMAC-derived keys, device binding, revocation) is ported, not rewritten, with the 2026-11-27 audit fixes as hard requirements — and one deliberate swap: the port replaces the donor’s Ed25519 token signing with **ML-DSA** (see the algorithm bullet below).

* **No MASTER\_KEY.** Operator testing and support reproduction use admin-issued, short-TTL trial licences — real rows: visible, revocable, capped.
* Keys are `LVN-` + HMAC-derived material; only key hashes are stored.
* **Algorithm: ML-DSA only** (FIPS 204, the Dilithium family) — decided 2026-12-03. No Ed25519, no dual-signing. The default parameter set is **ML-DSA-65**. WebCrypto does not expose ML-DSA, so signing (Worker side) and verification (client library) both use the pure-TypeScript **@noble/post-quantum** implementation. Size facts for ML-DSA-65: public key 1,952 bytes, signature 3,309 bytes — irrelevant in an API payload and fine for a licence file.
* Token envelopes carry `alg` and `kid` from the first token ever issued. Verifiers accept the current and previous signing keys (dual-key verification) so signing keys rotate without a flag day, and a future algorithm change arrives as a new `kid` under the same envelope, not as a new format.
* Claims carry product, plan/tier, entitlements (`ent`/`feats` grants), device cap, token TTL, issue/expiry. Per-plan device-cap policy and token TTL are configuration, not constants.
* Schema changes only through versioned migrations.

### L2 — Commerce (Paddle)

* Paddle is the merchant of record (account under Leverine; GST/HST registration when revenue requires it — decided 2026-12-03).
* Checkout → signed webhook → licence issued. Webhook handling is **signed and idempotent**: signature verified before any state change, event ids recorded so a replayed delivery is a no-op.
* Subscription lifecycle events drive licence state (active → grace → read-only → revoked) through the same event log as everything else.
* The service’s own tiers (Free ≤100 active licences · Indie \$19 · Studio \$49 · Publisher \$99) are plans in the same engine.

### L3 — Lifecycle API + operator tooling

Public, versioned, per product:

* `POST /v1/activate` — binds a deployment to a licence. **Hard-refuse** at the plan’s device cap: a full licence refuses a new activation until a deployment is deactivated (decided 2026-12-03).
* `POST /v1/deactivate` — releases a deployment slot.
* `POST /v1/refresh` — exchanges a still-valid token for a fresh one; this is where revocation and expiry bite.
* `POST /v1/status` — licence state for support and in-app display.
* `GET /v1/keys` — the deployment’s public verification keys (current + previous, by `kid`) so client apps rotate trust without an update.

Operator tooling (admin-authenticated, every action event-logged): issue/revoke licences, issue trial licences, inspect deployments, comp a licence. **No trials are sold** (decided 2026-12-03 — the demo is the trial); trial licences exist only as the operator/support tool that replaced the master key.

Grace and degradation (decided 2026-12-03): a lapsed licence gets **7 days of grace**, then the client goes **read-only** — never dead. Data stays exportable in read-only. Clients must connect occasionally to refresh; permanently-offline use is not a supported mode at launch.

### L4 — Hardening, runbooks, acceptance

* Threat pass over the whole surface (key handling, webhook forgery, replay, token theft, admin auth) against the audit’s §4 rules: no key material in logs or in the repo tree, ever.
* Signing-key runbook: generation, storage, rotation drill (dual-key), compromise procedure, D1 backup/restore drill.
* **Real-money acceptance:** one genuine Paddle purchase, end to end — checkout, webhook, issuance, activation, refresh, refund → revoke — on the production deployment before any external customer.
* Kill gate stands: **10 paying customers within 90 days of beta, or the service stops.** PowerDesk dealers count toward the gate (decided 2026-12-03).

## Part 2 — The hooks (how future apps integrate)

### Code

* A **versioned integration contract**: the token format, the lifecycle endpoints, the state machine (licensed → grace → read-only → revoked), and the error vocabulary are versioned documents in this repo; `/v1` never changes meaning under an integrator.
* A **reference validation library** (`packages/client`, when the first external integrator lands): verifies ML-DSA token signatures offline — the same @noble/post-quantum implementation the issuer uses — against the deployment’s public keys, and — the load-bearing rule — **derives licence state from the token and the clock on every check**. There is no stored “licensed” boolean anywhere for anyone to flip.
* **Conformance test vectors:** a published set of tokens and scenarios (valid, expired, in-grace, revoked, wrong product, wrong `kid`, wrong `alg`, tampered payload) that any client implementation must classify correctly before it ships.

### Domains

* `licence.leverine.com` is the single issuer host for Leverine’s own deployment; vendor deployments get their own issuer host on the same pattern. Clients pin the issuer in configuration, never in code.
* **Per-product registration:** a product is a row (id, name, plan set, device-cap policy, TTLs) — adding an app is registration, not code.

### Workers

* One Worker per deployment serves **all of that deployment’s products** by product id — API, webhooks, and key endpoint share one codebase, one D1, one event log. New products and new deployments both arrive as configuration.

### The integration playbook (per app, in order)

1. Register the product; define plans, caps, TTLs.
2. Integrate the validation library; pass conformance vectors.
3. Wire activate/refresh into the app’s first-run and startup paths.
4. Ship **dark** (licensing present, enforcement off), then **observe** (state logged/shown, nothing blocked), then **enforce** (grace/read-only rules live).

## Sequence

1. **Engine** — L0 → L1 → L3 on dev, L2 when the Paddle account exists.
2. **PVEZ-v2 (PowerDesk)** — first consumer: dark → observe → enforce, per its own integration plan (`docs/LICENSING-INTEGRATION-PLAN.md` in the pvdv-v2 repo). Matt’s founding licence is a **comped annual** licence on this engine (decided 2026-12-03).
3. **Hoistmark carve-out** — if Hoistmark’s validation gate passes, its licensing deployment is built ahead of the general beta (it does not count toward the kill gate).
4. **File Handler desktop/web tier** — integrates when that tier exists. No LVN- licensing in the mobile app (store policy).

## Locked decisions

**2026-11-27:** GO · TypeScript on Cloudflare Workers + D1 · per-customer deployments for V1 · Paddle-only billing · issuer `licence.leverine.com` · key prefix `LVN-` · tiers Free ≤100 active licences / Indie \$19 / Studio \$49 / Publisher \$99 · kill gate 10 paying customers within 90 days of beta · MASTER\_KEY removed · `kid` + dual-key verification and the other contract upgrades land before the first external customer bakes in a key.

**2026-12-03:** grace is 7 days, then read-only · activation cap is hard-refuse · no sold trials (demo is the trial) · occasional connectivity required at launch · founding (Matt/PowerDesk) licence is comped annual · PowerDesk dealers count toward the kill gate · Paddle account under Leverine, GST/HST registration when revenue requires it · build approved and started (“Okay lets start building it. keep the repos neat and organized.” / “you have cloudflare access.”) · token signing is **ML-DSA only** (FIPS 204), ML-DSA-65 by default, via @noble/post-quantum — no Ed25519 in the new system.

## Repo layout

docs/        plan of record, audits, build journals
src/         the Worker (entry, routes, core)
migrations/  D1 migrations, versioned, append-only
test/        Vitest suites (unit + Miniflare integration)
packages/    reserved: client validation library + conformance vectors

## Sources

* `docs/leverine-licensing-service-plan.pdf` — monetization plan.
* `docs/plan-outline.md` — the plan’s outline and sources.
* `docs/ferret-audit-2026-11-27.md` — donor audit and porting blueprint.
* pvdv-v2 `docs/LICENSING-INTEGRATION-PLAN.md` — consumer-side detail for the first integration.

***

*Source: Leverine/Leverine-Licensing\@7ac3cc9 — `docs/PLAN.md`.*


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.