/** * @name payments/prepaid-credit-store * @description In-memory + file-persisted prepaid credit store for x402 sessions. * * Allows agents to prepay a USDC balance once (via standard x402 challenge → * sign → settle) and then make multiple tool calls without per-call 402 * challenges. The server checks the prepaid balance in `onProtectedRequest` * and grants access by returning `{ grantAccess: true }` (standard x402 * Lifecycle Hooks extension) when sufficient balance exists. * * The store is backed by a JSON file at ~/.config/mcp-sap/payments/prepaid.json * so credits survive server restarts. An in-memory Map provides fast lookups. * * @module payments/prepaid-credit-store */ /** Prepaid credit entry for a wallet. */ export interface PrepaidCredit { /** Unique session ID used as the X-SAP-Prepaid-Session header value. */ sessionId: string; /** Wallet address that funded the session (payer). */ wallet: string; /** Total USDC deposited when the session was created. */ totalUsd: number; /** Remaining USDC balance. */ remainingUsd: number; /** Per-call cost deducted on each granted request. */ perCallCostUsd: number; /** ISO timestamp when the session was created. */ createdAt: string; /** ISO timestamp when the session expires. */ expiresAt: string; /** Number of calls made using this session. */ callCount: number; } /** Result of checking prepaid credit. */ export interface PrepaidCheckResult { /** True if the session has enough balance for a call. */ hasCredit: boolean; /** The session that was checked, if found. */ sessionId?: string; /** Remaining balance after this call (if hasCredit). */ remainingUsd?: number; /** Reason if no credit. */ reason?: string; } /** * Prepaid credit store with file persistence. * Uses a Map for fast lookups and a JSON file for durability. */ export declare class PrepaidCreditStore { private readonly credits; private readonly filePath; private dirty; constructor(configDir?: string); /** * Create a new prepaid session. Called after a successful x402 settlement * for the `sap_payments_fund_prepaid` hosted tool. * * @param wallet — Payer wallet address. * @param amountUsd — Total USDC deposited. * @param perCallCostUsd — Cost per call that will be deducted. * @param ttlHours — Session TTL in hours (default 24). * @returns The created PrepaidCredit entry. */ createSession(wallet: string, amountUsd: number, perCallCostUsd: number, ttlHours?: number): PrepaidCredit; /** * Check if a session has enough credit for a call and deduct it if so. * This is called by the monetization gate's `onProtectedRequest` hook. * * @param sessionId — Session ID from the X-SAP-Prepaid-Session header. * @param costUsd — Cost to deduct (defaults to session's perCallCostUsd). * @returns PrepaidCheckResult with hasCredit and remaining balance. */ checkAndDeduct(sessionId: string, costUsd?: number): PrepaidCheckResult; /** * Get the remaining balance for a session without deducting. * @param sessionId — Session ID. * @returns PrepaidCredit or null if not found. */ getBalance(sessionId: string): PrepaidCredit | null; /** * Get all active sessions for a wallet. * @param wallet — Wallet address. * @returns Array of PrepaidCredit entries. */ getSessionsByWallet(wallet: string): PrepaidCredit[]; /** * Manually close a session (refund unused balance is out of scope — * the balance simply becomes unavailable). * @param sessionId — Session ID to close. */ closeSession(sessionId: string): boolean; /** * Prune expired sessions. Called periodically. */ pruneExpired(): number; private load; private save; private startSyncTimer; } /** * Get the global PrepaidCreditStore singleton used by free payment tools. * Lazily creates the store on first access. */ export declare function getGlobalPrepaidStore(): PrepaidCreditStore; /** * Set the global PrepaidCreditStore singleton. Called by the server when the * monetization gate is initialized so the tools share the same store instance. */ export declare function setGlobalPrepaidStore(store: PrepaidCreditStore): void; //# sourceMappingURL=prepaid-credit-store.d.ts.map