Add runnable node, HTTP API, all-devices revocation and reconnect sync
- cmd/node: init (recovery phrase onboarding), run, recover - api: public reads of canonical signed records, token-protected writes - identity: RevokeAll voids every device certified before a cutoff, so recovery works with only the phrase; store purges by certificate age - p2p: redial bootstrap peers, sync on every new connection (in-flight guard only), validate bootstrap addresses up front - docs: API, recovery semantics, peer-ID change on recovery Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+71
-33
@@ -58,6 +58,7 @@ CREATE TABLE IF NOT EXISTS listings (
|
||||
author TEXT NOT NULL,
|
||||
id TEXT NOT NULL,
|
||||
device TEXT NOT NULL,
|
||||
cert_issued_at INTEGER NOT NULL,
|
||||
seq INTEGER NOT NULL,
|
||||
hash TEXT NOT NULL,
|
||||
tombstone INTEGER NOT NULL,
|
||||
@@ -75,11 +76,13 @@ CREATE TABLE IF NOT EXISTS listings (
|
||||
CREATE INDEX IF NOT EXISTS listings_geohash ON listings (geohash);
|
||||
CREATE INDEX IF NOT EXISTS listings_expires ON listings (expires_at);
|
||||
CREATE INDEX IF NOT EXISTS listings_device ON listings (author, device);
|
||||
CREATE INDEX IF NOT EXISTS listings_cert ON listings (author, cert_issued_at);
|
||||
CREATE INDEX IF NOT EXISTS listings_created ON listings (created_at, author, id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS revocations (
|
||||
author TEXT NOT NULL,
|
||||
device TEXT NOT NULL,
|
||||
device TEXT NOT NULL, -- '' = every device certified before revoked_at
|
||||
revoked_at INTEGER NOT NULL,
|
||||
cert_expires_at INTEGER NOT NULL,
|
||||
data BLOB NOT NULL,
|
||||
PRIMARY KEY (author, device)
|
||||
@@ -140,9 +143,10 @@ func (s *Store) Put(ctx context.Context, l *listing.Listing) (Outcome, error) {
|
||||
defer tx.Rollback()
|
||||
|
||||
var revoked int
|
||||
err = tx.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM revocations WHERE author = ? AND device = ?`, l.Author, l.Device).
|
||||
Scan(&revoked)
|
||||
err = tx.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*) FROM revocations
|
||||
WHERE author = ? AND (device = ? OR (device = '' AND revoked_at > ?))`,
|
||||
l.Author, l.Device, l.Cert.IssuedAt).Scan(&revoked)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
@@ -174,10 +178,10 @@ func (s *Store) Put(ctx context.Context, l *listing.Listing) (Outcome, error) {
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT OR REPLACE INTO listings
|
||||
(author, id, device, seq, hash, tombstone, created_at, expires_at, geohash,
|
||||
(author, id, device, cert_issued_at, seq, hash, tombstone, created_at, expires_at, geohash,
|
||||
kind, category, price_cents, area_m2, rooms_x2, data)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
l.Author, l.ID, l.Device, int64(l.Seq), hash, boolInt(l.Tombstone), l.CreatedAt, l.ExpiresAt,
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
l.Author, l.ID, l.Device, l.Cert.IssuedAt, int64(l.Seq), hash, boolInt(l.Tombstone), l.CreatedAt, l.ExpiresAt,
|
||||
l.Geohash, l.Kind, l.Category, price, l.AreaM2, l.RoomsX2, data)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
@@ -324,30 +328,17 @@ func (s *Store) Since(ctx context.Context, prefix string, cur Cursor, limit int)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []*listing.Listing
|
||||
for rows.Next() {
|
||||
var data []byte
|
||||
if err := rows.Scan(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l, err := decode(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, l)
|
||||
}
|
||||
return out, rows.Err()
|
||||
return collect(rows)
|
||||
}
|
||||
|
||||
// PutRevocation verifies and stores a revocation, and removes every listing
|
||||
// the revoked device signed. The owner re-publishes them from a new device.
|
||||
// PutRevocation verifies and stores a revocation and removes every listing it
|
||||
// voids. The owner republishes them from a new device.
|
||||
func (s *Store) PutRevocation(ctx context.Context, r *identity.Revocation) (Outcome, error) {
|
||||
if err := r.Check(s.difficulty); err != nil {
|
||||
return Ignored, fmt.Errorf("%w: %v", ErrInvalid, err)
|
||||
}
|
||||
if r.CertExpiresAt <= s.clock().Unix() {
|
||||
return Ignored, nil // the certificate is dead anyway
|
||||
return Ignored, nil // everything it covers is dead anyway
|
||||
}
|
||||
data, err := r.Bytes()
|
||||
if err != nil {
|
||||
@@ -359,17 +350,31 @@ func (s *Store) PutRevocation(ctx context.Context, r *identity.Revocation) (Outc
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
res, err := tx.ExecContext(ctx,
|
||||
`INSERT OR IGNORE INTO revocations (author, device, cert_expires_at, data) VALUES (?, ?, ?, ?)`,
|
||||
r.Author, r.Device, r.CertExpiresAt, data)
|
||||
// Keep only the latest revocation per (author, device); for the
|
||||
// all-devices form (device = '') that is the one with the newest cutoff.
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO revocations (author, device, revoked_at, cert_expires_at, data)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT (author, device) DO UPDATE SET
|
||||
revoked_at = excluded.revoked_at,
|
||||
cert_expires_at = excluded.cert_expires_at,
|
||||
data = excluded.data
|
||||
WHERE excluded.revoked_at > revocations.revoked_at`,
|
||||
r.Author, r.Device, r.RevokedAt, r.CertExpiresAt, data)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return Ignored, nil // already known
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`DELETE FROM listings WHERE author = ? AND device = ?`, r.Author, r.Device); err != nil {
|
||||
if r.AllDevices() {
|
||||
_, err = tx.ExecContext(ctx,
|
||||
`DELETE FROM listings WHERE author = ? AND cert_issued_at < ?`, r.Author, r.RevokedAt)
|
||||
} else {
|
||||
_, err = tx.ExecContext(ctx,
|
||||
`DELETE FROM listings WHERE author = ? AND device = ?`, r.Author, r.Device)
|
||||
}
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
@@ -401,11 +406,14 @@ func (s *Store) Revocations(ctx context.Context) ([]*identity.Revocation, error)
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// IsRevoked reports whether the device has been revoked.
|
||||
func (s *Store) IsRevoked(ctx context.Context, author, device string) (bool, error) {
|
||||
// IsRevoked reports whether the device, certified at certIssuedAt, has been
|
||||
// voided by a revocation.
|
||||
func (s *Store) IsRevoked(ctx context.Context, author, device string, certIssuedAt int64) (bool, error) {
|
||||
var n int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM revocations WHERE author = ? AND device = ?`, author, device).Scan(&n)
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*) FROM revocations
|
||||
WHERE author = ? AND (device = ? OR (device = '' AND revoked_at > ?))`,
|
||||
author, device, certIssuedAt).Scan(&n)
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
@@ -437,6 +445,36 @@ func (s *Store) Count(ctx context.Context) (int, error) {
|
||||
return n, err
|
||||
}
|
||||
|
||||
// ByAuthor returns the author's live (not deleted, not expired) listings.
|
||||
func (s *Store) ByAuthor(ctx context.Context, author string) ([]*listing.Listing, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT data FROM listings
|
||||
WHERE author = ? AND tombstone = 0 AND expires_at > ?
|
||||
ORDER BY created_at, id`, author, s.clock().Unix())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return collect(rows)
|
||||
}
|
||||
|
||||
// collect decodes a result set of listing blobs and closes it.
|
||||
func collect(rows *sql.Rows) ([]*listing.Listing, error) {
|
||||
defer rows.Close()
|
||||
var out []*listing.Listing
|
||||
for rows.Next() {
|
||||
var data []byte
|
||||
if err := rows.Scan(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l, err := decode(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, l)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func decode(data []byte) (*listing.Listing, error) {
|
||||
var l listing.Listing
|
||||
if err := json.Unmarshal(data, &l); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user