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:
+89
-6
@@ -43,14 +43,27 @@ func (o Outcome) String() string {
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
Difficulty int // required proof-of-work bits
|
||||
Difficulty int // required proof-of-work bits for listings and revocations
|
||||
Clock func() time.Time // defaults to time.Now
|
||||
|
||||
// MessageDifficulty is the proof-of-work bits required of message
|
||||
// envelopes (message.DefaultDifficulty on a real node).
|
||||
MessageDifficulty int
|
||||
// MaxMessages caps the envelopes this node holds; 0 means 20000.
|
||||
MaxMessages int
|
||||
// OwnerQueue and MailboxQueue cap the envelopes per listing and per
|
||||
// mailbox; 0 means MaxOwnerQueue and MaxMailboxQueue.
|
||||
OwnerQueue, MailboxQueue int
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
difficulty int
|
||||
clock func() time.Time
|
||||
db *sql.DB
|
||||
difficulty int
|
||||
clock func() time.Time
|
||||
msgDifficulty int
|
||||
maxMessages int
|
||||
ownerQueue int
|
||||
mailboxQueue int
|
||||
}
|
||||
|
||||
const schema = `
|
||||
@@ -87,6 +100,56 @@ CREATE TABLE IF NOT EXISTS revocations (
|
||||
data BLOB NOT NULL,
|
||||
PRIMARY KEY (author, device)
|
||||
) WITHOUT ROWID;
|
||||
|
||||
-- Encrypted message envelopes this node relays (see internal/message).
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
hash TEXT PRIMARY KEY,
|
||||
to_dir TEXT NOT NULL, -- 'owner' or 'buyer'
|
||||
author TEXT NOT NULL, -- listing reference
|
||||
listing_id TEXT NOT NULL,
|
||||
rcpt TEXT NOT NULL, -- owner: device key; buyer: mailbox tag
|
||||
region TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
expires_at INTEGER NOT NULL,
|
||||
work INTEGER NOT NULL, -- leading zero bits, decides what a full queue evicts
|
||||
data BLOB NOT NULL
|
||||
) WITHOUT ROWID;
|
||||
CREATE INDEX IF NOT EXISTS messages_listing ON messages (to_dir, author, listing_id);
|
||||
CREATE INDEX IF NOT EXISTS messages_mailbox ON messages (to_dir, rcpt, created_at);
|
||||
CREATE INDEX IF NOT EXISTS messages_region ON messages (region, created_at, hash);
|
||||
CREATE INDEX IF NOT EXISTS messages_expires ON messages (expires_at);
|
||||
CREATE INDEX IF NOT EXISTS messages_work ON messages (work, created_at);
|
||||
|
||||
-- The owner's decrypted conversations. These exist only on the owner's node:
|
||||
-- they are never gossiped.
|
||||
CREATE TABLE IF NOT EXISTS inbox (
|
||||
hash TEXT PRIMARY KEY, -- hash of the envelope that carried it
|
||||
author TEXT NOT NULL,
|
||||
listing_id TEXT NOT NULL,
|
||||
thread TEXT NOT NULL,
|
||||
dir TEXT NOT NULL, -- 'in' from a buyer, 'out' our reply
|
||||
n INTEGER NOT NULL,
|
||||
sent_at INTEGER NOT NULL,
|
||||
received_at INTEGER NOT NULL,
|
||||
hidden INTEGER NOT NULL, -- broke the first-message rules
|
||||
data BLOB NOT NULL -- canonical plaintext
|
||||
) WITHOUT ROWID;
|
||||
CREATE INDEX IF NOT EXISTS inbox_thread ON inbox (author, listing_id, thread, sent_at);
|
||||
-- Envelopes the owner's node has already looked at (decrypted or not), so
|
||||
-- garbage is not decrypted again on every collection.
|
||||
CREATE TABLE IF NOT EXISTS inbox_seen (
|
||||
hash TEXT PRIMARY KEY,
|
||||
expires_at INTEGER NOT NULL
|
||||
) WITHOUT ROWID;
|
||||
-- The exact address of a listing, only ever read by its owner's node.
|
||||
CREATE TABLE IF NOT EXISTS addresses (
|
||||
author TEXT NOT NULL,
|
||||
listing_id TEXT NOT NULL,
|
||||
address TEXT NOT NULL,
|
||||
phone TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
PRIMARY KEY (author, listing_id)
|
||||
) WITHOUT ROWID;
|
||||
`
|
||||
|
||||
// Open opens (creating if needed) the database at path. Use ":memory:" for
|
||||
@@ -106,10 +169,21 @@ func Open(path string, opt Options) (*Store, error) {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
s := &Store{db: db, difficulty: opt.Difficulty, clock: opt.Clock}
|
||||
s := &Store{db: db, difficulty: opt.Difficulty, clock: opt.Clock,
|
||||
msgDifficulty: opt.MessageDifficulty, maxMessages: opt.MaxMessages,
|
||||
ownerQueue: opt.OwnerQueue, mailboxQueue: opt.MailboxQueue}
|
||||
if s.ownerQueue <= 0 {
|
||||
s.ownerQueue = MaxOwnerQueue
|
||||
}
|
||||
if s.mailboxQueue <= 0 {
|
||||
s.mailboxQueue = MaxMailboxQueue
|
||||
}
|
||||
if s.clock == nil {
|
||||
s.clock = time.Now
|
||||
}
|
||||
if s.maxMessages <= 0 {
|
||||
s.maxMessages = defaultMaxMessages
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
@@ -433,7 +507,16 @@ func (s *Store) Expire(ctx context.Context) (int64, error) {
|
||||
return n, err
|
||||
}
|
||||
m, _ := res.RowsAffected()
|
||||
return n + m, nil
|
||||
res, err = s.db.ExecContext(ctx, `DELETE FROM messages WHERE expires_at <= ?`, now)
|
||||
if err != nil {
|
||||
return n + m, err
|
||||
}
|
||||
k, _ := res.RowsAffected()
|
||||
// Not counted: housekeeping of the owner's "already seen" markers.
|
||||
if _, err := s.db.ExecContext(ctx, `DELETE FROM inbox_seen WHERE expires_at <= ?`, now); err != nil {
|
||||
return n + m + k, err
|
||||
}
|
||||
return n + m + k, nil
|
||||
}
|
||||
|
||||
// Count returns the number of live listings.
|
||||
|
||||
Reference in New Issue
Block a user