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

如何從0構建區塊鏈之二

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

[[387990]]

本文轉載自微信公眾號「區塊鏈研究實驗室」,作者鏈三豐 。轉載本文請聯系區塊鏈研究實驗室公眾號。  

在上一篇文章中,我們討論了區塊鏈概念并構建了一個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中非常簡單,我們只用了幾行代碼就完成了。

整個代碼:

  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 

 

責任編輯:武曉燕 來源: 區塊鏈研究實驗室
相關推薦

2021-03-12 19:17:38

區塊鏈GoPython

2021-03-17 20:29:36

區塊鏈DEMOPython

2018-05-23 15:20:08

區塊鏈數字貨幣比特幣

2019-11-08 08:16:12

區塊鏈數據存儲去中心化

2021-04-16 20:43:18

Go區塊鏈編程

2021-12-22 23:28:04

區塊鏈人工智能技術

2018-03-19 19:30:19

2021-09-23 22:40:10

區塊鏈比特幣技術

2021-04-20 10:30:43

區塊鏈安全互聯網

2018-03-27 09:52:30

區塊鏈數字貨幣比特幣

2018-01-23 11:09:04

區塊鏈技術重用

2022-10-18 08:00:00

2021-05-10 15:09:47

區塊鏈互聯網金融

2018-05-06 16:17:01

2020-08-18 10:58:05

區塊鏈比特幣區塊鏈戰略

2021-03-16 14:33:12

區塊鏈比特幣加密貨幣

2021-04-12 10:57:28

區塊鏈信任銀行

2022-04-18 14:50:00

區塊鏈安全交易

2021-04-11 11:31:05

區塊鏈記賬比特幣

2018-06-14 10:32:25

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产一区h | 日韩成人高清 | 伊人网站 | 国产大片黄色 | 国产精品一区久久久 | 亚洲精品国产电影 | 成人精品国产免费网站 | 91久久久久久 | 欧美一区二区三区免费电影 | 欧美精品 在线观看 | 精品国产乱码久久久久久牛牛 | 天天插天天射天天干 | 精品综合| 黄视频免费 | 精品国产1区2区3区 一区二区手机在线 | 欧美a在线| 久久久久国产一区二区三区四区 | 国产日韩一区二区三免费 | 色综合久久88色综合天天 | 国产福利精品一区 | 国产综合视频 | 黄色片免费| 91久久视频 | 欧美一区视频在线 | 国产欧美日韩在线 | 二区中文字幕 | 99九色| 国产欧美精品一区二区色综合朱莉 | 欧美 日韩 国产 成人 在线 91 | 91在线精品一区二区 | 国产精品视频久久久 | 成人伊人网 | 亚洲一区 中文字幕 | 亚洲有码转帖 | 久久国产精品99久久久久久丝袜 | 国产精品一区二区久久精品爱微奶 | 亚洲免费视频一区 | 一区二区三区高清 | 亚洲成人精品免费 | 欧美高清视频一区 | 国产精品区一区二区三区 |