blockchains.js
Easy to use implementation of blockchains that allows you to store/validate data using NodeJS with high integrity. Save any object/string to blocks in the blockchain. Automatically saves and reloads your blockchains and chain validation at each initialization. Store your blockchain using your own unique encryption key. Allows setting block difficulty to increase security. Utilizes multithreading for difficult hashes. Store and run multiple chains at the same time with different difficulty levels.
Handles multiple block creations in a que, first-in-first-out, fashion to ensure that blocks occur in the order they are started regardless of the time it takes to hash the values. This functionality leaves the working process open due to a setTimeout on Blockchain.queTimeout. After all blocks have been created you can call process.exit() or clearTimeout(Blockchain.queTimeout) to stop the infinite loop that is checking if there are new blocks to hash.
Installation:
---
npm i blockchains.js
yarn add blockchains.js
USAGE
---
You can test that everything is working by running a test script:
---
require('blockchains.js/test')
---
Or test that everything is working by running a test script:
---
process.env.DBPATH = './chains' //SET THE FOLDER WHERE YOU WANT CHAINS TO BE STORED - defaults to ./database
const Blockchain = require('blockchains.js')
const blockchain = new Blockchain('level-3', 3) // new Blockchain(chainName, chainDifficulty, encryptionkey)
const saveBlock = async (data) => {
console.log('hashing/adding block with data:', data)
let block = await blockchain.addBlock(data)
console.log('new block:', block)
/*
{
index: 0,
timestamp: 1649446340824,
data: {data: 'test'},
previous: '',
nonce: 1649447320193,
hash: '000rXzsO35adrmEsShn4LX+mE1USI6pPsve5PKv6UBA='
}
clearTimeout(blockchain.queTimeout) // use this code or process.exit() after everything is done to stop the process.
*/
}
saveBlock({ data: 'test' })
---
Press control C to stop the process running or add clearTimeout(Blockchain.queTimeout) after all code has completed in your script or just do process.exit().
OBJECT STRUCTURE:
---
Block: {
index,
timestamp,
data,
previous,
nonce,
hash,
_id,
_t
}
Blockchain.chain : Chain {
chain: db {
data: [Blocks]
}
length: number
que: [function => Promise]
queTimeout: setTimeout || null
}
METHODS:
---
Blockchain.addBlock(data) => Promise => Block
Blockchain.getLatestBlock() => Block || null
Blockchain.validateChain() = true || false
Blockchain.validBlock(block) => true || false
Blockchan.manageQue() => null
Blockchain.chain:
push(data) => null
at(index) => Block || null
chain:
find(function || object) => Promise => Block || null
findAll(function || object) => Promise => [Blocks]
blockchains.js
1.0.3 • Public • PublishedReadme
Keywords
nonePackage Sidebar
Install
npm i blockchains.js
Weekly Downloads
1
Version
1.0.3
License
MIT
Unpacked Size
16 kB
Total Files
8