Vana SDK - v2.2.2
    Preparing search index...

    Class SignatureCache

    Simple signature cache using platform cache adapter to avoid repeated signing of identical messages.

    This cache significantly improves UX by avoiding repeated wallet signature prompts for identical operations. It's particularly useful for operations that may be retried or called multiple times with the same parameters.

    Features:

    • Platform-appropriate storage (sessionStorage in browser, memory in Node.js)
    • Configurable TTL (default 2 hours)
    • Automatic cleanup of expired entries
    • Cache keys based on wallet address + message hash
    // Check cache before requesting signature
    const cached = SignatureCache.get(cache, walletAddress, messageHash);
    if (cached) {
    return cached;
    }

    // Request signature and cache it
    const signature = await wallet.signTypedData(typedData);
    SignatureCache.set(cache, walletAddress, messageHash, signature);
    Index

    Methods

    • Get a cached signature if it exists and hasn't expired

      Parameters

      • cache: VanaCacheAdapter

        Platform cache adapter instance

      • walletAddress: string

        Wallet address that created the signature

      • messageHash: string

        Hash of the message that was signed

      Returns `0x${string}` | null

      The cached signature if valid, null if expired or not found

      const messageHash = SignatureCache.hashMessage(typedData);
      const cached = SignatureCache.get(cache, '0x123...', messageHash);
      if (cached) {
      console.log('Using cached signature:', cached);
      }
    • Store a signature in the cache with configurable TTL

      Parameters

      • cache: VanaCacheAdapter

        Platform cache adapter instance

      • walletAddress: string

        Wallet address that created the signature

      • messageHash: string

        Hash of the message that was signed

      • signature: `0x${string}`

        The signature to cache

      • ttlHours: number = ...

        Time to live in hours (default: 2)

      Returns void

      const signature = await wallet.signTypedData(typedData);
      const messageHash = SignatureCache.hashMessage(typedData);

      // Cache for default 2 hours
      SignatureCache.set(cache, walletAddress, messageHash, signature);

      // Cache for 24 hours
      SignatureCache.set(cache, walletAddress, messageHash, signature, 24);
    • Clear all cached signatures (useful for testing or explicit cleanup)

      Parameters

      • cache: VanaCacheAdapter

        Platform cache adapter instance

      Returns void

      // Clear all signatures when user logs out
      SignatureCache.clear(cache);

      // Clear before running tests
      beforeEach(() => {
      SignatureCache.clear(cache);
      });
    • Generate a deterministic hash of a message object for cache key generation

      Parameters

      • message: object

        The message object to hash (typically EIP-712 typed data)

      Returns string

      A hex string hash (SHA-256) suitable for cache keys

      Creates a cryptographically secure hash from complex objects including EIP-712 typed data. Uses SHA-256 for collision resistance and deterministic key generation. Handles BigInt serialization and sorts object keys for consistency.

      const typedData = {
      domain: { name: 'Vana', version: '1' },
      message: { nonce: 123n, grant: '...' }
      };

      const hash = SignatureCache.hashMessage(typedData);
      // Returns SHA-256 hash like: "a1b2c3d4e5f6..."