Skip to content

Instantly share code, notes, and snippets.

@thomasvds
Created April 2, 2018 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasvds/db99b1f9537bbfca0ef3814a951d6123 to your computer and use it in GitHub Desktop.
Save thomasvds/db99b1f9537bbfca0ef3814a951d6123 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.21;
contract InsuranceLedger {
// Event to broadcast to the network when a new motorbike has been added
event OnRecordMotorbike(bytes32 _hashedChassisNumber);
// The address of the contract owner (Seraphin), set when the contract is deployed
address public seraphinAddress = msg.sender;
// The mapping with all insured motorbikes - we stick to an extremely simple
// model representation with only the hashed chassis number. A given hashed
// chassis number is either insured or not, that is, true or false in this
// mapping.
// Note that calling Insurances[nonExistingEntry] returns false, which is what
// we need in our case (only recorded motorbikes are considered to be insured).
// Using Structs, we could easily extend this mapping to add datapoints such
// as licence plate, insurance policy subscriber, dates of insurance coverage etc.
// Note that bytes32 = 1 byte * 32 = 8 bits * 32 = 256 bits, which is the
// length of the SHA3-256 hash we use to encrypt the motorbike chassis number.
mapping (bytes32 => bool) public Insurances;
// Function modifier, added to a function to ensure only Seraphin can call this
// function (in a broader ecosystem, we'd have something like 'onlyByInsurers')
modifier onlyBySeraphin {
require(msg.sender == seraphinAddress);
_;
}
// WRITE function to record a new motorbike on the Blockchain, callable only
// by Seraphin for now. If calling the function from Remix, don't forget to
// preceed the SHA3-256 hash with an 0x flag to let the contract know that
// the hash is already in the correct hexadecimal format.
function recordMotorbike(bytes32 _hashedChassisNumber) public onlyBySeraphin {
Insurances[_hashedChassisNumber] = true;
emit OnRecordMotorbike(_hashedChassisNumber);
}
// READ function to check if a given motorbike is insured - callable by anyone.
// Similarly to the write function, one needs to call this function with an
// 0x flag preceding the input parameter to reflect its hexadecimal format.
function insured(bytes32 _hashedChassisNumber) public constant returns (bool){
return Insurances[_hashedChassisNumber];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment