如何從0構(gòu)建區(qū)塊鏈之二
本文轉(zhuǎn)載自微信公眾號(hào)「區(qū)塊鏈研究實(shí)驗(yàn)室」,作者鏈三豐 。轉(zhuǎn)載本文請(qǐng)聯(lián)系區(qū)塊鏈研究實(shí)驗(yàn)室公眾號(hào)。
在上一篇文章中,我們討論了區(qū)塊鏈概念并構(gòu)建了一個(gè)DEMO原型 [ 傳送機(jī):區(qū)塊鏈研究實(shí)驗(yàn)室 | 如何從0構(gòu)建區(qū)塊鏈(一)],在這一集中,我們將使用Javascript的另一種編程語言來實(shí)現(xiàn)相同的概念,用Go編寫代碼可能很困難。
因此,請(qǐng)參考我們?cè)诘?集中繪制的圖:
這次,我們將使用Javascript將應(yīng)用相同的機(jī)制。
為了使其成為可能,我們需要一臺(tái)可以運(yùn)行我們的Javascript代碼的服務(wù)器,可以使用網(wǎng)絡(luò)瀏覽器,但讓我們專業(yè)地做事。
要求:
- Nodejs:在Web瀏覽器外部執(zhí)行JavaScript代碼的運(yùn)行時(shí)環(huán)境。安裝它并嘗試建立一個(gè)項(xiàng)目,您可以按照此處的步驟進(jìn)行操作。
- Express:一個(gè)nodejs中間件Web應(yīng)用程序,稍后我們將使用它,但是讓我們先安裝它。
- Nodemon:一種工具,通過在修改文件后自動(dòng)重啟節(jié)點(diǎn)應(yīng)用程序來幫助開發(fā)基于node.js的應(yīng)用程序
- Bcrypt:一個(gè)用于快速加密的庫(kù),您還可以使用所需的任何哈希函數(shù)。
讓我們開始吧:
- 創(chuàng)建一個(gè)名為javascript的文件夾,并添加一個(gè)名為 entry.js
- 在npm init用于初始化項(xiàng)目的文件夾類型中,填寫所有要求,對(duì)于入口點(diǎn)輸入entry.js
- 打開終端,然后鍵入npm i --save-dev nodemon以安裝該nodemon工具。
- 也運(yùn)行npm i express安裝Express JS。
- 安裝bcrypt npm i bcrypt
畢竟我的package.json看起來像這樣:
文件夾結(jié)構(gòu)如下所示:
打開終端并轉(zhuǎn)到j(luò)avascript文件夾,鍵入“npm run start不要介意”是否看到錯(cuò)誤,這是因?yàn)閑ntry.js文件中沒有任何內(nèi)容。
現(xiàn)在我們準(zhǔn)備開始對(duì)我們的區(qū)塊鏈進(jìn)行編碼。entry.js在任何IDE中打開文件并編寫此代碼以理解它,請(qǐng)?zhí)^注釋:
以下是一些說明:
在上面的代碼中,我們創(chuàng)建了一個(gè)B鎖類,其中包含一個(gè)id,時(shí)間戳,哈希,以前的哈希和數(shù)據(jù)屬性。將來使用該類我們創(chuàng)建了一個(gè)構(gòu)造函數(shù),并添加了一個(gè)用于生成哈希的方法。
由于區(qū)塊鏈?zhǔn)且唤M塊,因此我們創(chuàng)建了另一個(gè)名為Blockchain的類來存儲(chǔ)所有塊,它只是Javascript中具有數(shù)組的承包商,然后我們添加了方法AddBlock將一個(gè)塊添加到我們的鏈中。
最后,我們初始化了鏈并通過發(fā)出3個(gè)不同的交易對(duì)其進(jìn)行了測(cè)試。
結(jié)果:
如果安裝了nodemon,只需檢查運(yùn)行它的終端,您將看到整個(gè)區(qū)塊鏈信息。
恭喜你!這在Javascript中非常簡(jiǎn)單,我們只用了幾行代碼就完成了。
整個(gè)代碼:
- const bcrypt = require('bcrypt') // import the bcrypt js librairy
- class Block{ // create the block structure or class
- constructor(blockid, previousHash, data){ // create a contractor. in a block we find this information :
- this.blockid = blockid; // the block id
- this.timestamp = Date.now(); // the timestamp
- this.blockhash = this.getHash(); // the block hash
- this.prevHash = previousHash; // the hash of the previous block
- this.data = data; // and all the transactions
- }
- getHash(){
- return bcrypt.hashSync(String(this.blockid + this.timestamp + this.blockhash + this.previousHash + JSON.stringify(this.data)) , 10) // this method will hash the data in the block using a salt of 10 and return that hash. We use the bcrypt library
- };
- }
- class BlockChain{ // the blochain structure or class
- constructor(){ // create a constractor.
- this.chain = []; // a blockchain is a series of blocks, so we need an array []
- }
- addBlock(data){ // create a method that will take the entire block and add it to the blockchain
- let blockid = this.chain.length; // The block id will be the length or the total number of blocks in the chain minus 1, so the first block will have 0 as an index
- let previousHash = this.chain.length !== 0 ? this.chain[this.chain.length - 1].blockhash : ''; // if it's the first block then its previous hash will be empty, if not then it will take the hash of the previous block
- let block = new Block(blockid, previousHash, data); // Now create the block
- this.chain.push(block); // Add the block to the blockchain
- }
- }
- const Myfirstblockchain = new BlockChain();
- Myfirstblockchain.addBlock({sender: "sinai", receiver: "kazadi", amount: 24034}); // first transaction
- Myfirstblockchain.addBlock({sender: "Dahouda", receiver: "Pat", amount: 32032}); // second transaction
- Myfirstblockchain.addBlock({sender: "Nkolomoni", receiver: "Mao", amount: 20993}); // third transaction
- console.log(JSON.stringify(Myfirstblockchain, null, 6)); // convert the result into a json and show it in the console