-
Notifications
You must be signed in to change notification settings - Fork 79
feat(core): contract root anchoring #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
33143be
790a222
675bb76
1583021
682aae2
0522913
c76b44e
462b347
ecff67d
3193db2
f60b6fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| use soroban_sdk::contracterror; | ||
|
|
||
| #[contracterror] | ||
| #[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
| pub enum ContractError { | ||
| AlreadyInitialized = 1, | ||
| NotInitialized = 2, | ||
| RootMismatch = 3, | ||
| InvalidProof = 4, | ||
| DuplicateCommitment = 5, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,18 @@ | ||
| #![no_std] | ||
|
|
||
| mod errors; | ||
| mod events; | ||
| mod storage; | ||
| mod types; | ||
|
|
||
| #[cfg(test)] | ||
| mod test; | ||
|
|
||
| use soroban_sdk::{contract, contractclient, contractimpl, panic_with_error, Address, BytesN, Env}; | ||
|
|
||
| pub use crate::errors::ContractError; | ||
| pub use crate::events::{MerkleRootUpdated, UsernameRegistered}; | ||
| pub use crate::types::{Proof, PublicSignals}; | ||
| pub mod events; | ||
| pub mod types; | ||
|
|
||
|
|
@@ -9,29 +22,63 @@ use soroban_sdk::{ | |
| use types::ResolveData; | ||
|
|
||
| #[contract] | ||
| pub struct Contract; | ||
| pub struct CoreContract; | ||
|
|
||
| #[contractclient(name = "VerifierContractClient")] | ||
| pub trait VerifierContract { | ||
| fn verify_proof(env: Env, proof: Proof, public_signals: PublicSignals) -> bool; | ||
| } | ||
|
|
||
| #[contracttype] | ||
| #[derive(Clone)] | ||
| pub enum DataKey { | ||
| Resolver(BytesN<32>), | ||
| fn current_merkle_root(env: &Env) -> BytesN<32> { | ||
| storage::get_merkle_root(env) | ||
| .unwrap_or_else(|| panic_with_error!(env, ContractError::NotInitialized)) | ||
| } | ||
|
|
||
| fn update_merkle_root(env: &Env, old_root: BytesN<32>, new_root: BytesN<32>) { | ||
| storage::set_merkle_root(env, &new_root); | ||
|
|
||
| MerkleRootUpdated { old_root, new_root }.publish(env); | ||
| #[contracterror] | ||
| #[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
| pub enum ResolverError { | ||
| NotFound = 1, | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #[contractimpl] | ||
| impl Contract { | ||
| pub fn register_resolver(env: Env, commitment: BytesN<32>, wallet: Address, memo: Option<u64>) { | ||
| let key = DataKey::Resolver(commitment); | ||
| let data = ResolveData { wallet, memo }; | ||
| impl CoreContract { | ||
| pub fn init(env: Env, verifier: Address, root: BytesN<32>) { | ||
| if storage::is_initialized(&env) { | ||
| panic_with_error!(&env, ContractError::AlreadyInitialized); | ||
| } | ||
|
|
||
| env.storage().persistent().set(&key, &data); | ||
| storage::set_verifier(&env, &verifier); | ||
| storage::set_merkle_root(&env, &root); | ||
| } | ||
|
||
|
|
||
| pub fn submit_proof(env: Env, proof: Proof, public_signals: PublicSignals) { | ||
| let current_root = current_merkle_root(&env); | ||
|
|
||
| if current_root != public_signals.old_root.clone() { | ||
| panic_with_error!(&env, ContractError::RootMismatch); | ||
| } | ||
|
|
||
| if storage::has_commitment(&env, &public_signals.commitment) { | ||
| panic_with_error!(&env, ContractError::DuplicateCommitment); | ||
| } | ||
|
|
||
| let verifier = storage::get_verifier(&env) | ||
| .unwrap_or_else(|| panic_with_error!(&env, ContractError::NotInitialized)); | ||
| let verifier_client = VerifierContractClient::new(&env, &verifier); | ||
| let is_valid = verifier_client.verify_proof(&proof, &public_signals); | ||
| if !is_valid { | ||
| panic_with_error!(&env, ContractError::InvalidProof); | ||
| } | ||
|
|
||
| storage::store_commitment(&env, &public_signals.commitment); | ||
| update_merkle_root(&env, current_root, public_signals.new_root.clone()); | ||
|
|
||
| UsernameRegistered { | ||
| commitment: public_signals.commitment, | ||
| pub fn set_memo(env: Env, commitment: BytesN<32>, memo_id: u64) { | ||
| let key = DataKey::Resolver(commitment); | ||
| let mut data = env | ||
|
|
@@ -51,6 +98,19 @@ impl Contract { | |
| Some(data) => (data.wallet, data.memo), | ||
| None => panic_with_error!(&env, ResolverError::NotFound), | ||
| } | ||
| .publish(&env); | ||
| } | ||
|
|
||
| pub fn get_merkle_root(env: Env) -> BytesN<32> { | ||
| current_merkle_root(&env) | ||
| } | ||
|
|
||
| pub fn get_verifier(env: Env) -> Option<Address> { | ||
| storage::get_verifier(&env) | ||
| } | ||
|
|
||
| pub fn has_commitment(env: Env, commitment: BytesN<32>) -> bool { | ||
| storage::has_commitment(&env, &commitment) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| use soroban_sdk::{contracttype, Address, BytesN, Env}; | ||
|
|
||
| #[contracttype] | ||
| #[derive(Clone)] | ||
| pub enum DataKey { | ||
| CurrentMerkleRoot, | ||
| Verifier, | ||
| Commitment(BytesN<32>), | ||
| } | ||
|
|
||
| pub fn is_initialized(env: &Env) -> bool { | ||
| env.storage().persistent().has(&DataKey::CurrentMerkleRoot) | ||
| && env.storage().instance().has(&DataKey::Verifier) | ||
|
||
| } | ||
|
|
||
| pub fn get_merkle_root(env: &Env) -> Option<BytesN<32>> { | ||
| env.storage().persistent().get(&DataKey::CurrentMerkleRoot) | ||
| } | ||
|
|
||
| pub fn set_merkle_root(env: &Env, root: &BytesN<32>) { | ||
| env.storage() | ||
| .persistent() | ||
| .set(&DataKey::CurrentMerkleRoot, root); | ||
| } | ||
|
|
||
| pub fn get_verifier(env: &Env) -> Option<Address> { | ||
| env.storage().instance().get(&DataKey::Verifier) | ||
| } | ||
|
|
||
| pub fn set_verifier(env: &Env, verifier: &Address) { | ||
| env.storage().instance().set(&DataKey::Verifier, verifier); | ||
| } | ||
|
|
||
| pub fn has_commitment(env: &Env, commitment: &BytesN<32>) -> bool { | ||
| env.storage() | ||
| .persistent() | ||
| .has(&DataKey::Commitment(commitment.clone())) | ||
| } | ||
|
|
||
| pub fn store_commitment(env: &Env, commitment: &BytesN<32>) { | ||
| env.storage() | ||
| .persistent() | ||
| .set(&DataKey::Commitment(commitment.clone()), &true); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.