-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (30 loc) · 914 Bytes
/
index.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
const createHash = require('sha.js');
var sha256 = createHash('sha256');
class Block {
constructor(id, ts, data, lastHash) {
this.id = id;
this.ts = ts;
this.data = data;
this.lastHash = lastHash;
this.hash = this.blockHash();
}
blockHash() {
return sha256.update(this.id.toString().concat(this.ts.toString(), this.data.toString(), '0'),'utf8').digest('hex');
}
}
function nextBlock(lastBlock) {
newId = lastBlock.id + 1;
newTs = Date.now();
newData = newId.toString() + " hello I am this block";
lastHash = lastBlock.hash;
return new Block(newId, newTs, newData, lastHash);
}
var chain = [new Block(0, Date.now(), 'test date', '0')];
var previousBlock = chain[0];
for(let i = 0; i < 20; i++){
let newBlock = nextBlock(previousBlock);
chain.push(newBlock);
previousBlock = newBlock;
console.log(`Block no. ${newBlock.id} has been added!`);
console.log(`Hash: ${newBlock.hash}`);
}