Skip to content

Instantly share code, notes, and snippets.

@computerphysicslab
Created February 24, 2017 12:13
Show Gist options
  • Save computerphysicslab/56d46fb053e1b7652a18c1bba6832414 to your computer and use it in GitHub Desktop.
Save computerphysicslab/56d46fb053e1b7652a18c1bba6832414 to your computer and use it in GitHub Desktop.
UnwiredApp message storage Smart Contract v3.0
/*
UnwiredApp message storage Smart Contract v3.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); }
}
contract unwiredApp is mortal {
uint256 countMessage;
struct mssg {
uint256 idMessage;
uint256 idPrevious;
uint256 timestamp;
address from;
address to;
string message; // a JSON object containing the ciphered message readable by sender and all recipients
}
// Array of incoming messages with structure messages[to][idMessage]
mapping (address => mapping (uint256 => mssg)) messagesTo;
// Array of sent messages with structure messages[from][idMessage]
mapping (address => mapping (uint256 => mssg)) messagesFrom;
// Array of sent messages with structure messages[from][to][idMessage]
mapping (address => mapping (address => mapping (uint256 => mssg))) messagesFromTo;
// Array storing last message id for a given account
mapping (address => uint256) public lastIdMessageTo;
mapping (address => uint256) public lastIdMessageFrom;
mapping (address => mapping (address => uint256)) public lastIdMessageFromTo; // indexed by thread
// Events
event LogRefillFunds(address from, uint256 timestamp, uint256 amount);
event LogFaucetResult(address from, address to, string result, uint256 timestamp);
event LogSendMessage(address from, address to, uint256 timestamp, string message);
event LogReadMessage(address from, address to, uint256 timestamp, string message);
/// @notice Read a given message specified by id whose recipient is sender
function readYourMessageById(uint256 idMessage) constant returns (string message) {
return messagesTo[msg.sender][idMessage].message;
}
/// @notice Read a given message specified by id sent by user
function readSentMessageById(uint256 idMessage) constant returns (string message) {
return messagesFrom[msg.sender][idMessage].message;
}
/// @notice Read metadata of a message specified by id whose recipient is sender
function readYourMessageMetadataById(uint256 idMessage) constant returns (uint256 _idMessage, uint256 _idPrevious, uint256 _timestamp, address _from, address _to, string _message) {
return (messagesTo[msg.sender][idMessage].idMessage, messagesTo[msg.sender][idMessage].idPrevious, messagesTo[msg.sender][idMessage].timestamp, messagesTo[msg.sender][idMessage].from, messagesTo[msg.sender][idMessage].to, messagesTo[msg.sender][idMessage].message);
}
/// @notice Read metadata of a message specified by id and sent by user
function readSentMessageMetadataById(uint256 idMessage) constant returns (uint256 _idMessage, uint256 _idPrevious, uint256 _timestamp, address _from, address _to, string _message) {
return (messagesFrom[msg.sender][idMessage].idMessage, messagesFrom[msg.sender][idMessage].idPrevious, messagesFrom[msg.sender][idMessage].timestamp, messagesFrom[msg.sender][idMessage].from, messagesFrom[msg.sender][idMessage].to, messagesFrom[msg.sender][idMessage].message);
}
/// @notice Read metadata of a message specified by thread (from, to) and id
function readThreadMessageMetadataById(address to, uint256 idMessage) constant returns (uint256 _idMessage, uint256 _idPrevious, uint256 _timestamp, address _from, address _to, string _message) {
LogReadMessage(messagesFromTo[msg.sender][to][idMessage].from, messagesFromTo[msg.sender][to][idMessage].to, messagesFromTo[msg.sender][to][idMessage].timestamp, messagesFromTo[msg.sender][to][idMessage].message); // Event Log
return (messagesFromTo[msg.sender][to][idMessage].idMessage, messagesFromTo[msg.sender][to][idMessage].idPrevious, messagesFromTo[msg.sender][to][idMessage].timestamp, messagesFromTo[msg.sender][to][idMessage].from, messagesFromTo[msg.sender][to][idMessage].to, messagesFromTo[msg.sender][to][idMessage].message);
}
/// @notice Read last message whose recipient is sender
function readYourLastMessage() constant returns (string message) {
return readYourMessageById(lastIdMessageTo[msg.sender]);
}
/// @notice Read last message sent by user
function readSentLastMessage() constant returns (string message) {
return readSentMessageById(lastIdMessageFrom[msg.sender]);
}
/// @notice Record new message from sender
/// @param to The address of the message recipient
/// @param message The message sent, or JSON-encapsulated object holding publicKeys and ciphered messages
function sendMessage(address to, string message) {
if (countMessage == 0) countMessage = 1; // Set init countMessage to 1
messagesTo[to][countMessage].idMessage = countMessage;
messagesTo[to][countMessage].idPrevious = lastIdMessageTo[to];
messagesTo[to][countMessage].timestamp = timestamp();
messagesTo[to][countMessage].from = msg.sender;
messagesTo[to][countMessage].to = to;
messagesTo[to][countMessage].message = message;
messagesFrom[msg.sender][countMessage].idMessage = countMessage;
messagesFrom[msg.sender][countMessage].idPrevious = lastIdMessageFrom[msg.sender];
messagesFrom[msg.sender][countMessage].timestamp = timestamp();
messagesFrom[msg.sender][countMessage].from = msg.sender;
messagesFrom[msg.sender][countMessage].to = to;
messagesFrom[msg.sender][countMessage].message = message;
messagesFromTo[msg.sender][to][countMessage].idMessage = countMessage;
messagesFromTo[msg.sender][to][countMessage].idPrevious = lastIdMessageFromTo[msg.sender][to];
messagesFromTo[msg.sender][to][countMessage].timestamp = timestamp();
messagesFromTo[msg.sender][to][countMessage].from = msg.sender;
messagesFromTo[msg.sender][to][countMessage].to = to;
messagesFromTo[msg.sender][to][countMessage].message = message;
messagesFromTo[to][msg.sender][countMessage].idMessage = countMessage;
messagesFromTo[to][msg.sender][countMessage].idPrevious = lastIdMessageFromTo[to][msg.sender];
messagesFromTo[to][msg.sender][countMessage].timestamp = timestamp();
messagesFromTo[to][msg.sender][countMessage].from = msg.sender;
messagesFromTo[to][msg.sender][countMessage].to = to;
messagesFromTo[to][msg.sender][countMessage].message = message;
lastIdMessageTo[to] = countMessage;
lastIdMessageFrom[msg.sender] = countMessage;
lastIdMessageFromTo[msg.sender][to] = countMessage;
lastIdMessageFromTo[to][msg.sender] = countMessage; // symmetrical
countMessage++;
LogSendMessage(msg.sender, to, timestamp(), message); // Event log
}
/// @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;
}
/// @notice Refill SC funds to feed faucet requests
function refillFunds() payable returns (uint256) {
LogRefillFunds(msg.sender, timestamp(), msg.value); // Event log
return 1;
}
/// @notice Faucet: Send funds, 1 Ether, to a new contact/friend
/// @param to The address of the friend recipient of the funds
function sendFundsToFriend(address to) returns (bool) {
if (to.balance >= 1000000000000000000) {
LogFaucetResult(msg.sender, to, 'Friends balance higher than 1 Ether', timestamp()); // Event log
return false; // If friend has enough funds (> 1 Ether), do not send anymore
} else if (this.balance < 1000000000000000000) {
LogFaucetResult(msg.sender, to, 'Faucets balance lower than 1 Ether', timestamp()); // Event log
return false; // If SC funds are not enough(< 1 Ether), do not send funds
} else {
if (!to.send(1000000000000000000)) { // Withdrawal Pattern to send Ethers. Prevents re-entrancy attacks when recording account balances
LogFaucetResult(msg.sender, to, 'Failed attempt to send 1 Ether to friends account', timestamp()); // Event log
return false;
} else {
LogFaucetResult(msg.sender, to, 'Sent 1 Ether to friends account', timestamp()); // Event log
return true;
}
}
}
/// @notice Query contract balance
function myBalance() constant returns (uint256) {
return this.balance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment