成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

[[387990]]

本文轉(zhuǎn)載自微信公眾號「區(qū)塊鏈研究實驗室」,作者鏈三豐 。轉(zhuǎn)載本文請聯(lián)系區(qū)塊鏈研究實驗室公眾號。  

在上一篇文章中,我們討論了區(qū)塊鏈概念并構(gòu)建了一個DEMO原型 [ 傳送機:區(qū)塊鏈研究實驗室 | 如何從0構(gòu)建區(qū)塊鏈(一)],在這一集中,我們將使用Javascript的另一種編程語言來實現(xiàn)相同的概念,用Go編寫代碼可能很困難。

因此,請參考我們在第1集中繪制的圖:

這次,我們將使用Javascript將應用相同的機制。

為了使其成為可能,我們需要一臺可以運行我們的Javascript代碼的服務器,可以使用網(wǎng)絡瀏覽器,但讓我們專業(yè)地做事。

要求:

  • Nodejs:在Web瀏覽器外部執(zhí)行JavaScript代碼的運行時環(huán)境。安裝它并嘗試建立一個項目,您可以按照此處的步驟進行操作。
  • Express:一個nodejs中間件Web應用程序,稍后我們將使用它,但是讓我們先安裝它。
  • Nodemon:一種工具,通過在修改文件后自動重啟節(jié)點應用程序來幫助開發(fā)基于node.js的應用程序
  • Bcrypt:一個用于快速加密的庫,您還可以使用所需的任何哈希函數(shù)。

讓我們開始吧:

  • 創(chuàng)建一個名為javascript的文件夾,并添加一個名為 entry.js
  • 在npm init用于初始化項目的文件夾類型中,填寫所有要求,對于入口點輸入entry.js
  • 打開終端,然后鍵入npm i --save-dev nodemon以安裝該nodemon工具。
  • 也運行npm i express安裝Express JS。
  • 安裝bcrypt npm i bcrypt

畢竟我的package.json看起來像這樣:

文件夾結(jié)構(gòu)如下所示:

打開終端并轉(zhuǎn)到javascript文件夾,鍵入“npm run start不要介意”是否看到錯誤,這是因為entry.js文件中沒有任何內(nèi)容。

現(xiàn)在我們準備開始對我們的區(qū)塊鏈進行編碼。entry.js在任何IDE中打開文件并編寫此代碼以理解它,請?zhí)^注釋:

以下是一些說明:

在上面的代碼中,我們創(chuàng)建了一個B鎖類,其中包含一個id,時間戳,哈希,以前的哈希和數(shù)據(jù)屬性。將來使用該類我們創(chuàng)建了一個構(gòu)造函數(shù),并添加了一個用于生成哈希的方法。

由于區(qū)塊鏈是一組塊,因此我們創(chuàng)建了另一個名為Blockchain的類來存儲所有塊,它只是Javascript中具有數(shù)組的承包商,然后我們添加了方法AddBlock將一個塊添加到我們的鏈中。

最后,我們初始化了鏈并通過發(fā)出3個不同的交易對其進行了測試。

結(jié)果:

如果安裝了nodemon,只需檢查運行它的終端,您將看到整個區(qū)塊鏈信息。

恭喜你!這在Javascript中非常簡單,我們只用了幾行代碼就完成了。

整個代碼:

  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 

 

責任編輯:武曉燕 來源: 區(qū)塊鏈研究實驗室
相關(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ù)存儲去中心化

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

2021-05-10 15:09:47

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

2018-10-15 10:59:56

2018-06-14 10:32:25

2019-01-24 15:50:06

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

2021-02-20 22:35:17

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

2019-10-29 15:46:07

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

2018-05-06 16:17:01

2020-08-18 10:58:05

區(qū)塊鏈比特幣區(qū)塊鏈戰(zhàn)略
點贊
收藏

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

主站蜘蛛池模板: 欧美 日韩 国产 一区 | 久草资源在线 | 成人在线观看亚洲 | 亚洲视频一区二区三区 | 欧美精品在线一区二区三区 | 国产日韩欧美 | 国产精品久久久久久久久久免费 | 久久婷婷国产香蕉 | 黄色在线播放视频 | 羞羞视频网 | 国产剧情一区二区三区 | 午夜精品一区二区三区在线观看 | 国产精品资源在线 | 日韩在线不卡视频 | 精品乱码一区二区三四区视频 | 国产片侵犯亲女视频播放 | 在线免费看毛片 | 国产精品69毛片高清亚洲 | 欧美激情欧美激情在线五月 | 色网站视频 | 97精品超碰一区二区三区 | 免费看黄色小视频 | 亚洲欧美视频一区 | 91在线网| 中文字幕动漫成人 | 色偷偷888欧美精品久久久 | 一区视频| 综合五月婷 | 日韩欧美三区 | 免费一级毛片 | 欧美久久久久 | 欧美日韩在线观看视频网站 | 久久久久一区二区三区 | 亚洲日韩中文字幕一区 | av影音资源 | 亚洲免费视频在线观看 | 丁香婷婷在线视频 | 农夫在线精品视频免费观看 | 一区二区三区四区五区在线视频 | 日韩一区二区黄色片 | 国产美女精品视频 |