I tested Elements (part 1) — setup and first transactions

Louis Singer
Mar 11, 2020 · 8 min read

The Elements project: https://blockstream.com/elements/

Elements code tutorial overview: https://elementsproject.org/elements-code-tutorial/overview.

This series of articles aims to document the Elements project, a Bitcoin sidechain with awesome new features. In the first part, we will see:

  • How to install Bitcoin and Elements.
  • Send our first transaction.

I used a Debian 10 system for that set of articles. If you used a VM make sure that your machine has at least 1.5 GB of memory and 12 GB of storage.

1. Install requirements on Debian 10

We need build tools, git, Haskell Platform and a Nix shell for the next tutorials.

⚠ The dependencies are quite heavy (a few Gigas). If you are on a VM, plan enough disk space.

Setup the project directory

Open the ~/.elementsProject using nano .

Put the following lines inside your ~/.elementsProject.

Load it:

Install Berkeley DB

Elements and Bitcoin require Berkeley DB 4.8. As this dependency is not available on official repositories, Elements provides a script to install it.

Install the Bitcoin’s Simplicity branch

Then, move into the newly created bitcoin folder and switch to the branch simplicity. The Berkeley prefix may change depending where do you install bd4.

The binaries are inside the src folder. We can directly launch a regtest node:

Install the Simplicity library

We also need the bech32-1.0.2 Haskell library, there also some dependencies. Use the cabal package manager to install it:

Move inside the simplicity directory to move on the commit used by Russel’O’Connor for the first transaction. We’ll use this version.

Install HAL

HAL is a rust utility and uses Cargo to be installed. If you do not have Rust and Cargo installed on your computer, run the following command, and follow the installation instructions:

Create a new folder for Hal run the commands to install HAL :.

Install Nix and create a Nix Environment

We’ll also use a nix-shell for our working environments. Install Nix on a Debian based system is very simple. Open a terminal and just launch the official installation script:

Create a Nix environment inside the simplicity cloned repository launch the following nix-shell command.

It will create our working environment. We use the default.nix configuration file and we have imported our two Haskell packages: Simplicity and bech32 .

At this stage, you’re inside a nix environment in the simplicity folder.

Install Elements

Build Elements instructions: https://github.com/ElementsProject/elements/blob/master/doc/build-unix.md

We cloned https://github.com/ElementsProject/elements.git. Finally, we can build and install Elements with the following commands (still in the elements folder and with superuser rights).

⚠ The make command may take a lot of time (and use a lot of memory).

Verify the install:

Save your shortcuts

Add the following lines inside your ~/.elementsProject.

Load it:

Save your personas

Add the following lines inside your ~/.elementsProject.

Load it:

Create personas:

Add the following lines inside your ~/.elementsProject.

We need to generate addresses. To achieve that, we will use the getnewaddress command. Let’s generate three addresses:

  • ALICE_MINER_ADDRESS : the address that will receive the fees collected when creating a new block.
  • ALICE_RECEIVER_ADDRESS : an address attached to node 1.
  • BOB_RECEIVER_ADDRESS : an address attached to node 2.

Each node controls a wallet. A wallet has several bitcoin addresses. When we use getnewaddress with alice-cli , we’ll get an address attached to the first node wallet. If we use the command with bob-cli , the address will be attached to the second node.

Add the following lines inside your ~/.elementsProject.

Start your environnement

Create and add the following lines inside your ~/elementsEnv.sh.

Now you can start daemons and save your personas addresses:

Final files:

https://github.com/crypto-hackathons/ElementsInstall/tree/master

2. Send our first transaction

First, let’s see the wallet information of our two Elements nodes. To achieve that we’ll use getwalletinfo .

This is what you get for the wallet alice:

You can see there’s 21000000 BTC on this wallet. This is a value initialized in the configuration files for testing purposes. And the second wallet (node e2) has the same balance. This value is initialized in the elements.conf files. It represents the coinbase, the initial amount of Bitcoins, that “anyone-can-spend”. Our first transaction consists to transfer half of these funds to a generated address.

Elements networks, like Bitcoin networks, require funds to have a maturity of at least 100 blocks to be spendable. 100 blocks are for consensus but the node won't broadcast early in fear of being banned so it waits until 102. We will send our 21 million BITCOIN to a first address. Then we will make them available by generating 102 blocks (100 for the consensus + 2 for security).

We will divide the “anyone-can-spend” bitcoins between the two nodes. So We’re going to execute two transactions:

  • First transfer all the coinbase (21 million BTC) to the ALICE_RECEIVER_ADDRESS.
  • Then transfer the half of the amount (10,5 million BTC) to the BOB_RECEIVER_ADDRESS.

Send the coinbase to the RECEIVER1

Let’s use the sendtoaddress command.

Note that I use alice-cli to transfer the 21 million bitcoins. However, I could have done the same thing with bob-cli because anyone can spend the coinbase.

This command creates a transaction. Here our transaction said: send 21000000 BTC to the $ALICE_RECEIVER_ADDRESS address. See the whole command’s reference here: https://chainquery.com/bitcoin-cli/sendtoaddress.

If all goes well, the command returns the ID of the transaction created. This is my transaction ID:

Then let’s generate 102 new blocks. Thus our bitcoins will become spendable.

The command generatetoaddress generates 102 blocks and sends the rewards to the $MINER . The references of the command can be found here: https://chainquery.com/bitcoin-cli/generatetoaddress

generatetoaddress returns an array of block hashes (the newly created ones). You probably get something like this:

Wejust generated 102 blocks. We can see with the command alice-cli listtransactions that a new transaction has appeared of the category “generate”:

Now we are able to send half of the amount to a second address: BOB_RECEIVER_ADDRESS.

Send 10,5 millions of BTC from node 1 (ALICE_RECEIVER_ADDRESS) to node 2 (BOB_RECEIVER_ADDRESS)

Again, you get the ID of the transaction created, which can be found with the command alice-cli listtransactions.

Then, let’s generate the blocks to complete the transaction.

Check wallets balance

To verify if everything has worked correctly, let’s use getwalletinfo .

You can see that each wallet balance has now 10,5 bitcoins.

Congratulations! Your nodes are working properly!

This is the end of this tutorial, don’t forget to stop the daemons with :

3. Bonus & Tips

A small bonus section that references some handy tips and commands.

Find docs and references

Get the general information about the blockchain to which your node is connected with getblockchaininfo

Directly get the wallet balance with getbalance

Get the distribution of the wallet’s bitcoins on the different addresses with listaddressgroupings

Working with JSON using jq

Clients often return JSON as results. jq is a CLI tool letting to handle JSON. Using as Bash pipe, it can be useful for handling results from Elements CLI commands.

ON-X Blockchain (Chain-Accelerator)

Consulting and development in blockchains and crypto-currencies