Message in an Ethereum nutshell

Simplicity and security

Find below a simple contract in order to implement a messaging service. There is no security implemented since this shall run with a private blockchain and optionally on a Virtual Private Network (VPN).  This way the contract remains simple, this is a proof of concept only anyway, and in times of Distributed Denial of Service (DDoS) with Internet of Things (IOT) devices  we would rather secure the transport layer.

So, with more and more devices connected to the internet a decentralization of networks makes sense.  We plan to use the public Ethereum blockchain only for registration and payment purposes. Once a token for usage has beed issued messaging users move on to a private backbone.

Messenger (Solidity contract)

you-have-a-message-2

You can run the contract as is. On creation a “Hello World” message is delivered to the contract owner. For every message sent an event is triggered.

contract messenger {
struct Message {
address sender;
string text;
}
struct Messages {
uint32 counter;
mapping (uint32 => Message) My_Messages;
}
mapping (address => Messages) All_Messages;
event Message_sent(address _receiver);
function messenger () {
All_Messages[msg.sender].counter= 1;
All_Messages[msg.sender].My_Messages[1].sender= msg.sender;
// send first message to creator
All_Messages[msg.sender].My_Messages[1].text= 'Hello World';
}
function get_latest_message(address _receiver) constant returns (string) {
uint32 counter= All_Messages[_receiver].counter;
return All_Messages[_receiver].My_Messages[counter].text;
}
function get_message(address _receiver, uint32 index) constant returns (string) {
return All_Messages[_receiver].My_Messages[index].text;
}
function get_sender(address _receiver, uint32 index) constant returns (address) {
return All_Messages[_receiver].My_Messages[index].sender;
}
function get_counter(address _receiver) constant returns (uint32) {
return All_Messages[_receiver].counter;
}
function send_message(address _receiver, string _text) returns (bool) {
if (All_Messages[_receiver].counter > 0)
All_Messages[_receiver].counter++;
else
All_Messages[_receiver].counter= 1;
uint32 counter= All_Messages[_receiver].counter;
All_Messages[_receiver].My_Messages[counter].text= _text;
All_Messages[_receiver].My_Messages[counter].sender= msg.sender;
Message_sent(_receiver);
return true;
}
}

Examples with geth console:


messenger.send_message.sendTransaction('0x53d6a48bb5374e611a497eebee146bd596c4c529','Hello! What is up?',{from: eth.accounts[2]});
...
messenger.get_counter('0x53d6a48bb5374e611a497eebee146bd596c4c529');
8
messenger.get_latest_message('0x53d6a48bb5374e611a497eebee146bd596c4c529');
"Hello! What is up?"

I am sure this can be improved (feel free to do so), but we want to keep it simple…

To be continued.

2 thoughts to “Message in an Ethereum nutshell”

Mentions

  • Ethereum application recipe – Blockchain Log
  • Pay to get in – Blockchain Log

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.