From 1ce00e25edc52b84a40ac15802651879d1a61d48 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Thu, 10 Oct 2024 15:40:49 -0400 Subject: [PATCH] Simplify interface --- .../vrf_provider/vrf_provider_component.cairo | 70 ++++--------------- 1 file changed, 14 insertions(+), 56 deletions(-) diff --git a/contracts/src/vrf_provider/vrf_provider_component.cairo b/contracts/src/vrf_provider/vrf_provider_component.cairo index edfaefe..edbe863 100644 --- a/contracts/src/vrf_provider/vrf_provider_component.cairo +++ b/contracts/src/vrf_provider/vrf_provider_component.cairo @@ -3,7 +3,7 @@ use stark_vrf::ecvrf::{Point, Proof, ECVRF, ECVRFImpl}; #[starknet::interface] trait IVrfProvider { - 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); @@ -46,6 +46,7 @@ impl PublicKeyIntoPoint of Into { pub mod VrfProviderComponent { use starknet::ContractAddress; use starknet::get_caller_address; + use core::poseidon::poseidon_hash_span; use starknet::storage::{ StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map }; @@ -61,8 +62,6 @@ pub mod VrfProviderComponent { #[storage] struct Storage { VrfProvider_pubkey: PublicKey, - // caller -> nonce - VrfProvider_nonces: Map, // seed -> random VrfProvider_random: Map, } @@ -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, @@ -90,7 +84,6 @@ pub mod VrfProviderComponent { #[event] enum Event { PublicKeyChanged: PublicKeyChanged, - RequestRandom: RequestRandom, SubmitRandom: SubmitRandom, } @@ -110,34 +103,25 @@ pub mod VrfProviderComponent { impl Owner: OwnableComponent::HasComponent, > of super::IVrfProvider> { // directly called by user to request randomness - fn request_random(ref self: ComponentState) -> 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, caller: ContractAddress, seed: felt252) {} // called by vrf providers - fn submit_random(ref self: ComponentState, seed: felt252, proof: Proof) { + fn submit_random(ref self: ComponentState, 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, seed: felt252, random: felt252 @@ -155,28 +139,19 @@ pub mod VrfProviderComponent { // // - // get next seed for a caller address - fn get_next_seed( - self: @ComponentState, 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, caller: ContractAddress + ref self: ComponentState, 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 } @@ -218,22 +193,5 @@ pub mod VrfProviderComponent { self.emit(PublicKeyChanged { pubkey: new_pubkey }) } - - // - // - // - - fn _get_nonce(self: @ComponentState, caller: ContractAddress,) -> felt252 { - self.VrfProvider_nonces.read(caller) - } - - fn _increase_nonce( - ref self: ComponentState, caller: ContractAddress - ) -> felt252 { - let nonce = self.VrfProvider_nonces.read(caller); - let new_nonce = nonce + 1; - self.VrfProvider_nonces.write(caller, new_nonce); - new_nonce - } } }