Scaffold Allmende: identity, canonical JSON, signed listings

- internal/jcs: RFC 8785 canonical JSON (integers only)
- internal/identity: ed25519 keypair, 0600 key file, no overwrite
- internal/listing: signed listing record with PoW, validation,
  tombstones and supersede rules
- docs/PROTOCOL.md: architecture and decisions so far

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 17:02:50 +01:00
co-authored by Claude Sonnet 5.5
commit cdc9c3d20b
9 changed files with 960 additions and 0 deletions
+199
View File
@@ -0,0 +1,199 @@
package listing
import (
"strings"
"testing"
"time"
"git.stinnesbeck.com/nils/allmende/internal/identity"
)
const testDifficulty = 12
var now = time.Unix(1_800_000_000, 0)
func sample(t *testing.T) *Listing {
t.Helper()
id, err := NewID()
if err != nil {
t.Fatal(err)
}
return &Listing{
Version: Version,
ID: id,
Seq: 1,
CreatedAt: now.Unix(),
ExpiresAt: now.Add(7 * 24 * time.Hour).Unix(),
Kind: "rent",
Category: "apartment",
Geohash: "u1xjhx",
Price: &Price{AmountCents: 120000, Currency: "EUR", Period: "month"},
AreaM2: 55,
RoomsX2: 5,
Features: []string{"balcony", "elevator"},
Title: "Helle 2,5-Zimmer-Wohnung",
Endpoint: "12D3KooWExamplePeerID",
}
}
func sealed(t *testing.T) (*Listing, *identity.Identity) {
t.Helper()
key, err := identity.Generate()
if err != nil {
t.Fatal(err)
}
l := sample(t)
if err := l.Seal(key, testDifficulty); err != nil {
t.Fatal(err)
}
return l, key
}
func TestSealCheckRoundTrip(t *testing.T) {
l, _ := sealed(t)
if err := l.Check(now, testDifficulty); err != nil {
t.Fatal(err)
}
}
func TestCanonicalBytesDeterministic(t *testing.T) {
l, _ := sealed(t)
a, _ := l.Bytes()
b, _ := l.Bytes()
if string(a) != string(b) {
t.Fatal("canonical bytes differ between calls")
}
// keys must be sorted: "author" comes first.
if !strings.HasPrefix(string(a), `{"area_m2":55,"author":`) {
t.Fatalf("unexpected canonical prefix: %s", a[:40])
}
}
func TestTamperDetected(t *testing.T) {
l, _ := sealed(t)
l.Price.AmountCents = 1
if err := l.Check(now, testDifficulty); err == nil {
t.Fatal("tampered listing accepted")
}
}
func TestForeignSignatureRejected(t *testing.T) {
l, _ := sealed(t)
other, _ := identity.Generate()
l.Author = other.PublicKeyHex()
if err := l.Check(now, testDifficulty); err == nil {
t.Fatal("listing with swapped author accepted")
}
}
func TestInsufficientPoW(t *testing.T) {
l, _ := sealed(t)
// Difficulty demanded by the verifier is far above what was mined.
if err := l.Check(now, 40); err == nil {
t.Fatal("expected proof-of-work failure")
}
}
func TestExpiryAndLifetime(t *testing.T) {
key, _ := identity.Generate()
l := sample(t)
l.ExpiresAt = l.CreatedAt + int64(31*24*3600)
_ = l.Seal(key, 0)
if err := l.Check(now, 0); err == nil {
t.Fatal("lifetime over 30 days accepted")
}
l = sample(t)
_ = l.Seal(key, 0)
if err := l.Check(now.Add(8*24*time.Hour), 0); err == nil {
t.Fatal("expired listing accepted")
}
l = sample(t)
l.CreatedAt = now.Add(time.Hour).Unix()
l.ExpiresAt = l.CreatedAt + 3600
_ = l.Seal(key, 0)
if err := l.Check(now, 0); err == nil {
t.Fatal("future-dated listing accepted")
}
}
func TestContentValidation(t *testing.T) {
key, _ := identity.Generate()
cases := map[string]func(*Listing){
"bad kind": func(l *Listing) { l.Kind = "swap" },
"bad category": func(l *Listing) { l.Category = "castle" },
"bad geohash": func(l *Listing) { l.Geohash = "u1xjh" },
"geohash alphabet": func(l *Listing) { l.Geohash = "u1xjha" }, // 'a' is not in the alphabet
"rent period": func(l *Listing) { l.Price.Period = "once" },
"zero price": func(l *Listing) { l.Price.AmountCents = 0 },
"currency": func(l *Listing) { l.Price.Currency = "USD" },
"empty title": func(l *Listing) { l.Title = "" },
"control char": func(l *Listing) { l.Title = "a\x00b" },
"bad image hash": func(l *Listing) { l.Images = []Image{{Hash: "zz", ThumbHash: "zz"}} },
"no endpoint": func(l *Listing) { l.Endpoint = "" },
"huge description": func(l *Listing) { l.Description = strings.Repeat("x", 4001) },
}
for name, mutate := range cases {
t.Run(name, func(t *testing.T) {
l := sample(t)
mutate(l)
if err := l.Seal(key, 0); err != nil {
t.Fatal(err)
}
if err := l.Check(now, 0); err == nil {
t.Fatal("invalid listing accepted")
}
})
}
}
func TestTombstoneAndSupersedes(t *testing.T) {
l, key := sealed(t)
ts := &Listing{
Version: Version,
ID: l.ID,
Seq: l.Seq + 1,
CreatedAt: now.Unix(),
ExpiresAt: now.Add(24 * time.Hour).Unix(),
Tombstone: true,
Geohash: l.Geohash,
}
if err := ts.Seal(key, testDifficulty); err != nil {
t.Fatal(err)
}
if err := ts.Check(now, testDifficulty); err != nil {
t.Fatal(err)
}
if !ts.Supersedes(l) || l.Supersedes(ts) {
t.Fatal("tombstone must supersede the original, not the reverse")
}
// A different author cannot supersede someone else's listing.
other, _ := identity.Generate()
forged := *ts
_ = forged.Seal(other, testDifficulty)
if forged.Supersedes(l) {
t.Fatal("tombstone by a different author must not supersede")
}
// Tombstones must not smuggle content.
ts.Title = "still here"
_ = ts.Seal(key, testDifficulty)
if err := ts.Check(now, testDifficulty); err == nil {
t.Fatal("tombstone with content accepted")
}
}
func TestHashChangesWithContent(t *testing.T) {
a, _ := sealed(t)
b := *a
b.Seq++
ha, _ := a.Hash()
hb, _ := b.Hash()
if ha == hb {
t.Fatal("hash must differ for different content")
}
}