Part 13: Working with Fullnodes 3
Introduction
This guide aims to program a website to accept Bitcoin. In the previous tutorial, we rebuilt the server component and the back office functionality. At the end of the tutorial, we said we would be replacing “Globee” which we currently use to sell our Open Source “Entity T-Shirt”. Although we are going to do that still, it has been broken down into two parts as the building of the server is a sufficient enough task for this tutorial.
Acknowledgements
The tutorial is built atop others work (as is most things in life) and would not exist without the following tutorials:
The first can be found “here” and it deals with setting up a full node on “Digital Ocean”
The second one can be found “here” and deals with setting up “Nginx” on “Digital Ocean”
Concept and design considerations
Up until this point we have hosted everything locally the“ Bitcoin Core wallet” our server and so on and whilst, this is fine for testing when we actually want to use this in the real world it really ought to be on a server (though in a future tutorial we will build a payment server on a “raspberry PI”).
We have used “Digital Oceans” cheapest box ($15 a month) as we are cheap. This comes with certain restraints that with a bit of creativity we can work around. Principally we cannot have a full node running (that requires 200 gigs) instead, we run Bitcoin Core (yes I know technically we are using bitcoind now I will get to that) in “prune” mode which allows us to limit the blockchain size (We have set it to 5 gigs).
The reason we can do this is that we are not using this node to secure the Bitcoin network we are using it to process payments. That said there is no harm in using a full node you will just have to have at least 200 gigs of Hard drive space which ramps up the cost (starting at $35 a month if you use the following “guide”)
I am going to assume you have already created a digital ocean account and added your SSH key if you have not done this please refer to this “guide” as this covers it very well.
Digital Ocean Setup
Create a droplet
Log in to your Digital Ocean account and click on “droplets” / “create” / “One-click apps” / “NodeJS 8.10.0 on 18.04” as shown below.
*note the version of node or Ubuntu may increase, this is correct as of now.
Next, chose the $15 per month as shown below.
Next, chose the data region that best suits you.
Lastly, tick the SSH key that you set up when you created your Digital Ocean account and click green create button.
If you did all the above correctly (and why would you not have) you will be redirected to the Droplet page and when the blue bar (shown in the screenshot below) is full your droplet will be ready to use. You will receive an email with your password for this Droplet, take a note of this as we will require it later.
Click on the droplet name and copy the IP address (top left) by clicking it
Access the droplet
Open a terminal session and enter the following command replacing the IP address with the IP address of your Droplet. This will ask you to enter the password (the one that was emailed to you) and then ask you to enter a new password (twice).
*Note, replace the IP address below with the one of your Droplet
ssh root@207.154.217.166
Once you have logged in you will see something like the screenshot below
Now we have accessed the Droplet it is time to set up it up correctly and install the necessary software.
Bitcoin core
Step 1: Install Bitcoin Core
We will be installing is the command line version of Bitcoin Core which is known as Bitcoind. It works pretty much the same, all the RPC calls are the same etc. The main difference being that there is no “GUI”, welcome to the world of servers where the terminal is your best friend.
To install bitcoind run the following commands:
sudo apt-add-repository ppa:bitcoin/bitcoin
sudo apt-get update
sudo apt-get install bitcoind
Step 2: Configure Bitcoin Core
First, we have to create a directory to store out Bitcoin configuration file. We do this by using the command below
mkdir ~/.bitcoin
Next, we have to create the config file in the directory we just created.
sudo nano ~/.bitcoin/bitcoin.conf
Paste the text below into the file and save it (ctrl x)
# Generated by https://jlopp.github.io/bitcoin-core-config-generator/# This config should be placed in following path:
# ~/.bitcoin/bitcoin.conf# [core]
# Set database cache size in megabytes; machines sync faster with a larger cache. Recommend setting as high as possible based upon machine's available RAM.
dbcache=5000
# [rpc]
# Accept command line and JSON-RPC commands.
server=1
# Username for JSON-RPC connections
rpcuser=test
# Password for JSON-RPC connections
rpcpassword=test
Let’s take a look at what this config file is doing.
dbcache: This is the size of the database cache.
server: This allows us to use “RPC” commands
rpcuser: The username for the RPC server
rpcpassword: The password for the RPC server
*Note it would be wise to change the username and password to something stronger
Finally, we want bitcoind to restart if it crashes we do this by creating a service for it. Type the following into the terminal
sudo nano /lib/systemd/system/bitcoin.service
and paste in the following into the file and save (Ctrl + x)
[Unit]
Description=Bitcoin's distributed currency daemon
After=network.target[Service]
User=root
#Group=Type=forking
ExecStart=/usr/bin/bitcoind -daemon -server -rest -testnet -deprecatedrpc=signrawtransaction -prune=5000Restart=always
PrivateTmp=true
TimeoutStopSec=60s
TimeoutStartSec=2s
StartLimitInterval=120s
StartLimitBurst=5[Install]
WantedBy=multi-user.target
Step 3: Run Bitcoin Core
To start the service we run the following command.
sudo service bitcoin start
Step 4: Encrypt Wallet
The next thing we want to do is “encrypt” the wallet. We do this by running the following command.
bitcoin-cli -testnet encryptwallet test
Step 5: Create a swap file
Downloading the initial blockchain is a very memory intensive task and as we are running on a cheap Droplet we do not have enough memory to perform the task. Luckily we have a solution to can create a swap file.
We can create a 4 Gigabyte file by typing:
sudo fallocate -l 4G /swapfile
The file is created, but the system does not know that this is supposed to be used for swap. We need to tell the system to format this file as swap and then enable it.
Firstly, we need to adjust the permissions on our file so that it isn’t readable by anyone besides root. Allowing other users to read or write to this file would be a huge security risk. We can lock down the permissions by typing:
sudo chmod 600 /swapfile
Now that our file is more secure, we can tell our system to set up the swap space by typing:
sudo mkswap /swapfile
Our file is now ready to be used as a swap space. We enable this by typing:
sudo swapon /swapfile
We can verify that the procedure was successful by checking whether our system reports swap space now:
sudo swapon -s
We have our swap file enabled, but when we reboot, the server will not automatically enable the file to have to edit the fstab to enable this.
sudo nano /etc/fstab
At the bottom of the file, add a line that will tell the operating system to automatically use the created file:
/swapfile none swap sw 0 0
Now we have a swap file this will help a lot with the initial download of the blockchain.
step 6: disable logging
This step is not necessary but the Bitcoin log file can get huge and is of little use in our circumstance. Bitcoin does not have a way to disable logging but we have a trick do that we simply write it to dev/null which essentially is putting it in the trash.
cd ~/.bitcoin && rm -f /.bitcoin/testnet3/debug.log && ln -s /dev/null debugtn3.log
Step 7: Test It is Running
Now bitcoind is running we can do a couple of tests using bitcoin-cli.
bitcoin-cli is a command-line interface for Bitcoin Core client that connects to a running instance of bitcoind daemon. A user can interact with this program and do any necessary functions with it that will control the bitcoind service as well as the possibility of using a Bitcoin wallet to send and receive funds.
getblockchaininfo
Returns information about the blockchain
bitcoin-cli -testnet getblockchaininfo
getblockcount
Returns the current block count.
bitcoin-cli -testnet getblockcount
get connections
Returns the number of active connections.
bitcoin-cli -testnet getnetworkinfo | grep connections
test the REST
Run a curl command to get make sure rest is running.
curl http://127.0.0.1:18332/rest/chaininfo.json
Configure Server
Step 1: Install Nginx
First, we install “Nginx”:
sudo apt-get install nginx
Step 2: Adjust the Firewall
Next, we reconfigure our firewall software to allow access to the service. We
At this point, as we will not configure SSL (as we will use “Cloudflare”) for that we enable Nginx to allow traffic on port 80 by typing:
sudo ufw allow 'Nginx HTTP'
Step 3: Check your Web Server
If the above has been done correctly you should be able to browse to your site by browsing to the IP address of the Droplet.
*Note, replace the IP address below with the one of your Droplet
http://207.154.217.166
if it worked you will see the Nginx default webpage as shown below
Step 4: Install Server
Now we have to load our “e-commerce code” onto the server we do this using “git”.
Firstly, init a new “git” repo
git init
Next, we add a repo
git remote add github https://github.com/cryptoskillz/Bitcoin-Tutorial.git
Finally, pull the latest code
git pull github master
Step 5: Starting the server
Just like when we run it locally we can start the server by typing the following
cd server
node app.js
Now, we can test this by opening the following URL in a browser
*Note, replace the IP address below with the one of your Droplet
http://207.154.217.166/backoffice/test
If it has been successful you will see something like the screenshot below
Of course just like bitcoind we want to the e-commerce server to keep running after crashes etc. We do this by using a package called “forerver.js”.
Install forever by typing the following command:
npm install forever -g
Then add our app to it (make sure you are in the server directory)
forever start app.js
We can now test everything worked as excepted by leaving the terminal session by typing the following
exit
and then requesting a Bitcoin Address from the server in a browser
http://207.154.217.166/api/address?uid=3
If it worked you will see something like the screenshot below. This is the server returning us a new bitcoin address
Add A Record
We add an A record in Cloudflare so it is a better URL we have pointed our server to “ecs.cryptoskillz.com”. If you want to know how to set up an A record on Cloudflare click “here”.
E-commerce server changes
The latest code can be found here. As we only made a couple changes so we will quickly cover them below.
app.js
We added a check to make sure the process.env vars were set and if not use defaults. This should make it work on a number of servers such as VPS that does not allow you to set environment vars.
*Note, you will see I set the port to 8080 this is again so it will work out of the box on the most number of configurations.
if (process.env.emailsmtp == undefined)
process.env.emailsmtp = 'smtp.ethereal.email';
if (process.env.emailusername == undefined)
process.env.emailusername = 'rjf2z2dghi4bn3yv@ethereal.email';
if (process.env.emailpassword == undefined)
process.env.emailpassword = 'NG4PPPuqvZaagwSjWV';
if (process.env.walletpassphrase == undefined)
process.env.walletpassphrase = 'test';
if (process.env.walletaccount == undefined)
process.env.walletaccount = 'theaccount';
if (process.env.PORT == undefined)
process.env.PORT = 8080;
Finally, We added an RPC call to the test back office/test endpoint so we can easily get a block count (good for testing)
app.get("/backoffice/test", (req, res) => {
//load the back office helper
let backofficehelper = require('./api/helpers/backoffice.js').backOffice;
let backoffice = new backofficehelper();//debug
backoffice.test(req,res);
});
backoffice.js
this.test = function test(req,res)
{
client.walletPassphrase(process.env.walletpassphrase, 10).then(() => {
//create a new address in theaccount account :]
client.getBlockCount().then(count => {
res.send(JSON.stringify({ status: "ok","count":count }));
});
});
We also made some changes to CDN js but they are unused as yet the will be covered in the next tutorial.
Conclusion
We have successfully deployed a Bitcoin Core node to a $15 a month Digital Ocean Droplet (which is about the cheapest way possible to have a Bitcoin Node) and installed our e-commerce server. Next time we will configure www, CDN and admin to use this new server. We will also update WWW so we can finally sell our T-Shirt using not 3rd parties!