Learn how to use SecretPath on EVM to access on-chain verifiable random numbers.
SecretVRF over SecretPath enables EVM developers to access on-chain verifiable random numbers at a fraction of the cost and block time of traditional RNG oracles such as ChainlinkVRF. With fewer than 100 lines of code, you will have access to an infinite supply of randomness.
See a fullstack cross-chain SecretVRF demo here
To learn how SecretVRF works underneath the hood, refer to the docs here. 🤓
To get started, clone the Secret Labs examples repo:
cd
into examples/EVM-secretpath-RNG:
Install the node dependencies:
Update the env
file with your EVM wallet private key and Infura API key.
Make sure your Infura API key is configured for Amoy testnet 😎
Compile your Solidity smart contract:
Once the contract is compiled successfully, upload the contract to Polygon testnet:
Note the contract address:
Add the RandomnessReceiver
contract address to your env
file:
Now that you've uploaded your contract, it's time to set the SecretPath gateway address for Polygon Amoy and then request on-chain verifiable random numbers!
Gateways are the on-chain smart contracts that handle the broadcasting, receipt, packaging, and verification of messages.
First, set the gateway address for Polygon Amoy testnet. You can do this by executing set_gateway.js
:
This tutorial is for Polygon testnet, but you can find a list of additional EVM gateway contract addresses here.
Next, create an event listener so you can listen to when the random numbers that you request have been fulfilled.
Open a new terminal window and cd
into examples/EVM-secretpath-RNG:
Then, create the event listener by executing fulfill_randomness_event.js
:
Now it's time to request random numbers! Currently, request_random.js
is configured to request 3 random numbers, but you can update how many numbers you would like to request here (up to 2000 for this example).
Once you have configured how many random numbers you want to request, execute request_random.js
:
Upon successful execution, your terminal will log the following:
Navigate to your event listener terminal to see the returned random numbers:
Congrats! You've just used SecretPath to request your first verifiable on-chain random numbers! 🎉
Secret VRF offers an innovative and cost-effective solution for EVM developers seeking access to verifiable random numbers. By following this guide, you've successfully set up your environment, deployed the RandomnessReceiver.sol
contract, and interacted with the SecretPath network to request and receive random numbers. Dive into the world of decentralized randomness with SecretPath, where security meets simplicity. 🌟
Need help with using encrypted payloads with Secretpath or want to discuss use cases for your dApp? Please ask in the Secret Network Telegram or Discord.
First, install all of the the dependencies via NPM:
Next, import the following into your code:
In your vite.config.ts
in the project, you need to add the support for bigInt
into the esbuildOptions:
To start, we first define all of our variables that we need for the encryption, as well as the gateway information:
First, we define the Gateway address that is specific to each chain, which can you can look up here Supported Networks.
Second, you need to input the private contract that you are going to call, in our case the Secret VRF RNG contact on Secret Network. The code for this example contract can be found here in case you want to deploy it yourself.
Next, init the Ethereum client that you are using to call the contract with. Here, we init the chainId to use the Ethereum sepolia testnet and use ethers.js to retrieve the address.
Next, you generate ephermal keys and load in the public encryption key for the Secret Gateway that you can look up in Supported Networks. Then, use ECDH to create the encryption key: