區塊鏈研究實驗室 | 如何從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
    classBlock{ // create the block structure or classconstructor(blockid, previousHash, data){ // create a contractor. in a block we find this information :this.blockid = blockid; // the block idthis.timestamp = Date.now(); // the timestampthis.blockhash = this.getHash(); // the block hashthis.prevHash = previousHash; // the hash of the previous blockthis.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 };}
    classBlockChain{ // the blochain structure or classconstructor(){ // 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 blockthis.chain.push(block); // Add the block to the blockchain }
    }
    const Myfirstblockchain = new BlockChain();Myfirstblockchain.addBlock({sender: "sinai", receiver: "kazadi", amount: 24034}); // first transactionMyfirstblockchain.addBlock({sender: "Dahouda", receiver: "Pat", amount: 32032}); // second transactionMyfirstblockchain.addBlock({sender: "Nkolomoni", receiver: "Mao", amount: 20993}); // third transaction console.log(JSON.stringify(Myfirstblockchain, null6)); // convert the result into a json and show it in the console

    作者:鏈三豐,來源:區塊鏈研究實驗室

    免責聲明:

    1. 本文版權歸原作者所有,僅代表作者本人觀點,不代表鏈報觀點或立場。
    2. 如發現文章、圖片等侵權行爲,侵權責任將由作者本人承擔。
    3. 鏈報僅提供相關項目信息,不構成任何投資建議

    推荐阅读

    ;