Kirill

Blockchain enthusiast developer and writer. My telegram: ksshilov

The Developers Guide on Building Real-time Distributed Apps

Nowadays, decentralized applications are at about the same stage of development as cars were during Henry Ford’s time. In other words, using dApps is a slow process, it’s unsafe, and everyone always asks, “What the heck is this thing?”

Indeed, after almost a year of a bearish market cycle, the state of dApps is like that of a scorched desert. Looking at the current state of affairs in this area it’s easy to see that most of the projects are dried up, almost completely dead. Against this background, however, there are a few distributed applications whose developers are still active. There are the successful outlying projects that have won the trust of market participants (like Storj, whose daily active audience is more than half of the total audience of all dApps). Of course, as the industry develops further, this situation will change. But as it stands today, distributed applications remain the toy of geeks and crypto enthusiasts. This is partly due to the small penetration of the cryptocurrency sphere in everyday life, but the fact remains — these applications are too slow and unreliable to be accepted by the masses right now.

https://dappradar.com/charts

Requirements for dApps in the real world

What qualities should a distributed application meet in order to satisfy the needs of an ordinary person who is used to centralized services? Well, they don’t need much. It should be safe and fast; really fast! It must be remembered that no considerations of decentralization and data control will force the average person to prefer a slow and lagging distributed application to a fast and convenient classic one.

What do I mean by “really fast”? Well, WhatsApp completes about 60 billion transactions every month, or about 700,000 tx/second. This is roughly the level of speed that decentralized applications should strive for in order to pose as strong competition against their “big brothers” such as Telegram, Facebook, Whatsapp, and many others. How far are we from that today? Very, very far away. Transaction confirmation time in BTC and ETH blockchains is calculated in minutes (you may find some particular estimates here and here); about 7 transactions per second pass through the Ethereum network (the current state of the blockchain can be tracked here).

https://www.blockchain.com/en/charts/n-transactions-per-block?timespan=1year

We must pay tribute to the efforts of Ethereum developers who are trying to make the blockchain significantly faster, such as the implementation of Plasma. But these figures show that the path to the desired speed is not short. Therefore, recently, other platforms that focus on solving this problem have appeared. One striking example of such a platform on which you can develop fast and convenient distributed applications is #Metahash.

#MetaHash enables the creation of decentralized applications that work in real time, like ordinary web services and applications and can respond to events both in any of
the blockchain networks and on the regular Internet. The #MetaHash network allows for a 3-second confirmation time at over 50,000 tx/s.

Such exceptional performance is the reason why this tutorial is devoted to working with #MetaHash and creating a dApp based on its blockchain.

Working with #MetaHash: a full guide

In the future, the #MetaHash network will include an array of decentralized modules to accelerate the development of new projects. For example, creators of each project will be able to use the existing #MetaChains service instead of creating their own Bitcoin or Ethereum parsing scripts.

Here we will describe basic actions like creating a #MetaHash address, sending transactions, as well as highlight the differences between the testnet and mainnet development environments. For further information, you can refer to #MetaHash’s yellow paper.

How to set up the development environment

To start with, you will need cURL software installed on your machine and basic knowledge of command line tools. The cryptographic functions used by #MetaHash are based on the OpenSSL library, which you will also need. Both programs are available to download for free and you can find instructions on how to set them up here and here. Be aware that these guides are dedicated to Windows users and if you’re using another operating system, please refer to the specific tutorials.

How to set up the client (#MetaGate)

#MetaGate is a light user-client with an option to use part of the hard drive as an archive storage when forging mode is live. You can download the client from metagate.metahash.org.

#MetaGate works as a crypto-wallet and a “gate” to decentralized Internet simultaneously. Everyone developing his or her own #MetaApp (a dApp built on #MetaHash’s blockchain) may submit it to the ecosystem through the interface above.

Generating keys

In order to complete transactions and store assets in #MetaHashCoin (MHC) cryptocurrency one must have a digital wallet. In order to programatically create such a wallet, you will need to understand the one fundamental principle of wallet development — asymmetric encryption.

Asymmetric encryption involves two cryptographic keys — a public key and a private key; the message is encrypted with the public key and decrypted with the secret private key. When you perform transactions, all data is also signed with your private key. So, the keys will act as your main tool for each action.

Generating a private key is the first step towards creating a wallet, with an address where you can send and receive payments on the #MetaHash network. Confirming the right to own the address is possible only with the private key.

To generate a private key you need to use a cryptographic algorithm based on the elliptic curve secp256r1. You can use its general implementation already available in the OpenSSL library: secp256k1 (prime256v1) in the following way:

Code (to be inserted in the command line tool):

openssl ecparam -genkey -name secp256k1 -out test.pem

Based on the private key, a public key should be generated immediately.

Code:

openssl ec -in test.pem -pubout -out test.pub

Creating an address

The presence of private and public keys allows you to proceed to the next step — creating an address. The address is the user ID in the #MetaHash network, which is necessary for any type of transaction. Each user on the network can have an infinite number of addresses that are stored in their wallets and used for operations.

The following sequence will create a #MetaHash address:

1. Take 65 bytes from the public key (where 1 byte stands for ‘0x04’, then 32 bytes-correspond to the x coordinate, 32 bytes-to the y coordinate within the network).

Code:

openssl ecparam -genkey -name secp256k1 -out test.pem
openssl ec -in test.pem -pubout -outform DER|tail -c 65|xxd -p -c 65 > btc_test.pub

2. SHA-256 hashing encryption of the public key is performed.

Code:

cat btc_test.pub | xxd-r-p | openssl dgst -sha256
(stdin)= 2969f47d4d442ed5210355741f1ca3908a62bee9b20a0c37db39b356d2aa0b8f

3. Next, the cryptographic hash function RIPEMD-160 hashes the value obtained in the previous step.

Code:

echo-e ‘your value from step 2’ | xxd-r-p / openssl dgst -rmd160

4. Over the received value SHA-256 hashing is performed again (look at Step 2).

5. From the last value, SHA-256 hashing is performed again and only the first 4 bytes (or 8 symbols) of the received hash are taken.

6. These 4 bytes are added to the end of the RIPEMD-160 hash from Step 3 and 0x is added at the beginning.

The output is the address.

Operations with the address

This part describes the methods used to work with a #MetaHash address. Requests can be made on the list of currently available torrent servers. These types of requests are valid in both production and test networks, as described below.

Current balance

It is possible to find out the current balance of your #MetaHash address using the fetch-balance method.

Code

Input
curl -x POST — data ‘
{
“id”: 1,
“params”:
{
address”: “0x005511…00000 “ / / address
}
}’ XXX.XXX.XXX.XXX:YYYY/fetch-balance // IP:PORT/Method_name
Output
{
“id”: 1,
“result”:
{
“address”: “0x005511…00000”, / / address
“received”: 0, / / received (received)
“spent”: 0, / / spent (gone)
“count_received”: 0, / / number of receipts
“count_spent”: 0, / / number of shipments
“block_number”: 0", / / block number
“currentBlock”: 22, / / number of blocks at the moment
}
}

Transaction history

In the MetaHash network, there is a fetch-history method that allows you to view the entire transaction history of an address.

Code:

Input
curl -x POST — data ‘
{
“id”: 1,
“params”:
{
address”: “0x005…555 “ / / address
}
}’ XXX.XXX.XXX.XXX:YYYY/fetch-history // IP:PORT/Method_name
Output
{
“id”: 1,
“result”:
[
{
“from”: “0x001…b”, / / from which address
“to”: “0x00c…8”, / / to what address
“value”: 10000000,, / / how much
“transaction”: “6b…34”, / / hash transactions
},
…,
{ … }
]
}

There are other methods like get-tx in the MetaHash network. To learn more about them, you can refer to the #MetaHash development portal or go to the #MetaHash GitHub.

https://github.com/metahashorg

Scripts in various programming languages — C++, Shell, PHP, Python — supporting all of the above methods can be found at https://github.com/metahashorg. The README file to the repositories describes, in detail, how to use these scripts as well as provides examples of their application in test or production networks.

Creating a transaction

A network transaction is an operation for transferring MHC between the two addresses. Transactions within the #MetaHash network is conducted in the presence of a digital signature. The data is then signed with a private key. Combined with a public key, the signature confirms that the transaction was created by the real owner of the given #MetaHash address.

The following describes the formation of a transaction in the #MetaHash network in the form of a POST request:

Code

curl -x POST — data ‘
{
“jsonrpc”: “2.0”, / / jsonrpc version
“method”: “mhc_send”, / / method name
“params”:
{
“to”: “0x00f…9D”, / / recipient address
“value”: “1000000”, / / amount to be sent, here value=1 MHC
“fee”:”, / / Commission
“nonce”: “1”, / / the number of outgoing transactions from the address at the time of this transaction, i.e. = count_spent + 1
“data”:””, / / data attached to the transaction in Hex-format
“pubkey”: “305…e6aa”, / / public key
“sign”: “304…84 “ / / signed text: sequence of values of fields to, value, fee, nonce, counter for data, data
in binary format (see below for signature of binary transactions), learn more about signing text.
}
}’ XXX.XXX.XXX.XXX:YYYY // IP:PORT. You can find out how to get a list of available IP and PORT by reading the next part of this tutorial.
Cryptosystem response
{
“result”: “ok”,
“params”: “dd2…452” // hash of the transaction
}
or
{
“result”: “ok”,
“error”:” Message “ / / error message
}

Production vs test environments

The MetaHash network is a set of servers divided into roles within the network. #TraceChain technology operates several networks, which are labeled Test Network (TMH network) and Production Network (MHC network).

The test network in the #MetaHash network is represented as net-dev and net-test, the Production Network network has the symbol “net-main”.

The main difference between the test and production networks is that the test network works not only with POST requests, as it happens in the production network, but it can also work with GET requests.

In any of these networks of #MetaHash, you can always view the entire digital transaction register;explorers are developed for this very purpose. All explorers can search by address, hash transaction, or block number in the blockchain for the convenience of users.

The explorers’ addresses:

Conclusion

We are gradually entering the era of decentralized applications. While developing your own dApps, you will definitely face the problem of choosing a blockchain that best suits your goals. If your goal is to create an application that has real-time features, like a game, chat room, or social network, then #MetaHash will be a great place to start.

About the author:

Kirill Shilov — Founder of Geekforge.io and Howtotoken.com. Interviewing the top 10,000 worldwide experts who reveal the biggest issues on the way to technological singularity. Join my #10kqachallenge: GeekForge Formula.

More by Kirill

Topics of interest

More Related Stories