Implement end-to-end encrypted buyer/seller messaging

Wire format (internal/message): sealed boxes to the listing's device key
(X25519 + HKDF + ChaCha20-Poly1305), padded, proof-of-work stamped, with
signed owner replies and address grants. Relay validation, bounded queues,
gossip on per-region message topics and sync (store, p2p).

Owner side (internal/inbox, API): inbox with hidden link messages and a
per-day new-thread limit, replies, grants, and a private per-listing
address. Public POST/GET /api/v1/messages for buyers.

Browser: noble-based sealing and reply verification checked against a Go
fixture, contact form, conversation view, message list, de/en strings.

Docs: PROTOCOL.md messaging section matches the implementation.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 19:44:59 +01:00
co-authored by Claude Sonnet 5.5
parent b9e1b0c058
commit 3b0351c774
45 changed files with 8228 additions and 292 deletions
+53 -22
View File
@@ -131,7 +131,13 @@ within 30 days.
revocations every 30 minutes, so newcomers hear about them without waiting
for a sync.
## Messaging (wire format, draft, not implemented yet)
## Messaging
Implemented in `internal/message` (wire format), `internal/inbox` (owner side)
and `web/src/message.ts` (buyer side). Both crypto implementations are tested
against a fixture written by the Go code
(`internal/message/testdata/fixture.json`, refresh with
`UPDATE_FIXTURES=1 go test ./internal/message -run TestGoldenFixture`).
A browser-only buyer contacts a listing's owner, and the owner can answer and
release the exact address. Messages are **end-to-end encrypted**, short,
@@ -194,8 +200,10 @@ different contexts; no separate encryption key is added to certificates.
**Padding:** plaintext = canonical JSON, then `0x80`, then zero bytes up to the
next size of 512, 1024, 2048 or 4096 bytes, so length reveals little.
Plaintext before padding is at most 4096 bytes. Relay and owner/buyer limits
are in UTF-8 bytes.
Plaintext before padding is at most 4095 bytes (one byte is the marker). Relay
and owner/buyer limits are in UTF-8 bytes. The plaintext must be *canonical*:
a receiver re-encodes what it decoded and rejects any difference (unknown
fields, odd escapes, reordering).
### Plaintext (inside `ct`)
@@ -236,12 +244,15 @@ the browser had the data and cannot be forced to forget it.
3. proof-of-work at the node's message difficulty;
4. `to = owner`: the listing must be known, live (not expired or deleted) and
in this region, and `rcpt` must equal its current `device`;
5. `to = buyer`: no listing check (the owner may have deleted the listing since,
and replies must still arrive), only the checks above;
6. per-queue caps: at most 200 live envelopes per listing (`owner`) and 50 per
mailbox (`buyer`). When full, the envelope with the **least work** is
evicted (oldest first on ties), so filling a queue costs more than the
proof-of-work of the messages it displaces.
5. `to = buyer`: the listing need not exist (the owner may have deleted it
since, and replies must still arrive). If the listing *is* known, the
envelope's region must still match it;
6. per-queue caps: at most 200 live envelopes per listing (`owner`), 50 per
mailbox (`buyer`) and 20000 per node (all configurable in the store). When
full, the envelope with the **least work** is evicted (oldest first on
ties); a newcomer with less work than everything queued is rejected. So
filling a queue costs more than the proof-of-work of the messages it
displaces.
Invalid envelopes are rejected and the relaying peer loses score. Unknown
listing, wrong `rcpt` after a device change, and clock failures are **ignored
@@ -256,28 +267,48 @@ text rules below are enforced by endpoints.
clickable links.
- **First-message restriction:** until the owner has sent a message in a
thread, the buyer's messages must be short text with **no links**: no
`://`, no `www.`, no hostname-like `label.tld` token. The owner's node hides
violating messages (marked "contains a link") and the buyer's browser
refuses to send them. This is a heuristic against scams, not a guarantee.
`://`, no `www.`, no hostname-like `label.tld` token (a regexp over letters,
digits and `-`, after lowercasing; it also flags "Hallo.Ich", which the UI
says). The owner's node stores such messages but marks them **hidden**: the
API withholds their text. The buyer's browser refuses to send them. The
rule is shared by Go and TypeScript and checked against common test vectors.
This is a heuristic against scams, not a guarantee.
- **Owner-side rate limit:** a node accepts at most N new threads per listing
per day (default 20). Surplus first messages stay queued and unread until
they expire; highest work first, then oldest.
per 24 hours (`--max-new-threads`, default 20). Surplus first messages stay
unseen until room frees up (or they expire); highest work first, then oldest.
Envelopes that fail to decrypt are remembered (`inbox_seen`) and not tried
again.
- Messages carry **no attachments and no images**.
- The decrypted inbox is stored only on the owner's node, in its database. The
exact address is a private per-listing field there, never signed or
gossiped; "release address" pre-fills a `grant` that the owner can edit.
### Transport (planned)
### Transport
- **Gossip** on `/allmende/1/messages/<region>`; **sync** extends
`/allmende/1/sync` with a message stream paged by `(created_at, hash)`.
- **HTTP**: `POST /api/v1/messages` (no token; the node validates, stores and
gossips the envelope; rate limited per client) and
`GET /api/v1/messages?mailbox=<tag>&after=<created_at>` for the buyer's
browser to poll. The owner's decrypted inbox and replies use the existing
admin token.
`/allmende/1/sync` with kind `messages`, paged by `(created_at, hash)`.
- **HTTP**, public: `POST /api/v1/messages` (the canonical envelope as the
body, at most 10 KB; the node validates, stores and gossips it; answers 202
`{"hash"}`; 400 invalid, 404 unknown listing, 413 too large, 429 rate
limited per client IP) and
`GET /api/v1/messages?mailbox=<tag>&since=<created_at>&limit=<n ≤ 100>` for
the buyer's browser to poll (`since` is inclusive; the browser re-reads the
last hour each time and dedupes by hash). Responses are `no-store`.
- **HTTP**, owner (admin token): `GET /api/v1/inbox` (threads),
`GET|POST|DELETE /api/v1/inbox/{listing}/{thread}` (read, reply, erase) and
`PUT|GET|DELETE /api/v1/listings/{id}/address` (the private address). A
reply body is `{type: "text"|"grant", text, address, phone, email, note,
valid_days}`; a grant without `address` uses the stored one.
- `GET /api/v1/status` reports `message_difficulty`, which the browser mines to.
- **Flags:** `--message-difficulty` (default 14) and `--max-new-threads`.
- A gateway sees the buyer's IP address and mailbox tag. Buyers can use any
gateway; nothing else about the conversation is exposed.
- **Buyer UI:** a contact form on every verified listing; a private link
`#/c/<author>/<id>/<thread secret>` (the secret stays in the URL fragment,
which is never sent to a server) that polls every 20 s, shows only verified
owner replies and counts the rest as rejected; `#/messages` lists the
conversations stored in this browser. There is **no owner UI yet**; the owner
uses the admin API.
### Limits of this design
@@ -348,7 +379,7 @@ Proof-of-work per listing and per revocation, plus subscribable blocklists
## Open
Messaging implementation and abuse reporting for messages, reciprocity rules, anti-
An owner UI for the inbox, abuse reporting for messages, reciprocity rules, anti-
discrimination (AGG) content rules, blocklists, peer scoring and rate limits,
incremental sync and anti-entropy, DHT discovery, UI publishing, image hosting,
recovering listings onto a fresh machine with an empty database, a helper to