Bitcoin Node Setup

Learn how to set up your own Bitcoin node, how to work with the various nets the BTC Blockchain supports, and how to set up your first wallet.

Bitcoin Node Setup

This is the part where the real fun with the Blockchain starts. I will show you how to set up a Bitcoin node, explain what the differences between modes are, show you how to send a simple transaction and describe the transaction lifecycle. This will be a prelude to integrating a JavaScript application with Bitcoin payment.

Requirements: For the purpose of our examples, I will assume that you are familiar with JavaScript and the npm package manager.

What is a Node

The node is a client and a server kept in one software application. It can either run as both or one can be disabled. If you want to integrate your application with Bitcoin payments, then maybe it is not the best idea to waste computation power for mining.

The node provides all necessary features that support mining and synchronizing blocks, sending transactions, etc. Once you install the node, it will look for other nodes to connect with and download the latest state of the Blockchain. Be prepared, this process takes a while. The current Bitcoin database is about 160GB, and it grows every day.

Peer-to-peer

The Bitcoin network is based on the peer-to-peer solution. There is no central point or a server that manages how the data is transferred between the users, and there is no central database where the data is kept. So, the whole blockchain needs to be downloaded and synchronized by every node.

Once the node starts running, it checks the list of last connected IP addresses to synchronize with them. This list is broadcasted with the last block. If no list is saved, the node pings well-known list of IPs that are connected to the network. This list has been hardcoded since 2010. The last step of seeking a node in the network is DNS seeding.

Which node should I choose

Bitcoin is not a specific piece of software or an application. I would say that it is a full description of a protocol that can be implemented by anyone, and anyone can join the network with their own software. It does not mean that if you implement your own client, it will be able to broadcast corrupted data and get it accepted. The Blockchain secured transactions and blocks from such a situation.

That is why there are so many available clients on the Internet. There is btcd (with btcwallet), Bitcore, Bcoin, decred, etc. There are a couple of differences between them — some separate the Blockchain management from transactions, some provide additional indexing features, andI will stick to Bitcore because it supports the JavaScript and is used by enterprise businesses like Trezor, Bitpay, or Streamium. It keeps all features in one application, so that will make our examples easier to work with. some add support for unspent transactions lists. However, all of them are fully-working nodes that can be used in our example.

I will stick to Bitcore because it supports the JavaScript and is used by enterprise businesses like Trezor, Bitpay, or Streamium. It keeps all features in one application, so that will make our examples easier to work with.

Modes

When we run the node without any additional flag, it will connect to the main Bitcoin Blockchain and download approximately 160 GB of data or more. This is expected behavior when you want to run the node in the production environment, start mining or make real transactions. But, during the development process, it would be much easier and cheaper to mine blocks faster and avoid wasting funds on the fees. Most of the nodes available on the Internet can connect to other Bitcoin networks or simulate own network, and each network mode has own purpose.

There are times when people get confused with the modes and transfer Bitcoins from the test mode to the main network. It is not possible to transfer coins between different networks. They will be lost.

In every mode, the addresses start with a different identificator. In the TestNet it is 0x6F or 0x9F rather than 0x00 in the MainNet. If you will transfer coins from a TestNet address starting with 0x6F to an address starting with 0x00, the node will not inform you about any problems and will just make this transaction. The funds, however, will be moved to an address that does not exist. This happens because the Blockchain supports full anonymity. It does not store a list of available addresses, only a list of transactions. Bitcoin does not know whether this address does or does not exist — it gives full freedom to the money owner, with all responsibilities and punishments.

MainNet or LiveNet

This is the main ledger that everybody operates on. If somebody is talking about the value of Bitcoin, she refers to this particular network and all Bitcoins stored there have real value. The name may depend on the node you are using. In the btcwallet, it will be MainNet, while in Bitcore, it will be the LiveNet.

The default listening port is 8333, and the RPC port is 8332.

TestNet

This mode is intended for test transactions and the development process. Twice before, people started trading these funds for real money, so the TestNet was reset twice, and all assets were lost. Bitcoins stored in the TestNet do not and should not have any real value.

The TestNet allows to fully simulate the behavior of the MainNet without risking real money or assets. Bitcoins are free and easy to mine. The minimal difficulty level is half of the minimal difficulty level of the MainNet, and if no block is mined in 20 minutes, the difficulty level is reset to the minimum value.

Also, ports are different — 18333 is for the listening protocol and 18332 for the RPC API. The Blockchain size is approximately 4-16GB.

SimNet

The Simulation Network is intended for tests on a local machine. This mode allows us to run your own, private network and to force the Bitcoin node to mine or generate blocks whenever we want. Every action is done manually. If a transaction is sent, it will not be mined or confirmed until you run a specific command.

I use this method to speed up the development and debug the transaction process, but because of the number of transactions that I can manually generate and confirm in one block, its use is limited, and I often receive empty blocks. I strongly recommend to re-test all features with the TestNet or even the MainNet.



Installing bitcore

The Bitcore installation process is fast if you know npm. The full operation is described at https://bitcore.io/start, and it contains only 3 points, but we will extend it a little bit because bitcore also requires additional libraries which are only mentioned in the official installation guide.

Install Required Software

Bitcore requires you to install node.js (version 4 or higher) and ZeroMq without root privileges.

For linux users:

  apt-get install libzmq3-dev build-essential

For mac users:

  brew install zeromq

Install Bitcore with NPM

Bitcore is installed as an npm package with the following command. It will be installed globally.

  npm install -g bitcore

Start your node

To synchronize Bitcore with MainNet, run it with the bitcored command. This process will take approximately a week or so because the Bitcoin blockchain is about 160 Gb at the moment.

  bitcored

But for development purposes, we would like to proceed a little bit faster, omit this process and go straight to the next steps of our example. I would recommend switching to the TestNet mode (Test network). Bitcore does not support the SimNet mode (Simulation network) yet. TestNet is about 10-16 Gb and will synchronize in at maximum a couple of hours if not faster.

Because the TestNet does not operate on real money, it is better for development and test purposes — there is no need to have real Bitcoins or spend them. Thus, the possibility of losing funds is limited.

To run Bitcore in the TestNet mode, an additional node needs to be created by running the command:

  bitcore create mynode --testnet

After that, a new directory will be created at mynode, where the Blockchain files will be stored. Another important thing is that the configuration, which can be found at ~/.bitcoin/bitcoin.conf, will describe the method and credentials for the API endpoint.

{
  "network": "testnet",
  "port": 3001,
  "services": [
    "bitcoind",
    "web"
  ],
  "servicesConfig": {
    "bitcoind": {
      "spawn": {
        "datadir": "./data",
        "exec": "/home/papi/.nvm/bitcore-node/bin/bitcoind"
      }
    }
  }
}

After the node is created with the config file above, it needs to be synchronized with the blockchain. Enter the mynode directory which was created for TestNet and run Bitcore.

cd mynode
bitcored

A list of downloaded blocks will appear. They should be grouped in bunches of around 2000 each. At the very end of each information line, there will be progress information. Each block can be described with the hash and also a number, and these numbers are in a specific order. The currently accepted block number can be previewed on every website dedicated to Bitcoin which also supports TestNet, e.g., https://live.blockcypher.com/btc-testnet/. So, at least you will know how many blocks there are to download and can calculate how much time your node needs to synchronize.

Block explorer

Bitcore can be connected with a user interface where all the information about the current state of the Blockchain can be presented. I am pretty sure that you are familiar with this interface. Maybe the colors are different, or the place where buttons are located, but if you have even the most basic knowledge about Bitcoin, you probably visited sites like blockchain.info.

Running commands on the command line can make the learning process faster, but copy-pasting the block and transaction hashes may be time-consuming during everyday work. Therefore, I prefer to use the web interface to control what is published in the blockchain.

Why do I not recommend you just visit an external page that will present all this information? It may happen that you will publish a transaction and want to see what happens with it before every other node on the internet will get information about it. Also, your node may be desynchronized (has not downloaded all the blocks from the Blockchain). So, if the transaction is on the Blockchain and your node doesn't know about it, you may miss this piece of information.

To install the bitcore user interface, you just need to run:

bitcore install insight-api insight-ui

This will download the packages insight-api and insight-ui and run the npm installation process. The next time you start your node, the user interface will be available at http://localhost:3001/insight.



Setting up Your Wallet

After the node is synchronized, to operate with Bitcoin, a Wallet needs to be created. This will allow you to not only review what is stored in the Blockchain but also send and receive transactions, create new addresses, etc. Most of the next steps will be done from the command line, but the information about the Blockchain state will be visible in the User Interface. After a wallet or a new address is created, it can be previewed on the website.

Installing the Wallet Service

For now, Bitcore is able only to read the data from the Blockchain and does not support sending and receiving funds or managing wallets and addresses. We need to install additional software called the Wallet Service. The Wallet Service’s main responsibility is to publish data to the blockchain and maintain your wallet.

The Wallet Service requires a MongoDB database to run. Other nodes do not need to operate with the local database but also do not provide a web interface, only the command line interface.

Mac OS X The easiest way to install MongoDB is to use brew:

brew install update
brew install mongodb
mkdir -p /data/db
sudo chown -R `whoami` /data/db #this assumes that the next step will be run by the current user
Mongod

Ubuntu/Debian MongoDB is included in the standard repository and can be installed from aptitude:

sudo apt-get install mongodb

This should automatically start the mongodb service in the background.

Install the Wallet Service to the MyNode The Wallet Service can be installed from the command line, like the User Interface.

cd mynode
bitcore install bitcore-wallet-service
bitcore install insight-api
npm install -g bitcore-wallet

Those steps will install the insight-api plugin for bitcore, the wallet-service, and the command line available from npm. Now, Bitcore should be restarted.

bitcored

These steps provide us with the ability to operate on the wallets and addresses, but we still need to open a new wallet.

Opening a New Wallet

The bitcore-wallet npm package adds two commands to the CLI — the wallet-create and the wallet. They allow us to operate on funds, addresses, and transactions, but first of all, the Wallet should be created.

The Wallet is the primary place where the funds are collected. The mechanism of Bitcoin assigns many addresses to a single Wallet.

wallet-create -h http://localhost:3232/bws/api --testnet 'myWallet' 1-1

Now, bitcored can manage the funds that will be sent to one of its addresses. Because Bitcoin provides full anonymity and the transaction title, the sender's name or any additional information cannot be included in the transfer; we need to create a new address for each payment or sender. All the funds received at the addresses that we will create will be automatically assigned to the wallet.

To create a new address, we can use the address method from the API which is available from the CLI:

wallet -h http://localhost:3232/bws/api address

The new address will be output. This address can be used to transfer funds to your wallet. However, the question is how to get the funds into TestNet. Mining requires a lot of computational power, time and does not give you any guarantees that it will succeed. Fortunately, Bitcoins available in the TestNet are free, and people like to share them. You can use one of the websites that will send you Bitcoins when you enter the address, e.g., http://tpfaucet.appspot.com/ or https://testnet.coinfaucet.eu/en/.

Information about my test transaction can be found at the address https://testnet.blockchain.info/address/mjm7r1GZDnKUFu5NdtiPg49gyRrwwuhV9g or in our Insight application at http://localhost:3001/insight/address/mjm7r1GZDnKUFu5NdtiPg49gyRrwwuhV9g.

Remember that TestNet addresses and LiveNet addresses are different. If you enter the LiveNet address into one of those services, the funds will be lost, because the networks are totally separated and do not know about each other.

Receiving Bitcoins

Now, we have to wait for the Bitcoins to be transferred to our wallet. The first step that will be done by the external application is to broadcast the information about a new transaction to the TestNet Blockchain. You can find this transaction by entering the transaction hash number into the User Interface that we installed. It should have 0 confirmations. Don't worry if the UI cannot find this transaction. Check if the node is fully synchronized with the blockchain. It may take a little bit.

The second thing that will happen is that the transaction will receive the first confirmation. Now, the transaction is included in the last block and can be easily found in the Blockchain. From now on, the funds are in your wallet, assigned to the address that they were sent to.

The third step is to get more confirmations. I described in the previous parts that the number of confirmations is strictly related to the number of blocks that are mined after the block that includes the transaction. The more confirmations a transaction has, the more confident you can be that the transfer is legit.

Checking the Wallet Balance

When the transaction has at least one confirmation, the Bitcoins will be assigned to your wallet. To check the current wallet balance, we can use an API method:

wallet -h http://localhost:3232/bws/api status

This will display the current state of the funds that are assigned to all the addresses you manage.

Now, it may be time for the first exercise for you. Please create a few new addresses, send Bitcoins from TestNet to them, and see what happens. All Bitcoins sent to all the different addresses will be summed in the wallet. However, if you transfer the funds from your wallet to another wallet, Bitcoin will get the funds from selected addresses, sum them, and send them to the address.

If the sum is different than the amount of funds that you want to transfer and the Blockchain fee, the difference will be transferred to newly created address. Do not worry; this address will be automatically assigned to your wallet.

On the left side of the screenshot, you can see all the addresses that are assigned to the wallet. The transfer could not be handled by only one address, so the Bitcoin mechanism created a transfer from three addresses assigned to your wallet to the final address. However, the sum of the funds does not match the amount of Bitcoins that should be transferred, so a new address was created (the 1ErMJj176E46irGo8vn6hrqaSpJ3yow4Sx) which is assigned to the initial wallet and the funds are back.

As you can see, you do not need to worry about the addresses that you created. After a while, there will be plenty of them, and you will probably not remember which was created for what purpose. But it does not matter, as long as you can transfer the funds from a single wallet.

Conclusion

After this chapter, our fully working node is installed, configured, and set to TestNet. We have already gained Bitcoins for development purposes, and we are ready to start programming. In the next part, I will explain the transaction lifecycle in details and we will set up a Node.js application that will communicate with the Blockchain API.

04 Milestones

SCALE YOUR DEVELOPMENT TEAM

We help you execute projects by providing trusted Blockchain developers who can join your team and immediately start delivering high-quality code.

Hire Blockchain Developers