Node.js語言將會告訴你如何打造專屬于自己的區(qū)塊鏈項目
最近,數(shù)字貨幣和底層區(qū)塊鏈技術(shù)非?;鸨I(lǐng)了世界潮流。這些天,區(qū)塊鏈都是非常熱門的詞匯,但是很少有人真正地了解這項技術(shù)是如何來推動數(shù)字貨幣,類似比特幣和以太坊的發(fā)展的。
此文中,我們嘗試來告訴大家如何使用Node.js語言,來編寫你們自己的區(qū)塊鏈項目。
區(qū)塊鏈區(qū)塊鏈一直在逐漸增加記錄列表,這可以對標為區(qū)塊,而區(qū)塊之間是通過加密算法互相連接。區(qū)塊的接連,讓區(qū)塊鏈中的任何區(qū)塊如果發(fā)生改變,那么鏈上其他的記錄就會無效。
無法更改的特性是數(shù)字貨幣增長的關(guān)鍵,因為它會讓人們在完成轉(zhuǎn)賬后,很難去進行更改。
創(chuàng)建區(qū)塊 就像剛才所說,區(qū)塊鏈是由很多區(qū)塊連接構(gòu)成。加密哈希被用來維持區(qū)塊鏈的完整性。
每個區(qū)塊都會有基于數(shù)據(jù)計算出來的哈希。它也會有前面區(qū)塊的哈希。如果任何區(qū)塊的哈希改變,它就會使得剩下的區(qū)塊無效。 在Node.js語言下,區(qū)塊算法就會是如下:
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(); }
像我們看到的,以上的函數(shù)中會實例化等級,并且引用如下的參數(shù): ? 索引:它會追蹤區(qū)塊鏈中區(qū)塊的位置 ? 時間戳:它會在每個轉(zhuǎn)賬完成的時候,放入時間戳。 ? 數(shù)據(jù):它會在轉(zhuǎn)賬完成的時候,提供信息,例如購買量。 ? 前個哈希- 它代表區(qū)塊鏈中前個區(qū)塊的哈希值。
我們使用computeHash 這個函數(shù)來根據(jù)上面的數(shù)值,計算出每個區(qū)塊的加密哈希。為了完成這個,我們會導入crypto-js library 并且使用它的SHA256哈希功能。
SH256是一個很強大的,不可逆哈希功能,它會應(yīng)用在大多數(shù)數(shù)字貨幣中,從而確保它們的安全。
為了設(shè)置crypto-js數(shù)據(jù)庫,定位到終點,并且在同樣的項目文件夾中,我們使用npm來安裝它。
你可以使用下面的代碼:
//remember to run npm init first npm install –save crypto-js
創(chuàng)建區(qū)塊鏈區(qū)塊鏈的意思是這些區(qū)塊都互相鏈接。因此,我們會開始將這些區(qū)塊和其他的鏈接在區(qū)塊鏈上。
代碼如下:
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); } }
從上面代碼,我們可以看出,這個等級是由以下函數(shù)實現(xiàn)的: A)。 構(gòu)造函數(shù) 區(qū)塊鏈是通過buildGenesisBlock來啟動的。 B)。 創(chuàng)建創(chuàng)世區(qū)塊 在區(qū)塊鏈中,創(chuàng)世區(qū)塊是區(qū)塊鏈的開始。這個區(qū)塊之前沒有數(shù)據(jù),接下來會有區(qū)塊基于它。我們會使用buildGenesisBlock() 函數(shù)來創(chuàng)建。
C)。 獲得最新區(qū)塊 為了獲得區(qū)塊鏈中的最新區(qū)塊,我們使用obtainLatestBlock() 函數(shù)。 D)。 增加新區(qū)塊 為了給區(qū)塊鏈Node.js增加新區(qū)塊,我們使用addBlock() 函數(shù)。為了完成這步驟,我們會將前個區(qū)塊的哈希加到新區(qū)塊上,為了保證區(qū)塊鏈的完整性。
因為我們改變了新區(qū)塊的細節(jié),所以很有必要去再次計算哈希。在完成之后,我們會將區(qū)塊放入鏈的數(shù)據(jù)集。 E)。 確定區(qū)塊鏈的有效性
confirmValidity() 功能是為了確保區(qū)塊鏈的完整性,并確保缺陷不存在。這個函數(shù)中引用了很多if功能,來確認是否每個區(qū)塊的哈希是不可以更改的。
并且,它也會檢查是否每兩個相關(guān)區(qū)塊的哈希值指向?qū)Ψ?。如果所有都有效,那么就回復true,不然就回復false。
下面是代碼: 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; }
測試區(qū)塊鏈這是最令人興奮的部分!
代碼如下: 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. Thereafter, 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, complexity) !== 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(new Block(2, “27/07/2018”, { quantity: 20 }));
如果我們將代碼保存為blockchain.js 文件,并且在終端上運行,那么下面就是結(jié)果:成功運行
結(jié)論上面所說的Node.js中的數(shù)字貨幣區(qū)塊鏈還遠沒有完成。其實,如果你努力完成,你可以是唯一使用它的人!
例如,它會缺少成功數(shù)字貨幣的關(guān)鍵要素,例如工作量證明和P2P網(wǎng)絡(luò)。盡管如此,區(qū)塊鏈node.js演示展示了區(qū)塊鏈運行的方法。和很多人想的不同,這個簡單的項目揭示了區(qū)塊鏈概念其實是很容易實施的。