"User Contract" Example
In this example, i will be using a persons detail and creating a signature (hash value) from it.
the hash value will be unique to each user given the assumption that there are no 2 people with the same details except TWINS.
Things learned:
The Ethereum Virtual Machine has three areas where it can store items. “memory”, this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.
The first is “storage”, where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls and quite expensive to use.
The second is “memory”, this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.
The third one is the stack, which is used to hold small local variables. It is almost free to use, but can only hold a limited amount of values.
the hash value will be unique to each user given the assumption that there are no 2 people with the same details except TWINS.
Things learned:
The Ethereum Virtual Machine has three areas where it can store items. “memory”, this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.
pragma solidity ^0.4.0;
contract UserAresh{
//Declare a struct similar to C++
struct Info{
string firstname;
string lastname;
string maiden;
bytes32 birthday;
}
//Events
event Log(string firstname, string lastname);
//Create a mapping array
mapping(bytes32 => Info) UserList;
//whos the Creator. The Value is the Hex value of the account that consumes GAS/ETHER
address creator;
//this is a constructor, notice it has the same name as the Contract name
function UserAresh() public {
creator = msg.sender;
}
//modifier
modifier onlyBy(address _account)
{
require(msg.sender == _account);
// Do not forget the "_;"! It will
// be replaced by the actual function
// body when the modifier is used.
_;
}
//register the user by using a unique hash based on the composition of users details
function RegisterUser(string _firstname, string _lastname, string _maiden, string _bday) public returns (bytes32)
{
bytes32 bdayBytes = strToBytes(_bday);
Info memory uinfo = Info(_firstname, _lastname, _maiden, bdayBytes);
// unique by user information composition
bytes32 hash = keccak256(_firstname, _lastname, _maiden, bdayBytes);
UserList[hash] = uinfo;
return hash;
}
function GetUser(bytes32 _hash) public constant returns (string _firstname, string _lastname, string _maiden, bytes32 _bday) {
Info memory uinfo = getUser(_hash);
_firstname = uinfo.firstname;
_lastname = uinfo.lastname;
_maiden = uinfo.maiden;
_bday = uinfo.birthday;
}
function getUser(bytes32 hash) private constant returns (Info) {
Info memory uinfo = UserList[hash];
if (uinfo.birthday == 0)
revert();
return uinfo;
}
function isExists(bytes32 hash) constant returns (bool) {
Info memory uinfo = UserList[hash];
if (uinfo.birthday == 0)
return false;
return true;
}
//Manually comparing strings function used as a utility function
//source https://github.com/ethereum/dapp-bin/blob/master/library/stringUtils.sol
//View This is generally the replacement for constant. It indicates that the function will not alter the storage state in any way.
//Pure This is even more restrictive, indicating that it won't even read the storage state.
function stringsEqual(string storage _a, string memory _b) internal view returns (bool) {
bytes storage a = bytes(_a);
bytes memory b = bytes(_b);
if (a.length != b.length)
return false;
// @todo unroll this loop
for (uint i = 0; i < a.length; i ++)
if (a[i] != b[i])
return false;
return true;
}
function strToBytes(string str) constant private returns (bytes32 ret) {
// var g = bytes(str).length;
// if (bytes(str).length > 32) throw;
assembly {
ret := mload(add(str, 32))
}
}
}
The first is “storage”, where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls and quite expensive to use.
The second is “memory”, this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.
The third one is the stack, which is used to hold small local variables. It is almost free to use, but can only hold a limited amount of values.
Updated 11/1/2017
Hash function of a struct
Comments
Post a Comment