Pay to get in

Now let’s make some coin!

Below you find a mapper contract to collect more than 0.49 ether in order to register the sender for a service. You can adjust the amount in function mapper(), of course. Once the minimum amount has been sent to the contract, the sender is registered for the service:

  • Contract accepts Ether using function() payable
  • The sender is registered by create_Member(address new_address)
  • You can send more than 0.5 ether (less is rejected and send back), but only one payment is accepted for each sender address.
  • The contract is owned
  • Only owner can get the money out with send_Ether_To_Owner()

This is very simple and Ethereum tokens offer more possibilities.

Our contract is here: 0x4f51b5bc96dc85226bd1e9510394f83a2d74bbb0

The registration will be a gateway to enter the messaging service on a private chain. You can either wait for the next blog post or send some coin already 😉

unknown

Mapper (Solidity contract)


pragma solidity ^0.4.4;
contract owned {
function owned() { owner = msg.sender; }
address owner;
modifier onlyOwner {
if (msg.sender != owner)
throw;
_;
}
function kill() {
if (msg.sender == owner) suicide(owner);
}
}


contract mapper is owned {
uint32 counter;
uint256 private value;
uint256 private price;
mapping (address => address) members;
event CreateMember(address new_address);
function mapper() {
counter = 0;
value= 0;
price= 490000000000000000;
}
function() payable {
if ( msg.value > price && check_Address(msg.sender) == false ) {
create_Member(msg.sender);
value+= msg.value;
} else throw;
}
function create_Member(address new_address) private returns (bool) {
members[msg.sender]= new_address;
counter++;
CreateMember(new_address);
return true;
}
function send_Ether_To_Owner() onlyOwner returns (bool) {
value= 0;
return owner.send(this.balance);
}
function get_Counter() constant returns (uint32) {
return counter;
}
function get_Value() constant returns (uint256) {
return value;
}
function check_Address (address a) constant returns (bool) {
if (members[a] == a) return true; else return false;
}
}

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.