room

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: GPL-3.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package room is a party's side of a v2 room: the room card (the contract's § Room card), the room state record `rm.<id>` every fleet host shares (plan decision 2 — the folded roster, own keys, cursor, seen ids, counters, membership version, the add setting; snapshot + Commit under CAS as v1's channel record), and the fold (plan decision 4 — rules 7 and 4 of results/group-design-chosen.md and the contract's sub-rules in one function). It reads verified records (Verified) and never the sealed bytes: internal/sealed opens a record, the room commands hand the fold what came out.

Index

Constants

View Source
const (
	KindRoster   = "roster"
	KindLeave    = "leave"
	KindRoomCard = "roomcard"
	KindHandover = "handover"
)

The membership record kinds (contract § Kinds). Every other kind in a room is a v1 message kind, delivered unchanged; the fold drops anything else.

View Source
const CardVersion = 1

CardVersion is the room card format version.

View Source
const LinkKeySize = 32

LinkKeySize is a feed link key's length in bytes.

View Source
const MaxSealed = 256 * 1024

MaxSealed is the fleet store's cap on a record: the UTF-8 bytes of the pages envelope {"schema","data"} with the sealed record in standard base64 (internal/store/pages MaxEnvelope). SealedSize measures a record against it.

View Source
const MaxSeen = 200

MaxSeen is how many message ids a room record remembers (plan decision 2); the oldest go first. The relay's seq and the writer's ctr already order a writer; the ring is the fleet's dedupe by message id.

View Source
const Retention = 48 * time.Hour

Retention is how long a replaced seal key is kept after the commit that replaced it: one rotation plus a margin (rule 3).

View Source
const RotateEvery = 24 * time.Hour

RotateEvery is how old a member's folded room card may be before serve rotates the seal key.

View Source
const SigSize = 64

SigSize is the length of the identity-key signature a room card carries.

Variables

View Source
var (
	ErrSignature = errors.New("room card: identity-key signature does not verify")
	ErrMalformed = errors.New("room card: malformed")
)

Errors a reader can match.

View Source
var (
	ErrNoRoom = errors.New("room: no state record")
	ErrCap    = errors.New("room: the record would pass the sealed cap")
)

Errors a caller can match.

Functions

func GenerateSealKey

func GenerateSealKey() (*ecdh.PrivateKey, error)

GenerateSealKey makes a fresh X25519 seal key, the daily rotation's input.

func Key

func Key(id string) string

Key is the room record's key in the store.

func LinkSealKey

func LinkSealKey(linkKey []byte, room string) (*ecdh.PrivateKey, error)

LinkSealKey is the link stanza's X25519 key: HKDF-SHA256 over the link key with info "peer feed link v1" ‖ room. The owner seals to its public half; a link holder opens the stanza at idx 65535 with it.

func MembershipKind

func MembershipKind(kind string) bool

MembershipKind reports whether kind is one the fold reads.

func SealedSize

func SealedSize(n int) int

SealedSize is the envelope size of a record of n plaintext bytes on the fleet store.

func ValidID

func ValidID(id string) bool

ValidID reports whether id is a room id: 32 lowercase hex characters.

Types

type Add

type Add string

Add is the room's add setting, fixed at creation (rule 5): who may post a roster add.

const (
	// AddAdmins: the admin set adds.
	AddAdmins Add = "admins"
	// AddMembers: any write member adds (entries only).
	AddMembers Add = "members"
)

type Card

type Card struct {
	V      int       `json:"v"`
	Room   string    `json:"room"`
	Idx    uint16    `json:"idx"`
	Seal   string    `json:"seal"`
	Sign   string    `json:"sign"`
	Issued time.Time `json:"issued"`
}

Card is a member's per-room public keys at an idx: the X25519 seal key the daily rotation replaces (rule 8) and the long-lived Ed25519 signing key. It travels as its JSON bytes followed by the 64-byte XEdDSA signature of the party's identity key over them (Signed / Verify), only inside channel messages and room records — never in the clear on the relay.

func NewCard

func NewCard(room string, idx uint16, seal *ecdh.PublicKey, sign ed25519.PublicKey, issued time.Time) *Card

NewCard builds the card for seal and sign at idx in room, issued at a time (kept to the millisecond, UTC, as v1 stamps messages).

func Verify

func Verify(signed []byte, pc *card.Card, at time.Time) (*Card, error)

Verify checks signed — a card's JSON bytes followed by its signature — against the identity keys the party card lists and has not retired at `at`, then parses it. A room card whose signature fails is not parsed.

func (*Card) SealKey

func (c *Card) SealKey() (*ecdh.PublicKey, error)

SealKey is the card's X25519 seal public key; a small-order point refuses.

func (*Card) SignKey

func (c *Card) SignKey() (ed25519.PublicKey, error)

SignKey is the card's Ed25519 signing public key.

func (*Card) Signed

func (c *Card) Signed(id *keys.Identity) ([]byte, error)

Signed serializes c and signs the bytes with the party's identity key (XEdDSA, as v1 signs a signed prekey): the bytes a room card travels as.

type Channels

type Channels func(identity [32]byte) (root ed25519.PublicKey, ok bool)

Channels answers rule 4 for the fold: the root a confirmed v1 channel pins for the peer that holds an identity key, ok false when no confirmed channel holds it. nil skips the cross-check.

type Entry

type Entry struct {
	Idx      uint16    `json:"idx"`
	Party    string    `json:"party"`
	Card     PartyCard `json:"card"`
	RoomCard []byte    `json:"roomcard"`
}

Entry is one roster entry as it travels in a roster record's `members` and a room-invite's `roster`, and as the state keeps it: the member's idx, its party id, its root-signed party card and its identity-signed room card (Card.Signed's bytes, standard base64 in JSON).

type HandoverBody

type HandoverBody struct {
	Idx uint16 `json:"idx"`
}

HandoverBody is a handover record's body: the idx that joins the admin set.

type Link struct {
	Key    []byte   `json:"key"`
	Token  string   `json:"token"`
	Idx    uint16   `json:"idx"`
	Owner  uint16   `json:"owner"`
	Revoke []uint16 `json:"revoke,omitempty"`
}

Link is a feed's link as its owner's state holds it (contract § Sealed record, Feed link): the link key, the multi-use read token and its relay idx, the roster idx whose room signing key the link names, and the idxs of earlier links whose revoke has not landed yet.

type Member

type Member struct {
	Entry
	// contains filtered or unexported fields
}

Member is one folded roster entry with its room card opened.

func (*Member) Issued

func (m *Member) Issued() time.Time

Issued is when the member's folded room card was issued.

func (*Member) Seal

func (m *Member) Seal() *ecdh.PublicKey

Seal is the member's current X25519 seal public key — the stanza's recipient key.

func (*Member) Sign

func (m *Member) Sign() ed25519.PublicKey

Sign is the member's Ed25519 signing public key — what its records verify under.

type OwnKeys

type OwnKeys struct {
	Sign ed25519.PrivateKey
	Seal *ecdh.PrivateKey
}

OwnKeys is the member's own per-room key pairs: the signing key that lives as long as the membership (rule 8) and the seal key the daily rotation replaces.

func GenerateKeys

func GenerateKeys() (*OwnKeys, error)

GenerateKeys makes a fresh signing key and seal key.

func (*OwnKeys) Card

func (k *OwnKeys) Card(room string, idx uint16, issued time.Time) *Card

Card is the unsigned room card for the keys at idx in room.

type PartyCard

type PartyCard struct {
	Body []byte `json:"body"`
	Sig  []byte `json:"sig"`
}

PartyCard is a party card as it travels inside an entry: the exact bytes and the root's detached signature (internal/card Signed).

type Result

type Result struct {
	// Dropped is why the whole record was dropped — rule 7, a malformed body,
	// a kind outside the table — the line the human is told; "" when it folded.
	Dropped string
	// Refused lists the entries the fold set aside and the human is told of:
	// rule 4, the cap, a card that does not verify, a held or removed idx.
	Refused []string
	// Joined, Updated and Left are the idxs the record added, re-carded and
	// removed; Admitted the idx a handover added to the admin set.
	Joined, Updated, Left, Admitted []uint16
}

Result is what one fold did.

func (*Result) Folded

func (r *Result) Folded() bool

Folded reports whether the record was applied (entries may still be refused).

type RoomCardBody

type RoomCardBody struct {
	RoomCard []byte `json:"roomcard"`
}

RoomCardBody is a roomcard record's body: the writer's new room card.

type RosterBody

type RosterBody struct {
	Add     Add      `json:"add,omitempty"`
	Members []Entry  `json:"members,omitempty"`
	Removed []uint16 `json:"removed,omitempty"`
}

RosterBody is a roster record's body: entries added or re-carded, idxs removed, and on record #0 the add setting.

type State

type State struct {
	// contains filtered or unexported fields
}

State is a room's record `rm.<id>`, loaded at a version: the folded roster and admin set, the member's own keys, the pull cursor, the seen ids, the writers' counters, the relay's membership version and the add setting. Mutations stay in memory until Commit; a Commit that finds another host's version refuses with store.ErrConflict and changes nothing — reload and redo the work (plan decision 5).

func Load

func Load(ctx context.Context, b store.Backend, id string) (*State, error)

Load reads the room's record; ErrNoRoom when absent.

func New

func New(b store.Backend, id string, idx uint16, add Add, own *OwnKeys) (*State, error)

New is a fresh state at version 0 for the member at idx of room id with its own keys; the admin set is idx 0. Commit births the record, and refuses with store.ErrConflict when one exists.

func (*State) Add

func (s *State) Add() Add

Add is the room's add setting.

func (*State) Admins

func (s *State) Admins() []uint16

Admins is the admin set: idx 0 plus folded handovers, less whoever left or was removed.

func (*State) CardCurrent

func (s *State) CardCurrent() bool

CardCurrent reports whether the folded own entry announces the current seal key — false after a rotation until the roomcard that carries it is folded (at send for a write member, from an admin's roster entry for a read member).

func (*State) CheckCounter

func (s *State) CheckCounter(idx uint16, ctr uint64) bool

CheckCounter records a writer's ctr and reports whether it continued the writer's count (rule 10): true in order, false on a gap or a step back — reported by the caller, reading continues. A writer's first record is in order whatever it carries: a newcomer's floor hides what came before.

func (*State) Commit

func (s *State) Commit(ctx context.Context) error

Commit writes the record as version Version()+1; a record past MaxSealed refuses with ErrCap before the store sees it, another host's version with store.ErrConflict.

func (*State) Counter

func (s *State) Counter(idx uint16) (uint64, bool)

Counter is the highest ctr seen from a writer, false before its first record.

func (*State) Cursor

func (s *State) Cursor() int64

Cursor is the relay seq of the last record pulled, -1 before the first.

func (*State) Drop

func (s *State) Drop(ctx context.Context) error

Drop replaces the record with a tombstone at the next version: the party left or was removed, or the room closed, and its keys and roster are not kept. Load answers ErrNoRoom from then on; a fresh state's Commit takes the tombstone's place. Another host's version refuses with store.ErrConflict.

func (*State) Dropped

func (s *State) Dropped() int64

Dropped is how many records and roster entries the fold set aside (rules 7 and 4): a record dropped whole, an entry refused.

func (*State) Feed

func (s *State) Feed() bool

Feed reports whether the room is a feed this state holds the link of.

func (*State) Fold

func (s *State) Fold(rec Verified, ch Channels) Result

Fold applies one membership record to the state in memory: rule 7's table (who may author which record), rule 4's cross-check of every entry against the member's channels, and the contract's sub-rules. The caller folds in seq order and commits; an own record folds at send, before its put. Anything the table does not allow is dropped and named in the Result; an entry that does not hold is refused and named, the rest fold.

func (*State) ID

func (s *State) ID() string

ID is the room id.

func (*State) Idx

func (s *State) Idx() uint16

Idx is the member's own idx.

func (*State) IsAdmin

func (s *State) IsAdmin(idx uint16) bool

IsAdmin reports whether idx is in the admin set.

func (*State) LastRotated

func (s *State) LastRotated() time.Time

LastRotated is when the seal key last changed: the commit that replaced the newest retired key, else the folded own card's issued time; zero while the party's own entry is not folded.

func (s *State) Link() *Link

Link is the feed's link, nil for a room that is not a feed.

func (*State) MarkSeen

func (s *State) MarkSeen(id string, seq int64)

MarkSeen records a message id with the relay seq it arrived under; the ring keeps the last MaxSeen.

func (*State) Member

func (s *State) Member(idx uint16) *Member

Member is the folded entry at idx, nil when none.

func (*State) Members

func (s *State) Members() []*Member

Members is the folded roster in idx order.

func (*State) Membership

func (s *State) Membership() int64

Membership is the relay's membership version as last observed — what a put carries.

func (*State) NextCounter

func (s *State) NextCounter() uint64

NextCounter is the ctr the member's next record carries: its own count of records put in this room, +1 each.

func (*State) NoteDropped

func (s *State) NoteDropped(n int)

NoteDropped counts n more records or entries the fold set aside.

func (*State) NoteReported

func (s *State) NoteReported()

NoteReported counts one more reported record.

func (*State) ObserveMembership

func (s *State) ObserveMembership(v int64)

ObserveMembership records the membership version a relay answer carried; the version only rises.

func (*State) OwnCard

func (s *State) OwnCard(issued time.Time) *Card

OwnCard is the unsigned room card for the member's current keys; the caller signs it with the party's identity key (Card.Signed).

func (*State) Posted

func (s *State) Posted(seq int64)

Posted records the relay seq the roomcard announcing the current seal key landed at, on every retired key still waiting for it.

func (*State) Removed

func (s *State) Removed(idx uint16) bool

Removed reports whether idx was removed or left and no admin has re-added it.

func (*State) Reported

func (s *State) Reported() int64

Reported is how many records the receiver reported: a counter gap or step back, a stanza that did not open, a record that did not verify.

func (*State) Retire

func (s *State) Retire(now time.Time) int

Retire drops the retired seal keys whose time is up: Retention past the commit that replaced them, and never while the cursor is below the seq of the roomcard that replaced them (rule 3) — a key whose roomcard was never Posted is kept. It returns how many it dropped.

func (*State) RetiredSealKeys

func (s *State) RetiredSealKeys() []*ecdh.PrivateKey

RetiredSealKeys lists the replaced seal keys still held, newest first — tried after the current one on a record's own stanza.

func (*State) Rotate

func (s *State) Rotate(k *ecdh.PrivateKey, now time.Time)

Rotate makes k the current seal key and retires the one it replaces, stamped with the time of the commit that follows (rule 9: commit, then post the roomcard; Posted records where it landed).

func (*State) RotationDue

func (s *State) RotationDue(now time.Time) bool

RotationDue reports whether serve rotates now: the party's own entry is folded, no rotation is pending, and RotateEvery has passed since the key last changed.

func (*State) RotationPending

func (s *State) RotationPending() bool

RotationPending reports whether a rotation committed and its roomcard has not been recorded as posted (Posted) — the next round posts it, nobody rotates again.

func (*State) SealKey

func (s *State) SealKey() *ecdh.PrivateKey

SealKey is the member's current seal key: what the next room card announces.

func (*State) Seed

func (s *State) Seed(entries []Entry, admins []uint16, ch Channels, at time.Time) Result

Seed folds the roster a room-invite carries into a newcomer's fresh state — the inviter's word, bounded by rule 4 and each entry's signatures as a roster record is, with no author to authorize — and takes the admin set the invite names as the starting one: record #0 and the handovers before the newcomer's from are below it, and a creator that left is out of it.

func (*State) Seen

func (s *State) Seen(id string) bool

Seen reports whether a message id was marked seen.

func (*State) SetCursor

func (s *State) SetCursor(seq int64)

SetCursor records the relay seq of the last record pulled.

func (s *State) SetLink(l Link) error

SetLink makes the room a feed with link l, or replaces its link; Commit keeps it.

func (*State) SignKey

func (s *State) SignKey() ed25519.PrivateKey

SignKey is the member's room signing key.

func (*State) Size

func (s *State) Size() (int, error)

Size is the record's sealed envelope size as it stands, against MaxSealed.

func (*State) Version

func (s *State) Version() int64

Version is the loaded version; Commit raises it.

type Verified

type Verified struct {
	// Seq is the relay's seq; -1 for an own record folded at send before its put.
	Seq int64
	// Idx is the writer's idx.
	Idx uint16
	// Kind and Body are the message's kind and body JSON.
	Kind string
	Body []byte
	// At is when the relay stored the record (its `at`) — the moment a party
	// card's key retirement is read against, never the header's `at`, which
	// the writer chooses and signs. An own record folded at send takes the
	// local clock.
	At time.Time
}

Verified is a record after the sealed layer opened it — the seam the room commands wire from internal/sealed: the fields the fold reads. The relay's idx and the header's already agree, the signature verified under the roster's key for Idx, the body key hashed to kh.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL