state

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 4 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 state is what the SDK keeps on disk (sdk-contract.md 5.5): the stored state, a JSON object every KeyGrant SDK reads and writes the same way, how a save lands on it (merge.go), and the clock guard (clock.go).

The state is kept as the JSON object it is, so the fields another SDK version wrote travel through a save untouched, and read through typed getters: a known field of the wrong type reads as absent (sdk-contract.md 5.5, "a typed port").

Index

Constants

View Source
const (
	Key             = "key"
	ActivationID    = "activationId"
	Lease           = "lease"
	TrialStart      = "trialStart"
	TrialEndsAt     = "trialEndsAt"
	TrialLease      = "trialLease"
	TrialAt         = "trialAt"
	LastSeen        = "lastSeen"
	CheckedMajor    = "checkedMajor"
	KeptMajor       = "keptMajor"
	BackoffSince    = "backoffSince"
	BackoffMs       = "backoffMs"
	Refused         = "refused"
	RefusedMajor    = "refusedMajor"
	RefusedFor      = "refusedFor"
	DeviceKind      = "deviceKind"
	VerdictAt       = "verdictAt"
	TrialReportedAt = "trialReportedAt"
	LicenseFile     = "licenseFile"
	Spends          = "spends"
	MeterFigures    = "meterFigures"
	Rev             = "rev"
)

The state's fields (sdk-contract.md 5.5). Times are milliseconds.

View Source
const ClockToleranceMs = 60 * 60_000

ClockToleranceMs is how far the clock guard may run ahead of the time the server signed a fresh lease before it is brought back.

Variables

View Source
var RetiredFields = []string{"purchaseToken", "purchaseTokenAt", "claimAskedAt"}

RetiredFields are what an earlier version stored to buy from the app with no key typed: no save keeps them.

Functions

func ClockCorrection

func ClockCorrection(clockNow float64, lastSeen *float64, claims *SignedAt, heldIat *float64, key, activationID string) *float64

ClockCorrection is where the clock guard is brought back to on a lease the server has just signed, or nil to leave it (clockCorrection): only a lease for this key (without case) and activation, no older than the one held, when the guard is MORE than ClockToleranceMs past its iat; to the device clock, never below iat.

func GuardedNow

func GuardedNow(clockNow float64, lastSeen *float64) float64

GuardedNow is now by the clock guard: never earlier than the latest time the device has seen (guardedNow).

func NumberOf

func NumberOf(v any) (float64, bool)

NumberOf is a value of the state as a number: as JSON read it, or as this process wrote it; false for anything else.

func Outdated

func Outdated(stored *float64, stamp Stamp) bool

Outdated is whether a stored stamp is a NEWER answer than stamp's: saved since that asker read the state (another stamp than the one it saw), and asked for later than it (outdated).

func SavedLastSeen

func SavedLastSeen(stored *float64, clockNow float64, guard *float64) float64

SavedLastSeen is the latest time seen a save writes: the guard brought back exactly as given when there is one, else never earlier than what is stored nor than the device clock (savedLastSeen).

func Superseded

func Superseded(latest State, asked Asked) string

Superseded is whether an ask's answer is out of date by the time it is saved: "activation" when the state names another activation, or none; "answer" when a newer answer about the same activation has been saved; "" otherwise.

Types

type About

type About struct {
	ActivationID *string
}

About names the activation a save that is no answer is about.

type Asked

type Asked struct {
	Stamp
	ActivationID *string
}

Asked is an ask of the server about an activation (nil: none).

type Basis

type Basis struct {
	Answer     *Asked
	Activation *Asked
	About      *About
	Trial      *Stamp
}

Basis is what a save is based on (merge.ts Basis): the answer to an ask, an activation's answer, a note about an activation, or a trial answer. The zero Basis is unbased.

type Patch

type Patch map[string]any

Patch is a change to a state: a field set to nil is cleared, a field not in the map is left as it is (the reference's Partial<StoredState>, undefined clearing).

func TrialOnly

func TrialOnly(p Patch) Patch

TrialOnly is only the trial evidence of a patch: what lands on another activation. A field the patch clears stays cleared.

func WithoutVerdict

func WithoutVerdict(p Patch) Patch

WithoutVerdict is a patch without the verdict fields: what of an out-of-date answer is still saved.

func (Patch) Clone

func (p Patch) Clone() Patch

Clone is a copy of the patch.

func (Patch) Merge

func (p Patch) Merge(q Patch) Patch

Merge is the patch with q's fields over its own ({...p, ...q}).

type SignedAt

type SignedAt struct {
	Sub, Aid string
	Iat      float64 // seconds
}

SignedAt is a fresh lease's claims as the clock guard reads them.

type Stamp

type Stamp struct {
	At   float64
	Seen *float64
}

Stamp is when something was asked of the server (guarded device clock, ms), and the stored stamp of that kind the asker saw (nil: none).

type State

type State map[string]any

State is a stored state: absent fields are not in the map.

func Merged

func Merged(latest State, patch Patch, basis Basis) State

Merged is a patch merged onto the state as stored now, by what it is based on (merge.ts merged): stamped (an answer or an activation with verdictAt, a trial answer with trialAt); a save about an activation no longer stored keeps only its trial evidence; an answer older than the stored one loses its verdict fields; a trial answer older than the stored one loses its end and lease; the trial start only moves earlier; the kept major of the stored activation only moves up; the retired fields are left out.

func (State) Clone

func (s State) Clone() State

Clone is a copy of the state's top level.

func (State) Filled

func (s State) Filled(field string) (string, bool)

Filled is a text field that JavaScript reads as true: present and not empty.

func (State) Num

func (s State) Num(field string) (float64, bool)

Num is a number field, or false when it is absent or not a number.

func (State) NumPtr

func (s State) NumPtr(field string) *float64

NumPtr is a number field, or nil.

func (State) Text

func (s State) Text(field string) (string, bool)

Text is a text field, or false when it is absent or not a text.

func (State) TextPtr

func (s State) TextPtr(field string) *string

TextPtr is a text field, or nil.

func (State) Texts

func (s State) Texts(field string) ([]string, bool)

Texts is a field that is a list of texts, or false.

func (State) With

func (s State) With(p Patch) State

With is the state with a patch applied, as the reference spreads one over a state ({...state, ...patch}): a field the patch sets is set, one it clears is gone.

Jump to

Keyboard shortcuts

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