Overview
This guide provides security best practices for developers integrating with the Polymarket CTF Exchange and operators running exchange infrastructure. Following these guidelines will help ensure the security of your integration and protect your users.For Integrators
Signature Validation
The CTF Exchange supports multiple signature types. Ensure you’re using the correct type for your use case.EOA Signatures (SignatureType.EOA)
EOA Signatures (SignatureType.EOA)
Standard ECDSA signatures for externally owned accounts.Requirements (
Signatures.sol:67-73):- Signer and maker must be the same address
- Valid ECDSA signature using EIP-191 or EIP-712
- Users are signing with standard Ethereum wallets (MetaMask, etc.)
- No proxy or smart contract wallet is involved
Proxy Wallet Signatures (SignatureType.POLY_PROXY)
Proxy Wallet Signatures (SignatureType.POLY_PROXY)
For Polymarket proxy wallets.Requirements (
Signatures.sol:96-102):- Valid ECDSA signature from the wallet owner
- Maker address must match the deterministic proxy address
- Proxy address is computed from signer, factory, and implementation
- Users are trading through Polymarket proxy wallets
- You need deterministic wallet addresses
Gnosis Safe Signatures (SignatureType.POLY_GNOSIS_SAFE)
Gnosis Safe Signatures (SignatureType.POLY_GNOSIS_SAFE)
For multisig wallets using Gnosis Safe.Requirements (
Signatures.sol:112-118):- Valid ECDSA signature from a Safe owner
- Maker address must match the deterministic Safe address
- Implementing institutional trading with multisig
- Users require multiple signers for security
Smart Contract Signatures (SignatureType.POLY_1271)
Smart Contract Signatures (SignatureType.POLY_1271)
For smart contracts implementing EIP-1271.Requirements (
Signatures.sol:125-132):- Signer and maker must be the same address
- Contract must have code deployed
- Must implement EIP-1271
isValidSignatureinterface
- Integrating with smart contract wallets
- Building automated trading strategies
- Implementing custom signature logic
isValidSignature function cannot be exploited.Order Construction
Essential Validations
-
Token Validation
-
Nonce Validation
-
Amount Validation
-
Side Validation
Nonce Management
The exchange uses nonces to prevent replay attacks and allow order cancellation.Each address has a single nonce value. Incrementing it invalidates all outstanding orders from that address.
Emergency Order Cancellation
NonceManager.sol:9
Use cases:
- User wants to cancel all orders at once
- Suspected account compromise
- Market conditions change drastically
Error Handling
Implement comprehensive error handling for all exchange interactions.Reentrancy Protection
All state-changing functions in the exchange are protected by OpenZeppelin’s ReentrancyGuard.
Gas Optimization
Batch Operations
Use batch functions when filling multiple orders:- Lower gas costs per order
- Atomic execution (all or nothing)
- Better UX for users
Frontend Security
Display Order Details Clearly
Display Order Details Clearly
Always show users exactly what they’re signing:
Validate User Input
Validate User Input
Sanitize and validate all user inputs:
Protect Against Phishing
Protect Against Phishing
- Verify contract addresses match expected values
- Display contract addresses to users
- Implement address whitelisting for production
- Use ENS names where appropriate
Monitor for Suspicious Activity
Monitor for Suspicious Activity
Track and alert on unusual patterns:
For Operators
Access Control
Operators can execute trades on behalf of users. Secure your operator keys carefully.Key Management
-
Use Hardware Security Modules (HSMs)
- Store operator keys in HSMs or secure enclaves
- Never store keys in plain text
- Rotate keys periodically
-
Implement Rate Limiting
-
Monitor Operator Activity
- Log all operator transactions
- Alert on unusual patterns
- Implement automated circuit breakers
-
Use Multiple Operators
- Distribute load across multiple operator addresses
- Limit exposure if one key is compromised
- Implement operator rotation
Order Validation
Operators should validate orders before submission:Monitoring and Alerting
Implement comprehensive monitoring for exchange operations:Monitor Pause Events
Monitor Pause Events
Track Role Changes
Track Role Changes
Monitor Factory Updates
Monitor Factory Updates
Track Transaction Failures
Track Transaction Failures
Infrastructure Security
Network Security
- Use private RPC endpoints
- Implement request authentication
- Rate limit RPC calls
- Monitor for unusual network activity
Database Security
- Encrypt sensitive data at rest
- Use parameterized queries
- Implement access controls
- Regular security audits
API Security
- Require authentication for all endpoints
- Implement request signing
- Use HTTPS exclusively
- Rate limit all endpoints
Deployment Security
- Use infrastructure as code
- Implement least privilege access
- Regular security patches
- Audit logging for all access
Emergency Procedures
For Admins
In case of a security incident:Assess the Situation
- Identify the nature of the incident
- Determine scope of impact
- Review recent transactions
- Check if any operators are compromised
Implement Mitigation
- Deploy fixes if necessary
- Update configurations
- Rotate keys
- Notify affected users
For Users
If you suspect your account is compromised:Security Checklist
Integration Checklist
Pre-Launch Security Review
Pre-Launch Security Review
- All order parameters validated before signing
- Signature types correctly implemented
- Error handling for all exchange interactions
- User inputs sanitized and validated
- Contract addresses verified against expected values
- Reentrancy protection in custom contracts
- Gas estimation implemented
- Comprehensive logging and monitoring
- Security audit completed (for production)
- Incident response plan documented
Operator Checklist
Operational Security Review
Operational Security Review
- Operator keys stored in HSM or secure enclave
- Rate limiting implemented
- Order validation before submission
- Monitoring for all exchange events
- Automated alerting configured
- Circuit breakers implemented
- Key rotation procedures documented
- Multiple operators for redundancy
- Regular security audits scheduled
- Incident response procedures tested
Additional Resources
Security Audit
Review the ChainSecurity audit report
Admin Controls
Learn about administrative functions
Order Structure
Understand how orders work
Signature Types
Deep dive into signature validation
Security is an ongoing process. Stay informed about updates to the protocol and regularly review your security practices.