Note

If you are new to the blockchain technology, taking our Introduction to Blockchain Technology self-paced course is highly recommended. Also, for a comprehensive coverage of blockchain development in Ethereum or mastering Solidity programming, taking our below self paced courses is highly recommended:

Recap

In our previous article (How to Install Ethereum Swarm Storage), we discussed how to install Ethereum Swarm storage.

 

In this article, we learn how to process Ethereum messages with Whisper.

Ethereum messages – Whisper

An application typically needs three kinds of resources for application services:

  • Compute
  • Storage
  • Messaging

Ethereum EVM and smart contracts provide compute, while Swarm/IPFS handles large data storage and is the decentralized storage layer. Whisper handles Ethereum messages.
Here is a high-level architecture diagram showing these three in action:

 Ethereum blockchain development in Infura

 

Whisper is an Ethereum P2P communication protocol that allows messaging between DApps. It provides a simple API that we can use to send an encrypted message through the Ethereum blockchain and receive and decrypted messages with the hash key. Whisper is currently at the POC 2 stage and supports the Geth and Parity clients. It can be used for DApps publish-subscribe coordination signaling and building secure, untraceable decentralized communication.

 

Whisper protocol

Whisper currently uses the ssh protocol string of devp2p. When sending an encrypted message, the message content can be encrypted by default either asymmetrically or symmetrically.

Asymmetric cryptography, also known as public key cryptography, uses public and private keys to encrypt and decrypt data. One key is public and it is shared with everyone. The other is a private key; only the owner can see or access private key information. When encrypting the Whisper message, it uses the standard Elliptic Curve Integrated Encryption Scheme with the SECP-256k1 public key to encrypt a message; the other key is used for decryption. Symmetric cryptography (also known as the secret key), on the other hand, uses the hash key with the AES GCM algorithm with a random 96-bit nonce for both encryption and decryption. It typically facilitates one-to-many messages. The sender and receiver use the same symmetric key to encrypt and decrypt the message.

 

Whisper envelopes

Whisper envelopes contain the encrypted payload and some metadata in plain format. It is sent and received by Whisper nodes.

Here is the structure of the envelope. It contains topic-related information, as shown in the following diagram:

 Ethereum blockchain development in Infura

 

Each field in the Whisper envelope contains important information for the message:

  • Version: This can be up to 4 bytes (currently one byte containing zero) and indicates the encryption method. If the Version is higher than the current one, the envelope cannot be decrypted, and therefore can only be forwarded to the peers.
  • Expiry: This is the message expiry time (Unix time in seconds).
  • TTL: This defines the message’s time-to-live in seconds.
  • Topic: This is 4 bytes of arbitrary data.
  • AESNonce: This is used in symmetric encryption and represents 12 bytes of random data.
  • Data: This is encrypted byte array data.
  • EnvNonce: This is 8 bytes of arbitrary integer data that’s used for Proof-of- Work (PoW) calculations.

Whisper message

Envelope’s payload has encrypted byte array data; it is the Whisper message in plain format.
Here is the message’s structure:

[ flags, optional padding, payload, optional signature]

Let’s explain the structure in more detail:

  • Flags: There’s a single byte for the flag to specify the message has a signature.
  • Padding: This is used to align the message size, and it can contain random data.
  • Signature: This is the signature that’s used for sending the message. It is the ECDSA signature of the Keccak-256 hash of the unencrypted data.
  • Payload: This is the payload of the message.

Whisper messages are sent to all Whisper nodes with TTL and PoW consensus to prevent Direct Denial-of-Service (DDoS) attacks. The nodes pass envelopes around, and only the receiver who has the private key can read the message.
Now, we understand the basic Whisper message structure. It is time to look at an example.

 

Whisper example

To run Whisper, you need to install geth and then connect to a geth node with the Whisper option, as shown here:
geth –rpc –shh -ws

In the preceding command, we can see the following:

  • The –rpc option is used to enable message communication via RPC. The –ssh option is used to enable the Whisper protocol.
  • The –ws option is used to enable the WebSocket protocol for real-time message communication.

Here is the result after running the preceding command:

 Ethereum blockchain development in Infura

 

Posting a message: To post a Whisper message, we need a public key to encrypt a message:

  • First, we will initialize the Whisper client by using the go-ethereum Whisper client API and connect to a local Geth node over WebSocket at the default port number 8546.
  • Then, we will generate public and private keys through the NewKeyPair

function.

The go-ethereum whisperv6 package provides a NewMessage API to broadcast the message to the network. Here is a code sample for the publish message:

import (
  ...
  "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/Whisper/shhclient" "github.com/ethereum/go-ethereum/Whisper/whisperv6"
  )
  client, err := shhclient.Dial("ws://127.0.0.1:8546") keyID,  err := client.NewKeyPair(context.Background())
  publicKey, err := client.PublicKey(context.Background(), keyID)  message := whisperv6.NewMessage{
  Payload:        []byte("Hello Whisper"), PublicKey: publicKey,
  TTL:                 120,
  PowTime:        3,
  PowTarget: 2,
  }
  messageHash, err := client.Post(context.Background(), message)

 

Receiving a message: To receive a Whisper message, we need a private key to decrypt the message:

  • First, we initialize the Whisper client by using the go-ethereum Whisper client API and connect to a local Geth node over WebSocket at the default port number 8546.
  • Then, we call NewKeyPair to get the private key with keyID.

After that, we get the message using the private key. Here is a subscribe message code sample:

client, err :=  shhclient.Dial("ws://127.0.0.1:8546") keyID, err := client.NewKeyPair(context.Background()) messages := make(chan *whisperv6.Message)
  criteria := whisperv6.Criteria{ PrivateKeyID: keyID,
  }
  sub, err := client.SubscribeMessages(context.Background(), criteria,  messages
  go func() {
  for {
  select {
  ..
  case  message := <-messages: fmt.Printf(string(message.Payload)) // "Hello Whisper" os.Exit(0)
  }
  }
  }()
  ...

Whisper nodes use the devp2p Wire protocol for P2P communication. It is currently in the alpha stage. If you want to learn more, the API documentation can be found on the Whisper GitHub page (https://github.com/ethereum/wiki/wiki/Whisper). Now, we will have a look at popular smart contract libraries.

 

Next Article
In our next article (Review of Popular Ethereum Smart Contract Libraries), we review the most popular libraries for Ethereum smart contract development.

This article is written in collaboration with Brian Wu who is a leading author of “Learn Ethereum: Build your own decentralized applications with Ethereum and smart contracts” book. He has written 7 books on blockchain development.

Resources

Free Webinars on Blockchain

Here is the list of our free webinars that are highly recommended:

 

Free Courses

Here is the list of our 10 free self-paced courses that are highly recommended:

 

Self-Paced Blockchain Courses

If you like to learn more about Hyperledger Fabric, Hyperledger Sawtooth, Ethereum or Corda, taking the following self-paced classes is highly recommended:

  1. Intro to Blockchain Technology
  2. Blockchain Management in Hyperledger for System Admins
  3. Hyperledger Fabric for Developers
  4. Intro to Blockchain Cybersecurity
  5. Learn Solidity Programming by Examples
  6. Introduction to Ethereum Blockchain Development
  7. Learn Blockchain Dev with Corda R3
  8. Intro to Hyperledger Sawtooth for System Admins

 

Live Blockchain Courses

If you want to master Hyperledger Fabric, Ethereum or Corda, taking the following live classes is highly recommended:

 

Articles and Tutorials on Blockchain Technology

If you like to learn more about blockchain technology and how it works, reading the following articles is highly recommended:

 

Articles and Tutorials on Ethereum and Solidity

If you like to learn more about blockchain development in Ethereum with Solidity, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Hyperledger Family

If you like to learn more about blockchain development with Hyperledger, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on R3 Corda

If you like to learn more about blockchain development on Corda , reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Other Blockchain Platforms

If you like to learn more about blockchain development in other platforms, reading the following articles and tutorials is highly recommended: