DEV Community

Gabriel Leonte
Gabriel Leonte

Posted on

The chain - Just a simple way to integrate a blockchain

So, let's talk about The chain. The chain its a npm package made for anyone interested to build a Node.JS Blockchain App.

How it started?

Some months ago, i've been looking for a blockchain package, something that can help me build a blockchain based social media.
So i have been searching the hole npm for one, that can create a simple blockchain database in less then 5 minutes, which can create new block at a custom time interval, and also provide a simple API to make my life easier.

Why The Chain instead of a simple database

The Chain its not only a sqlite3 database with some default table, The Chain its a package that comes with ways that must help the developer reduce the time to build his awesome blockchain application.

Features

  • Easy to integrate to existing apps
  • A very customizable chain
  • Custom block time
  • Block verify function
  • Local database

Incoming Features!

  • Node system - this will let you sync the chain between multiple nodes
  • Multiple extract data features
  • Block confirmation system
  • SocketIO API
  • Documentation

Installation

The Chain requires Node.js v10 (only tested) to run.

Install The Chain...

$ npm install thechain --save
Enter fullscreen mode Exit fullscreen mode

A simple demo

const Chain = require("thechain");
const blockchain = new Chain(path, port, blockTime); // or new Chain() for a fast blockchain

const block_one = blockchain.getBlock(1);
console.log(block_one); // this will print the genesis block
Enter fullscreen mode Exit fullscreen mode

Default params:

const path = "./chain/chain.db3";
const port = 4444;
const blocktime =  60000; // 1 minute blocktime in ms
Enter fullscreen mode Exit fullscreen mode

Methods

Verify the Chain

blockchain.verifyChain(); // No return
Enter fullscreen mode Exit fullscreen mode

This method will be initially called to check every block, you can use it whenever you want, but warning, this method will stop your script until every block is checked.

Create new Block (WARNING! Better to set a block time and never use this method!)

blockchain.createNewBlock(); // No return
Enter fullscreen mode Exit fullscreen mode

This method will create a new block when it is called, the block data will be the data contained in the data object at the block time.

Data push

const Object = ["test1", "test2"];
blockchain.push(Object); // No return
Enter fullscreen mode Exit fullscreen mode

This method will push data into the block, and the block will be generated with the pushed data.

Get last 100 blocks

const last100blocks = blockchain.getLast100();
console.log(last100blocks); 
Enter fullscreen mode Exit fullscreen mode

This method will return the last 100 blocks.

Get x block

const blockNumber = 2; // Block number id
const block = blockchain.getBlock(blockNumber);
console.log(block); // This will print the block data
Enter fullscreen mode Exit fullscreen mode

This method will return the block with x number.

Development

PRs Welcome

Want to contribute? Great! Start by telling us your wishes!

License

The chain is licensed as MIT

Free Blockchain Software, DAM Yeah!

Top comments (4)

Collapse
 
pghislain profile image
Pierre Ghislain • Edited

Hi Gabriel, I am on a new project that will use block chain and I would like to start using your node package. May be we can have a chat ? pmh.ghislain arobase gmail etc...

Collapse
 
gabrielleonte profile image
Gabriel Leonte

Of course, you can find me on Discord: Gabi#6896

Collapse
 
pghislain profile image
Pierre Ghislain

Pierre#2055 I requested your friendship

Thread Thread
 
gabrielleonte profile image
Gabriel Leonte

Gabu#6896 Sorry