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:
+68
-3
@@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/message"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/store"
|
||||
)
|
||||
|
||||
@@ -30,11 +32,12 @@ const (
|
||||
)
|
||||
|
||||
type syncRequest struct {
|
||||
Kind string `json:"kind"` // "listings" or "revocations"
|
||||
Kind string `json:"kind"` // "listings", "messages" or "revocations"
|
||||
Region string `json:"region,omitempty"`
|
||||
AfterCreated int64 `json:"after_created,omitempty"`
|
||||
AfterAuthor string `json:"after_author,omitempty"`
|
||||
AfterID string `json:"after_id,omitempty"`
|
||||
AfterAuthor string `json:"after_author,omitempty"` // listings
|
||||
AfterID string `json:"after_id,omitempty"` // listings
|
||||
AfterHash string `json:"after_hash,omitempty"` // messages
|
||||
Limit int `json:"limit,omitempty"`
|
||||
}
|
||||
|
||||
@@ -81,6 +84,27 @@ func (n *Node) handleSync(s network.Stream) {
|
||||
return
|
||||
}
|
||||
}
|
||||
case "messages":
|
||||
if !listing.ValidRegion(req.Region) {
|
||||
_ = s.Reset()
|
||||
return
|
||||
}
|
||||
limit := req.Limit
|
||||
if limit <= 0 || limit > n.cfg.SyncPageSize {
|
||||
limit = n.cfg.SyncPageSize
|
||||
}
|
||||
items, err := n.cfg.Store.MessagesSince(n.ctx, req.Region,
|
||||
store.MessageCursor{CreatedAt: req.AfterCreated, Hash: req.AfterHash}, limit)
|
||||
if err != nil {
|
||||
_ = s.Reset()
|
||||
return
|
||||
}
|
||||
for _, e := range items {
|
||||
if !write(e.Bytes()) {
|
||||
_ = s.Reset()
|
||||
return
|
||||
}
|
||||
}
|
||||
case "revocations":
|
||||
revs, err := n.cfg.Store.Revocations(n.ctx)
|
||||
if err != nil {
|
||||
@@ -147,6 +171,47 @@ func (n *Node) SyncFrom(ctx context.Context, p peer.ID) error {
|
||||
if err := n.syncRegion(ctx, p, region); err != nil {
|
||||
return err
|
||||
}
|
||||
// After the listings: owner-bound messages are only accepted for
|
||||
// listings we already hold.
|
||||
if err := n.syncMessages(ctx, p, region); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Node) syncMessages(ctx context.Context, p peer.ID, region string) error {
|
||||
var cur store.MessageCursor
|
||||
for page := 0; page < maxSyncPages; page++ {
|
||||
var count int
|
||||
var last *message.Envelope
|
||||
err := n.request(ctx, p, syncRequest{
|
||||
Kind: "messages", Region: region, Limit: n.cfg.SyncPageSize,
|
||||
AfterCreated: cur.CreatedAt, AfterHash: cur.Hash,
|
||||
}, func(line []byte) {
|
||||
var e message.Envelope
|
||||
if json.Unmarshal(line, &e) != nil {
|
||||
return
|
||||
}
|
||||
count++
|
||||
last = &e
|
||||
switch _, err := n.cfg.Store.PutMessage(ctx, region, &e); {
|
||||
case err == nil, errors.Is(err, store.ErrNoListing), errors.Is(err, store.ErrQueueFull):
|
||||
default:
|
||||
n.log.Debug("sync: message rejected", "peer", p, "err", err)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count < n.cfg.SyncPageSize || last == nil {
|
||||
return nil
|
||||
}
|
||||
c, err := store.MessageCursorOf(last)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cur = c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user