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
+31 -2
View File
@@ -19,6 +19,7 @@ import (
"time"
"git.stinnesbeck.com/nils/allmende/internal/identity"
"git.stinnesbeck.com/nils/allmende/internal/inbox"
"git.stinnesbeck.com/nils/allmende/internal/listing"
"git.stinnesbeck.com/nils/allmende/internal/p2p"
"git.stinnesbeck.com/nils/allmende/internal/store"
@@ -39,10 +40,20 @@ type Config struct {
Token string // admin bearer token; empty disables all writes
Difficulty int
Clock func() time.Time
// MessageDifficulty is reported to browsers so they can mine the
// proof-of-work of the messages they send.
MessageDifficulty int
// PostsPerMinute limits envelopes accepted per client address; 0 means 30.
PostsPerMinute int
// Inbox is the owner's conversations. Without it the owner endpoints are
// absent; relaying messages for buyers still works.
Inbox *inbox.Inbox
}
type server struct {
Config
limiter *limiter
}
// New returns the HTTP handler.
@@ -50,7 +61,10 @@ func New(cfg Config) http.Handler {
if cfg.Clock == nil {
cfg.Clock = time.Now
}
s := &server{cfg}
if cfg.PostsPerMinute <= 0 {
cfg.PostsPerMinute = defaultPostsPerMinute
}
s := &server{Config: cfg, limiter: newLimiter(cfg.PostsPerMinute, time.Minute, cfg.Clock)}
mux := http.NewServeMux()
mux.HandleFunc("GET /api/v1/status", s.status)
mux.HandleFunc("GET /api/v1/listings", s.search)
@@ -58,6 +72,20 @@ func New(cfg Config) http.Handler {
mux.HandleFunc("POST /api/v1/listings", s.admin(s.create))
mux.HandleFunc("PUT /api/v1/listings/{id}", s.admin(s.update))
mux.HandleFunc("DELETE /api/v1/listings/{id}", s.admin(s.remove))
// Messaging: public relay endpoints for buyers' browsers...
mux.HandleFunc("POST /api/v1/messages", s.postMessage)
mux.HandleFunc("GET /api/v1/messages", s.getMailbox)
// ...and the owner's inbox and private address, behind the admin token.
if cfg.Inbox != nil {
mux.HandleFunc("GET /api/v1/inbox", s.admin(s.listThreads))
mux.HandleFunc("GET /api/v1/inbox/{id}/{thread}", s.admin(s.getThread))
mux.HandleFunc("POST /api/v1/inbox/{id}/{thread}", s.admin(s.reply))
mux.HandleFunc("DELETE /api/v1/inbox/{id}/{thread}", s.admin(s.deleteThread))
mux.HandleFunc("PUT /api/v1/listings/{id}/address", s.admin(s.putAddress))
mux.HandleFunc("GET /api/v1/listings/{id}/address", s.admin(s.getAddress))
mux.HandleFunc("DELETE /api/v1/listings/{id}/address", s.admin(s.deleteAddress))
}
mux.Handle("GET /", s.ui())
return secure(mux)
}
@@ -106,7 +134,8 @@ func (s *server) status(w http.ResponseWriter, r *http.Request) {
"peers": s.Node.PeerCount(),
"listings": n,
// Lets the browser report how much anti-spam work a listing carries.
"difficulty": s.Difficulty,
"difficulty": s.Difficulty,
"message_difficulty": s.MessageDifficulty,
})
}