Security Guide

1. Token Validation

ALWAYS validate token transfers server-side:

// WRONG - Client-side only
async function buyItem(price: number) {
    await sdk.triggerAction("transferTokens", {
        recipient: gameWalletAddress,
        amount: price
    });
    // DON'T update game state here!
}

// RIGHT - Server-side validation
app.post('/validate', async (req, res) => {
    const { sender, recipient, amount, validatedTransfer } = req.body;
    
    // 1. Check Games.fun validation
    if (!validatedTransfer) {
        return res.json({ error: 'Transfer not validated' });
    }
    
    // 2. Verify recipient is your game wallet
    if (recipient !== gameWalletAddress) {
        return res.json({ error: 'Invalid recipient' });
    }
    
    // 3. Verify token mint
    if (validatedTransfer.token !== process.env.GAME_TOKEN_MINT) {
        return res.json({ error: 'Invalid token' });
    }
    
    // 4. Only NOW update game state
    await updatePlayerBalance(sender, amount * 10);
});

2. JWT Authentication

NEVER skip JWT verification:

Key Points

  1. Token Security

  • Games.fun validates transfers on-chain

  • Your server MUST verify the validation

  • Only update state after validation

  • Check token mint address

  1. Authentication Security

  • Always verify JWT tokens

  • Check token expiration

  • Verify player matches token

  • Keep tokens in memory only

  1. Common Mistakes

  • Updating state before validation

  • Skipping JWT verification

  • Not checking token mint

  • Storing JWT in localStorage

  1. Best Practices

  • Use environment variables

  • Log security events

  • Rate limit requests

  • Keep dependencies updated