Skip to content

Smart contract built in Solidity for decentralized user profile management with registration, updates, and retrieval functionality

License

Notifications You must be signed in to change notification settings

YuliaYa2025/User_profile_registry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

UserProfileRegistry Smart Contract

A decentralized user profile management system built with Solidity that allows users to register, update, and retrieve their profiles on the Ethereum blockchain.

πŸ“‹ Table of Contents

πŸ” Overview

The UserProfileRegistry contract provides a simple and secure way to store user profile information on the blockchain. Each Ethereum address can register one unique profile containing personal information such as name, email, age, and a custom user ID.

✨ Features

  • User Registration: Register a new user profile with personal details
  • Profile Updates: Modify existing profile information
  • Profile Retrieval: Get profile data for any registered user
  • Registration Verification: Check if a user is registered
  • Event Logging: Track registration and update activities
  • Access Control: Users can only update their own profiles

πŸ—οΈ Contract Structure

UserProfile Struct

struct UserProfile {
    uint256 userId;                    // Custom user identifier
    uint256 age;                      // User's age
    string name;                      // User's name
    string email;                     // User's email address
    uint256 registrationTimestamp;    // When the profile was created
    bool isRegistered;                // Registration status
}

State Variables

  • mapping(address => UserProfile) public profiles - Stores user profiles
  • mapping(address => bool) public isRegistered - Tracks registration status

πŸ“ Functions

Core Functions

registerUser()

function registerUser(string memory _name, uint256 _userId, uint256 _age, string memory _email) public

Registers a new user profile. Each address can only register once.

Parameters:

  • _name: User's full name
  • _userId: Custom user identifier
  • _age: User's age
  • _email: User's email address

Requirements:

  • User must not be already registered

updateProfile()

function updateProfile(string memory _name, uint256 _userId, uint256 _age, string memory _email) public

Updates an existing user profile.

Parameters:

  • Same as registerUser()

Requirements:

  • User must be registered

getProfile()

function getProfile(address _user) external view returns (uint256, string memory, string memory, uint256, uint256)

Retrieves complete profile information for a given address.

Returns:

  • User ID, name, email, age, registration timestamp

fetchUserInfo()

function fetchUserInfo(address _user) external view returns (string memory, string memory, uint256, uint256)

Alternative getter function for basic profile information.

Returns:

  • Name, email, age, user ID

Utility Functions

isUserRegistered()

function isUserRegistered(address _user) external view returns (bool)

Checks if a user is registered.

getUserCount()

function getUserCount() external view returns (uint256)

Returns total number of registered users (placeholder implementation).

πŸ“’ Events

UserRegistered

event UserRegistered(address indexed user, string name, uint256 timestamp)

Emitted when a new user registers.

UserUpdated

event UserUpdated(address indexed user, string name)

Emitted when a user updates their profile.

πŸš€ Getting Started

Prerequisites

  • Remix IDE or local development environment
  • MetaMask or similar Ethereum wallet
  • Test ETH for deployment (Sepolia, Goerli, or local testnet)

Deployment

  1. Using Remix:

    • Open Remix IDE
    • Create a new file UserProfileRegistry.sol
    • Paste the contract code
    • Compile with Solidity ^0.8.17
    • Deploy using Injected Provider (MetaMask)
  2. Using Hardhat/Truffle:

    # Install dependencies
    npm install
    
    # Compile contracts
    npx hardhat compile
    
    # Deploy to testnet
    npx hardhat run scripts/deploy.js --network sepolia

πŸ’‘ Usage Examples

JavaScript (Web3.js)

// Register a new user
await contract.methods.registerUser(
    "John Doe",
    12345,
    25,
    "john@example.com"
).send({ from: userAddress });

// Update profile
await contract.methods.updateProfile(
    "John Smith",
    12345,
    26,
    "johnsmith@example.com"
).send({ from: userAddress });

// Get profile data
const profile = await contract.methods.getProfile(userAddress).call();
console.log(profile);

Solidity Integration

// Import and use in another contract
import "./UserProfileRegistry.sol";

contract MyDApp {
    UserProfileRegistry registry;
    
    constructor(address _registryAddress) {
        registry = UserProfileRegistry(_registryAddress);
    }
    
    function checkUser(address _user) public view returns (bool) {
        return registry.isUserRegistered(_user);
    }
}

πŸ”’ Security Considerations

  • Access Control: Users can only update their own profiles
  • Input Validation: Consider adding validation for email format and name length
  • Reentrancy: Current functions are safe from reentrancy attacks
  • Gas Optimization: Profile updates could be optimized to only change specific fields

Recommended Improvements

  • Add input validation (email format, name length limits)
  • Implement profile deletion functionality
  • Add admin controls for emergency situations
  • Consider using OpenZeppelin's security patterns

πŸ§ͺ Testing

Test the contract thoroughly before mainnet deployment:

// Test registration
it("should register a new user", async () => {
    await registry.registerUser("Test User", 1, 25, "test@example.com");
    const isRegistered = await registry.isUserRegistered(accounts[0]);
    assert.equal(isRegistered, true);
});

// Test duplicate registration prevention
it("should prevent duplicate registration", async () => {
    await registry.registerUser("Test User", 1, 25, "test@example.com");
    await expectRevert(
        registry.registerUser("Test User 2", 2, 30, "test2@example.com"),
        "User already registered"
    );
});

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ž Support

If you have questions or need help:


⚠️ Disclaimer: This contract is for educational purposes. Perform thorough testing and security audits before using in production.

About

Smart contract built in Solidity for decentralized user profile management with registration, updates, and retrieval functionality

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors