Part 1: How to Build a Blockchain Application in Javascript

Nick Saponaro
HackerNoon.com
4 min readJun 26, 2019

--

Motivation

Blockchain and Cryptocurrency are burgeoning tech fields that you should know about as a developer in the modern age. But where do you begin? I find it’s always best to acquaint myself with new technologies by doing and learning along the way. In this series, I will show you how to create a simple cryptocurrency application in NodeJS that can be used to return data about the blockchain.

This app could be further extended to create a blockchain explorer, wallet, among other use-cases. If you build something cool, let me know in the comments below.

To review this project’s code, check out the GitHub page.

Pre-requisites

Before moving forward, ensure that you have met the following pre-requisites first:

  • Node version 8.9.0+
  • NPM version 6.9.0+
  • Postman 7.1.1+

Setup

Create a new project or fork mine. For this example, I’ll use node-rpc-tutorial as my project name.

For this tutorial, we will be using Divi Project’s Divi Core blockchain.

Mac, Windows, Linux

Download the latest version of Divi for your operating system.

ARM-based machines

If you’re using ARM architecture, download this version.

Configure Divi

Unzip the file and navigate to the unzipped directory via your favorite terminal application.

If you run ./divid from this directory, the daemon will return the following error:

Error: To use divid, or the -server option to divi-qt, you must set an rpcpassword in the configuration file:
/Users/99darwin/Library/Application Support/DIVI/divi.conf
It is recommended you use the following random password:
rpcuser=divirpc
rpcpassword=3PfKoXerwDMCA7vHv95WbecWsHzL664sfnNpottDH8Uz

(you do not need to remember this password)
The username and password MUST NOT be the same.
If the file does not exist, create it with owner-readable-only file permissions.
It is also recommended to set alertnotify so you are notified of problems;
for example: alertnotify=echo %s | mail -s "DIVI Alert" admin@foo.com

The critical part here is the section I have highlighted in bold.

Open another terminal tab or window and navigate to the relevant Divi Data Directory for your OS. Open the divi.conf file in your favorite text editor and make the following edits using the rpcuserand rpcpassword generated in the error response above:

# divi.confrpcuser=divirpc
rpcpassword=UseYourRandomlyGeneratedPWHere
daemon=1

Now, back in the first terminal window, you can run

./divid -addressindex -txindex

The addressindex and txindex flags ensure that your node will sync all transaction & address info from throughout the network.

Configure Application

Your node will take a while to sync, so in the meantime, we can get the application set up.

Let’s get the filesystem situated before we write any code.

Install dependencies

Initialize a new package.json file by running npm init from the root directory of your project and install the following dependencies.

npm install --save body-parser path divid-rpc express request request-promise

Open package.json and add a start script.

# package.json{    ...    "scripts": {        "start": "node index.js"    }    ...}

Build the file structure

Create the following files in the root directory of your project:

  • keys.js
  • index.js
  • src/config.js
  • src/api.js

If you’re on OSX or Linux, you can copy/paste the line below.

touch keys.js index.js && mkdir -p src && touch src/config.js src/api.js

Configure authentication

Open keys.js in a text editor and add your rpcuser and rpcpassword to the key/value pairs to be exported by the module as in the example below.

# keys.jsmodule.exports = {    user: 'YOUR_RPCUSERNAME',    pass: 'YOUR_RPCPASSWORD'}

Open src/config.js and configure divid-rpc to work with your local node.

# config.jsconst keys = require('../keys')module.exports = {    config: {        protocol: 'http',        user: keys.user,        pass: keys.pass,        host: '127.0.0.1',        port: '51473'    }}

Now your project is ready to send/receive RPC messages to the Divi daemon programmatically using Javascript.

Build the API

Now that the application is configured, we can begin writing some endpoints that will be used for our blockchain explorer. Let’s start with something simple.

The /blockcount endpoint will return the current number of blocks that our node currently has synced.

Configure server

Open index.js and configure a simple server using Express

Test your server by running npm start from the root directory.

You should receive the following response:

App listening on port: 1337

Test the API

Ensure that divid is running as well as your server before continuing. If you stopped the daemon at any time while following this tutorial, go back to the unzipped directory where the Divi executables are stored and run ./divid.

Open Postman and start a new GET request.

In the “Enter request URL” field, type

localhost:1337/blockcount

Press “Send” and Postman will return a response similar to the following:

{
"blockcount": 384339
}

Voila! You have successfully created an API endpoint using a real, running blockchain.

In Part 2, I will discuss how to extend this API to be more robust and how to use the data returned to create a simple blockchain explorer application.

--

--