Created
April 26, 2017 10:27
-
-
Save computerphysicslab/5a32910873a36ad4d30aa849a8e5bf20 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
/* | |
ETNO personal data manager Smart Contract v1.0 | |
developed by: | |
MarketPay.io , 2017 | |
https://www.unwiredapp.com | |
http://lnked.in/blockchain | |
*/ | |
pragma solidity ^0.4.6; | |
contract Mortal { | |
address owner; | |
function Mortal() { owner = msg.sender; } | |
function kill() { if (msg.sender == owner) suicide(owner); } | |
/// @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; | |
} | |
} | |
contract DAppFunding is Mortal { | |
// **** EVENTS | |
// A generic error log | |
event Error(string error); | |
// Triggered when the owner refills Smart Contract funds | |
event RefillFunds(address from, uint256 timestamp, uint256 amount); | |
// Triggered when a user receives funds from the Smart Contract | |
event SendMeFunds(address to, uint256 timestamp); | |
// **** METHODS | |
/// @notice Refill SC funds to feed faucet requests | |
function refillFunds() payable returns (uint256) { | |
RefillFunds(msg.sender, timestamp(), msg.value); // Event log | |
return 1; | |
} | |
/// @notice Faucet: Send funds, 1 Ether, to the caller | |
function sendMeFunds() returns (bool) { | |
if (msg.sender.balance >= 1000000000000000000) { | |
Error('sendMeFunds: The caller account balance is higher than 1 Ether'); | |
return false; // If you already have enough funds (> 1 Ether), do not send anymore | |
} else if (this.balance < 1000000000000000000) { | |
Error('sendMeFunds: Faucets balance lower than 1 Ether'); | |
return false; // If SC funds are not enough(< 1 Ether), do not send funds | |
} else { | |
if (!msg.sender.send(1000000000000000000)) { // Withdrawal Pattern to send Ethers. Prevents re-entrancy attacks when recording account balances | |
Error('sendMeFunds: Failed attempt to send 1 Ether to user account'); | |
return false; | |
} else { | |
SendMeFunds(msg.sender, timestamp()); // Event log | |
return true; | |
} | |
} | |
} | |
/// @notice Query contract balance | |
function myBalance() constant returns (uint256) { | |
return this.balance; | |
} | |
} | |
contract PersonalDataManagerInterface is DAppFunding { | |
// **** DATA | |
mapping (address => uint40) phoneNumbers; | |
mapping (uint40 => address) phoneNumbersInverse; | |
// (operatorAddress, alertMessage) = requests[phoneNumber] | |
struct rqs { | |
address operatorAddress; | |
string alertMessage; | |
} | |
mapping (uint40 => rqs) requests; | |
// (operatorAddress, personalData) = personalDatas[phoneNumber] | |
struct pd { | |
address operatorAddress; | |
string personalData; | |
} | |
mapping (uint40 => pd) personalDatas; | |
// **** METHODS | |
// User signs up, registering its mobile phone number | |
function signUp(uint40 phoneNumber); | |
// User checks whether there is any alert pending to be retrieved | |
function getLastRequest() constant returns (address operatorAddress, string alertMessage); | |
// User sends personal data to a given operator | |
function sendPersonalData(address _operatorAddress, string _personalData) returns (bool success); | |
// Operator checks whether a given user has been registered | |
function hasSignedUp(uint40 phoneNumber) constant returns (bool signedUp); | |
// Operator asks for personal data to a given user | |
function requestPersonalData(uint40 _phoneNumber, string _alertMessage); | |
// Operator reads the personal data sent by a user after requested | |
function readPersonalDataFrom(uint40 phoneNumber) constant returns (string personalData); | |
// **** EVENTS | |
// Triggered when a new user signs up from its DApp | |
event SignUp(uint40 phoneNumber, address who, uint256 timestamp); | |
// Triggered when a user queries last data request | |
event GetLastRequest(uint40 phoneNumber, address operatorAddress, string alertMessage); | |
// Triggered when a user sends its personal data to an operator | |
event SendPersonalData(address operatorAddress, string personalData); | |
// Triggered when an operator requests personal data to a user | |
event RequestPersonalData(uint40 phoneNumber, string alertMessage, address operatorAddress); | |
// Triggered when an operator requests personal data sent by user to the smart contract | |
event ReadPersonalDataFrom(uint40 phoneNumber, address operatorAddress, string personalData); | |
// Triggered when successfully sent funds to caller | |
event SendMeFunds(address recipient, uint256 timestamp); | |
} | |
contract PersonalDataManager is PersonalDataManagerInterface { | |
// **** FUNCTIONS | |
function signUp(uint40 phoneNumber) { | |
phoneNumbers[msg.sender] = phoneNumber; | |
phoneNumbersInverse[phoneNumber] = msg.sender; | |
SignUp(phoneNumber, msg.sender, timestamp()); // Event log | |
} | |
function getLastRequest() constant returns (address operatorAddress, string alertMessage) { | |
uint40 phoneNumber = phoneNumbers[msg.sender]; | |
if (phoneNumber == 0) { | |
Error("getLastRequest: User has not signed up"); | |
return(0, ''); | |
} | |
GetLastRequest(phoneNumber, requests[phoneNumber].operatorAddress, requests[phoneNumber].alertMessage); // Event log | |
return (requests[phoneNumber].operatorAddress, requests[phoneNumber].alertMessage); | |
} | |
function sendPersonalData(address _operatorAddress, string _personalData) returns (bool success) { | |
uint40 phoneNumber = phoneNumbers[msg.sender]; | |
if (phoneNumber == 0) { | |
Error("sendPersonalData: User has not signed up"); | |
return(false); | |
} | |
personalDatas[phoneNumber].operatorAddress = _operatorAddress; | |
personalDatas[phoneNumber].personalData = _personalData; | |
SendPersonalData(_operatorAddress, _personalData); // Event log | |
return(true); | |
} | |
function hasSignedUp(uint40 phoneNumber) constant returns (bool signedUp) { | |
if (phoneNumbersInverse[phoneNumber] == address(0)) { // check if the address is not set | |
return(false); | |
} else { | |
return(true); | |
} | |
} | |
function requestPersonalData(uint40 _phoneNumber, string _alertMessage) { | |
requests[_phoneNumber].operatorAddress = msg.sender; | |
requests[_phoneNumber].alertMessage = _alertMessage; | |
RequestPersonalData(_phoneNumber, _alertMessage, msg.sender); // Event Log | |
} | |
function readPersonalDataFrom(uint40 phoneNumber) constant returns (string personalData) { | |
if (msg.sender != personalDatas[phoneNumber].operatorAddress) { | |
Error("readPersonalDataFrom: Your account is not allowed to read this information"); | |
return(''); | |
} | |
/*if (personalDatas[phoneNumber].personalData == '') { | |
Error("readPersonalDataFrom: Personal data seems to be void"); | |
return(''); | |
}*/ | |
ReadPersonalDataFrom(phoneNumber, msg.sender, personalDatas[phoneNumber].personalData); // Event Log | |
return(personalDatas[phoneNumber].personalData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment