Skip to content

Commit

Permalink
Simplify interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Oct 10, 2024
1 parent 347e6fe commit 1ce00e2
Showing 1 changed file with 14 additions and 56 deletions.
70 changes: 14 additions & 56 deletions contracts/src/vrf_provider/vrf_provider_component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use stark_vrf::ecvrf::{Point, Proof, ECVRF, ECVRFImpl};

#[starknet::interface]
trait IVrfProvider<TContractState> {
fn request_random(ref self: TContractState) -> felt252;
fn request_random(ref self: TContractState, seed: felt252) -> felt252;
fn submit_random(ref self: TContractState, seed: felt252, proof: Proof);
//
fn submit_random_no_proof(ref self: TContractState, seed: felt252, random: felt252);
Expand Down Expand Up @@ -46,6 +46,7 @@ impl PublicKeyIntoPoint of Into<PublicKey, Point> {
pub mod VrfProviderComponent {
use starknet::ContractAddress;
use starknet::get_caller_address;
use core::poseidon::poseidon_hash_span;
use starknet::storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map
};
Expand All @@ -61,8 +62,6 @@ pub mod VrfProviderComponent {
#[storage]
struct Storage {
VrfProvider_pubkey: PublicKey,
// caller -> nonce
VrfProvider_nonces: Map<ContractAddress, felt252>,
// seed -> random
VrfProvider_random: Map<felt252, felt252>,
}
Expand All @@ -73,14 +72,9 @@ pub mod VrfProviderComponent {
}

#[derive(Drop, starknet::Event)]
struct RequestRandom {
struct SubmitRandom {
#[key]
caller: ContractAddress,
seed: felt252,
}

#[derive(Drop, starknet::Event)]
struct SubmitRandom {
#[key]
seed: felt252,
proof: Proof,
Expand All @@ -90,7 +84,6 @@ pub mod VrfProviderComponent {
#[event]
enum Event {
PublicKeyChanged: PublicKeyChanged,
RequestRandom: RequestRandom,
SubmitRandom: SubmitRandom,
}

Expand All @@ -110,34 +103,25 @@ pub mod VrfProviderComponent {
impl Owner: OwnableComponent::HasComponent<TContractState>,
> of super::IVrfProvider<ComponentState<TContractState>> {
// directly called by user to request randomness
fn request_random(ref self: ComponentState<TContractState>) -> felt252 {
let caller = get_caller_address();
let nonce = self._increase_nonce(caller);
let chain_id = starknet::get_execution_info().tx_info.unbox().chain_id;
let seed = get_seed(caller, nonce, chain_id);

self.emit(RequestRandom { caller, seed, });

seed
}
fn request_random(ref self: ComponentState<TContractState>, caller: ContractAddress, seed: felt252) {}

// called by vrf providers
fn submit_random(ref self: ComponentState<TContractState>, seed: felt252, proof: Proof) {
fn submit_random(ref self: ComponentState<TContractState>, caller: ContractAddress, seed: felt252, proof: Proof) {
// verify proof
let pubkey: Point = self.get_public_key().into();
let ecvrf = ECVRFImpl::new(pubkey);
let chain_id = starknet::get_execution_info().tx_info.unbox().chain_id;
let full_seed = poseidon_hash_span(array![seed, caller.into(), chain_id].span());

let random = ecvrf
.verify(proof.clone(), array![seed.clone()].span())
.verify(proof.clone(), array![full_seed.clone()].span())
.expect(Errors::INVALID_PROOF);

// write random
self.VrfProvider_random.write(seed, random);

self.emit(SubmitRandom { seed, proof });
self.emit(SubmitRandom { caller, seed, proof });
}


// for testing purpose
fn submit_random_no_proof(
ref self: ComponentState<TContractState>, seed: felt252, random: felt252
Expand All @@ -155,28 +139,19 @@ pub mod VrfProviderComponent {
//
//

// get next seed for a caller address
fn get_next_seed(
self: @ComponentState<TContractState>, caller: ContractAddress,
) -> felt252 {
let nonce = self._get_nonce(caller) + 1;
let chain_id = starknet::get_execution_info().tx_info.unbox().chain_id;
get_seed(caller, nonce, chain_id)
}

// consume randomness
fn consume_random(
ref self: ComponentState<TContractState>, caller: ContractAddress
ref self: ComponentState<TContractState>, seed: felt252
) -> felt252 {
let nonce = self._get_nonce(caller);
let caller = get_caller_address();
let chain_id = starknet::get_execution_info().tx_info.unbox().chain_id;
let seed = get_seed(caller, nonce, chain_id);
let random = self.VrfProvider_random.read(seed);
let full_seed = poseidon_hash_span(array![seed, caller.into(), chain_id].span());

let random = self.VrfProvider_random.read(full_seed);
assert(random != 0, Errors::NOT_FULFILLED);

// enforce one time consumtion
self.VrfProvider_random.write(seed, 0);
self.VrfProvider_random.write(full_seed, 0);

random
}
Expand Down Expand Up @@ -218,22 +193,5 @@ pub mod VrfProviderComponent {

self.emit(PublicKeyChanged { pubkey: new_pubkey })
}

//
//
//

fn _get_nonce(self: @ComponentState<TContractState>, caller: ContractAddress,) -> felt252 {
self.VrfProvider_nonces.read(caller)
}

fn _increase_nonce(
ref self: ComponentState<TContractState>, caller: ContractAddress
) -> felt252 {
let nonce = self.VrfProvider_nonces.read(caller);
let new_nonce = nonce + 1;
self.VrfProvider_nonces.write(caller, new_nonce);
new_nonce
}
}
}

0 comments on commit 1ce00e2

Please sign in to comment.