Skip to content
Closed
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
76 changes: 76 additions & 0 deletions src/UserRegistry.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#[starknet::contract]
mod UserRegistry {
use starknet::contract_address::ContractAddress;
use starknet::storage;
use starknet::syscalls::get_caller_address;
use core::integer::u128;

#[derive(Drop, Copy, Serde)]
struct Profile {
id: u128,
user_address: ContractAddress,
name: felt252,
is_active: bool,
}

#[storage]
struct Storage {
profiles: Map<u128, Profile>,
profile_by_address: Map<ContractAddress, u128>, // Maps address to profile ID
profile_count: u128, // Tracks the total number of profiles
}

#[event]
struct ProfileUpdated {
user: ContractAddress,
}

#[event]
struct ProfileDeactivated {
user: ContractAddress,
}

#[starknet::interface]
trait IUserRegistry<TContractState> {
fn update_profile(ref self: TContractState, name: felt252);
fn deactivate_profile(ref self: TContractState);
fn get_profile_by_id(self: @TContractState, id: u128) -> Profile;
fn get_profile_by_address(self: @TContractState, address: ContractAddress) -> Profile;
}

#[external(v0)]
impl IUserRegistry<ContractState> of IUserRegistry<ContractState> {
fn update_profile(ref self: ContractState, name: felt252) {
let caller = get_caller_address();
let id = self.profile_by_address.get(caller).unwrap_or_else(|| panic!("Profile not found"));

let mut profile = self.profiles.get(id).unwrap_or_else(|| panic!("Profile not found"));
assert!(profile.user_address == caller, "Unauthorized");

profile.name = name;
self.profiles.insert(id, profile);
self.emit(ProfileUpdated { user: caller });
}

fn deactivate_profile(ref self: ContractState) {
let caller = get_caller_address();
let id = self.profile_by_address.get(caller).unwrap_or_else(|| panic!("Profile not found"));

let mut profile = self.profiles.get(id).unwrap_or_else(|| panic!("Profile not found"));
assert!(profile.user_address == caller, "Unauthorized");

profile.is_active = false;
self.profiles.insert(id, profile);
self.emit(ProfileDeactivated { user: caller });
}

fn get_profile_by_id(self: @ContractState, id: u128) -> Profile {
return self.profiles.get(id).unwrap_or_else(|| panic!("Profile not found"));
}

fn get_profile_by_address(self: @ContractState, address: ContractAddress) -> Profile {
let id = self.profile_by_address.get(address).unwrap_or_else(|| panic!("Profile not found"));
return self.profiles.get(id).unwrap_or_else(|| panic!("Profile not found"));
}
}
}
15 changes: 15 additions & 0 deletions src/interfaces/IUserRegistry.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@

#[starknet::interface]
trait IUserRegistry<TContractState> {
/// Updates the name of the caller's profile
fn update_profile(ref self: TContractState, name: felt252);

/// Deactivates the caller's profile without data loss
fn deactivate_profile(ref self: TContractState);

/// Retrieves a profile by its unique ID
fn get_profile_by_id(self: @TContractState, id: u128) -> Profile;

/// Retrieves a profile by the associated wallet address
fn get_profile_by_address(self: @TContractState, address: ContractAddress) -> Profile;
}

Empty file added tests/test_UserRegistry.cairo
Empty file.