How to install and customize a blockchain and a sidechain?

Laurent OLIVIER
ON-X Blockchain (Chain-Accelerator)
8 min readNov 25, 2020

--

  • Introduction
  • Installation of Horizen node with a bash script for a fast and automatic installation
  • Installation of the Zendoo sidechain with a bash script
  • Transfer from the mainchain to the sidechain and transfer within the sidechain
  • Conclusion

Introduction

Horizen, formerly ZenCash, is a blockchain developed by Zen Blockchain Foundation. It is a fork of Z-Classic, which early used Zero-Knowledge Proof cryptography. Horizen blockchain is mainly based on confidentiality, they have several projects under development, including the Zendoo sidechain. A sidechain is a second layer implemented on top of the main blockchain (mainchain), to meet a growing need in the blockchain domain: scalability.

Zendoo is a transfer protocol (2WP — 2 Way-Peg), which allows to send ZENs bilaterally, between the mainchain and the sidechain and vice versa, but also to perform exchanges between the sidechains. To verify that a transaction is valid, the sidechain nodes include in the header block the hash of the previous block and the Merkle tree. Thanks to the integration of the Zero Knowledge Proof (Zk-SNARKs), the sidechains can watch what happens in the mainchain, but the mainchain cannot watch the sidechains’ operations. Each sidechain has its own SNARK, which is used for withdrawal certificates. It is possible to configure each sidechain independently according to the needs of your projects.
To learn more about the different transfer protocols I recommend the Horizen Academy website.

In this article we will show you how to install a Horizen node, as well as the Zendoo sidechain and some transactions. To simplify the different installations and transfers we have created 3 scripts in bash and python so that you can do the installations automatically and quickly. The first one allows you to install and configure a Horizen node and the second one allows you to implement Zendoo in it. The third is to simplify transfers.

Installing your first Horizen node

For this you must first have a VPS server, a virtual machine or a Raspberry Pi.
In our case we installed the node under Linux (Debian10). Then go to the ON-X blockchain GitHub to clone the script named eazen.sh. This script is installed from root, when you launch the script you’ll have to specify in which user you want to install the node.
Once Debian10 is installed, go to your terminal. There are two ways to launch the script, the first one is to create a file using the vi command:

# vi eaZen.sh

then copy the script into it. The second way is to run the command :

# cd /home/<USER>/
# git clone https://github.com/chainaccelerator/eazen.git

Then copy the script to your user. All this in the #ROOT.

# cd /home/<USER>/eazen
# cp eazen.sh /home/<USER>
# cd ..

The last step is to give the rights to this script which will allow to execute it, by typing:

# chmod +x eaZen.sh 

Now let’s execute the script :

$ ./eaZen.sh 

The installation of the node can take several tens of minutes depending on your machine. At the end of it, return to your user by typing exit. You will only have to type:

zend

And there you have your Horizen node! You can configure your node by modifying the .conf file:

~/.zen/zen.conf

If you wish to continue working on your terminal, insert in this file:

daemon=1
showmetrics=0

Creation of the Zendoo sidechain

We will create it using the SDK available on Horizen’s github, which allows us to develop completely decentralized blockchain applications. For this installation we have also created a script that allows to install it automatically and quickly. In this script, we will declare the sidechain on our own mainchain created with the previous script, currently it is not possible to peg the sidechain to the mainchain in mainnet. To configure the sidechain we have to populate some blocks. Before that, we need to create cryptographic keys including Schnorr signatures (a cryptographic authentication protocol, proof of the Zero Knowledge Proof), checks that will allow us to perform 4 types of actions:

  • Forward Transfer (FT)
  • Withdrawal Certificate (WCert)
  • Backward Transfer Request (BTR)
  • Ceased sidechain withdrawal (CSW) — Ceased sidechain withdrawal, when a sidechain does not submit a withdrawal certificate within the specified time, it becomes inactive and no further withdrawal certificates will be accepted.
# cd /home/<USER>/eazen
# cp sceaZen.sh /home/<USER>
"cd ..

Beforehand, add the user to sudoers as root, we advise to add it via the command

sudo visudo

at the very end of the file enter:

<USER> ALL=(ALL:ALL) NOPASSWD:ALL

so that the script is not interrupted by a password request.
Also change the permission of the script when returning:

# chmod +x  ./sceaZen.sh
# exit

Then run the script in $USER.

$ ./sceaZen.sh

In this script we install Java 8, Maven and Scala, then the Zendoo SDK. This script contains a random number string generator (open ssl), no risk that another user will have the same one. The bootstrap contained in the SDK will generate the different keys and signatures. We connect this sidechain to our previously created mainchain, to activate the sidechains we must create 220 blocks, we also find several parameters such as the number of tokens to send to an address created by the script, in this one we chose 100 ZEN and the block frequency between two transfers between the sidechain and the mainchain. It is possible to view the information of a transfer using the gettransaction command and check its IDs. In this script the transfer is also undermined with always the possibility to check the ID of the block with the getblock command.
You can of course change the values of all these parameters and play with the script!

Some useful commands:

#the number of blocks generated
zen-cli getblockcount
#generate a number of blocks
zen-cli generate < NUMBER>
#allows to declare the sidechain
zen-cli sc_create
#obtain transaction details and number of confirmations
zen-cli gettransaction
#mine the block in question
zen-cli generate
#obtain information from a block
zen-cli getblock
#get information from a sidechain
zen-cli getscinfo
#obtain information from the sidechain genesis
zen-cli getscgenesisinfo
#get the balance of the handchain
zen-cli getbalance

You can check if the sidechain (port 9084) and the API(port 9085) are well connected by entering the command:

netstat -plnt

Transfers between mainchain and sidechain

First of all, you can find all the API documentation at http://127.0.0.1:9085/swagger.
We also made a little python script for this, transferZen.py.

# cd /home/<USER>/eazen/
# cp transferZen.py /home/<USER>/
# cd ..
# chmod +x transferZen.py
# exit
$ python3 transferZen.py

Here’s how to do it “manually”:
Let’s start by checking the balance of the mainchain and the sidechain.

#get the balance of the handchain
$ zen-cli getbalance

To see the balance of the sidechain:

$ curl -X POST "http://127.0.0.1:9085/wallet/balance" -H "accept: application/json" -H "Content-Type: application/json"

To send coins from the mainchain to the sidechain, you will need the publicKey (GKPUBLIC), the first address of the wallet on the sidechain, as well as the SidechainID (SCID). These 2 informations are present in the file /SidechainSDK/keys.txt generated during the previous script or just with the command:

$ source .bashrc
$ echo $GKPUBLIC
$ echo $SCID

Here, we send 100ZEN to the sidechain.

$ zen-cli sc_send "<GenerateKey publicKey>" 100 "<SidechainID>"

Vous obtiendrez en retour l’ID de la transaction, que vous pouvez vérifier avec:

#obtain transaction details and number of confirmations
$ zen-cli gettransaction

Now you have to undermine the block that contains the transaction, fees will be applied to the transaction. Knowing that you undermine the block you will get the fees in return, so the fees will be cancelled.

#mine the block
$ zen-cli generate 1

If you check the balance of the sidechain you will see that it hasn’t moved yet. For the transaction to be taken into account you have to generate an additional block. For this we will need the time we are in and the number of slots. There are 720 blocks in an epoch. To get this information we will use the call:

$ curl -X POST "http://127.0.0.1:9085/block/forgingInfo" -H "accept: application/json" -H "Content-Type: application/json"
{
"result" : {
"consensusSecondsInSlot" : 120,
"consensusSlotsInEpoch" : 720,
"bestEpochNumber" : 1,
"bestSlotNumber" : 720
}

If, for example, you are at the time 1 block “720. you must go to the second time block #1. If you are at the time 2 block #10, you have to go to the next block, 11th block.
Now let’s generate this new block:

$ curl -X POST "http://127.0.0.1:9085/block/generate" -H "accept: application/json"  -H "Content-Type: application/json" -d "{\"epochNumber\":2,\"slotNumber\":1}"or$ curl -X POST "http://127.0.0.1:9085/block/generate" -H "accept: application/json"  -H "Content-Type: application/json" -d "{\"epochNumber\":2,\"slotNumber\":11}"

You get in return a blockID. To see what is written in this block you have the call:

$ curl -X POST "http://127.0.0.1:9085/block/findById" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"blockId\":\"<blockID>\"}"

There you go, you have made a transaction between the mainchain and the sidechain. You can check the balance of the sidechain. This one has been credited with 100ZEN.

$ curl -X POST "http://127.0.0.1:9085/wallet/balance" -H "accept: application/json" -H "Content-Type: application/json"

Transfers within the sidechain

Let’s now make a transfer within the sidechain. Let’s first create a new address in the sidechain.

$ curl -X POST "http://127.0.0.1:9085/wallet/createPrivateKey25519" -H "accept: application/json"

Let’s send 50 ZEN to this new address without any fee. On the sidechain it is expressed in zennies i.e. 1 ZEN = 100 million zennies.

$ curl -X POST "http://127.0.0.1:9085/transaction/sendCoinsToAddress" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"outputs\":[{\"publicKey\":\"<new adress>\",\"value\":5000000000}],\"fee\":0}"

Let’s check the pending transactions. If true then we get the content of a transaction. If false we get the IDs of the pending transactions.

$ curl -X POST "http://127.0.0.1:9085/transaction/allTransactions" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"format\":true}"

We can generate this block as previously /block/generate/, remember to look at the forging info /block/forgingInfo/, then check that there are no more pending transactions with the call /transactions/allTransactions/. Finally let’s look at all the “Boxes” to get all the information available in the sidechain wallets.

$ curl -X POST "http://127.0.0.1:9085/wallet/allBoxes" -H "accept: application/json" -H "Content-Type: application/json"

You will find the balance of all the wallets present in the sidechain.

Conclusion

Voilà!

In this article and with the help of these scripts you have installed a Horizen node, configured and declared a sidechain on your Horizen mainchain, as well as perform a transfer between the mainchain and the sidechain, and then a transfer within the sidechain.

The wallet user and developer friendly Sphere by Horizen and in particular the version 2.0 beta will make it possible to carry out this kind of transfers much more easily. ZenChat is still integrated in the wallet. ZenChat messages are stored permanently in the blockchain to prevent metadata leakage and are encrypted with algorithms such as AES-256 with Perfect Forward Secrecy (PFS).

I would like to thank Xavier Garreau, you will find the functioning of the scripts in more detail on his website.

We are also the organizers of the Paris Blockchain Week Summit. It will take place on December 9 and 10, 2020. Do not hesitate to ask us about the event! Rob Viglione, co-founder of Horizen will be there!

Find ON-X blockchain on https://www.on-x.com/services/blockchain.

--

--