@shamdasani/

Blockchain

Nodejs

No description

fork
loading
Files
  • index.js
  • package-lock.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.201409723529282","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"bGV0IFNIQTI1NiA9IHJlcXVpcmUoJ2NyeXB0by1qcy9zaGEyNTYnKTsKCnZhciBkdCA9IG5ldyBEYXRlKCk7CnZhciB0aW1lc3RhbXAgPSBkdC50b1N0cmluZygpOwoKY2xhc3MgQmxvY2sgewogIGNvbnN0cnVjdG9yKGluZGV4LCB0aW1lc3RhbXAsIGRhdGEsIHByZXZpb3VzSGFzaCA9ICIiKSB7CiAgICB0aGlzLmluZGV4ID0gaW5kZXg7CiAgICB0aGlzLnRpbWVzdGFtcCA9IHRpbWVzdGFtcDsKICAgIHRoaXMuZGF0YSA9IGRhdGE7CiAgICB0aGlzLnByZXZpb3VzSGFzaCA9IHByZXZpb3VzSGFzaDsKICAgIHRoaXMuaGFzaCA9IHRoaXMuY2FsY3VsYXRlSGFzaCgpOwogICAgdGhpcy5ub25jZSA9IDA7CiAgfQogIGNhbGN1bGF0ZUhhc2goKSB7CiAgICByZXR1cm4gU0hBMjU2KAogICAgICB0aGlzLmluZGV4ICsKICAgICAgICB0aGlzLnByZXZpb3VzSGFzaCArCiAgICAgICAgdGhpcy50aW1lc3RhbXAgKwogICAgICAgIEpTT04uc3RyaW5naWZ5KHRoaXMuZGF0YSkgKwogICAgICAgIHRoaXMubm9uY2UKICAgICkudG9TdHJpbmcoKTsKICB9CgogIG1pbmVCbG9jayhkaWZmaWN1bHR5KSB7CiAgICB3aGlsZSAoCiAgICAgIHRoaXMuaGFzaC5zdWJzdHJpbmcoMCwgZGlmZmljdWx0eSkgIT09IEFycmF5KGRpZmZpY3VsdHkgKyAxKS5qb2luKCIwIikKICAgICkgewogICAgICB0aGlzLm5vbmNlKys7CiAgICAgIHRoaXMuaGFzaCA9IHRoaXMuY2FsY3VsYXRlSGFzaCgpO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VybiB0cnVlOwogIH0KfQoKbGV0IGVubGlnaHRDaGFpbiA9IG5ldyBCbG9ja2NoYWluKCk7Cgpjb25zb2xlLmxvZygiTWluaW5nIGJsb2NrLi4uIik7CmVubGlnaHRDaGFpbi5hZGRCbG9jayhuZXcgQmxvY2soMSwgdGltZXN0YW1wLCAiVGhpcyBpcyBibG9jayAxIikpOwpjb25zb2xlLmxvZygiTWluaW5nIGJsb2NrLi4uIik7CmVubGlnaHRDaGFpbi5hZGRCbG9jayhuZXcgQmxvY2soMiwgdGltZXN0YW1wLCAiVGhpcyBpcyBibG9jayAyIikpOwoKY29uc29sZS5sb2coSlNPTi5zdHJpbmdpZnkoZW5saWdodENoYWluLCBudWxsLCA0KSk7CmNvbnNvbGUubG9nKCJJcyBvdXIgYmxvY2tjaGFpbiB2YWxpZD8gIiArIGVubGlnaHRDaGFpbi5jaGVja1ZhbGlkKCkudG9TdHJpbmcoKSk7Cg=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
let SHA256 = require('crypto-js/sha256');

var dt = new Date();
var timestamp = dt.toString();

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

  mineBlock(difficulty) {
    while (
      this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")
    ) {
      this.nonce++;
      this.hash = this.calculateHash();
    }
    console.log("Block mined: " + this.hash);
  }
}

class Blockchain {
  constructor() {
    this.chain = [this.createGenesis()];
    this.difficulty = 4;
  }

  createGenesis() {
    return new Block(0, "01/01/2018", "Genesis block", "0");
  }

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

  addBlock(newBlock) {
    newBlock.previousHash = this.latestBlock().hash;
    newBlock.mineBlock(this.difficulty);
    this.chain.push(newBlock);
  }

  checkValid() {
    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.previousHash !== previousBlock.hash) {
        return false;
      }
    }
    return true;
  }
}

let enlightChain = new Blockchain();

console.log("Mining block...");
enlightChain.addBlock(new Block(1, timestamp, "This is block 1"));
console.log("Mining block...");
enlightChain.addBlock(new Block(2, timestamp, "This is block 2"));

console.log(JSON.stringify(enlightChain, null, 4));
console.log("Is our blockchain valid? " + enlightChain.checkValid().toString());
node v10.16.0