Skip to main content

Simple Blockchain Library Built With Nodejs

· 3 min read
Sivabharathy

Building a Single-Node Blockchain Library with Nodejs, ExpressJS and LevelDB

Introduction

In the ever-evolving world of blockchain technology, creating your own blockchain can seem like a daunting task. However, with the right tools and libraries, it can be simplified significantly. This article will walk you through the features, installation, usage, and development process of a blockchain library that provides core blockchain functionality accessible via REST APIs and stores data using LevelDB. The current release supports a single-node blockchain feature.

Features

Our blockchain library comes packed with essential features to get you started with blockchain development:

  • Core Blockchain Functionality: The library exposes fundamental blockchain operations via REST APIs.
  • Data Persistence: Utilizes LevelDB to store blockchain data, ensuring durability and efficient data retrieval.
  • Single-Node Blockchain Implementation: Ideal for learning and testing blockchain concepts on a single node.

Installation

Prerequisites

Before you begin, ensure you have Node.js installed on your machine. If not, download and install it from the official Node.js website.

Cloning the Repository

First, clone the repository to your local machine:

git clone https://github.com/gotocva/nilajs-blockchain.git

Navigate into the project directory:

cd nilajs-blockchain

Installing Dependencies

Install the required dependencies using npm:

npm install

Usage

Running the Server

To start the Blockchain server, run the following command:

node index.js

This will start the server on http://localhost:3000.

REST API Endpoints

Add a New Block/Transaction

  • Endpoint: POST /block
  • Description: Adds a new block/transaction to the blockchain with the specified data.
  • Request Body:
{
"data": {
"from": "aravind",
"to": "boobalan",
"amount": "125"
}
}
  • Response:
{
"index": 1,
"timestamp": "1718867161",
"data": {
"from": "aravind",
"to": "boobalan",
"amount": "125"
},
"nonce": 10457,
"previousHash": "00005042da8f149d499f3942ea5bf077e681e2d49e12a57e692a9fadbc99ec8f",
"hash": "0000931257811c594529f0474b50d8c0bd3d1a940e77cd4796235abf10aa8df1"
}

Get Blockchain

  • Endpoint: GET /blockchain
  • Description: Retrieves the entire blockchain.
  • Response:
[
{
"index": 0,
"timestamp": "1718866875",
"data": {
"title": "Genesis block of the blockchain",
"difficulty": 4,
"consensus": "Proof of work"
},
"nonce": 4104,
"previousHash": "0000XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"hash": "00005042da8f149d499f3942ea5bf077e681e2d49e12a57e692a9fadbc99ec8f"
},
{
"index": 1,
"timestamp": "1718867161",
"data": {
"from": "aravind",
"to": "boobalan",
"amount": "125"
},
"nonce": 10457,
"previousHash": "00005042da8f149d499f3942ea5bf077e681e2d49e12a57e692a9fadbc99ec8f",
"hash": "0000931257811c594529f0474b50d8c0bd3d1a940e77cd4796235abf10aa8df1"
}
]

Example

Adding a New Block

You can use curl to add a new block:

curl -X POST http://localhost:3000/block -H "Content-Type: application/json" -d '{
"data": {
"from": "aravind",
"to": "boobalan",
"amount": "125"
}
}'

Retrieving the Blockchain

Retrieve the entire blockchain using curl:

curl http://localhost:3000/blockchain

Roadmap

The following features are planned for future releases:

  • Core Blockchain
  • Proof of work - Consensus
  • Persistent storage using LevelDB
  • REST API
  • Blockchain Explorer
  • Digital signature
  • Distributed networks
  • Broadcasting & Queuing the transactions
  • Consensus for Peer to Peer

Conclusion

With this blockchain library, you have a solid foundation to build and experiment with blockchain technology. Whether you are looking to learn more about blockchain, develop your own blockchain applications, or contribute to open-source blockchain projects, this library provides the tools you need to get started. The simplicity of REST APIs combined with the robustness of LevelDB makes it an excellent choice for both beginners and experienced developers alike.