0

I am working with django project that is empowered with blockchain. I want to create an authentication system using blockchain technology. I write a smart contract and deploy it in ganache. In here I want to develop the registration access metamask cetain account. How I implement it. It means solidity msg.sender command should want to idintify the certain metamask account which user used.

Smart contract

pragma solidity ^0.4.6;

contract Auth{
    struct UserDetails {
        uint id;
        address UserAddress;
        string name;
        string email;
        string password;
        // string ipfsImageHash;
        bool isUserLoggedIn;
    }

    mapping(address => UserDetails) User;
    UserDetails[] public User_arr;
  
    address _address=msg.sender;

    constructor() public {
        
    }
    event RegNewUser(uint _id,string _name, string _email,string _password);
    event LogInUser(string _name, string _password);

    
    //get_address
    function get_address() public view returns(address){
        return _address;
    }
    // User registration function

    function registerUser(
        string memory _name,
        string memory _email,
        string memory _password
        // string memory _ipfsImageHash
    ) public returns (bool) {
        
        // require(User[_address].UserAddress != msg.sender);

        User[_address].UserAddress = _address;
        User[_address].name = _name;
        User[_address].email=_email;
        User[_address].password = _password;
        // User[_address].ipfsImageHash = _ipfsImageHash;
        User[_address].isUserLoggedIn = false;
        User[_address].id=User_arr.length;

        emit RegNewUser(
        User[_address].id,
        User[_address].name,
        User[_address].email,
        User[_address].password
        
        );
       User_arr.push(User[_address]);
        return true;
    }

Python file

import sys
from .auth_contract_connection import contract_address,abi
sys.path.append('../')
from connection import connection


auth_contract = connection.con.eth.contract(address = contract_address, abi = abi)
# addr=auth_contract.functions.get_address().call()
# print("address:",addr)
print(connection.con.eth.accounts[0])
# print(connection.con.eth.accounts[0])
def execTxn(txName,*args,**kwargs):
    # print(*args )
    return_value=None
    nonce =connection.con.eth.getTransactionCount(connection.wallet_address)
    print(nonce)
    buildData = {
        'from':connection.con.eth.accounts[1],
        'chainId': 4,
        'gas': 400000,
        'gasPrice': connection.con.toWei('40', 'gwei'),
        'nonce': nonce,
    }
    
    

    try:
        if (txName == 'registerUser'):
            txn_dict = auth_contract.functions.registerUser(*args).buildTransaction(buildData)
            return_value=auth_contract.functions.registerUser(*args).call()
            print(txn_dict)
    
   except Exception as e:
           print(e)
       if return_value!=None:
           return return_value
    

1 Answer 1

0

You set _address to msg.sender when you define it. Therefore the value is set on deployment of the contract to the deployer and after that never updated.

If you want to have the msg.sender of the call to registerUser you need to read msg.sender inside this function.

A possible change could look this way:

    function registerUser(
        string memory _name,
        string memory _email,
        string memory _password
        // string memory _ipfsImageHash
    ) public returns (bool) {
        
        // Get account that called this method
        address _address = msg.sender;

        // Check that the account is not already registered
        require(User[_address].UserAddress == address(0));

        User[_address].UserAddress = _address;
        User[_address].name = _name;
        User[_address].email=_email;
        User[_address].password = _password;
        // User[_address].ipfsImageHash = _ipfsImageHash;
        User[_address].isUserLoggedIn = false;
        User[_address].id=User_arr.length;

        emit RegNewUser(
            User[_address].id,
            User[_address].name,
            User[_address].email,
            User[_address].password
        );
        
        User_arr.push(User[_address]);
        return true;
    }
3
  • yeah, that's right but metamask account does not recognize the system. That is a major problem? Commented Jan 5, 2022 at 8:37
  • You mean MetaMask is not decoding the transaction details?
    – Richard
    Commented Jan 5, 2022 at 8:42
  • @Richard Did you solve this problem?
    – bushytop
    Commented Dec 7, 2022 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.