Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
blocktron-lib
Advanced tools
blocktron-lib module is a member of the Blocktron Project. blocktron-lib is a javascript library housing a blockchain class, and the core blockchain data structures with various blockchain methods and functionalities. This library is fully extensible to accommodate any blockchain applications. Its completely independent and follows a class constructor design pattern and is written using ES6 specifications.
Blocktron is a simple yet elegant and efficient blockchain framework written in Javascript for Node.js environment. Blocktron is aimed at developing generic, multipurpose blockchain platforms and softwares for various application use-cases, and also for educational and awareness purposes. blocktron-lib is completely tested using Jest framework. This library is built from the ground up using only opensource technologies.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
Prerequisites
The following programs/libraries/languages/compilers/editors/tools etc are required to use the blocktron-lib.
VSCode preferably
)blocktron-lib module has two assets in every version release builds, a compressed build and an uncompressed build.
build | asset | size |
---|---|---|
compressed | main.min.js | 3KiB |
uncompressed | main.js | 14.9KiB |
by default npm module is set to choose the compressed build.
NPM
You can install blocktron-lib from npm using:
npm install blocktron-lib --save
GitHub
You can download a zip of the project repo or clone the repo from your terminal using the command:
git clone https://github.com/Blocktron-Project/blocktron-lib.git
Sample usage
//In your app.js
const Blocktron = require('blocktron-lib');
const blockchain = new Blocktron;
console.log(blockchain);
then run your app.js by
node app.js
Your blockchain with the default genesis block will be displayed as:
Blocktron {
chain: [{
index: 1,
timeStamp: 1529827768993,
transactions: [],
nonce: 1,
hash: '0',
previousHash: '0'
}],
pendingTransactions: []
}
The blocktron-lib library is dependent only on the sha256
npm module to run on production. Its a JavaScript component to compute the SHA256
of strings or bytes.
For a development setup this library depends on the following npm modules:
v 0.0.5
v 0.0.2
v 0.0.1
The various apis provided by the blocktron-lib are described below:
You can start the blockchain by importing the blocktron-lib to your app using:
const Blocktron = require('blocktron-lib');
const blockchain = new Blocktron;
//then simply console logging will give you the blockchain in its initial form
console.log(blockchain);
which will result in the following output to the terminal
Blocktron {
chain: [{
index: 1,
timeStamp: 1529827768993,
transactions: [],
nonce: 1,
hash: '0',
previousHash: '0'
}],
pendingTransactions: []
}
You can see that the blockchain object already has a block with some data in it. This block is the default block of every blockchain. It is called as the Genesis Block. Whenever a new blockchain is initialized, it defaults with a genesis block and further blocks are linked from the genesis block.
A blockchain method to create a new block
blockchain.createNewBlock('<nonce>', '<previousHash>', '<hash>');
Parameter | Type | Description | Optional |
---|---|---|---|
nonce | Number | The nonce number obtained from proof-of-work method | No |
previousHash | String | The hash of the previous block | No |
hash | String | The hash generated from this block's data | No |
result:
Blocktron {
chain: [{
index: 1,
timeStamp: 1529852953815,
transactions: [],
nonce: 1,
hash: '0',
previousHash: '0'
},
{
index: 2,
timeStamp: 1529852953815,
transactions: [],
nonce: 2389,
hash: '23huih2342jh34j',
previousHash: 'a87sdfs8df8ds89f'
}
],
pendingTransactions: []
}
Note: The genesis block is created using this method internally.
A blockchain method to get the last block on the chain
blockchain.getLastBlock()
Note: This method does not accept any parameters, it simply returns the block object from the data structure at the penultimate position
result:
{
index: 2,
timeStamp: 1529853936748,
transactions: [],
nonce: 2389,
hash: '23huih2342jh34j',
previousHash: 'a87sdfs8df8ds89f'
}
A blockchain method to create a new transaction to be recorded on to the chain.
blockchain.createNewTransaction('<amount>', '<sender>', '<receiver>')
Parameter | Type | Description | Optional |
---|---|---|---|
amount | Number | The amount/value to be recorded | No |
sender | String | The adress of the sender | No |
receiver | String | The address of the receiver | No |
result:
Blocktron {
chain: [{
index: 1,
timeStamp: 1529854616451,
transactions: [],
nonce: 1,
hash: '0',
previousHash: '0'
},
{
index: 2,
timeStamp: 1529854616451,
transactions: [],
nonce: 2389,
hash: '23huih2342jh34j',
previousHash: 'a87sdfs8df8ds89f'
}
],
pendingTransactions: [{
amount: 100,
sender: 'ROSS2IU3Y42U3',
reciever: 'RACHELJ234J234KJ20'
}]
}
Note: The new transaction is always added to the pending transactions array, for it to be mined later.
A helper method to generate a hash string out of a blocks data
blockchain.hashBlock('<previousBlockHash>', '<currentBlockData>', '<nonce>')
Parameter | Type | Description | Optional |
---|---|---|---|
previousBlockHash | String | The hash of the previous block | No |
currentBlockData | Object | The current block's data | No |
nonce | Number | The nonce of the block | Yes |
result:
445101fbd33c3b30f00d143dba65cfe219bace079d1d06b4b7b28922d7a611d6
An opinionated, standardized, and universally approved blockchain method to validate random blocks added to the blockchain. The Proof of work algorithm implemented in this library is as follows:
O(n)
.Algorithm:
ProofOfWork()
Input <previousBlockHash>, <currentBlockData>
Output <nonce>
START
SET nonce = 0
GET hashString = hashBlock(previousBlockHash, currentBlockData, nonce)
DO nonce++
hashString = hashBlock(previousBlockHash, currentBlockData, nonce)
WHILE hashString.substring(0, 4) !== '0000'
END
blockchain.proofOfWork('<previousBlockHash>', '<currentBlockData>');
Parameter | Type | Description | Optional |
---|---|---|---|
previousBlockHash | String | The hash of the previous block | No |
currentBlockData | Object | The current block's data | No |
result:
1441212 //some large integer
The result of this proof of work method is a number which is the nonce which generates the correct hash of the format '0000<hash>'
. This nonce when used to generate a hash will generate the correctly formated hash, thus making it the proof we need. This is secure because, the algorithm has to run 1441212
times to generate the correct hash, in this case. This is a time consuming and resource intensive process, Thus when someone tries to tamper the integrity of a blockchain, they have to rebuild the entire blockchain using this proof of work algorithm to generate correct hash and nonce combination for the entire blockchain, which is impossible.
Further examples and documentations will be updated soon, or you can run the test specs from the tests folder.
Continuous Integration services monitor repositories for changes, then automatically run unit tests on your behalf, typically in a containerized environment. To test this setup works in a continuous integration environment, an integration was done with Travis CI & CircleCI. According to the Travis Node.js Documentation, Travis automatically runs npm install
and npm test
. The only additional thing I had to add to the Travis configuration was to run npm run build
before running the tests. The working Travis config looks like this:
language: node_js
node_js:
- stable
install:
- npm install
script:
- npm run build
- npm test
Here's the Travis build page for this project, which shows the tests passing.
CircleCI is similar to Travis-CI, but is more extensible and has much more control over the build process. The CircleCI config looks like this:
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:7.10
# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/mongo:3.4.4
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: npm install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
# run tests!
- run: npm run build
- run: npm test
This project uses Jest framework for unit testing. All unit tests are available in the test folder. Tests can be run by the command:
npm run test
This project is built with the following technology stack:
Please read CONTRIBUTING.md for details on contributing to the project and CODE_OF_CONDUCT.md for the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
See also the list of contributors who participates in this project.
The MIT License
Copyright (c) 2018- Sandeep Vattapparambil, http://www.sandeepv.in
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Thanks to Eric Traub for publishing a wonderful course on Blockchains.
Made with ❤️ by Sandeep Vattapparambil.
FAQs
A simple blockchain data structure library
The npm package blocktron-lib receives a total of 390 weekly downloads. As such, blocktron-lib popularity was classified as not popular.
We found that blocktron-lib demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.