|
| 1 | +import type { AsyncStorage } from "../utils/storage/AsyncStorage.js"; |
| 2 | +import type { RequestedPaymentPayload } from "./schemas.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Cached permit signature data structure |
| 6 | + */ |
| 7 | +type CachedPermitSignature = { |
| 8 | + payload: RequestedPaymentPayload; |
| 9 | + deadline: string; |
| 10 | + maxAmount: string; |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * Parameters for generating a permit cache key |
| 15 | + */ |
| 16 | +export type PermitCacheKeyParams = { |
| 17 | + chainId: number; |
| 18 | + asset: string; |
| 19 | + owner: string; |
| 20 | + spender: string; |
| 21 | +}; |
| 22 | + |
| 23 | +const CACHE_KEY_PREFIX = "x402:permit"; |
| 24 | + |
| 25 | +/** |
| 26 | + * Generates a cache key for permit signature storage |
| 27 | + * @param params - The parameters to generate the cache key from |
| 28 | + * @returns The cache key string |
| 29 | + */ |
| 30 | +function getPermitCacheKey(params: PermitCacheKeyParams): string { |
| 31 | + return `${CACHE_KEY_PREFIX}:${params.chainId}:${params.asset.toLowerCase()}:${params.owner.toLowerCase()}:${params.spender.toLowerCase()}`; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Retrieves a cached permit signature from storage |
| 36 | + * @param storage - The AsyncStorage instance to use |
| 37 | + * @param params - The parameters identifying the cached signature |
| 38 | + * @returns The cached signature data or null if not found |
| 39 | + */ |
| 40 | +export async function getPermitSignatureFromCache( |
| 41 | + storage: AsyncStorage, |
| 42 | + params: PermitCacheKeyParams, |
| 43 | +): Promise<CachedPermitSignature | null> { |
| 44 | + try { |
| 45 | + const key = getPermitCacheKey(params); |
| 46 | + const cached = await storage.getItem(key); |
| 47 | + if (!cached) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + return JSON.parse(cached) as CachedPermitSignature; |
| 51 | + } catch { |
| 52 | + return null; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Saves a permit signature to storage cache |
| 58 | + * @param storage - The AsyncStorage instance to use |
| 59 | + * @param params - The parameters identifying the signature |
| 60 | + * @param payload - The signed payment payload to cache |
| 61 | + * @param deadline - The deadline timestamp of the permit |
| 62 | + * @param maxAmount - The maximum amount authorized |
| 63 | + */ |
| 64 | +export async function savePermitSignatureToCache( |
| 65 | + storage: AsyncStorage, |
| 66 | + params: PermitCacheKeyParams, |
| 67 | + payload: RequestedPaymentPayload, |
| 68 | + deadline: string, |
| 69 | + maxAmount: string, |
| 70 | +): Promise<void> { |
| 71 | + try { |
| 72 | + const key = getPermitCacheKey(params); |
| 73 | + const data: CachedPermitSignature = { |
| 74 | + payload, |
| 75 | + deadline, |
| 76 | + maxAmount, |
| 77 | + }; |
| 78 | + await storage.setItem(key, JSON.stringify(data)); |
| 79 | + } catch { |
| 80 | + // Silently fail - caching is optional |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Clears a cached permit signature from storage |
| 86 | + * @param storage - The AsyncStorage instance to use |
| 87 | + * @param params - The parameters identifying the cached signature |
| 88 | + */ |
| 89 | +export async function clearPermitSignatureFromCache( |
| 90 | + storage: AsyncStorage, |
| 91 | + params: PermitCacheKeyParams, |
| 92 | +): Promise<void> { |
| 93 | + try { |
| 94 | + const key = getPermitCacheKey(params); |
| 95 | + await storage.removeItem(key); |
| 96 | + } catch { |
| 97 | + // Silently fail |
| 98 | + } |
| 99 | +} |
0 commit comments