区块链是包含信息的区块链。 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 和数据,并使用 cryptojs 库将其传递到 sha256 哈希函数来计算块的哈希值。
mine(difficulty){ while(!this.hash.startswith(0.repeat(difficulty))){ this.pf_work++; this.hash=this.calculatehash(); }}
此函数使用工作量证明来查找以一定数量的零开头的哈希值。零的数量由传递给该方法的难度参数确定。 pf_work 属性会递增,直到找到有效的哈希值。
class blockchain{ constructor(){ let genesisblock=new block(0, {isgenesisblock: true}); this.chain=[genesisblock]; }}
chain - 这是一个 block 对象数组,它构成了一个块的链。
区块链类有两个方法 -
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用于查找有效的哈希值,并将其添加到链数组中。
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;}
此方法通过迭代链数组中的每个块并验证其哈希属性与计算的哈希值匹配来检查区块链的有效性。
let blockchain=new blockchain();blockchain.addnewblock({ from: joe, to:juhi, amount: 100,});blockchain.addnewblock({ from: martin, to: genny, amount: 150,});
这里将使用两个块创建一个对象,这两个块将具有区块链类的属性。
此实现可以用作构建需要安全且不可变数据存储的更复杂区块链应用程序的起点。但需要注意的是,这只是一个基本的实现,一个功能齐全的区块链系统还需要许多附加功能,例如交易验证、共识机制和安全措施。
示例:完整代码区块链.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; }}//testlet 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程序文件。这里,文件名是区块链。
node blockchain.js
输出
以上就是如何评估用 javascript 实现的区块链?的详细内容。