With the recent heightened interest in crypto-space technologies in general and blockchains in particular, I thought it would be interesting to show how to build and run a simple blockchain explorer.
The easiest way to go would obviously be to consume a ready-made API service such as blockchain.info’s Data API, however then we’d miss most of the fun, and so instead we’re going to set up a Bitcoin core full node, and consume its native JSON RPC API in a web front-end Blockchain Explorer application.
Ready? let’s do it!
Architecture and Technologies
The frontend SPA is written in Angular 5 using Angular CLI so we can quickly scaffold and modify it. It communicates with a Bitcoin JSON RPC Proxy Server is written in node.js and using the Express framework. The Proxy communicates directly with either bitcoind (the bitcoin core daemon) or bitcoin-qt (bitcoin core bundled with a simple UI).
System layout is pretty simple. Here are two possible configurations:
Blockchain Explorer – Layout Option 1
In this configuration, both the Bitcoin Core node and the JSON RPC Proxy server are running on the same machine.
This is the simplest and easiest configuration to setup.
Blockchain Explorer – Layout Option 2
In this configuration, the Bitcoin Core node runs on a different machine than the JSON RPC Proxy server. If we want the Bitcoin Core node to accept requests from any source, we will need to use a reverse proxy to tunnel any source requests to localhost port 8332.
Another possibility (without using a reverse proxy) would be to configure the JSON RPC Proxy server machine’s IP address in bitcoin.conf file as follows
Running The Bitcoin Core Node
There are several options for running a bitcoin core full node; it can be downloaded directly from bitcoin.org, launched as a docker container (for example you can use this docker image, note that this option is limited to bitcoind, i.e. headless), or compiled from sources (you can find many instruction sources on the web).
Whichever path you take, make sure to provide it with a volume with at least 170GB of free space, 2GB swap space and 1GB of RAM.
Before you run the node (either bitcoind or bitcoin-qt) make sure to setup (create it if it doesn’t exist yet) the bitcoin.conf configuration file as follows:
bitcoin.conf should be located at the bitcoin core root data directory. This is the folder that will be used by bitcoin core to create and maintain its databases, indexes and the config file.
By default on Windows installations its location is %APPDATA%\Bitcoin, on Linux it is ~/.bitcoin/ and on Mac OS it is ~/Library/Application Support/Bitcoin/.
server=1 property tells the node to run the JSON RPC service and accept connection requests (by default for mainnet on port 8332)
txindex=1 tells the node to maintain a transactions index. Without it, the node will not index transactions (only blocks) so we won’t be able to run RPC queries to get raw transaction details. If you forget to set this flag before running the node and then want to enable it, then you’ll first need to run the node with the -reindex command line argument to construct the transactions index.
disablewallet=1 tells the node not to provide any wallet management functionality, as we’re only interested in querying the blockchain data for the sake of this exercise.
rpcallowip=127.0.0.1 tells the node to accept incoming connections on the local interface. If you plan to install the proxy on a different machine than the bitcoin core node, you’ll need to either set the ip address to your proxy’s machine address, or add more rpcallowip entries (you can add as many as you need), or install a local reverse proxy to tunnel requests from all interfaces to the local 127.0.0.1 interface.
If you closely follow the instructions detailed above you should have your bitcoin core node running without too much hassle, and all you need now is to allow it a few days to sync with the network, download the blockchain and create the transactions index.
Its purpose is to accept AJAX calls from the frontend application over its REST API, format the requests according to the JSON RPC protocol, relay them to the bitcoin core node and send the response back to the frontend. It currently exposes the following REST endpoints:
health route is used by the frontend to check that the proxy is up and running.
The connect route is used to establish a connection with the bitcoin JSON RPC Proxy server. This method has to be called first before we can make any further queries. The frontend calls this method when we click the “Connect” button.
JSON RPC Methods
The proxy calls the following RPC methods on the Bitcoin core full node:
Go into the project folder: cd bitcoin-json-rpc-proxy
Install dependencies and compile: npm install
Run the proxy server: npm start
The Web Frontend Application
Our web frontend application (project on github) is written in Angular 5 and was built using the Angular CLI scaffolding tool.
This version is modest and currently provides the ability to view basic blockchain information, search for blocks and traverse them by paging to the next/previous block, and drill down into individual transactions.
To start the exploration the user needs to login by providing the full node details: host, port, rpc username and password.
Run the frontend (will automatically spawn the browser): npm start
To Be Continued…
As I’ve long wanted to write a multi-part post, this would be an excellent opportunity. In the next parts of this series I’ll cover additional interesting subjects (not necessarily in that order):
Add a blockchain indexer to the mix so we can further search for and check any public (wallet) address (look at all its related transactions, calculate its current balance based on the unspent transaction outputs etc.)
Find block by block number (height)
Decypher secret messages cleverly hidden within the blockchain data
Fiddle with cryptographic functions
Sign and verify messages
Thanks for reading through. I do urge you to go ahead and try it out yourself, since there are plenty learning opportunities during the process (and of course you’d be contributing to the network’s security by running a full node).