Skip to content

0xendale/ecvrf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ECVRF - Elliptic Curve Verifiable Random Function

A decentralized Verifiable Random Function (VRF) implementation for Ethereum, providing cryptographically secure and verifiable random number generation for smart contracts.

🌟 Overview

ECVRF is a production-ready VRF solution that combines:

  • Verifiable Randomness: Cryptographically provable random number generation
  • Smart Contract Integration: Seamless integration with Solidity contracts
  • Off-chain Worker: Automated VRF request processing and fulfillment
  • Dual Framework Support: Built with both Hardhat and Foundry

✨ Features

  • πŸ” Cryptographic Security: ECVRF-based provable randomness
  • 🎯 Request-Response Model: Consumer contracts request, coordinator fulfills
  • ⚑ Automated Fulfillment: Off-chain worker listens and processes requests
  • πŸ”„ Subscription Model: Efficient gas management via VRFCoordinatorV2
  • πŸ› οΈ Developer Friendly: Comprehensive testing and deployment scripts
  • πŸ“Š TypeScript Integration: Type-safe integration services

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  VRF Consumer   │────────▢│ VRF Coordinator  │◀────────│   VRF Worker    β”‚
β”‚   (Your Dapp)   β”‚         β”‚   (On-chain)     β”‚         β”‚   (Off-chain)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                            β”‚                              β”‚
       β”‚ 1. requestRandomWords()    β”‚                              β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Άβ”‚                              β”‚
       β”‚                            β”‚ 2. RandomWordsRequested      β”‚
       β”‚                            β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Άβ”‚
       β”‚                            β”‚                              β”‚
       β”‚                            β”‚ 3. fulfillRandomWords()      β”‚
       β”‚                            │◀──────────────────────────────
       β”‚ 4. rawFulfillRandomWords() β”‚                              β”‚
       │◀────────────────────────────                              β”‚

πŸ“‹ Prerequisites

  • Node.js >= 18.x
  • Yarn or npm
  • Foundry (optional, for Forge commands)

πŸš€ Installation

# Clone the repository
git clone https://github.com/yourusername/ecvrf.git
cd ecvrf

# Install dependencies
yarn install

# Compile contracts
yarn compile

βš™οΈ Configuration

Create a .env file in the root directory:

# Network Configuration
TESTNET_RPC_URL=https://your-testnet-rpc-url
MAINNET_RPC_URL=https://your-mainnet-rpc-url

# Private Keys
DEPLOYER_PRIVATE_KEY=your_deployer_private_key
ORACLE_PRIVATE_KEY=your_oracle_private_key

# Contract Addresses (populated after deployment)
VRF_COORDINATOR_ADDRESS=
DAPP_CONSUMER_ADDRESS=

# VRF Configuration
VRF_PUBLIC_KEY=
VRF_SECRET_KEY=

πŸ“¦ Usage

Deployment

Deploy the VRF Coordinator and Consumer contracts:

# Deploy to testnet
yarn deploy --testnet

# Deploy to mainnet
yarn deploy

Start VRF Worker

Run the off-chain worker to process VRF requests:

# Start worker on testnet
yarn start --testnet

# Start worker on mainnet
yarn start

# Development mode with hot reload
yarn dev --testnet

Request Random Numbers

Send a randomness request from your consumer contract:

yarn send-random

Testing

# Run all tests
yarn test

# Run Foundry tests
forge test

# Run with gas reporting
forge test --gas-report

πŸ“ Project Structure

ecvrf/
β”œβ”€β”€ contracts/           # Solidity smart contracts
β”‚   β”œβ”€β”€ VRFCoordinator.sol      # Main coordinator contract
β”‚   β”œβ”€β”€ VRFConsumer.sol         # Base consumer contract
β”‚   β”œβ”€β”€ KeeperVRFConsumer.sol   # Automation-compatible consumer
β”‚   └── core/VRF.sol            # Core VRF cryptographic logic
β”œβ”€β”€ integration-services/        # TypeScript integration layer
β”‚   └── services/
β”‚       β”œβ”€β”€ VRFCoordinator.service.ts
β”‚       └── VRFConsumer.service.ts
β”œβ”€β”€ jobs/               # Off-chain worker services
β”‚   β”œβ”€β”€ index.ts        # Worker entry point
β”‚   β”œβ”€β”€ listeners/vrfListener.ts    # Event listener
β”‚   └── handlers/requestRandomness.ts
β”œβ”€β”€ scripts/            # Deployment and utility scripts
β”‚   β”œβ”€β”€ deploy.ts
β”‚   └── contractDeployment/
β”œβ”€β”€ utils/              # Cryptographic utilities
β”‚   β”œβ”€β”€ vrfProof.ts     # VRF proof generation
β”‚   β”œβ”€β”€ crypto.ts       # Cryptographic primitives
β”‚   └── solidityProof.ts # Solidity-compatible proof formatting
β”œβ”€β”€ tests/              # Test suites
β”œβ”€β”€ abis/               # Contract ABIs
└── config/             # Configuration files

πŸ”§ Available Scripts

Script Description
yarn start Start VRF worker (mainnet)
yarn dev Start VRF worker with hot reload
yarn build Compile TypeScript
yarn compile Compile Solidity contracts and export ABIs
yarn test Run Hardhat tests
yarn deploy Deploy contracts
yarn send-random Send a random number request
yarn lint Lint TypeScript files
yarn prettier Format code
forge build Build contracts with Foundry
forge test Run Foundry tests

πŸ”¨ Development

Building Contracts

# Using Hardhat
yarn compile

# Using Foundry
forge build

Running Tests

# Hardhat tests
yarn test

# Foundry tests
forge test

# With verbosity
forge test -vvv

Code Formatting

# Format TypeScript
yarn prettier

# Format Solidity
forge fmt

🎯 How to Integrate

1. Inherit VRFConsumerBaseV2

import "./VRFConsumer.sol";

contract MyContract is VRFConsumerBaseV2 {
    constructor(address vrfCoordinator) 
        VRFConsumerBaseV2(vrfCoordinator) {}
    
    function requestRandomness() external {
        requestRandomWords(
            keyHash,
            requestConfirmations,
            callbackGasLimit,
            numWords
        );
    }
    
    function fulfillRandomWords(
        uint256 requestId,
        uint256[] memory randomWords
    ) internal override {
        // Use your random numbers here
    }
}

2. Configure Your Consumer

  • Set the VRF Coordinator address
  • Configure callback gas limit
  • Set number of random words needed

3. Deploy and Test

  • Deploy your consumer contract
  • Request random words
  • VRF worker automatically fulfills requests

πŸ” Security Considerations

  • Keep private keys secure and never commit them
  • Use hardware wallets for mainnet deployments
  • Audit callback gas limits to prevent out-of-gas errors
  • Validate VRF proofs on-chain before accepting randomness
  • Implement access controls for sensitive functions

πŸ“š Resources

πŸ‘€ Author

0xEndale

πŸ“„ License

This project is licensed under the MIT License.

🀝 Contributing

Contributions, issues, and feature requests are welcome!


Built with ❀️ using Solidity, TypeScript, Hardhat, and Foundry

About

self research ecvrf

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •