Search code examples
ethereumsolidity

Contract for Person Identification


I am creating a contract for identification person, and I need to verify if there are some contracts with the same address, email, or phone number.

Example:

contract Person {
//date of create
uint public dateCreate;

//name of person
string public name;

//variables to be validates
string public email;

string public phone;

// constructor
function Person(string _name, string _email, string _phone) public {
    name = _name;
    email = _email;
    phone = _phone;
    owner = msg.sender;
}
}

I have the option to save the address contract in a mapping with the key email or phone.

contract RegisterPerson {
  //save the contract address person using the key of the email
  mapping(bytes32=>address) public addressEmail;
}

There is this solution, but I believe it's not the better because the mapping it will be very big and the contract expensive.

Does anybody know another solution?


Solution

  • You shouldn't be using a contract to represent an object like you're attempting to do here. Not only is it very costly as contract deployments are usually much more expensive than transactions, but you also can't guarantee uniqueness.

    You should use a struct to represent the individual.

    contract PersonStorage {
      struct Person {
        uint dateCreate;
        string name;
        string email;
        string phone;
      }
    
      mapping(bytes32 => Person) persons;
    
      function addPerson(string _name, string _email, string _phone) public {
        Person memory person;
    
        person.name = _name;
        person.email = _email;
        person.phone = _phone;
    
        persons[keccak256(person.email)] = person;
      }
      ...
    }
    

    Now, your contract is data storage for all Persons. You can deploy this version and pass the contract address to whatever contracts need access to it. You'll also have all your data centralized in case you need to allow multiple business logic contracts to use it or if you need to upgrade your business contract.

    EDIT - I should note that if this is in its own contract, you'll have to change from string to bytes32. You can't send strings between contracts.