A generic "Claim and Endorse" Ethereum Smart Contract

Words
244
Reading
2 min
Listen
Play
10y

Did you already endorse someone at LinkedIn? For instance, someone claims that he knows C++ and you endorse this claim because you know it’s true. A large number of processes can be modelled in this way.

A simple Solidity contract for managing claims and endorsements could look like below.

contract ClaimAndEndorse {
  struct ENDORSEMENT {
  uint creationTime;
 }
 
 struct CLAIM {
  uint creationTime;
  uint claimHash;
  mapping (address => ENDORSEMENT) endorsements;
 }
 
 mapping (address => 
  mapping (uint /* CLAIM GUID */ => CLAIM)) claims;
 
 function setClaim(uint claimGuid, uint claimHash) {
  CLAIM c = claims[msg.sender][claimGuid];
  if(c.claimHash > 0) throw; // unset first!
  c.creationTime = now;
  c.claimHash = claimHash;
 }
 
 function unsetClaim(uint claimGuid) {
  delete claims[msg.sender][claimGuid];
 }
 
 function setEndorsement(
  address claimer, uint claimGuid, uint expectedClaimHash
 ) {
  CLAIM c = claims[claimer][claimGuid];
  if(c.claimHash != expectedClaimHash) throw;
  ENDORSEMENT e = c.endorsements[msg.sender];
  e.creationTime = now;
 }
 
 function unsetEndorsement(address claimer, uint claimGuid) {
  delete claims[claimer][claimGuid]
          .endorsements[msg.sender];
 }
 
 function checkClaim(
  address claimer, uint claimGuid, uint expectedClaimHash
 ) constant returns (bool) {
  return claims[claimer][claimGuid].claimHash 
         == expectedClaimHash;
 }
 
 function checkEndorsement(
  address claimer, uint claimGuid, address endorsedBy
 ) constant returns (bool) {
  return claims[claimer][claimGuid]
   .endorsements[endorsedBy].creationTime > 0;
 }
}

The fantastic thing about this very simple contract is that we now can answer the following question:

Who claims what and who endorses it?

...

Read the full article at blockchainers.org.

A generic "Claim and Endorse" Ethereum Smart Contract | Ecency