How decentralized blockchain messenger works

Elena Andreeva
Sep 21, 2019 · 6 min read

At the beginning of 2017 we started creating ADAMANT with a discussion of advantages over classic P2P messengers:

  • One click to create an account — no phone number or email; ADAMANT apps have no access to contacts and location;
  • No direct connections, all data goes through the distributed nodes. No access to user IP addresses.
  • All messages are end-to-end encrypted using curve25519xsalsa20poly1305. No surprise here, but ADAMANT’s advantage is completely open source code.
  • MITM attack possibility is excluded — each message is a transaction and is signed by Ed25519 EdDSA;
  • A message goes to a block. There is no way to change the sequence and timestamps of blocks, and therefore the order of messages.
  • “I didn’t say it” won’t work with messages stored in a blockchain.
  • No central structure that makes message authenticity checks. This is controlled by the distributed consensus-based host system, and it belongs to users.
  • No censorship — impossible to block accounts and delete messages.
  • 2FA blockchain is a healthy alternative to hellish SMS 2FA that has done some harm.
  • The ability to get all your dialogs from any device at any time is the ability to not store dialogs locally at all.
  • Message delivery confirmation. Not to the user’s device, but to the network. In fact, this is a confirmation of the recipient’s ability to read your message. This is a useful feature for sending critical notifications.

The blockchain also allows tight integration with Ethereum, Dogecoin, Lisk, Dash, Bitcoin (this is still in progress) cryptocurrencies and the ability to send tokens in chats. We even made a built-in crypto exchanger.

After 2,5 years we are able to confirm our concept of the blockchain messenger — iOS, Web PWA, Windows, GNU / Linux, Mac OS and Android are now available.

Today we want to tell you how the blockchain messenger is built and how client applications can work with its API.

Message is a transaction

Everyone is already used to the fact that tokens (coins) in blockchain are transferred by transactions (like Bitcoin). But we have created a special type of transaction for sending messages.

To send a message in the blockchain messenger, you need to go through several stages:

  1. Encrypt the message text
  2. Put the ciphertext into transaction
  3. Sign transaction
  4. Send the transaction to a node
  5. The distributed system of nodes determines the message authenticity
  6. If everything is OK, the transaction containing the message is included in the next block.
  7. The recipient retrieves the transaction and decrypts the message

The stages 1–3, 7 are made locally; the stages 5–6 are made on network nodes.

Message encryption

The message is encrypted with the sender private key and the recipient public key. We take the public key from the network, but for this the recipient’s account must be initialized, that is, have at least one transaction. You can use the REST request GET /api/accounts/getPublicKey?address={ADAMANT address}, and when downloading chats, the public keys will already be available.

ADAMANT encrypts messages with curve25519xsalsa20poly1305 (NaCl Box) algorithm. Since the account contains Ed25519 keys, in order to form a box, the keys must first be converted to Curve25519 Diffie-Hellman.

Here’s JavaScript example:

Making a message transaction

In general the transaction has the following structure:

For a message transaction, the most important is asset: you need to put the message in it in the chat object with this structure:

  • message — the encrypted message
  • own_message — nonce
  • type — message type

Messages are also typed. Essentially, the type parameter tells how to understand the message. You can send just text, or you can send an object with interesting content inside — for example, ADAMANT does cryptocurrency transfers in chats this way.


Transaction signature

In order for everyone to be sure of the sender and recipient authenticity, time of sending and the message contents, the transaction is signed. The digital signature allows you to verify the transaction authenticity with a public key; a private key is not needed for this.

But the signature itself is performed by the private key:

It can be seen from the diagram that we first hash the transaction with SHA-256, and then sign using Ed25519 EdDSA and get the signature; and the transaction identifier is part of the SHA-256 hash.

Implementation example:

1 — Forming the data, including the message

2 — Calculate SHA-256 of data

3 — Signing the transaction

Sending a message transaction to a node

Since the network is decentralized, any of the nodes with an open API will do. We make a POST request for the api/transactions:

curl 'api/transactions' -X POST \-d 'TX_DATA'

In response, we get a transaction ID:

{"success": true,"nodeTimestamp": 63228852,"transactionId": "6146865104403680934"}

Transaction validation

A distributed consensus-based system of nodes determines the “reliability” of a message transaction. From whom and to whom, when, whether the message was replaced by another, and whether the time of sending was indicated correctly. This is a very important blockchain advantage — there is no central structure that is responsible for checks, and the sequence of messages and their contents cannot be faked.

First, one node checks the reliability, and then sends it to others — if most say that everything is in order, the transaction will be included in the next block — this is consensus.

You can check the code responsible for checking on GitHub — validator.js and verify.js. Yeah, the node runs on Node.js.

Including the message transaction in a block

If consensus is reached, the message transaction will get into the next block along with other validated transactions.

Blocks are strictly sequenced, and each subsequent block is formed on the basis of hashes of previous blocks.

The bottom line is that our message is also included in this sequence and cannot be “rearranged”. If several messages get into the block, their order will be determined by the timestamp of messages.

Reading messages

The messenger app retrieves transactions from the blockchain that are sent to the addressee. To do this, we made the api/chatrooms endpoint.

All transactions are available to everyone — you can receive any encrypted message. But only the recipient can decrypt it with his private key and the public key of the sender:

What else?

Since messages are delivered in ~5 seconds this way (new block formation time) — we came up with client-node and node-to-node socket connections. When a node receives a new transaction, it checks its validity and transfers it to other nodes. The transaction is available to messenger clients even before consensus and inclusion in a block. So we deliver messages instantly as other messengers do.

To store the address book, we made ADAMANT KVS — Key-Value Storage — this is another transaction type in which asset is encrypted not with NaCl-box, but with NaCl-secretbox. That is the way the messenger stores other data also.

File / image transfers and group chats still require lots of work. Of course, we could do it quickly and dirty, but we want to maintain the same level of privacy.

Yes, there is still work to be done — ideally, real privacy implies that users will not connect to public network nodes, but will raise their own. What do you think, how many percent of users do this? That’s right, 0. Partially, we managed to solve this issue with the Tor version of the messenger.

We have proven that a blockchain messenger can exist. Earlier there was only one attempt in 2012 — Bitmessage, which failed due to the long message delivery time, CPU load and lack of mobile apps.

And recent skepticism is related to the fact that ADAMANT is ahead of time — people are not ready to take responsibility for their account, the ownership of personal information is not yet in trend, and current technologies do not provide high speeds on the blockchain. But more advanced analogues of ADAMANT will appear in the future. You will see!

Further reading:

Relax. You are safe now.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store