Last active
September 15, 2017 13:59
-
-
Save computerphysicslab/8d6d5f8849931c38d5976bba4125dea7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Watafan-issuers-1.1.sol | |
/* | |
Watafan issuers Smart Contract v1.1 | |
developed by: | |
MarketPay.io , 2017 | |
https://marketpay.io/ | |
http://lnked.in/blockchain | |
v1.0 https://gist.github.com/computerphysicslab/5ef16811325fd74fc482bc556b3b9d86 | |
+ Owner grants new issuers and revokes them too | |
+ This is a mortal contract | |
v1.1 https://gist.github.com/computerphysicslab/8d6d5f8849931c38d5976bba4125dea7 | |
+ Indexed issuers | |
+ Query endpoint for full scan of issuers | |
*/ | |
pragma solidity ^0.4.6; | |
/* | |
* @title Mortal | |
* | |
* Abstract contract that allows children to implement the functionality of killing the contract and other generic functions. | |
* | |
*/ | |
contract Mortal { | |
address owner; | |
// @notice Constructor sets owner | |
function Mortal() { owner = msg.sender; } | |
// @notice For debugging purposes when using solidity online browser | |
function whoAmI() constant returns (address) { | |
return msg.sender; | |
} | |
// @notice Get the current timestamp from last mined block | |
function timestamp() constant returns (uint256) { | |
return block.timestamp; | |
} | |
// **** EVENTS | |
// @notice A generic error log | |
event Error(string error); | |
// @notice Triggered when owner kills the contract | |
event Kill(address killer, uint256 _timestamp); | |
// **** MORE FUNCTIONS | |
// @notice To limit functions usage to contract owner | |
modifier onlyOwner() { | |
if (msg.sender != owner) { | |
Error('Mortal: onlyOwner function called by user that is not owner'); | |
} else { | |
_; | |
} | |
} | |
// @notice To kill the contract | |
function kill() onlyOwner { | |
Kill(msg.sender, timestamp()); | |
suicide(owner); | |
} | |
} | |
contract Issuers is Mortal { | |
// **** DATA | |
struct issu { | |
uint256 issuerId; | |
bool issuerAuth; | |
address issuerAddress; | |
string issuerContent; // a JSON object containing data about the issuer | |
} | |
mapping (address => issu) issuerData; | |
mapping (uint256 => address) issuerAddressById; // indexed issuers so as to be full scannable | |
uint256 lastId; | |
// **** METHODS | |
// Checks whether a given user is an authorized issuer | |
function isIssuer(address _issuer) constant returns (bool) { | |
return (issuerData[_issuer].issuerAuth); | |
} | |
function newIssuer(address _issuer, string _content) internal onlyOwner returns (uint256 id) { | |
// Update Index | |
id = lastId + 1; | |
issuerData[_issuer].issuerId = id; | |
issuerData[_issuer].issuerAuth = false; | |
issuerData[_issuer].issuerAddress = _issuer; | |
issuerData[_issuer].issuerContent = _content; | |
issuerAddressById[id] = _issuer; | |
lastId = lastId + 1; | |
NewIssuer(_issuer, id, timestamp()); // Event log | |
} | |
function grantIssuer(address _issuer, string _content) external onlyOwner { | |
// Checks whether this user has been previously added as an issuer | |
uint256 id; | |
if (issuerData[_issuer].issuerId > 0) { | |
id = issuerData[_issuer].issuerId; | |
} else { | |
id = newIssuer(_issuer, _content); | |
} | |
issuerData[_issuer].issuerAuth = true; | |
GrantIssuer(_issuer, id, timestamp()); // Event log | |
} | |
function revokeIssuer(address _issuer) external onlyOwner { | |
issuerData[_issuer].issuerAuth = false; | |
RevokeIssuer(_issuer, timestamp()); // Event log | |
} | |
// Queries the issuer, knowing the id | |
function getIssuerById(uint256 id) constant returns (uint256 _issuerId, bool _issuerAuth, address _issuerAddress, string _issuerContent) { | |
address _issuer = issuerAddressById[id]; | |
return (issuerData[_issuer].issuerId, issuerData[_issuer].issuerAuth, issuerData[_issuer].issuerAddress, issuerData[_issuer].issuerContent); | |
} | |
// **** EVENTS | |
// Triggered when a new issuer is created | |
event NewIssuer(address who, uint256 _id, uint256 _timestamp); | |
// Triggered when a user is granted to become an issuer | |
event GrantIssuer(address who, uint256 _id, uint256 _timestamp); | |
// Triggered when a user is revoked for being an issuer | |
event RevokeIssuer(address who, uint256 _timestamp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment