人身份确认合同

时间:2018-01-25 15:50:47

标签: ethereum solidity

我正在为身份识别人创建合同,我需要验证是否有一些合同具有相同的地址,电子邮件或电话号码。

示例:

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;
}
}

我可以选择将地址合同保存在使用关键电子邮件或电话的映射中。

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

有这个解决方案,但我相信它并不是更好,因为它的映射会非常大而且合同也很昂贵。

有人知道另一种解决方案吗?

1 个答案:

答案 0 :(得分:1)

您不应该使用合同来代表您尝试在此处执行的对象。由于合同部署通常比交易昂贵得多,因此不仅成本高昂,而且您也无法保证其独特性。

您应该使用struct代表个人。

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;
  }
  ...
}

现在,您的合同是所有Person的数据存储空间。您可以部署此版本并将合同地址传递给需要访问它的任何合同。如果您需要允许多个业务逻辑合同使用它,或者您需要升级业务合同,您还可以集中所有数据。

编辑 - 我应该注意,如果这是自己的合同,则必须从string更改为bytes32。你不能在合同之间发送字符串。