如何从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-04-20 10:30:43

区块链安全互联网

2021-09-23 22:40:10

区块链比特币技术

2018-03-27 09:52:30

区块链数字货币比特币

2019-10-29 15:46:07

区块链区块链技术

2021-05-10 15:09:47

区块链互联网金融

2022-10-18 08:00:00

2018-01-23 11:09:04

区块链技术重用

2022-02-21 14:29:19

区块链技术医疗保健

2021-02-20 22:35:17

区块链比特币记账

2021-03-14 10:21:36

数据库区块链DNS

2018-06-14 10:32:25

2019-01-24 15:50:06

区块链数字货币比特币

2021-03-16 14:33:12

区块链比特币加密货币
点赞
收藏

51CTO技术栈公众号