Sending Mailchain Messages with Substrate On Edgeware

Introduction

In this tutorial, we’re going to send messages with substrate (https://www.substrate.io/) on the Edgeware network (https://edgewa.re/).

Mailchain with Substrate on Edgewa.re

A similar video tutorial is also available here.

We’ll start by setting up some accounts for Edgware, then we’ll install Mailchain, and finally we will send a message from Alice to Bob and then Bob will reply to that message.

Setting Up Accounts

So let’s get started by setting up Alice and Bob’s accounts…

Go to https://polkadot.js.org.

In the top left, click on the polkadot network. A menu comes up to display the available networks.

Switching To Edgeware in Polkadot-js

Choose Edgeware, then click on Switch.

Wait for the page to reload and then you will see Edgeware in the top left.

Alice

Next, click Add Account to create Alice’s account.

In the form, enter a meaningful name.

Creating Alice’s Account

Then change from Mnemonic to Raw Seed. Copy the seed and save it somewhere safe. Never create an account without saving these details.

NOTE: It’s important to keep track of secret seeds related to accounts so that we can import them into Mailchain later on.

Enter a password that will be used to encrypt and protect the account. You need this to send tokens or sign calls to contracts in the polkadot-js interface. It’s also used to restore the account from backup if needed.

Leave the other settings as default.

Click Save, then when prompted, click Create and Backup Account. This downloads a .json wallet file that you can be used to restore the account. It should be backed up securely.

Bob

Now we’ll add an account for Bob.

Creating Bob’s Account

Change to Raw Seed from Mnemonic, enter a password, and make sure we save that seed and backup the account.

You can now see both of the accounts on the Accounts tab. Keep a record of the addresses so we know who’s who later on.

Inspecting Alice’s and Bob’s Addresses

Add A Balance Of EDG (Edgeware Tokens) To The Accounts

Each Mailchain message costs 0.102 EDG, including gas. This is less than 0.2 cents of a US Dollar at today’s rate.

To complete the tutorial, you will need to send some Edge to your new accounts. This may be some you already have or you may need to purchase some from an exchange.

I sent 0.5 EDG to each of those addresses from another address I use for distributing tokens for tutorials.

IMPORTANT NOTE! Please don’t send any EDG to the addresses displayed in this tutorial. The secret seed or private keys can be seen and used by anyone.

Installing Mailchain

Now we’ll install Mailchain.

In your browser go to https://docs.mailchain.xyz/installation and follow the instructions for your operating system.

I’m using a Mac with Homebrew already installed, so in my terminal, I installed the Tap:

brew tap mailchain/tap

Then install the package:

brew install mailchain

Mailchain is now installed.

Configuring Substrate In Mailchain

Now we’ll configure accounts. Instructions on how to do this can be found in Substrate Instructions from the Mailchain docs.

Setting Up

Following instructions from https://docs.mailchain.xyz/substrate-instructions/setting-up, we configure our accounts for Alice and Bob by running the `mailchain add account` command in the terminal.

Using the private key (aka the Raw Seed) value obtained for Alice and Bob’s accounts when we created the accounts in polkadot-js, remove the first two `0x` characters.

Enter the same passphrase for each key.

Alice

For Alice, the raw seed `0xbcce8…56d1` value becomes `bcce8…56d1`

mailchain account add --protocol=substrate --private-key=bcce81d8898fd23386ac157dae3527a3921f2e7534251646acbdcee631db56d1 --key-type=sr25519

Mailchain will ask you for a passphrase which it uses for encrypting the key. You will need to enter this each time you start Mailchain.

Bob

We do the same for Bob, removing the `0x` at the start of the seed.

mailchain account add --protocol=substrate --private-key=cff669eba6ec3bec012575eb57f636e73b4d4bb36f9eb7d22ad4e0be776f0fd5 --key-type=sr25519

NOTE: Remember to use the same passphrase for both Alice and Bob’s accounts.

Installing And Running The Transaction Indexer

Now, we need to install the indexer for transactions (or extrinsics in substrate’s case). This watches for messages on the network. More information can be found here: https://docs.mailchain.xyz/substrate-instructions/installing-starting-stopping-the-indexer, including commands to run on the different networks (mainnet, beresheet testnet, and local).

As a prerequisite, you need to have Docker Compose installed which comes with Docker Desktop. Instructions are linked to in the docs under the prerequisites section.

Now clone the Mailchain repository to your .mailchain/mailchain folder and navigate to the repository.

git clone https://github.com/mailchain/mailchain.git ~/.mailchain/mailchaincd ~/.mailchain/mailchain

We’ll run the command for Edgeware Mainnet:

make edgeware-mainnet

This downloads and starts the docker containers. Once connected to the network, it should start to output blocks.

Mailchain Indexer for Substrate

This means it is listening to extrinsics, looking for Mailchain messages (for more information see the docs).

Starting Mailchain

Leave the indexer running and open a new terminal tab or window.

Run the `mailchain serve` command and enter in the passphrase that you were prompted for when you added the mailchain accounts for Bob and Alice in the Terminal.

The mailchain api application is now running (for more information, see Running the Application)

Configure the Mailchain Inbox

Now, go to the mailchain inbox in the browser. You may receive an error telling you that no addresses have been configured. This is because the inbox is configured for the Ethereum Mainnet by default.

To change this, click Close on the error message, then head to settings in the top right of the page.

Under Blockchain Protocol Settings, select `Substrate` as the Blockchain Protocol and then select `Edgeware-mainnet` as the Blockchain Network.

Save the settings, then once the page reloads, click Back To Messages.

In the left pane, you should see Alice’s and Bob’s addresses.

Sending A Message

Now, let’s compose a message from Alice to Bob.

Change the From field to Alice’s account, then paste Bob’s address into the To field.

Fill in the Subject and Message Body with your message, then click Send.

Once the extrinsic has been sent, the mailchain client outputs the hash.

Mailchain Output in Terminal

You can see Alice’s message extrinsic on subscan.io: https://edgeware.subscan.io/extrinsic/0xff97061f74d34588c834bb47c758fd983a38f493c3049c0d4a69972529962880

Inspecting the Extrinsic Data (it’s encrypted)

You can see the details of the transaction or extrinsic, including the destination address (Bob’s address) and the data field.

The data field contains the encrypted message location which is stored offchain. The mailchain client handles decrypting this location, fetching the encrypted message and decrypting that for the inbox.

Go back to the mailchain inbox.

Checking Messages And Reading Alice’s Message to Bob

On clicking Check Messages, Alice’s message appears in Bob’s account.

Click to read it.

Replying To A Message

Now we can compose a reply to Alice and send it.

Next, click Check Messages again.

Bob Replying to Alice

You will see Bob’s reply in Alice’s account (also visible in subscan).

Viewing Account Balances

To look at the balances, go back to the accounts tab in polkadot-js.

From the screenshot, you can see that from the 0.5 EDG transferred to the accounts I used, 0.398 remains for each, because each account sent a message (costing 0.102 EDG for data and gas).

Wrapping Up

This completes the tutorial where we set up new accounts for Alice and Bob, then installed the Mailchain api and indexer for substrate, and sent an encrypted message from Alice to Bob, which Bob replied to.

You can head over to https://mailchain.xyz to find out more about mailchain and its use cases.

Useful Resources:

More information can be found here:

Feel free to reach out to the team on your favourite channel.

Sender of email-like messages on blockchain.