Mailchain users can send messages to any Substrate account address. This section details how the Substrate implementation of the Message Flow is achieved.
Mailchain plans to support substrate networks that have the Contract Substrate Runtime Module Library enabled (srml_contracts).
To send an encrypted message, a public key is required. Mailchain uses the public key associated with an address to encrypt the data, and if specified by the envelope, the message contents. This ensures the recipient is also the owner of the private key and only they can decrypt data. The public key finder decodes a recipient address from base58 and extracting the public key (dropping the first byte and last two bytes before prepending '0x').
Address (base58 encoded)
Decoded Address (from Base58)
Extracting Public Key
Mailchain uses the functionality provided by the Contract module (srml_contracts) `call` function which is similar to a standard Substrate transaction (signed extrinsic) with the addition of a data field (Vec<u8>). A substrate parachain (or parathread) with the Contract module enabled permits sending transactions with an envelope containing a message.
Contract transactions (signed calls) contain a
data field that stores the envelope. Bytes can be stored in the
data field and must be hexadecimal encoded, prefixed with
0x. Data stored in a transaction must follow the Mailchain standard encoding format:
Required Contract SRML
"mailchain" encoded as hexadecimal
Envelope encoded as hexadecimal
To send a message the following transaction fields must be specified.
Once the fields have been populated, the extrinsic must be signed using the sender private key. The signed extrinsic bytes can then be transmitted to the substrate network over JSON-RPC using the Contract module
To read Mailchain messages for a specific address, Contract calls sent to that address need to be identified.
Substrate does not natively support an address index or similar functionality that identifies all transactions sent to or from a specific address. Third parties provide some of this information through a web interface (including Subscan.io) but as of August 2020, no APIs consistently provide a transaction index for received transactions and their data fields.
Mailchain provides a simple transaction indexer which listens for transactions and filters mailchain messages for the recipient. Further details and instructions for running it can be found here: Substrate Transaction Indexer.