@@ -6,73 +6,91 @@ pub mod ChainLib {
66 };
77 use starknet :: {ContractAddress , get_block_timestamp, get_caller_address};
88 use crate :: interfaces :: IChainLib :: IChainLib ;
9- use crate :: base :: types :: {TokenBoundAccount };
9+ use crate :: base :: types :: {TokenBoundAccount , User , Role , Rank };
1010
1111
1212 #[storage]
1313 struct Storage {
1414 // Contract addresses for component management
15- deployed : bool ,
15+ admin : ContractAddress ,
1616 current_account_id : u256 ,
1717 accounts : Map <u256 , TokenBoundAccount >,
1818 accountsaddr : Map <ContractAddress , TokenBoundAccount >,
1919 next_course_id : u256 ,
20- nuum : Map <u8 , u8 >,
20+ user_id : u256 ,
21+ users : Map <u256 , User >,
2122 }
2223
2324
2425 #[constructor]
25- fn constructor (ref self : ContractState ) {
26+ fn constructor (ref self : ContractState , admin : ContractAddress ) {
2627 // Store the values in contract state
27- self . deployed . write (true );
28+ self . admin . write (admin );
2829 }
2930
3031 #[event]
3132 #[derive(Drop , starknet:: Event )]
3233 pub enum Event {
33- TokenBountAccountcreated : TokenBountAccountcreated ,
34+ TokenBoundAccountCreated : TokenBoundAccountCreated ,
35+ UserCreated : UserCreated ,
3436 }
3537
3638 #[derive(Drop , starknet:: Event )]
37- pub struct TokenBountAccountcreated {
39+ pub struct TokenBoundAccountCreated {
40+ pub id : u256 ,
41+ }
42+
43+ #[derive(Drop , starknet:: Event )]
44+ pub struct UserCreated {
3845 pub id : u256 ,
3946 }
4047
4148 #[abi(embed_v0)]
4249 impl ChainLibNetImpl of IChainLib <ContractState > {
50+ /// @notice Creates a new token-bound account.
51+ /// @dev This function generates a unique ID, initializes the account, and emits an event.
52+ /// @param self The contract state reference.
53+ /// @param user_name The unique username associated with the token-bound account.
54+ /// @param init_param1 An initialization parameter required for the account setup.
55+ /// @param init_param2 An additional initialization parameter.
56+ /// @return account_id The unique identifier assigned to the token-bound account.
4357 fn create_token_account (
44- ref self : ContractState , user_name : felt252 , init_param1 : felt252 , init_param2 : felt252 ,
58+ ref self : ContractState , user_name : felt252 , init_param1 : felt252 , init_param2 : felt252
4559 ) -> u256 {
46- // Validate input parameters .
60+ // Ensure that the username is not empty .
4761 assert! (user_name != 0 , " User name cannot be empty" );
62+
63+ // Validate initialization parameters.
4864 assert! (init_param1 != 0 , " Initialization parameter 1 cannot be empty" );
4965
5066 // Retrieve the current account ID before incrementing.
5167 let account_id = self . current_account_id. read ();
5268
53- // Create a new token bound account struct .
69+ // Create a new token- bound account with the provided parameters .
5470 let new_token_bound_account = TokenBoundAccount {
5571 id : account_id ,
56- address : get_caller_address (),
72+ address : get_caller_address (), // Assign the caller's address.
5773 user_name : user_name ,
5874 init_param1 : init_param1 ,
5975 init_param2 : init_param2 ,
60- created_at : get_block_timestamp (),
61- updated_at : get_block_timestamp (),
76+ created_at : get_block_timestamp (), // Capture the creation timestamp.
77+ updated_at : get_block_timestamp () // Set initial updated timestamp.
6278 };
6379
64- // Store the new account in the accounts map .
80+ // Store the new account in the accounts mapping .
6581 self . accounts. write (account_id , new_token_bound_account );
6682
67- // Increment the account ID counter after using the current value .
83+ // Increment the account ID counter for the next registration .
6884 self . current_account_id. write (account_id + 1 );
6985
70- // Emit an event to signal the creation of the token bound account.
71- self . emit (TokenBountAccountcreated { id : account_id });
86+ // Emit an event to notify about the new token- bound account creation .
87+ self . emit (TokenBoundAccountCreated { id : account_id });
7288
89+ // Return the assigned account ID.
7390 account_id
7491 }
7592
93+
7694 fn get_token_bound_account (ref self : ContractState , id : u256 ) -> TokenBoundAccount {
7795 let token_bound_account = self . accounts. read (id );
7896 token_bound_account
@@ -83,8 +101,88 @@ pub mod ChainLib {
83101 let token_bound_account = self . accountsaddr. read (address );
84102 token_bound_account
85103 }
86- fn test_deployment (ref self : ContractState ) -> bool {
87- self . deployed. read ()
104+
105+
106+ /// @notice Registers a new user in the system.
107+ /// @dev This function assigns a unique ID to the user, stores their profile, and emits an
108+ /// event.
109+ /// @param self The contract state reference.
110+ /// @param username The unique username of the user.
111+ /// @param wallet_address The blockchain address of the user.
112+ /// @param role The role of the user (READER or WRITER).
113+ /// @param rank The rank/level of the user.
114+ /// @param metadata Additional metadata associated with the user.
115+ /// @return user_id The unique identifier assigned to the user.
116+ fn register_user (
117+ ref self : ContractState , username : felt252 , role : Role , rank : Rank , metadata : felt252
118+ ) -> u256 {
119+ // Ensure that the username is not empty.
120+ assert! (username != 0 , " User name cannot be empty" );
121+
122+ // Retrieve the current user ID before incrementing.
123+ let user_id = self . user_id. read ();
124+
125+ // Create a new user profile with provided details.
126+ let new_user = User {
127+ id : user_id ,
128+ username : username ,
129+ wallet_address : get_caller_address (), // Assign the caller's address.
130+ role : role ,
131+ rank : rank ,
132+ verified : false , // Default verification status is false.
133+ metadata : metadata
134+ };
135+
136+ // Store the new user in the users mapping.
137+ self . users. write (user_id , new_user );
138+
139+ // Increment the user ID counter for the next registration.
140+ self . current_account_id. write (user_id + 1 );
141+
142+ // Emit an event to notify about the new user registration.
143+ self . emit (UserCreated { id : user_id });
144+
145+ // Return the assigned user ID.
146+ user_id
147+ }
148+
149+
150+ /// @notice Verifies a user in the system.
151+ /// @dev Only an admin can verify a user.
152+ /// @param self The contract state reference.
153+ /// @param user_id The unique identifier of the user to be verified.
154+ /// @return bool Returns true if the user is successfully verified.
155+ fn verify_user (ref self : ContractState , user_id : u256 ) -> bool {
156+ let caller = get_caller_address ();
157+ // Ensure that only an admin can verify users.
158+ assert ((self . admin. read () == caller ), ' Only admin can verify users' );
159+ let mut user = self . users. read (user_id );
160+ user . verified = true ;
161+ self . users. write (user . id, user );
162+ true
163+ }
164+ /// @notice Retrieves a user's profile from the system.
165+ /// @dev This function fetches the user profile based on the provided user ID.
166+ /// @param self The contract state reference.
167+ /// @param user_id The unique identifier of the user whose profile is being retrieved.
168+ /// @return User The user profile associated with the given user ID.
169+ fn retrieve_user_profile (ref self : ContractState , user_id : u256 ) -> User {
170+ // Read the user profile from the storage mapping.
171+ let user = self . users. read (user_id );
172+
173+ // Return the retrieved user profile.
174+ user
175+ }
176+
177+ fn is_verified (ref self : ContractState , user_id : u256 ) -> bool {
178+ let mut user = self . users. read (user_id );
179+ user . verified
180+ }
181+
182+
183+ fn getAdmin (self : @ ContractState ) -> ContractAddress {
184+ let admin = self . admin. read ();
185+ admin
88186 }
89187 }
90188}
0 commit comments