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 aVerifiedGrant 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 aConsentBundle 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
ThrowsScopeViolationError if any of requiredScopes is missing from grantScopes.
hasScope
Returnsboolean — 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 ofSignedAuditEntry objects. Checks:
- Each entry’s
hashmatches the recomputed value - Each entry’s
prevHashmatches the previous entry’shash(first entry must useGENESIS_HASH) - Sequence numbers are consecutive
{ 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 ADKFunctionTool 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 LangChainStructuredTool 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.