-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
82 lines (44 loc) · 1.61 KB
/
main.js
File metadata and controls
82 lines (44 loc) · 1.61 KB
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
var AES = require("crypto-js/aes");
var sha1 = require("crypto-js/sha256");
var $ = require("./utils");
var Hash = require('multi-hashing');
class Block{
constructor(index, data, prevHash,merkel){
this.index = index;
this.timestamp = Math.floor(Date.now() / 1000);
this.data = data;
this.prevHash = prevHash;
this.merkel=merkel;
this.hash = this.getHash();
}
getHash(){
return sha1(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);
}
}
class BlockChain{
constructor(){
this.chain = [];
}
addBlock(data){
let index = this.chain.length;
let prevHash = this.chain.length !== 0 ? this.chain[this.chain.length - 1].hash : 0;
let merkel = this.chain.length !== 0 ? this.chain[0].hash : 0;
let nounce =0;
let block = new Block(index, data, prevHash,merkel);
this.chain.push(block);
}
chainIsValid(){
for(var i=0;i<this.chain.length;i++){
if(this.chain[i].hash !== this.chain[i].getHash())
return false;
if(i > 0 && this.chain[i].prevHash !== this.chain[i-1].hash)
return false;
}
return true;
}
}
const YudizCode = new BlockChain();
YudizCode.addBlock({sender: "Bruce wayne", reciver: "Tony stark", amount: 100});
YudizCode.addBlock({sender: "Harrison wells", reciver: "Han solo", amount: 50});
YudizCode.addBlock({sender: "Tony stark", reciver: "Ned stark", amount: 75});
console.log(JSON.stringify(YudizCode, null,4));