Add device keys, revocation and libp2p gossip with sync
- identity: 12-word recovery phrase, device certificates, revocations, device key storage; the identity key is never stored on a node - listing: signed by a device with an embedded certificate, domain-separated signatures, ErrTimeWindow for clock-related failures, region helpers - store: revocations purge a device's listings; keyset-paged Since for sync - p2p: gossipsub topic per geohash region plus a global revocation topic, validators run before relaying, /allmende/1/sync catch-up protocol, bootstrap peers and mDNS - docs: key model, replication and conflict rules Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+61
-32
@@ -9,7 +9,6 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/bits"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
@@ -17,6 +16,7 @@ import (
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/jcs"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/pow"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -24,12 +24,35 @@ const (
|
||||
MaxLifetime = 30 * 24 * time.Hour
|
||||
MaxClockSkew = 10 * time.Minute
|
||||
GeohashPrecision = 6
|
||||
MaxSize = 16 << 10
|
||||
maxSafeInt = 1<<53 - 1
|
||||
// RegionPrecision is the geohash prefix length that names a gossip
|
||||
// region (~150 km cells).
|
||||
RegionPrecision = 3
|
||||
MaxSize = 16 << 10
|
||||
maxSafeInt = 1<<53 - 1
|
||||
|
||||
geohashAlphabet = "0123456789bcdefghjkmnpqrstuvwxyz"
|
||||
|
||||
listingContext = "allmende-listing-v1\n"
|
||||
)
|
||||
|
||||
// ErrTimeWindow marks failures caused by the clock (expired, or created in
|
||||
// the future). They are not the sender's fault, so the p2p layer ignores such
|
||||
// messages instead of penalizing the peer that relayed them.
|
||||
var ErrTimeWindow = errors.New("outside time window")
|
||||
|
||||
// Region returns the gossip region of a geohash.
|
||||
func Region(geohash string) string {
|
||||
if len(geohash) < RegionPrecision {
|
||||
return geohash
|
||||
}
|
||||
return geohash[:RegionPrecision]
|
||||
}
|
||||
|
||||
// ValidRegion reports whether s is a well-formed region name.
|
||||
func ValidRegion(s string) bool {
|
||||
return len(s) == RegionPrecision && ValidGeohashPrefix(s)
|
||||
}
|
||||
|
||||
var (
|
||||
kinds = map[string]bool{"rent": true, "sale": true}
|
||||
categories = map[string]bool{"apartment": true, "house": true, "room": true, "commercial": true}
|
||||
@@ -51,7 +74,8 @@ type Image struct {
|
||||
type Listing struct {
|
||||
Version int `json:"v"`
|
||||
ID string `json:"id"` // stable across versions
|
||||
Author string `json:"author"` // hex ed25519 public key
|
||||
Author string `json:"author"` // identity public key (hex)
|
||||
Device string `json:"device"` // signing device's public key (hex)
|
||||
Seq uint64 `json:"seq"` // higher replaces lower
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
ExpiresAt int64 `json:"expires_at"`
|
||||
@@ -66,10 +90,12 @@ type Listing struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Images []Image `json:"images,omitempty"`
|
||||
Endpoint string `json:"endpoint,omitempty"` // owner's peer ID
|
||||
Delegate string `json:"delegate,omitempty"` // optional always-on host's peer ID
|
||||
PoW uint64 `json:"pow"`
|
||||
Sig string `json:"sig,omitempty"` // hex, over canonical bytes without sig
|
||||
// Cert proves that Device may sign for Author. The owner's node is the
|
||||
// peer whose ID derives from Device.
|
||||
Cert *identity.Cert `json:"cert"`
|
||||
Sig string `json:"sig,omitempty"` // device signature, hex
|
||||
}
|
||||
|
||||
// NewID returns a fresh random listing ID (32 hex characters).
|
||||
@@ -104,29 +130,20 @@ func (l *Listing) Hash() (string, error) {
|
||||
return hex.EncodeToString(sum[:]), nil
|
||||
}
|
||||
|
||||
func leadingZeroBits(sum [32]byte) int {
|
||||
n := 0
|
||||
for _, b := range sum {
|
||||
if b == 0 {
|
||||
n += 8
|
||||
continue
|
||||
}
|
||||
return n + bits.LeadingZeros8(b)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (l *Listing) powOK(difficulty int) (bool, error) {
|
||||
b, err := l.unsignedBytes()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return leadingZeroBits(sha256.Sum256(b)) >= difficulty, nil
|
||||
return pow.LeadingZeroBits(b) >= difficulty, nil
|
||||
}
|
||||
|
||||
// Seal sets the author, mines the proof-of-work and signs the listing.
|
||||
func (l *Listing) Seal(id *identity.Identity, difficulty int) error {
|
||||
l.Author = id.PublicKeyHex()
|
||||
// Seal fills in author, device and certificate from dev, mines the
|
||||
// proof-of-work and signs the listing with the device key.
|
||||
func (l *Listing) Seal(dev *identity.Device, difficulty int) error {
|
||||
l.Author = dev.Cert.Author
|
||||
l.Device = dev.Cert.Device
|
||||
l.Cert = dev.Cert
|
||||
l.Sig = ""
|
||||
for l.PoW = 0; ; l.PoW++ {
|
||||
if l.PoW > maxSafeInt {
|
||||
@@ -144,7 +161,7 @@ func (l *Listing) Seal(id *identity.Identity, difficulty int) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l.Sig = hex.EncodeToString(id.Sign(b))
|
||||
l.Sig = hex.EncodeToString(dev.Key.Sign(append([]byte(listingContext), b...)))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -169,7 +186,7 @@ func (l *Listing) Check(now time.Time, difficulty int) error {
|
||||
if err != nil || len(sig) != 64 {
|
||||
return errors.New("listing: malformed signature")
|
||||
}
|
||||
if !identity.Verify(l.Author, unsigned, sig) {
|
||||
if !identity.Verify(l.Device, append([]byte(listingContext), unsigned...), sig) {
|
||||
return errors.New("listing: bad signature")
|
||||
}
|
||||
ok, err := l.powOK(difficulty)
|
||||
@@ -194,8 +211,23 @@ func (l *Listing) validate(now time.Time) error {
|
||||
if !isHex(l.ID, 32) {
|
||||
return errors.New("listing: id must be 32 hex characters")
|
||||
}
|
||||
if !isHex(l.Author, 64) {
|
||||
return errors.New("listing: author must be 64 hex characters")
|
||||
if !isHex(l.Author, 64) || !isHex(l.Device, 64) {
|
||||
return errors.New("listing: author and device must be 64 hex characters")
|
||||
}
|
||||
if l.Cert == nil {
|
||||
return errors.New("listing: certificate required")
|
||||
}
|
||||
if err := l.Cert.Check(); err != nil {
|
||||
return fmt.Errorf("listing: %w", err)
|
||||
}
|
||||
if l.Cert.Author != l.Author || l.Cert.Device != l.Device {
|
||||
return errors.New("listing: certificate does not match author and device")
|
||||
}
|
||||
if !l.Cert.Covers(l.CreatedAt) {
|
||||
return errors.New("listing: created outside the certificate's validity")
|
||||
}
|
||||
if now.Unix() >= l.Cert.ExpiresAt {
|
||||
return fmt.Errorf("listing: certificate expired: %w", ErrTimeWindow)
|
||||
}
|
||||
if l.Seq > maxSafeInt || l.PoW > maxSafeInt {
|
||||
return errors.New("listing: number out of range")
|
||||
@@ -207,10 +239,10 @@ func (l *Listing) validate(now time.Time) error {
|
||||
return errors.New("listing: lifetime exceeds 30 days")
|
||||
}
|
||||
if time.Unix(l.CreatedAt, 0).After(now.Add(MaxClockSkew)) {
|
||||
return errors.New("listing: created_at is in the future")
|
||||
return fmt.Errorf("listing: created_at is in the future: %w", ErrTimeWindow)
|
||||
}
|
||||
if !time.Unix(l.ExpiresAt, 0).After(now) {
|
||||
return errors.New("listing: expired")
|
||||
return fmt.Errorf("listing: expired: %w", ErrTimeWindow)
|
||||
}
|
||||
if err := validGeohash(l.Geohash); err != nil {
|
||||
return err
|
||||
@@ -224,7 +256,7 @@ func (l *Listing) validate(now time.Time) error {
|
||||
func (l *Listing) validateTombstone() error {
|
||||
if l.Kind != "" || l.Category != "" || l.Price != nil || l.AreaM2 != 0 || l.RoomsX2 != 0 ||
|
||||
len(l.Features) > 0 || l.Title != "" || l.Description != "" || len(l.Images) > 0 ||
|
||||
l.Endpoint != "" || l.Delegate != "" {
|
||||
l.Delegate != "" {
|
||||
return errors.New("listing: tombstone must not carry content")
|
||||
}
|
||||
return nil
|
||||
@@ -276,9 +308,6 @@ func (l *Listing) validateContent() error {
|
||||
return errors.New("listing: image hashes must be 64 hex characters")
|
||||
}
|
||||
}
|
||||
if !validPeerID(l.Endpoint) {
|
||||
return errors.New("listing: invalid endpoint")
|
||||
}
|
||||
if l.Delegate != "" && !validPeerID(l.Delegate) {
|
||||
return errors.New("listing: invalid delegate")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user