我正在尝试构建这个简单的代码“ BlockChain”,我在网上找到了它,以使我对区块链技术有更好的了解。

但是当我运行它时,它给我一个错误。

这是我得到的错误:

从“区块链”导入{BlockChain};
       ^

语法错误:意外的令牌



类块

 export class  Block{

    constructor(timestamp, data, previousHash = '') {
        this.timestamp = timestamp;
        this.data = data;
        this.previousHash = previousHash;
        // The calculation of the hash must be at the end so to ensure that all data is assigned correctly before calculation
        this.hash = this.calculateHash(); }

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


类BlockChain

import {Block} from 'Block';



  export class  BlockChain {

    constructor() {
        this.chain = [this.createGenesisBlock()];
    }

    createGenesisBlock(){
        return new Block("2018-11-11 00:00:00", "Genesis block of simple chain", "");
    }

    getLatestBlock() {
        return this.chain[this.chain.length - 1];
    }

    addBlock(newBlock) {
        // The previous hash value of the new block is the hash value of the last block of the existing blockchain;
        newBlock.previousHash = this.getLatestBlock().hash;
        // Recalculate the hash value of the new block (because the previousHash is specified);
        newBlock.hash = newBlock.calculateHash();
        //Add new blocks to the chain;
        this.chain.push(newBlock);
    }

    isChainValid() {
        //Traverse all the blocks
        for (let i = 1; i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];
            //Recalculate the has value of the current block. If the hash value is not matched, it indicates that data of the block was changed without permission, and therefore the has value is not recalculated.
            if (currentBlock.hash !== currentBlock.calculateHash()) {
                console.error("hash not equal: " + JSON.stringify(currentBlock));
                return false;
            }
            // Determine whether the previousHash of the current block is equal to the hash of the previous block. If they are not equal to each other, this means that the previous block was changed without permission. Although the hash value is recalculated correctly, the hash value of the subsequent block is not recalculated, resulting the the whole chain breaking.
            if (currentBlock.previousHash !== previousBlock.calculateHash) {
                console.error("previous hash not right: " + JSON.stringify(currentBlock));
                return false;
            }
        }
        return true;
    }

}


类测验

import {BlockChain} from 'BlockChain';
import {Block} from 'Block';



    let simpleChain = new BlockChain();

    simpleChain.addBlock(new Block("2018-11-11 00:00:01", {amount: 10}));
    simpleChain.addBlock(new Block("2018-11-11 00:00:02", {amount: 20}));


    console.log(JSON.stringify(simpleChain, null, 4));

    console.log("is the chain valid? " + simpleChain.isChainValid());

最佳答案

代码很好。您可以如下运行代码:


使用Node安装crypto-js:



  npm安装crypto-js



从“命令”窗口/终端打开“节点”提示:



  节点



在节点中打开编辑器:


javascript - 简单的BlockChain程序-LMLPHP


需要sha256模块。将以下代码粘贴到编辑器中:



  const SHA256 = require('crypto-js / sha256');



在编辑器中粘贴Block,BlockChain和Test类:


类块

class  Block{

    constructor(timestamp, data, previousHash = '') {
        this.timestamp = timestamp;
        this.data = data;
        this.previousHash = previousHash;
        // The calculation of the hash must be at the end so to ensure that all data is assigned correctly before calculation
        this.hash = this.calculateHash(); }

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


类区块链

class  BlockChain {

    constructor() {
        this.chain = [this.createGenesisBlock()];
    }

    createGenesisBlock(){
        return new Block("2018-11-11 00:00:00", "Genesis block of simple chain", "");
    }

    getLatestBlock() {
        return this.chain[this.chain.length - 1];
    }

    addBlock(newBlock) {
        // The previous hash value of the new block is the hash value of the last block of the existing blockchain;
        newBlock.previousHash = this.getLatestBlock().hash;
        // Recalculate the hash value of the new block (because the previousHash is specified);
        newBlock.hash = newBlock.calculateHash();
        //Add new blocks to the chain;
        this.chain.push(newBlock);
    }

    isChainValid() {
        //Traverse all the blocks
        for (let i = 1; i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];
            //Recalculate the has value of the current block. If the hash value is not matched, it indicates that data of the block was changed without permission, and therefore the has value is not recalculated.
            if (currentBlock.hash !== currentBlock.calculateHash()) {
                console.error("hash not equal: " + JSON.stringify(currentBlock));
                return false;
            }
            // Determine whether the previousHash of the current block is equal to the hash of the previous block. If they are not equal to each other, this means that the previous block was changed without permission. Although the hash value is recalculated correctly, the hash value of the subsequent block is not recalculated, resulting the the whole chain breaking.
            if (currentBlock.previousHash !== previousBlock.calculateHash) {
                console.error("previous hash not right: " + JSON.stringify(currentBlock));
                return false;
            }
        }
        return true;
    }

}


类测验

let simpleChain = new BlockChain();

    simpleChain.addBlock(new Block("2018-11-11 00:00:01", {amount: 10}));
    simpleChain.addBlock(new Block("2018-11-11 00:00:02", {amount: 20}));


    console.log(JSON.stringify(simpleChain, null, 4));

    console.log("is the chain valid? " + simpleChain.isChainValid());



按^ D完成


这将为您提供以下输出:

javascript - 简单的BlockChain程序-LMLPHP

关于javascript - 简单的BlockChain程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59863510/

10-09 22:26