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