Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions contracts/identity-registry-contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use soroban_sdk::{Address, Env};
use crate::storage;
use crate::error::RegistryError;
use crate::types::ExpertStatus;
use crate::events;

pub fn initialize_registry(env: &Env, admin: &Address) -> Result<(), RegistryError> {
if storage::has_admin(env) {
Expand All @@ -9,5 +11,23 @@ pub fn initialize_registry(env: &Env, admin: &Address) -> Result<(), RegistryErr

storage::set_admin(env, admin);

Ok(())
}

pub fn verify_expert(env: &Env, expert: &Address) -> Result<(), RegistryError> {
let admin = storage::get_admin(env).ok_or(RegistryError::NotInitialized)?;

admin.require_auth();

let current_status = storage::get_expert_status(env, expert);

if current_status == ExpertStatus::Verified {
return Err(RegistryError::AlreadyVerified);
}

storage::set_expert_record(env, expert, ExpertStatus::Verified);

events::emit_status_change(env, expert.clone(), current_status, ExpertStatus::Verified, admin);

Ok(())
}
5 changes: 5 additions & 0 deletions contracts/identity-registry-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ impl IdentityRegistryContract {
pub fn init(env: Env, admin: Address) -> Result<(), RegistryError> {
contract::initialize_registry(&env, &admin)
}

/// Add an expert to the whitelist (Admin only)
pub fn add_expert(env: Env, expert: Address) -> Result<(), RegistryError> {
contract::verify_expert(&env, &expert)
}
}
77 changes: 76 additions & 1 deletion contracts/identity-registry-contract/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#![cfg(test)]

extern crate std;

use crate::{IdentityRegistryContract, IdentityRegistryContractClient};
use soroban_sdk::{Env, testutils::Address as _};
use soroban_sdk::{Env, testutils::Address as _, Symbol, Address, IntoVal, TryIntoVal};
use soroban_sdk::testutils::{AuthorizedFunction, AuthorizedInvocation, Events};

#[test]
fn test_initialization() {
Expand All @@ -18,4 +22,75 @@ fn test_initialization() {
// 3. Call init again (Should fail)
let res_duplicate = client.try_init(&admin);
assert!(res_duplicate.is_err());
}

#[test]
fn test_add_expert() {
let env = Env::default();
env.mock_all_auths();

let contract_id = env.register_contract(None, IdentityRegistryContract);
let client = IdentityRegistryContractClient::new(&env, &contract_id);

let admin = Address::generate(&env);
let expert = Address::generate(&env);

client.init(&admin);

let res = client.try_add_expert(&expert);
assert!(res.is_ok());

assert_eq!(
env.auths()[0],
(
admin.clone(),
AuthorizedInvocation {
function: AuthorizedFunction::Contract((
contract_id.clone(),
Symbol::new(&env, "add_expert"),
(expert.clone(),).into_val(&env)
)),
sub_invocations: std::vec![]
}
)
);
}

#[test]
#[should_panic]
fn test_add_expert_unauthorized() {
let env = Env::default();

let contract_id = env.register_contract(None, IdentityRegistryContract);
let client = IdentityRegistryContractClient::new(&env, &contract_id);

let admin = Address::generate(&env);
let expert = Address::generate(&env);

client.init(&admin);

client.add_expert(&expert);
}

#[test]
fn test_expert_status_changed_event() {
let env = Env::default();
env.mock_all_auths();

let contract_id = env.register_contract(None, IdentityRegistryContract);
let client = IdentityRegistryContractClient::new(&env, &contract_id);

let admin = Address::generate(&env);
let expert = Address::generate(&env);

client.init(&admin);
client.add_expert(&expert);

let events = env.events().all();
let event = events.last().unwrap();

assert_eq!(event.0, contract_id);

let topic: Symbol = event.1.get(0).unwrap().try_into_val(&env).unwrap();
assert_eq!(topic, Symbol::new(&env, "status_change"));
}