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
- Variables
- func GenerateSealKey() (*ecdh.PrivateKey, error)
- func Key(id string) string
- func LinkSealKey(linkKey []byte, room string) (*ecdh.PrivateKey, error)
- func MembershipKind(kind string) bool
- func SealedSize(n int) int
- func ValidID(id string) bool
- type Add
- type Card
- type Channels
- type Entry
- type HandoverBody
- type Link
- type Member
- type OwnKeys
- type PartyCard
- type Result
- type RoomCardBody
- type RosterBody
- type State
- func (s *State) Add() Add
- func (s *State) Admins() []uint16
- func (s *State) CardCurrent() bool
- func (s *State) CheckCounter(idx uint16, ctr uint64) bool
- func (s *State) Commit(ctx context.Context) error
- func (s *State) Counter(idx uint16) (uint64, bool)
- func (s *State) Cursor() int64
- func (s *State) Drop(ctx context.Context) error
- func (s *State) Dropped() int64
- func (s *State) Feed() bool
- func (s *State) Fold(rec Verified, ch Channels) Result
- func (s *State) ID() string
- func (s *State) Idx() uint16
- func (s *State) IsAdmin(idx uint16) bool
- func (s *State) LastRotated() time.Time
- func (s *State) Link() *Link
- func (s *State) MarkSeen(id string, seq int64)
- func (s *State) Member(idx uint16) *Member
- func (s *State) Members() []*Member
- func (s *State) Membership() int64
- func (s *State) NextCounter() uint64
- func (s *State) NoteDropped(n int)
- func (s *State) NoteReported()
- func (s *State) ObserveMembership(v int64)
- func (s *State) OwnCard(issued time.Time) *Card
- func (s *State) Posted(seq int64)
- func (s *State) Removed(idx uint16) bool
- func (s *State) Reported() int64
- func (s *State) Retire(now time.Time) int
- func (s *State) RetiredSealKeys() []*ecdh.PrivateKey
- func (s *State) Rotate(k *ecdh.PrivateKey, now time.Time)
- func (s *State) RotationDue(now time.Time) bool
- func (s *State) RotationPending() bool
- func (s *State) SealKey() *ecdh.PrivateKey
- func (s *State) Seed(entries []Entry, admins []uint16, ch Channels, at time.Time) Result
- func (s *State) Seen(id string) bool
- func (s *State) SetCursor(seq int64)
- func (s *State) SetLink(l Link) error
- func (s *State) SignKey() ed25519.PrivateKey
- func (s *State) Size() (int, error)
- func (s *State) Version() int64
- type Verified
Constants ¶
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.
const CardVersion = 1
CardVersion is the room card format version.
const LinkKeySize = 32
LinkKeySize is a feed link key's length in bytes.
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.
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.
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).
const RotateEvery = 24 * time.Hour
RotateEvery is how old a member's folded room card may be before serve rotates the seal key.
const SigSize = 64
SigSize is the length of the identity-key signature a room card carries.
Variables ¶
var ( ErrSignature = errors.New("room card: identity-key signature does not verify") ErrMalformed = errors.New("room card: malformed") )
Errors a reader can match.
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 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 ¶
MembershipKind reports whether kind is one the fold reads.
func SealedSize ¶
SealedSize is the envelope size of a record of n plaintext bytes on the fleet store.
Types ¶
type Add ¶
type Add string
Add is the room's add setting, fixed at creation (rule 5): who may post a roster add.
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 ¶
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.
type Channels ¶
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 ¶
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.
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 ¶
GenerateKeys makes a fresh signing key and seal key.
type PartyCard ¶
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.
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 New ¶
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) Admins ¶
Admins is the admin set: idx 0 plus folded handovers, less whoever left or was removed.
func (*State) CardCurrent ¶
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 ¶
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 ¶
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 ¶
Counter is the highest ctr seen from a writer, false before its first record.
func (*State) Drop ¶
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 ¶
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) Fold ¶
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) LastRotated ¶
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 (*State) MarkSeen ¶
MarkSeen records a message id with the relay seq it arrived under; the ring keeps the last MaxSeen.
func (*State) Membership ¶
Membership is the relay's membership version as last observed — what a put carries.
func (*State) NextCounter ¶
NextCounter is the ctr the member's next record carries: its own count of records put in this room, +1 each.
func (*State) NoteDropped ¶
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 ¶
ObserveMembership records the membership version a relay answer carried; the version only rises.
func (*State) OwnCard ¶
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 ¶
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 ¶
Removed reports whether idx was removed or left and no admin has re-added it.
func (*State) Reported ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) SetLink ¶
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.
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.