From f2af08bfc414481a4196422758170309e2025c11 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 | 78 ++++++------------- 1 file changed, 25 insertions(+), 53 deletions(-) diff --git a/contracts/src/vrf_provider/vrf_provider_component.cairo b/contracts/src/vrf_provider/vrf_provider_component.cairo index edfaefe..f7bb6b6 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 }; @@ -73,14 +74,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 +86,6 @@ pub mod VrfProviderComponent { #[event] enum Event { PublicKeyChanged: PublicKeyChanged, - RequestRandom: RequestRandom, SubmitRandom: SubmitRandom, } @@ -110,19 +105,17 @@ 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, salt: Option + ) {} // 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); @@ -131,13 +124,11 @@ pub mod VrfProviderComponent { .verify(proof.clone(), array![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,24 +146,23 @@ 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, salt: Option ) -> 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 seed = match salt { + Option::Some(s) => poseidon_hash_span(array![s, caller.into(), chain_id].span()), + Option::None => { + let nonce = self.VrfProvider_nonces.read(caller); + poseidon_hash_span(array![nonce, caller.into(), chain_id].span()) + self.VrfProvider_nonces.write(caller, nonce + 1); + } + }; + + let random = self.VrfProvider_random.read(seed); assert(random != 0, Errors::NOT_FULFILLED); // enforce one time consumtion @@ -187,7 +177,6 @@ pub mod VrfProviderComponent { assert(random == 0, Errors::NOT_CONSUMED); } - // // // @@ -218,22 +207,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 - } } }