izigma-blockchain

2.0.1 • Public • Published

iZigma Blockchain

This is a blockchain library that can be used to create a complete blockchain network with multiple blockchain support. To launch a you only need to create a object in your project using the library. If you want more blockchains, create more objects. To mine blocks, read the chain or to create trasacations use the methods binded to the object. Complete method list can be obtain by reading the index.js file of the library.

Configuration

To use or test this library you need to add a configuration file. Create a file named izigma-config.js on the project root directory.

Then past the following code

const DIFFICULTY = 2;
const MINE_RATE = 1000;
const INITIAL_BALANCE = 500;
const MINING_REWARD = 50;
module.exports = { DIFFICULTY,MINE_RATE,INITIAL_BALANCE,MINING_REWARD};

Here the DIFFICULTY attribute is the initial difficulty for the proof of work consesus. MINE_RATE is the how often a block is created in the mine in mille seconds. INITIAL_BALANCE hold the wallate balance of the node. And MINING_REWARD is the reward recives for the miner for doing mining works...

Method List

  • addBlock(data) -> add a new block to the chain without mining.
  • getChain() -> Read the blockchain.
  • isValidChain(chain) -> Check incoming chain from the network is valid compaird to the current blockchain
  • replaceChain(newChain) -> Replace the current chain with a new chain
  • getChainFileName() -> geth the blockchain file name
  • calculateBlockchainHash() -> calculate a hash value for the whole chain
  • createWallet(publicKey, privateKey, algorithm) -> use only when you need to change the wallet.if not auto generated wallet is there,keys generated with RSA algorithm
  • createTransaction(recipient, amount) -> create a new transaction
  • createRecord(data) -> create a new record
  • calculateBalance() -> calculate the wallet balance for the chain
  • calculateSpecialCoinBalance(coin) -> calculate the balance for a customly generated coin
  • getPublicKey() -> Get the public key of the node
  • isContributed() -> Check is there any previous transactions
  • getTransactionPool() -> get the transactions in the transaction pool
  • clearTransactionPool() -> clear transaction pool
  • updateOrAddTransactionToTransactionPool(transaction) -> add or update existing transaction in the pool
  • getSpecialCoinTransactionPool() -> get the custom coin transactions in the transaction pool
  • clearSpecialCoinTransactionPool() -> clear special coin transactions in the pool
  • updateOrAddTransactionToSpecialCoinTransactionPool(transaction) -> add or update existing custom coin transaction in the pool
  • getRecordPool() -> get the current record pool
  • clearRecordPool() -> clear the current record pool
  • addRecordToRecordPool() -> add record to the the record pool
  • mineTransactions() -> mine the transaction
  • mineRecords() -> mine the records
  • generateSpecailCoinandDeploy(recivers, coinsForEachReciver, coinName) -> create and deploy a custome coin to the blockchain

Samaple Code

const express = require ('express');
const bodyParser = require('body-parser');
const Blockchain = require('izigma-blockchain');

const HTTP_PORT = process.env.HTTP_PORT || 3001;

const app = express();
//To work with more chains create more objects like chain_2,chain_3
const chain = new Blockchain();
chain.createWallet();
app.use(bodyParser.json());

app.get('/blocks',async(req,res) =>{
    let blockchain = await chain.getChain();
    res.json(blockchain);
});

app.post('/mine',async(req,res) =>{
    const block = await chain.addBlock(req.body.data);
    console.log(`New block added : ${ block.toString()}`);

    let blockchain = await chain.getChain();

    res.redirect('/blocks');
});

app.get('/transactions',(req,res)=>{
    res.json(chain.getTransactionPool());
})

app.post('/transact',(req,res)=>{
    const {recipient, amount} = req.body;
    const transaction = chain.createTransaction(recipient,amount);
    res.redirect('/transactions');
});

app.get('/mine-transactions',(req,res)=>{
    const block = chain.mineTransaction();
    chain.clearTransactionPool();
    console.log(`New block added : ${block.toString()}`);
    res.redirect('/blocks');
});

app.get('/public-key',(req,res)=>{
    res.json({
        publicKey: chain.getPublicKey()
    });
})

app.listen(HTTP_PORT, async () => {
  console.log(`Listeing on port ${HTTP_PORT}`);
});

Readme

Keywords

Package Sidebar

Install

npm i izigma-blockchain

Weekly Downloads

2

Version

2.0.1

License

MIT

Unpacked Size

92.5 kB

Total Files

36

Last publish

Collaborators

  • chamikaravinda