The Node.js language will tell you how to build your own blockchain project

Recently, digital currency and underlying blockchain technology are very popular, leading the world trend. These days, blockchain is a very popular vocabulary, but few people really understand how this technology promotes the development of digital currencies, similar to the development of Bitcoin and Ethereum.

In this article, we try to tell you how to use the Node.js language to write your own blockchain projects.

Blockchain

The blockchain has been gradually increasing the list of records, which can be marked as blocks, and the blocks are connected to each other through encryption algorithms. The concatenation of blocks makes any block in the block chain change, then other records on the chain will be invalid.

The unchangeable feature is the key to the growth of digital currency, because it makes it difficult for people to make changes after completing the transfer.

The Node.js language will tell you how to build your own blockchain project

Creating blocks is just like what I just said, the blockchain is made up of many blocks connected. The cryptographic hash is used to maintain the integrity of the blockchain.

Each block will have a hash calculated based on the data. It will also have the hash of the previous block. If the hash of any block changes, it will invalidate the remaining blocks. In the Node.js language, the block algorithm will be as follows:

const SHA256 = require("crypto-js/sha256"); class Block {constructor(index, timestamp, data, previousHash = ") {this.index = index; this.previousHash = previousHash; this.TImestamp = TImestamp; this. data = data; this.hash = this.computeHash(); this.nonce = 0;} computeHash() {return SHA256(this.index + this.previousHash + this.TImestamp + JSON.stringify(this.data) + this .nonce).toString();}

As we can see, the above function will instantiate the level and quote the following parameters:  Index: It will track the position of the block in the blockchain  Timestamp: It will put the value when each transfer is completed Enter the timestamp.  Data: It will provide information when the transfer is completed, such as the purchase amount.  Previous Hash-It represents the hash value of the previous block in the blockchain.

We use the computeHash function to calculate the cryptographic hash of each block based on the above values. To accomplish this, we will import the crypto-js library and use its SHA256 hash function.

SH256 is a very powerful, irreversible hash function, it will be applied to most digital currencies to ensure their safety.

In order to set up the crypto-js database, locate the end point, and in the same project folder, we use npm to install it.

You can use the following code:

//remember to run npm init first npm install –save crypto-js

Create a blockchain

Blockchain means that these blocks are all linked to each other. Therefore, we will start to link these blocks and others on the blockchain.

code show as below:

class Blockchain{ constructor() {this.chain = [this.buildGenesisBlock()]; this.complexity = 5;} buildGenesisBlock() {return new Block(0, "17/07/2018", "genesis block", " 0”);} obtainLatestBlock() {return this.chain[this.chain.length – 1];} addBlock(newBlock) {newBlock.previousHash = this.obtainLatestBlock().hash; newBlock.mineBlock(this.complexity); this.chain.push(newBlock);}}

From the above code, we can see that this level is achieved by the following functions: A). The constructor blockchain is started by buildGenesisBlock. B). Create a genesis block In the blockchain, the genesis block is the beginning of the blockchain. There is no data before this block, and there will be blocks based on it. We will use the buildGenesisBlock() function to create it.

C). Get the latest block In order to get the latest block in the blockchain, we use the obtainLatestBlock() function. D). Adding a new block In order to add a new block to the blockchain Node.js, we use the addBlock() function. In order to complete this step, we will add the hash of the previous block to the new block to ensure the integrity of the blockchain.

Because we changed the details of the new block, it is necessary to calculate the hash again. After completion, we will put the block into the data set of the chain. E). Determine the effectiveness of the blockchain

The confirmValidity() function is to ensure the integrity of the blockchain and ensure that there are no defects. Many if functions are referenced in this function to confirm whether the hash of each block cannot be changed.

And, it will also check whether the hash values ​​of every two related blocks point to each other. If everything is valid, then return true, otherwise, return false.

Here is the code: confirmValidity() {for (let i = 1; i "this.chain.length; i++) {const currentBlock = this.chain[i]; const previousBlock = this.chain[i – 1]; if ( currentBlock.hash !== currentBlock.computeHash()) {return false;} if (currentBlock.previousHash !== previousBlock.hash) {return false;}} return true;}

Test the blockchain

This is the most exciting part!

The code is as follows: let liveEduCoin = new Blockchain(); console.log('《》'); liveEduCoin.addBlock(new Block(1, “27/07/2018”, {quanTIty: 10 })); console.log( '《》'); liveEduCoin.addBlock(new Block(2, “27/07/2018”, {quantity: 20 })); We'll create a new instance of the Blockchain class and name it liveEduCoin. Then, we 'll add some arbitrary blocks into the blockchain. You can add any kind of data into the blocks. In this simple blockchain Node.js tutorial, we decided to add an object with the quantityproperty. Here is the entire code for our project: const SHA256 = require("crypto-js/sha256"); class Block {constructor(index, timestamp, data, previousHash = ") {this.index = index; this.previousHash = previousHash; this.timestamp = timestamp; this.data = data; this.hash = this.computeHash(); this.nonce = 0;} computeHash() {return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data) + this. nonce).toString();} mineBlock(complexity) {while (this.hash.substring(0, comp lexity)! == Array(complexity + 1).join("0")) {this.nonce++; this.hash = this.computeHash();} console.log("Mining is taking place: ”+ this.hash);} } class Blockchain{ constructor() {this.chain = [this.buildGenesisBlock()]; this.complexity = 5;} buildGenesisBlock() {return new Block(0, "17/07/2018", "genesis block", "0");} obtainLatestBlock() {return this.chain[this.chain.length – 1];} addBlock(newBlock) {newBlock.previousHash = this.obtainLatestBlock().hash; newBlock.mineBlock(this.complexity) ; this.chain.push(newBlock);} confirmValidity() {for (let i = 1; i 《this.chain.length; i++){ const currentBlock = this.chain[i]; const previousBlock = this.chain[ i – 1]; if (currentBlock.hash !== currentBlock.computeHash()) {return false;} if (currentBlock.previousHash !== previousBlock.hash) {return false;}} return true;}} let liveEduCoin = new Blockchain(); console.log('《》'); liveEduCoin.addBlock(new Block(1, “27/07/2018”, {quantity: 10 })); console.log('《》'); liveEduCoin.addBlock(ne w Block(2, "27/07/2018", {quantity: 20 }));

If we save the code as a blockchain.js file and run it on the terminal, then the following is the result: successful operation

in conclusion

The digital currency blockchain in Node.js mentioned above is far from complete. In fact, if you work hard to finish it, you can be the only one who uses it!

For example, it will lack the key elements of a successful digital currency, such as proof of work and P2P networks. Nevertheless, the blockchain node.js demo shows how the blockchain works. Contrary to what many people think, this simple project reveals that the blockchain concept is actually very easy to implement.

Multi-port USB Charger

Multi-Port Charger Usb,5V2A Usb Charger,Mobile Phone Charger,Multi Ports Usb-A Charging Hub

shenzhen ns-idae technology co.,ltd , https://www.best-charger.com