From 6dbd81e5f48b10702744b279cf669bd2171ffdbb Mon Sep 17 00:00:00 2001 From: Filip Krawczyk Date: Wed, 31 Jul 2024 14:00:17 +0200 Subject: [PATCH] FactRegistry ownership and verifier registration --- fact_registry/src/lib.cairo | 40 ++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/fact_registry/src/lib.cairo b/fact_registry/src/lib.cairo index 4f0fa57d3..9d8aaae81 100644 --- a/fact_registry/src/lib.cairo +++ b/fact_registry/src/lib.cairo @@ -3,6 +3,7 @@ mod verifier; use cairo_verifier::{StarkProofWithSerde, CairoVersion}; use starknet::ContractAddress; + #[derive(Drop, Copy, Serde)] struct VerifierSettings { layout: felt252, @@ -18,12 +19,14 @@ trait IFactRegistry { ref self: TContractState, stark_proof: StarkProofWithSerde, settings: VerifierSettings ); fn is_valid(self: @TContractState, fact: felt252) -> bool; + fn register_verifier(ref self: TContractState, settings: VerifierSettings, address: ContractAddress); + fn transfer_ownership(ref self: TContractState, new_owner: ContractAddress); } #[starknet::contract] mod FactRegistry { use cairo_verifier::{StarkProofWithSerde, CairoVersion}; - use starknet::ContractAddress; + use starknet::{ContractAddress, get_caller_address}; use core::{ poseidon::{Poseidon, PoseidonImpl, HashStateImpl}, keccak::keccak_u256s_be_inputs, starknet::event::EventEmitter @@ -33,6 +36,7 @@ mod FactRegistry { #[storage] struct Storage { + owner: ContractAddress, verifiers: LegacyMap, facts: LegacyMap, } @@ -43,6 +47,7 @@ mod FactRegistry { // #[flat] // CairoVerifierEvent: CairoVerifier::Event, FactRegistered: FactRegistered, + OwnershipTransferred: OwnershipTransferred, } #[derive(Drop, starknet::Event)] @@ -51,12 +56,24 @@ mod FactRegistry { fact: felt252, } + #[derive(Drop, starknet::Event)] + struct OwnershipTransferred { + previous_owner: ContractAddress, + new_owner: ContractAddress + } + + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.owner.write(owner); + } + #[abi(embed_v0)] impl FactRegistryImpl of IFactRegistry { fn verify_and_register_fact( ref self: ContractState, stark_proof: StarkProofWithSerde, settings: VerifierSettings, ) { let verifier_address = self.verifiers.read(self._hash_settings(settings)); + assert(verifier_address.into() != 0, 'VERIFIER_NOT_FOUND'); let (program_hash, output_hash) = ICairoVerifierDispatcher { contract_address: verifier_address }.verify_proof(stark_proof.into(), settings.cairo_version); @@ -66,6 +83,27 @@ mod FactRegistry { fn is_valid(self: @ContractState, fact: felt252) -> bool { self.facts.read(fact) } + + fn register_verifier(ref self: ContractState, settings: VerifierSettings, address: ContractAddress) { + assert(self.owner.read() == get_caller_address(), 'ONLY_OWNER'); + assert(address.into() != 0, 'INVALID_VERIFIER_ADDRESS'); + let settings_hash = self._hash_settings(settings); + assert(self.verifiers.read(settings_hash).into() == 0, 'VERIFIER_ALREADY_EXISTS'); + self.verifiers.write(settings_hash, address); + } + + fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { + let caller = get_caller_address(); + assert(self.owner.read() == caller, 'ONLY_OWNER'); + self.owner.write(new_owner); + + self + .emit( + Event::OwnershipTransferred( + OwnershipTransferred { previous_owner: caller, new_owner } + ) + ); + } } #[generate_trait]