RolebookDocs

Offline check snapshots

@rolebook/offline: signed exact-check snapshots with explicit trust, freshness and rollback boundaries.

@rolebook/offline issues and verifies rolebook/offline-snapshot/v1 bundles. Each bundle carries the complete Core policy and inputs for one exact check. Verification authenticates the bytes, validates the current root-authorized trust lease and evaluates the carried inputs through Core. Callers cannot replace or supplement signed facts.

Use it for a device, a kiosk or an edge process that must answer a prepared check without reaching your server. Choose how decisions run places it beside the embedded engine and the decision service, and the security model lists what verification guarantees and what your host establishes.

Not on npm yet

rolebook and @rolebook/offline are not on npm yet. The npm package named rolebook is an unrelated project, so do not install it.

A snapshot is not a precomputed grant, delegated capability or one-time authorization. It is reusable subject to its freshness and rollback checks. Protecting a side effect requires a separate write/idempotency protocol.

Verify a snapshot

createOfflineVerifier(config) returns { verifySnapshot, installTrustLease }. The root record, the clock, the state store and Ed25519 verification are host adapters that you supply; the package contains none of them.

verify.ts
import {
  createOfflineVerifier,
  type OfflineRoot,
  type OfflineVerifyCrypto,
  type QualifiedClock,
  type TrustedStateStore,
} from "@rolebook/offline";

// Host adapters supplied by your deployment. The package ships no implementation of any of them.
declare const root: OfflineRoot; // provisioned out of band: root id, key and its one namespace
declare const clock: QualifiedClock; // returns undefined when it cannot vouch for the time
declare const state: TrustedStateStore; // durable trust lease and anti-rollback state
declare const ed25519: OfflineVerifyCrypto; // strict Ed25519 verification
declare const bundleBytes: Uint8Array; // the compact JWS exactly as received

const verifier = createOfflineVerifier({
  roots: [root],
  clock,
  state,
  crypto: ed25519,
  qualification: "ui_only",
});

const result = await verifier.verifySnapshot(bundleBytes, {
  mode: "ui_hint",
  rootId: root.rootId,
  expectedIssuer: root.issuer,
  expectedAudience: root.audience,
  expectedScope: root.authorityScope,
});

// A hint for the interface. A no-decision is never a deny and never an allow.
export const showEditControl = result.kind === "decision" && result.allowed;
if (result.kind === "no_decision") console.warn(`offline check unavailable: ${result.reason}`);

The verifier needs an installed trust lease in the state store before it can accept a bundle. The store is provisioned out of band; after that, pass each newer root-signed lease to installTrustLease(bytes), which answers installed, unchanged or a no-decision.

To enforce with a result, construct the verifier with qualification: "enforcement_qualified" and the hostQualificationId of your reviewed host record, and ask for mode: "enforcement". See UI hints and enforcement.

createOfflineIssuer(config) returns { issue }. An issuer signs the P1 bytes of one OfflineCoreSnapshotV1 with a key the installed lease authorizes, after it reserves a sequence from the durable allocator you supply and evaluates the snapshot with Core.

Bundles, leases and rollback state

Bundles and trust leases use compact JWS with Ed25519. Signatures cover the exact received bytes; parsing and reserializing a payload does not reproduce its signed message. All JSON boundaries use the shared rolebook/scalars-p1/v1 profile. Policy and provider-manifest digests additionally bind the interpreted contents to the installed lease.

Root-authorized leases bind the issuer, scope, audience, signing keys, authority epoch and permitted policy identities. Installing a newer lease atomically retains its exact bytes and rollback floor. The verifier refuses an older lease that could restore a retired key. Bundle rollback state is separate for each exact check, so a newer check for one resource does not invalidate an unrelated check.

The issuer’s durable allocator also orders authority observations. An older fact observation cannot acquire a newer sequence after a more recent observation has been reserved. Equal observation times with different snapshot contents are refused.

UI hints and enforcement

A ui_only verifier can return only an explicitly labelled UI hint. Asking it for enforcement returns host_not_qualified. UI hints still require signature, time, freshness and available-state checks; invalid or missing trust does not become a hint that allows access.

Enforcement requires a constructor-bound qualification record and reviewed host adapters for trustworthy time, rollback-resistant state, root-key operations and complete authority inputs. There is no default qualified clock, state store or issuer allocator. Ordinary browser storage, a restorable file or an independently mutable wall clock does not establish those guarantees.

Disconnected revocation is bounded, not immediate. A verifier stops accepting a signer when it installs a newer root-signed lease that no longer authorizes that signer’s key. Until then the bound is the key and lease validity (at most 7 days), the bundle expiry (at most 24 hours) and the fact age the lease allows. Lost trust state fails closed until authenticated recovery; it cannot be reset from an unsigned bundle.

Limits and runtime

LimitValue
Compact bundle, and its decoded payload1,400,000 bytes and 1,048,576 bytes
Compact lease, and its decoded payload96,000 bytes and 65,536 bytes
Bundle lifetimeAt most 24 hours
Lease and signing-key lifetimeAt most 7 days
Clock uncertaintyAt most 120,000 ms
Signing keys and allowed policies in one lease32 and 256

The package imports node:crypto for SHA-256 and runs on Node.js 24 or later and on Bun. A browser is not a supported runtime. Ed25519 comes from the adapter you inject. Distribution of bundles and leases to devices, and the qualification of each host, are your deployment’s work.

API reference

createOfflineIssuer

function createOfflineIssuer(config: IssuerConfig): OfflineIssuer;

AntiRollbackStateV1

interface AntiRollbackStateV1 {
  readonly highestEpoch: number;
  readonly highestSequence: number;
  readonly bundleId: string;
  readonly payloadSha256: string;
  readonly leaseId: string;
  readonly leaseRevision: number;
  readonly updatedAtMs: number;
}

ClockReading

type ClockReading = Readonly<{
  nowMs: number;
  uncertaintyMs: number;
}>;

InstallResult

type InstallResult = {
  readonly kind: "installed" | "unchanged";
} | {
  readonly kind: "no_decision";
  readonly reason: OfflineFailure;
};

IssueResult

type IssueResult = {
  readonly kind: "issued";
  readonly bytes: Uint8Array;
} | {
  readonly kind: "no_decision";
  readonly reason: OfflineFailure;
};

IssuerConfig

interface IssuerConfig {
  readonly root: OfflineRoot;
  readonly clock: QualifiedClock;
  readonly state: Pick<TrustedStateStore, "read">;
  readonly crypto: OfflineIssuerCrypto;
  readonly randomBytes: (length: number) => Uint8Array;
  readonly signingKid: string;
  readonly modelId: string;
  readonly policyGeneration: string;
  readonly sequenceAllocator: SequenceAllocator;
}

IssuerMetadata

type IssuerMetadata = Readonly<{
  issuer: string;
  authorityScope: string;
  audience: string;
  issuedAtMs: number;
  notBeforeMs: number;
  expiresAtMs: number;
  observedAtMs: number;
  maxFactAgeMs: number;
}>;

LeaseFloor

type LeaseFloor = Readonly<{
  leaseRevision: number;
  leasePayloadSha256: string;
  leaseId: string;
  authorityEpoch: number;
}>;

LeaseSlot

type LeaseSlot = Omit<Slot, "snapshotKey">;

OfflineCoreSnapshotV1

type OfflineCoreSnapshotV1 = Readonly<{
  policy: RolebookPolicy;
  binding: Pick<AuthorizationRequest, "subject" | "principals" | "assignments" | "context">;
  resource: AuthorizationRequest["resource"];
  request: Pick<AuthorizationRequest, "permission" | "field">;
}>;

OfflineFailure

type OfflineFailure = "malformed_bundle" | "unsupported_profile" | "trust_unavailable" | "signature_invalid" | "audience_mismatch" | "scope_mismatch" | "not_yet_valid" | "expired" | "clock_unqualified" | "rollback_detected" | "issuer_equivocation" | "state_unavailable" | "trust_changed" | "host_not_qualified" | "incomplete_snapshot" | "policy_rejected" | "policy_digest_mismatch" | "internal_fault";

OfflineIssuer

interface OfflineIssuer {
  issue(snapshotP1Json: Uint8Array, metadata: IssuerMetadata): Promise<IssueResult>;
}

OfflineIssuerCrypto

interface OfflineIssuerCrypto extends OfflineVerifyCrypto {
  sign(message: Uint8Array): Promise<Uint8Array>;
}

OfflineResult

type OfflineResult = {
  readonly kind: "decision";
  readonly mode: VerifyMode;
  readonly allowed: boolean;
  readonly code: "ALLOWED" | "EXPLICIT_DENY" | "NOT_GRANTED";
  readonly policyDigest: string;
  readonly bundleId: string;
} | {
  readonly kind: "no_decision";
  readonly mode: VerifyMode;
  readonly reason: OfflineFailure;
  readonly bundleId?: string;
};

OfflineRoot

type OfflineRoot = Readonly<{
  rootId: string;
  kid: string;
  publicKey: Uint8Array;
  issuer: string;
  authorityScope: string;
  audience: string;
}>;

OfflineSnapshotV1

interface OfflineSnapshotV1 {
  readonly kind: "rolebook/offline-snapshot/v1";
  readonly bundleId: string;
  readonly issuer: string;
  readonly authorityScope: string;
  readonly audience: string;
  readonly issuedAtMs: number;
  readonly notBeforeMs: number;
  readonly expiresAtMs: number;
  readonly authorityEpoch: number;
  readonly snapshotKey: string;
  readonly sequence: number;
  readonly semanticProfile: "rolebook/core-v4";
  readonly scalarProfile: "rolebook/scalars-p1/v1";
  readonly modelId: string;
  readonly policyGeneration: string;
  readonly policyDigest: string;
  readonly observedAtMs: number;
  readonly maxFactAgeMs: number;
  readonly providerManifestId: string;
  readonly providerManifestDigest: string;
  readonly snapshot: OfflineCoreSnapshotV1;
}

OfflineTrustLeaseV1

interface OfflineTrustLeaseV1 {
  readonly kind: "rolebook/offline-trust-lease/v1";
  readonly leaseId: string;
  readonly issuer: string;
  readonly authorityScope: string;
  readonly audience: string;
  readonly issuedAtMs: number;
  readonly notBeforeMs: number;
  readonly expiresAtMs: number;
  readonly leaseRevision: number;
  readonly authorityEpoch: number;
  readonly providerManifest: ProviderManifestV1;
  readonly allowedPolicies: readonly {
    readonly modelId: string;
    readonly policyGeneration: string;
    readonly policyDigest: string;
  }[];
  readonly keys: readonly {
    readonly kid: string;
    readonly publicKey: string;
    readonly notBeforeMs: number;
    readonly expiresAtMs: number;
  }[];
}

OfflineVerifier

interface OfflineVerifier {
  verifySnapshot(compactJws: Uint8Array, options: VerifyOptions): Promise<OfflineResult>;
  installTrustLease(compactJws: Uint8Array): Promise<InstallResult>;
}

OfflineVerifyCrypto

interface OfflineVerifyCrypto {
  verify(key32: Uint8Array, message: Uint8Array, signature64: Uint8Array): Promise<boolean>;
}

ProviderManifestClass

type ProviderManifestClass = "subject" | "principals" | "assignments" | "resource" | "ancestors" | "resource_attributes" | "context";

ProviderManifestV1

interface ProviderManifestV1 {
  readonly id: string;
  readonly maxFactAgeMs: number;
  readonly required: readonly {
    readonly class: ProviderManifestClass;
    readonly paths: readonly string[];
  }[];
  readonly optional: readonly {
    readonly class: ProviderManifestClass;
    readonly paths: readonly string[];
  }[];
}

QualifiedClock

interface QualifiedClock {
  now(): ClockReading | undefined;
}

SequenceAllocator

interface SequenceAllocator {
  reserve(slot: Slot, expectedLease: LeaseFloor, observedAtMs: number, snapshotContentSha256: string): Promise<SequenceReservation>;
}

SequenceReservation

type SequenceReservation = {
  readonly kind: "reserved";
  readonly authorityEpoch: number;
  readonly sequence: number;
} | {
  readonly kind: "no_decision";
  readonly reason: "trust_changed" | "state_unavailable" | "incomplete_snapshot";
};

Slot

type Slot = Readonly<{
  issuer: string;
  authorityScope: string;
  audience: string;
  snapshotKey: string;
}>;

TrustedState

type TrustedState = Readonly<{
  leaseJws: Uint8Array;
  leaseDigest: string;
  floor: LeaseFloor;
  slot: AntiRollbackStateV1 | undefined;
}>;

TrustedStateStore

interface TrustedStateStore {
  read(slot: LeaseSlot, snapshotKey?: string): Promise<TrustedState | "missing_bootstrap" | "state_lost">;
  finalize(slot: Slot, expectedLease: LeaseFloor, expectedSlot: AntiRollbackStateV1 | undefined, nextSlot: AntiRollbackStateV1): Promise<"applied" | "changed" | "conflict" | "capacity_exhausted" | "state_lost">;
  installLease(slot: LeaseSlot, next: LeaseFloor, compactRootJws: Uint8Array): Promise<"installed" | "unchanged" | "rollback_detected" | "issuer_equivocation" | "state_lost">;
}

VerifyConfig

type VerifyConfig = (VerifyConfigBase & {
  readonly qualification: "ui_only";
}) | (VerifyConfigBase & {
  readonly qualification: "enforcement_qualified";
  readonly hostQualificationId: string;
});

VerifyMode

type VerifyMode = "ui_hint" | "enforcement";

VerifyOptions

interface VerifyOptions {
  readonly mode: VerifyMode;
  readonly rootId: string;
  readonly expectedIssuer: string;
  readonly expectedAudience: string;
  readonly expectedScope: string;
}

createOfflineVerifier

function createOfflineVerifier(config: VerifyConfig): OfflineVerifier;