如何評估用 JavaScript 實現的區塊鏈?


區塊鏈是一串包含資訊的區塊。2009年,這項技術被中本聰用於建立比特幣這種數字加密貨幣。它對任何想要開發或分析的人完全開放。這項技術的一個特點是,一旦一些資料被記錄到區塊鏈中,就很難更改。

以下是一些用於評估區塊鏈程式的術語。

  • 區塊 - 區塊鏈中的區塊包含資訊,例如資料、雜湊值和前一個區塊的雜湊值。

  • 資料 - 此資料完全取決於區塊的型別,例如加密貨幣包含交易資訊,例如交易的雙方以及交易的貨幣數量。

  • 雜湊 - 這是一個唯一的字串 ID,就像 Aadhar 號碼可以用來查詢一個人的詳細資訊一樣,雜湊值用於標識區塊的詳細資訊。一旦建立了一個區塊,它的雜湊值就會被建立。更改區塊雜湊值很容易被識別。一旦區塊雜湊值被更改,它就不再是同一個區塊了。

  • 前一個雜湊值 - 這是前一個區塊的雜湊值,用於連線或構成區塊鏈。

在上圖中,您可以看到前一個雜湊值包含前一個區塊的雜湊值。第一個區塊也稱為創世區塊,因為它無法指向前一個區塊。如果您更改雜湊值,則具有前一個雜湊值的下個區塊將由於更改而無效。

我們將使用的包是crypto.js。這是一個 JavaScript 庫,提供加密演算法和函式。它可用於執行各種加密操作,例如在 Web 瀏覽器或 Node.js 等伺服器端 JavaScript 環境中進行雜湊、加密、解密和金鑰生成。

該庫廣泛用於 Web 應用程式,以提供安全通訊、資料保護和使用者身份驗證。例如,它可用於在透過網際網路傳送敏感資料之前對其進行加密,或為使用者身份驗證生成安全的密碼雜湊。

讓我們透過一個使用 Crypto.JS 庫進行雜湊和工作量證明的程式來理解。

這裡有兩個類:Block 和 Blockchain。

class Block{
   constructor(prev_hashValue, data){
      this.data=data;
      this.hash=this.calculateHash();
      this.prev_hashValue=prev_hashValue;
      this.time_stamp= new Date();
      this.pf_work=0;
   }
}

Block 類有五個屬性:

  • data - 這將儲存區塊中的資料。

  • hash - 透過呼叫 calculateHash 方法來儲存區塊的雜湊值。

  • prev_hashValue - 這將儲存前一個區塊的雜湊值。

  • time_stamp - 時間戳將包含建立區塊的時間。

  • pf_work - 在挖掘過程中遞增的數字。

Block 類包含兩個方法:

calculateHash(){
   return SHA256(this.pf_work + this.prev_hashValue + this.timestamp + JSON.stringify(this.data)).toString();
}

此函式將透過將 pf_work、prev_hashValue、time_stamp 和 data 連線起來,並透過使用 CryptoJS 庫的SHA256雜湊函式傳遞來計算區塊的雜湊值。

mine(difficulty){
   while(!this.hash.startsWith("0".repeat(difficulty))){
      this.pf_work++;
      this.hash=this.calculateHash();
   }
}

此函式使用工作量證明來查詢以特定數量的零開頭的雜湊值。零的數量由傳遞給該方法的 difficulty 引數確定。pf_work 屬性會遞增,直到找到有效的雜湊值。

class Blockchain{
   constructor(){
      let genesisBlock=new Block("0", {isGenesisBlock: true});
      this.chain=[genesisBlock];
   }
}

chain - 這是一個 Block 物件陣列,構成區塊鏈。

Blockchain 類有兩個方法:

addNewBlock(data){
   let lastBlock=this.chain[this.chain.length-1];
   let newBlock=new Block(lastBlock.hash, data);
   newBlock.mine(2); //find a hash for new block
   this.chain.push(newBlock);
}

此方法建立一個新的 Block 物件,其中資料作為引數傳遞,使用 mines 查詢有效的雜湊值,並將其新增到 chain 陣列。

isValid_hash(){
      for(let i=1; i<this.chain.length; i++){
      const currentBlock=this.chain[i];
      const previousBlock=this.chain[i-1];
      if(currentBlock.hash!=currentBlock.calculateHash()) return false;
      if(currentBlock.prev_hashValue!=previousBlock.hash) return false;
      }
      return true;
}

此方法透過迭代 chain 陣列中的每個區塊並驗證其 hash 屬性是否與計算出的雜湊值匹配來檢查區塊鏈的有效性。

let blockchain=new Blockchain();
blockchain.addNewBlock({
   from: "joe",
   to:"Juhi",
   amount: 100,
});
blockchain.addNewBlock({
   from: "martin",
   to: "Genny",
   amount: 150,
});

這裡將建立一個包含兩個區塊的物件,這些區塊將具有區塊鏈類的屬性。

此實現可以用作構建更復雜的區塊鏈應用程式的起點,這些應用程式需要安全且不可變的資料儲存。但是,需要注意的是,這只是一個基本的實現,完全功能的區塊鏈系統還需要許多其他功能,例如交易驗證、共識機制和安全措施。

示例:完整程式碼

Blockchain.js

const SHA256 = require('crypto-js/sha256');
class Block{
   constructor(prev_hashValue, data){
      this.data=data;
      this.hash=this.calculateHash();
      this.prev_hashValue=prev_hashValue;
      this.time_stamp= new Date();
      this.pf_work=0;
   }

   calculateHash(){
      return SHA256(this.pf_work + this.prev_hashValue + this.time_stamp + JSON.stringify(this.data)).toString();
   }

   mine(difficulty){
      while(!this.hash.startsWith("0".repeat(difficulty))){
         this.pf_work++;
         this.hash=this.calculateHash();
      }
   }
}

class Blockchain{
   constructor(){
      let genesisBlock=new Block("0", {isGenesisBlock: true});
      this.chain=[genesisBlock];
   }

   addNewBlock(data){
      let lastBlock=this.chain[this.chain.length-1];
      let newBlock=new Block(lastBlock.hash, data);
      newBlock.mine(2); //find a hash for new block
      this.chain.push(newBlock);
   }

   isValid_hash(){
      for(let i=1; i<this.chain.length; i++){
         const currentBlock=this.chain[i];
         const previousBlock=this.chain[i-1];
         if(currentBlock.hash!=currentBlock.calculateHash()) return false;
         if(currentBlock.prev_hashValue!=previousBlock.hash) return false;
      }
      return true;
   }
}
//test
let blockchain=new Blockchain();

blockchain.addNewBlock({
   from: "joe",
   to:"Juhi",
   amount: 100,
});

blockchain.addNewBlock({
   from: "martin",
   to: "Genny",
   amount: 150,
});

console.log(blockchain);
console.log("Blockchain is valid: "+blockchain.isValid_hash());

要編譯程式,您必須安裝node.js。使用這篇文章 (Node.js - 環境設定 )安裝 Node.js。然後使用以下命令安裝 crypto.js 庫。

npm install crypto-js

然後編譯 JavaScript 程式檔案。這裡,檔名是 blockchain。

node blockchain.js

輸出

更新於:2023年7月14日

63 次檢視

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.