Skip to content

SDK for verifiable randomness function (VRF / RNG) on Aptos

Notifications You must be signed in to change notification settings

orao-network/aptos-vrf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ORAO VRF for Aptos SDKs

This repository provides JS, Rust and Move SDKs. With these libs you'll be able to request on-chain randomness using ORAO VRF module.

SDK sections:

Examples

The example section provides three sample apps for each of the SDKs: Move, JS and Rust. Please note that all sample apps utilize testnet network.

  • For Aptos Move VRF we've built a Russian Roulette game as a move module.
  • The JS example provides a frontend web3 dApp built using React. We integrated Pontem and Petra browser wallets. Any wallet for Aptos blockchain should work provided it generates payloads to spec.
  • There's also a Rust CLI app that generates a new wallet, gets an airdrop from aptos testnet, requests and outputs randomness.

Anatomy of randomness request

The RandomnessData structure is used to store the requested randomness:

  • seeds table – stores the request seed as key and the fulfilled randomness as value.

  • responses field – you may look at this field in case you are willing to perform off-chain verification (there are helpers for this in both SDKs)

On-chain example

The contract we'll use to illustrate the C2C is a simple single-player Russian Roulette where the outcome of a round is derived from a fulfilled randomness. (full code is available on GitHub)

Note: the randomness will not be immediately available for your contract, so you'll need to design it in a way that it'll wait for randomness being fulfilled. In our example a player won't be able to start another round until the current one is finished (until the randomness is fulfilled).

1. Create the contract

This examples is based on Move programming language.

To perform a C2C call you'll need to add the orao VRF move SDK into the list of your dependencies:

[dependencies.AptosFramework]
git = 'https://github.com/aptos-labs/aptos-core.git'
rev = 'mainnet'
subdir = 'aptos-move/framework/aptos-framework'

[dependencies.orao-vrf]
git = 'https://github.com/orao-network/aptos-vrf.git'
rev = 'master'
subdir = 'move'

2. Perform a C2C call

vrf_v2::request_with_callback(user, seed, module_addr, module_name, func_name, fee_amount);

3. Use the fulfilled randomness

public entry fun callback(user_addr: address, seed: vector<u8>) {
    let randomness: vector<u8> = vrf_v2::get_randomness(user_addr, seed);
    assert!(successfull_outcome(&randomness), E_DEAD);
}

Off-chain Rust example

This section will illustrate the off-chain usage (full code is available on GitHub)

Set up the connection

let client = OraoVrfV2::new(NODE_URL.clone().to_string());

Create a request

let seed = rand::random::<[u8; 32]>().to_vec();
let hash = client
    .request(&mut alice, seed.clone(), None)
    .await?;

Wait for fulfillment

let randomness = wait_fulfilled(&client, &alice.address(), &seed).await?;