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:
@@ -0,0 +1,152 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/message"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/store"
|
||||
)
|
||||
|
||||
const msgRegion = "u1x"
|
||||
|
||||
func hello(t *testing.T, text string) *message.Plain {
|
||||
t.Helper()
|
||||
return &message.Plain{Type: message.TypeText, N: 1, SentAt: time.Now().Unix(), Text: text}
|
||||
}
|
||||
|
||||
func TestMessagesTravelBetweenBuyerGatewayAndOwner(t *testing.T) {
|
||||
owner := startNode(t, nil, msgRegion)
|
||||
gateway := startNode(t, nil, msgRegion)
|
||||
connect(t, gateway, owner)
|
||||
|
||||
l := owner.listing(t, "u1xjhx")
|
||||
if err := owner.Publish(context.Background(), l); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eventually(t, "gateway learns the listing", func() bool {
|
||||
_, err := gateway.s.Get(context.Background(), l.Author, l.ID)
|
||||
return err == nil
|
||||
})
|
||||
|
||||
// The buyer's browser sends through the gateway.
|
||||
tk, err := message.NewThreadKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env, err := message.SealToOwner(l, tk, hello(t, "Ist die Wohnung noch frei?"), time.Now(), msgDifficulty)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := gateway.PublishMessage(context.Background(), msgRegion, env); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var got []*message.Envelope
|
||||
eventually(t, "owner receives the message", func() bool {
|
||||
got, _ = owner.s.OwnerMessages(context.Background(), l.Author)
|
||||
return len(got) == 1
|
||||
})
|
||||
plain, err := got[0].OpenAsOwner(owner.dev)
|
||||
if err != nil || plain.Text != "Ist die Wohnung noch frei?" {
|
||||
t.Fatalf("owner could not read it: %v %+v", err, plain)
|
||||
}
|
||||
|
||||
// The owner answers; the reply waits in the gateway's mailbox.
|
||||
reply := &message.Plain{Type: message.TypeText, Thread: plain.Thread, N: 1, SentAt: time.Now().Unix(), Text: "Ja!"}
|
||||
renv, err := message.SealToBuyer(owner.dev, message.Ref{Author: l.Author, ID: l.ID}, reply, time.Now(), msgDifficulty)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := owner.PublishMessage(context.Background(), msgRegion, renv); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mailbox, _ := tk.Mailbox()
|
||||
var box []*message.Envelope
|
||||
eventually(t, "reply reaches the gateway's mailbox", func() bool {
|
||||
box, _ = gateway.s.Mailbox(context.Background(), mailbox, 0, 10)
|
||||
return len(box) == 1
|
||||
})
|
||||
back, err := box[0].OpenAsBuyer(tk)
|
||||
if err != nil || back.Text != "Ja!" {
|
||||
t.Fatalf("buyer could not read the reply: %v %+v", err, back)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLateJoinersSyncMessagesAfterListings(t *testing.T) {
|
||||
owner := startNode(t, nil, msgRegion)
|
||||
l := owner.listing(t, "u1xjhx")
|
||||
if err := owner.Publish(context.Background(), l); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tk, _ := message.NewThreadKey()
|
||||
env, err := message.SealToOwner(l, tk, hello(t, "hallo"), time.Now(), msgDifficulty)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := owner.PublishMessage(context.Background(), msgRegion, env); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reply := &message.Plain{Type: message.TypeText, Thread: tk.PublicHex(), N: 1, SentAt: time.Now().Unix(), Text: "ok"}
|
||||
renv, _ := message.SealToBuyer(owner.dev, message.Ref{Author: l.Author, ID: l.ID}, reply, time.Now(), msgDifficulty)
|
||||
if err := owner.PublishMessage(context.Background(), msgRegion, renv); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Joins later, and in another region as well, which must stay empty.
|
||||
late := startNode(t, nil, msgRegion)
|
||||
other := startNode(t, nil, "u1y")
|
||||
connect(t, late, owner)
|
||||
connect(t, other, owner)
|
||||
|
||||
eventually(t, "late joiner has the owner-bound message", func() bool {
|
||||
got, _ := late.s.OwnerMessages(context.Background(), l.Author)
|
||||
return len(got) == 1
|
||||
})
|
||||
mailbox, _ := tk.Mailbox()
|
||||
eventually(t, "late joiner has the reply", func() bool {
|
||||
box, _ := late.s.Mailbox(context.Background(), mailbox, 0, 10)
|
||||
return len(box) == 1
|
||||
})
|
||||
if n, _ := other.s.MessageCount(context.Background()); n != 0 {
|
||||
t.Fatalf("a node in another region holds %d envelopes", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidMessagesAreNotAccepted(t *testing.T) {
|
||||
a := startNode(t, nil, msgRegion)
|
||||
l := a.listing(t, "u1xjhx")
|
||||
if err := a.Publish(context.Background(), l); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tk, _ := message.NewThreadKey()
|
||||
|
||||
// No proof-of-work at this node's difficulty.
|
||||
var weak *message.Envelope
|
||||
for {
|
||||
e, err := message.SealToOwner(l, tk, hello(t, "x"), time.Now(), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w, _ := e.Work(); w < msgDifficulty {
|
||||
weak = e
|
||||
break
|
||||
}
|
||||
}
|
||||
if err := a.PublishMessage(context.Background(), msgRegion, weak); !errors.Is(err, store.ErrInvalid) {
|
||||
t.Fatalf("weak message: %v, want ErrInvalid", err)
|
||||
}
|
||||
|
||||
// A listing this node does not hold.
|
||||
b := startNode(t, nil, msgRegion)
|
||||
foreign := b.listing(t, "u1xjhx")
|
||||
env, _ := message.SealToOwner(foreign, tk, hello(t, "x"), time.Now(), msgDifficulty)
|
||||
if err := a.PublishMessage(context.Background(), msgRegion, env); !errors.Is(err, store.ErrNoListing) {
|
||||
t.Fatalf("unknown listing: %v, want ErrNoListing", err)
|
||||
}
|
||||
if n, _ := a.s.MessageCount(context.Background()); n != 0 {
|
||||
t.Fatalf("rejected messages were stored (%d)", n)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user