自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何從0構(gòu)建區(qū)塊鏈之二

區(qū)塊鏈
為了使其成為可能,我們需要一臺(tái)可以運(yùn)行我們的Javascript代碼的服務(wù)器,可以使用網(wǎng)絡(luò)瀏覽器,但讓我們專業(yè)地做事。

[[387990]]

本文轉(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è)代碼:

  1. const bcrypt = require('bcrypt') // import the bcrypt js librairy 
  2.  
  3. class Block{ // create the block structure or class 
  4.       
  5.     constructor(blockid,  previousHash, data){ // create a contractor. in a block we find this information : 
  6.         this.blockid = blockid;  // the block id 
  7.         this.timestamp = Date.now(); // the timestamp 
  8.         this.blockhash = this.getHash(); // the block hash 
  9.         this.prevHash = previousHash; // the hash of the previous block 
  10.         this.data = data; // and all the transactions 
  11.         
  12.         
  13.     } 
  14.     getHash(){ 
  15.         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 
  16.     }; 
  17.  
  18. class BlockChain{ // the blochain structure or class 
  19.     constructor(){ // create a constractor.  
  20.         this.chain = []; // a blockchain is a series of blocks, so we need an array [] 
  21.     } 
  22.  
  23.     addBlock(data){ // create a method that will take the entire block and add it to the blockchain 
  24.         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 
  25.         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 
  26.         let block = new Block(blockid, previousHash, data); // Now create the block 
  27.       
  28.         this.chain.push(block); // Add the block to the blockchain  
  29.     } 
  30.  
  31.  
  32. const Myfirstblockchain = new BlockChain(); 
  33.   
  34. Myfirstblockchain.addBlock({sender: "sinai", receiver: "kazadi", amount: 24034}); // first transaction 
  35. Myfirstblockchain.addBlock({sender: "Dahouda", receiver: "Pat", amount: 32032}); // second transaction 
  36. Myfirstblockchain.addBlock({sender: "Nkolomoni", receiver: "Mao", amount: 20993}); // third transaction  
  37.   
  38. console.log(JSON.stringify(Myfirstblockchain, null, 6)); // convert the result into a json and show it in the console 

 

責(zé)任編輯:武曉燕 來源: 區(qū)塊鏈研究實(shí)驗(yàn)室
相關(guān)推薦

2021-03-12 19:17:38

區(qū)塊鏈GoPython

2021-03-17 20:29:36

區(qū)塊鏈DEMOPython

2018-05-23 15:20:08

區(qū)塊鏈數(shù)字貨幣比特幣

2019-11-08 08:16:12

區(qū)塊鏈數(shù)據(jù)存儲(chǔ)去中心化

2021-04-16 20:43:18

Go區(qū)塊鏈編程

2021-12-22 23:28:04

區(qū)塊鏈人工智能技術(shù)

2018-03-19 19:30:19

2021-04-20 10:30:43

區(qū)塊鏈安全互聯(lián)網(wǎng)

2021-09-23 22:40:10

區(qū)塊鏈比特幣技術(shù)

2018-03-27 09:52:30

區(qū)塊鏈數(shù)字貨幣比特幣

2018-01-23 11:09:04

區(qū)塊鏈技術(shù)重用

2022-10-18 08:00:00

2019-10-29 15:46:07

區(qū)塊鏈區(qū)塊鏈技術(shù)

2021-05-10 15:09:47

區(qū)塊鏈互聯(lián)網(wǎng)金融

2018-06-14 10:32:25

2019-01-24 15:50:06

區(qū)塊鏈數(shù)字貨幣比特幣

2021-03-16 14:33:12

區(qū)塊鏈比特幣加密貨幣

2021-04-12 10:57:28

區(qū)塊鏈信任銀行

2021-04-11 11:31:05

區(qū)塊鏈記賬比特幣

2018-05-06 16:17:01

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)