使用crypto-js运行基本Javascript区块链模拟的语法错误

时间:2018-01-26 17:10:49

标签: javascript node.js

为了理解区块链技术的基本雏形,我在Youtube上做了这个Javascript教程:https://www.youtube.com/watch?v=zVqczFZr124

本教程将指导您创建一个定义对象和构造函数方法的基本JS文件,然后创建一个对象:

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

class Block{
  constructor(index, timestamp, data, previousHash = ''){
    this.index = index;
    this.timestamp = timestamp;
    this.data = data;
    this.previousHash = previousHash;
    this.hash = this.calculateHash;
  }
  calculateHash() {
    return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();
  }
}

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

  createGenesisBlock(){
    return new Block(0, "01/01/2017", "Genesis block", "0");
  }

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

  addBlock(newBlock){
    newBlock.previousHash = this.getLatestBlock().hash;
    newBlock.hash = newblock.calculateHash();
    this.chain.push(newBlock);
  }
}

let savjeeCoin = new Blockchain();
savjeeCoin.addBlock(new Block(1, "10/07/2017", {amount: 4}));
savjeeCoin.addBlock(new Block(2, "12/07/2017", {amount: 10}));

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

尽管我自己,我设法通过npm命令在我的目录中安装了crypto-js库。

通过node main.js运行脚本,它突然出现了一个错误,这个错误超出了我的掌握(这是我第一次通过命令行运行脚本,我' m通常只是为了设计原型设计而修补JQuery:)

SyntaxError: Unexpected identifier
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:545:28)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:382:7)
    at startup (bootstrap_node.js:137:9)
    at bootstrap_node.js:497:3

我甚至不确定从哪里开始,已经搜索了部分错误。

对node.js来说是全新的,我对Javascript的了解已足以重用JQuery库进行原型设计。感谢任何抛出的骨头。

0 个答案:

没有答案