-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.js
67 lines (54 loc) · 1.92 KB
/
blockchain.js
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
const SHA256 = require('crypto-js/sha256');
class Block {
constructor (index , timestamp, data, prevhash = '', hash){
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.prevhash = prevhash;
this.hash = this.calculateHash();
}
calculateHash(){
return SHA256(this.index + this.timestamp + this.prevhash + JSON.stringify(this.data)).toString();
}
}
class Blockchain {
constructor(){
this.chain = [this.createGenesisBlock()];
}
createGenesisBlock(){
return new Block(0, '01/01/2019', '0')
}
getLatestBlock(){
return this.chain[this.chain.length - 1];
}
addBlock(newBlock){
newBlock.prevhash = this.getLatestBlock().hash
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
}
isChainValid(){
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.prevhash !== previousBlock.hash){
return false;
}
}
return true;
}
}
let MarkCoin = new Blockchain();
MarkCoin.addBlock(new Block(1, '25/03/2019', {amount: 14}));
MarkCoin.addBlock(new Block(2, '26/03/2019', {amount: 12}));
MarkCoin.addBlock(new Block(3, '27/03/2019', {amount: 4}));
MarkCoin.addBlock(new Block(4, '28/03/2019', {amount: 10}));
MarkCoin.addBlock(new Block(5, '29/03/2019', {amount: 9}));
console.log('Klopt deze blockchain?' + MarkCoin.isChainValid());
MarkCoin.chain[3].data = {amount: 100};
MarkCoin.chain[3].hash = MarkCoin.chain[3].calculateHash();
console.log(MarkCoin.chain[4].prevhash);
console.log(MarkCoin.chain[3].hash);
console.log('Klopt de blockchain met de vervalste data nog steeds?' + MarkCoin.isChainValid());