如何从0构建区块链(二)

区块链研究实验室 阅读 31284 2021-3-16 21:20
分享至
微信扫一扫,打开网页后点击屏幕右上角分享按钮

在上一篇文章中,我们讨论了区块链概念并构建了一个DEMO原型 [ 传送机:区块链研究实验室 | 如何从0构建区块链(一)],在这一集中,我们将使用Javascript的另一种编程语言来实现相同的概念,用Go编写代码可能很困难。

因此,请参考我们在第1集中绘制的图:

如何从0构建区块链(二)

这次,我们将使用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看起来像这样:

如何从0构建区块链(二)

文件夹结构如下所示:

如何从0构建区块链(二)

打开终端并转到javascript文件夹,键入“npm run start不要介意”是否看到错误,这是因为entry.js文件中没有任何内容。

现在我们准备开始对我们的区块链进行编码。entry.js在任何IDE中打开文件并编写此代码以理解它,请跳过注释:

如何从0构建区块链(二)

以下是一些说明:

在上面的代码中,我们创建了一个B锁类,其中包含一个id,时间戳,哈希,以前的哈希和数据属性。将来使用该类我们创建了一个构造函数,并添加了一个用于生成哈希的方法。

由于区块链是一组块,因此我们创建了另一个名为Blockchain的类来存储所有块,它只是Javascript中具有数组的承包商,然后我们添加了方法AddBlock将一个块添加到我们的链中。

最后,我们初始化了链并通过发出3个不同的交易对其进行了测试。

结果:

如果安装了nodemon,只需检查运行它的终端,您将看到整个区块链信息。

如何从0构建区块链(二)

恭喜你!这在Javascript中非常简单,我们只用了几行代码就完成了。

整个代码:

const bcrypt = require('bcrypt') // import the bcrypt js librairyclass Block{ // create the block structure or class  constructor(blockid, previousHash, data){ // create a contractor. in a block we find this information : this.blockid = blockid; // the block id this.timestamp = Date.now(); // the timestamp this.blockhash = this.getHash(); // the block hash this.prevHash = previousHash; // the hash of the previous block this.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 };}class BlockChain{ // the blochain structure or class constructor(){ // 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 block  this.chain.push(block); // Add the block to the blockchain  }}const Myfirstblockchain = new BlockChain(); Myfirstblockchain.addBlock(); // first transactionMyfirstblockchain.addBlock(); // second transactionMyfirstblockchain.addBlock(); // third transaction  console.log(JSON.stringify(Myfirstblockchain, null, 6)); // convert the result into a json and show it in the console
btcfans公众号

微信扫描关注公众号,及时掌握新动向

来源链接
免责声明:
2.本文版权归属原作所有,仅代表作者本人观点,不代表比特范的观点或立场
2.本文版权归属原作所有,仅代表作者本人观点,不代表比特范的观点或立场
标签: 区块链
上一篇:2021.3.16—比特币连续下跌,多头还在吗? 下一篇:ChainX 即将上线 XBTC 2.0 测试网

相关资讯