如何從0構建區塊鏈之二
本文轉載自微信公眾號「區塊鏈研究實驗室」,作者鏈三豐 。轉載本文請聯系區塊鏈研究實驗室公眾號。
在上一篇文章中,我們討論了區塊鏈概念并構建了一個DEMO原型 [ 傳送機:區塊鏈研究實驗室 | 如何從0構建區塊鏈(一)],在這一集中,我們將使用Javascript的另一種編程語言來實現相同的概念,用Go編寫代碼可能很困難。
因此,請參考我們在第1集中繪制的圖:
這次,我們將使用Javascript將應用相同的機制。
為了使其成為可能,我們需要一臺可以運行我們的Javascript代碼的服務器,可以使用網絡瀏覽器,但讓我們專業地做事。
要求:
- Nodejs:在Web瀏覽器外部執行JavaScript代碼的運行時環境。安裝它并嘗試建立一個項目,您可以按照此處的步驟進行操作。
- Express:一個nodejs中間件Web應用程序,稍后我們將使用它,但是讓我們先安裝它。
- Nodemon:一種工具,通過在修改文件后自動重啟節點應用程序來幫助開發基于node.js的應用程序
- Bcrypt:一個用于快速加密的庫,您還可以使用所需的任何哈希函數。
讓我們開始吧:
- 創建一個名為javascript的文件夾,并添加一個名為 entry.js
- 在npm init用于初始化項目的文件夾類型中,填寫所有要求,對于入口點輸入entry.js
- 打開終端,然后鍵入npm i --save-dev nodemon以安裝該nodemon工具。
- 也運行npm i express安裝Express JS。
- 安裝bcrypt npm i bcrypt
畢竟我的package.json看起來像這樣:
文件夾結構如下所示:
打開終端并轉到javascript文件夾,鍵入“npm run start不要介意”是否看到錯誤,這是因為entry.js文件中沒有任何內容。
現在我們準備開始對我們的區塊鏈進行編碼。entry.js在任何IDE中打開文件并編寫此代碼以理解它,請跳過注釋:
以下是一些說明:
在上面的代碼中,我們創建了一個B鎖類,其中包含一個id,時間戳,哈希,以前的哈希和數據屬性。將來使用該類我們創建了一個構造函數,并添加了一個用于生成哈希的方法。
由于區塊鏈是一組塊,因此我們創建了另一個名為Blockchain的類來存儲所有塊,它只是Javascript中具有數組的承包商,然后我們添加了方法AddBlock將一個塊添加到我們的鏈中。
最后,我們初始化了鏈并通過發出3個不同的交易對其進行了測試。
結果:
如果安裝了nodemon,只需檢查運行它的終端,您將看到整個區塊鏈信息。
恭喜你!這在Javascript中非常簡單,我們只用了幾行代碼就完成了。
整個代碼:
- 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