Automatic Reward Distribution: Smart contract-based system for bug bounty rewards
Implements an automatic reward distribution system using Ethereum smart contracts that automatically distributes bug bounty rewards to successful bug hunters in their chosen cryptocurrency upon approval of their submission.
Implementation Overview
This PR adds a complete blockchain-based reward distribution system that:
- Automatically sends cryptocurrency rewards when bugs are approved
- Uses Ethereum smart contracts for transparent, immutable transactions
- Supports user cryptocurrency preferences (ETH, BTC, BCH - currently ETH automated)
- Falls back to manual distribution if automatic fails
- Includes comprehensive security measures and testing
Changes Made
1. Smart Contract (contracts/BugBountyReward.sol)
- Solidity smart contract for managing bug bounty rewards on Ethereum
- Features: hunt creation, fund management, automatic reward distribution
- Security: access control, reentrancy protection, double-spend prevention
- Event logging for transparency and on-chain verification
2. Reward Distribution Service (website/services/reward_distribution.py)
- Python service layer using web3.py for blockchain interaction
- Handles transaction signing, submission, and verification
- Configurable settings (ETH price, transaction timeouts)
- Comprehensive error handling with graceful fallback
3. Database Enhancements
-
Issue Model: Added
blockchain_tx_hashandreward_distributed_atfields to track distributions -
UserProfile Model: Added
preferred_cryptocurrencyfield for user preferences - Migration file:
0253_add_crypto_preference_fields.py
4. Workflow Integration (website/views/company.py)
- Modified
accept_bugfunction to trigger automatic distribution - Checks user crypto preferences and wallet addresses
- Provides clear success/error feedback to users
- Falls back to manual distribution on errors
5. Deployment Tools
- Management command:
python manage.py deploy_reward_contractfor deploying to testnet/mainnet - Configuration validation and deployment feedback
- Safety checks for mainnet deployment
6. Testing (website/tests/test_reward_distribution.py)
- Comprehensive unit tests for all components
- Mock-based blockchain interaction testing
- Edge case and error scenario coverage
- Model field validation tests
7. Documentation
-
docs/REWARD_DISTRIBUTION.md: Complete technical documentation -
docs/QUICK_START_REWARD_DISTRIBUTION.md: 5-minute setup guide -
contracts/README.md: Smart contract documentation - Security best practices and deployment guides
Configuration
New environment variables in .env.example:
# Blockchain configuration
ETHEREUM_NODE_URL=https://sepolia.infura.io/v3/YOUR_INFURA_PROJECT_ID
ETHEREUM_PRIVATE_KEY=your_private_key_here
CONTRACT_ADDRESS=0x_your_deployed_contract_address_here
# Optional configurations
ETH_PRICE_USD=2000 # Update regularly or use price oracle
BLOCKCHAIN_TX_TIMEOUT=120 # Adjust based on network conditions
How It Works
- Bug hunter submits a bug with their ETH wallet configured in profile
- Organization approves the bug submission with a reward amount
- System automatically:
- Validates hunter's cryptocurrency preference and wallet
- Converts USD reward to ETH (Wei) using configured rate
- Creates and signs blockchain transaction
- Distributes reward to hunter's wallet with reentrancy protection
- Records transaction hash on-chain for transparency
- If automatic distribution fails, system falls back to manual distribution
Security Features
- ✅ Reentrancy protection in smart contract (
noReentrancymodifier) - ✅ Access control (only platform owner can distribute rewards)
- ✅ Double-spend prevention through on-chain issue tracking
- ✅ Private key management via environment variables
- ✅ Event logging for all ETH transfers
- ✅ Comprehensive error handling and logging
Important Notes
⚠️ Before Production Deployment:
- Price Oracle Required: Static ETH price must be replaced with real-time price oracle (e.g., Chainlink)
- Security Audit: Smart contract should be professionally audited
- Testnet Testing: Thoroughly test on Sepolia testnet before mainnet
- Key Management: Use hardware wallet or HSM for production private keys
Current Status
- Automated Distribution: ETH only (via smart contract)
- Manual Distribution: BTC and BCH (existing system integration possible)
- Configuration: Disabled by default, requires environment setup
- Testing: Ready for testnet deployment and testing
Quick Start
See docs/QUICK_START_REWARD_DISTRIBUTION.md for a 5-minute setup guide to test on Sepolia testnet.
Deployment Steps
- Compile contract:
solc --bin --abi contracts/BugBountyReward.sol -o contracts/build/ - Configure environment variables (testnet first!)
- Deploy to testnet:
python manage.py deploy_reward_contract --network testnet - Test thoroughly with various scenarios
- Integrate price oracle before production
- Get professional security audit
- Deploy to mainnet when ready
- Fixes OWASP-BLT/BLT#1915
Original prompt
This section details on the original issue you should resolve
<issue_title>Automatic Reward Distribution: Create a smart contract-based system to automatically distribute bug hunt rewards to successful bug hunters in their chosen cryptocurrency upon approval of their submission.</issue_title> <issue_description></issue_description>
Comments on the Issue (you are @copilot in this section)
- Fixes OWASP-BLT/BLT#1915
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.