Skip to main content

Overview

@grantex/gemma (TypeScript) and grantex-gemma (Python) provide offline authorization for Gemma 4 on-device agents. The SDK handles consent bundle creation, JWT verification without network calls, scope enforcement, tamper-evident audit logging, and cloud sync.

Installation


createConsentBundle

Create a consent bundle from the Grantex API. This is the only call that requires network connectivity.

Parameters

Returns

Promise<ConsentBundle> — see ConsentBundle type below.

Example

Errors


createOfflineVerifier

Create an offline JWT verifier using a pre-fetched JWKS snapshot. No network calls are made during verification.

Parameters

Returns

OfflineVerifier — an object with a single verify(token: string) method.

OfflineVerifier.verify(token)

Verifies a Grantex grant token offline. Returns a VerifiedGrant on success. VerifiedGrant fields:

Example

Errors


createOfflineAuditLog

Create an append-only, Ed25519-signed, SHA-256 hash-chained audit log backed by a JSONL file.

Parameters

Returns

OfflineAuditLog with methods:

AuditEntry fields

Example


storeBundle / loadBundle

Encrypt a ConsentBundle to disk with AES-256-GCM, or decrypt it back.

storeBundle

Returns Promise<void>.

loadBundle

Returns Promise<ConsentBundle>. Throws BundleTamperedError if decryption or integrity check fails.

File format

Example


refreshBundle / shouldRefresh

Manage consent bundle lifecycle. shouldRefresh returns true when the bundle has less than 20% of its TTL remaining. refreshBundle calls the Grantex API to get a fresh bundle.

shouldRefresh

Returns boolean.

refreshBundle

Returns Promise<ConsentBundle> — a fresh bundle with extended offlineExpiresAt, new JWKS snapshot, and rotated audit keys.

Example


enforceScopes / hasScope

Utility functions for scope checking.

enforceScopes

Throws ScopeViolationError if any of requiredScopes is missing from grantScopes.

hasScope

Returns boolean — whether a specific scope is present.

computeEntryHash / verifyChain

Audit log integrity utilities.

computeEntryHash

Compute the SHA-256 hash of an audit entry. Hash input format:

verifyChain

Verify the integrity of an ordered sequence of SignedAuditEntry objects. Checks:
  1. Each entry’s hash matches the recomputed value
  2. Each entry’s prevHash matches the previous entry’s hash (first entry must use GENESIS_HASH)
  3. Sequence numbers are consecutive
Returns { valid: true } or { valid: false, brokenAt: number }.

Example


syncAuditLog

Sync un-synced audit log entries to the Grantex cloud in batches with exponential back-off retry.

Parameters (SyncOptions)

Returns

SyncResult:

withGrantexAuth (Google ADK)

Wrap a Google ADK FunctionTool with offline Grantex authorization. Before the tool executes, the grant token is verified and scopes are enforced. After execution, an audit entry is logged.

Parameters (GoogleADKAuthOptions)


withGrantexAuth (LangChain)

Wrap a LangChain StructuredTool with offline Grantex authorization. Same behavior as the ADK adapter but wraps _call or invoke methods.

Parameters (LangChainAuthOptions)


ConsentBundle Type


Error Classes

All error classes extend GrantexAuthError, which extends Error and includes a code property.
Last modified on April 3, 2026