-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
106 lines (84 loc) · 2.43 KB
/
main.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import SHA256 from "crypto-js/sha256";
type PublicBlock = {
blockState: {
index: number;
timestamp: string;
hash: string;
previousHash: string;
data: any;
};
calculateHash: () => string;
recalculateHash: () => void;
setPreviousHash: (newPreviousHash: string) => void;
}
function Block(index: number, timestamp: string, data: Object, previousHash = ""): PublicBlock {
const blockState = {
index,
timestamp,
hash: calculateHash(),
previousHash,
dataValue: data,
get data() {
return this.dataValue;
},
set data(v) {
this.dataValue = v;
this.hash = recalculateHash();
}
}
function calculateHash() {
return SHA256(index + previousHash + timestamp + JSON.stringify(data)).toString();
}
function recalculateHash() {
return SHA256(blockState.index + blockState.previousHash + blockState.timestamp + JSON.stringify(blockState.data)).toString();
}
function setPreviousHash(newPreviousHash: string) {
blockState.previousHash = newPreviousHash;
}
return {
blockState,
calculateHash,
recalculateHash,
setPreviousHash,
};
}
function Blockchain() {
const chain = [createGenesisBlock()];
function createGenesisBlock() {
return Block(0, "01/01/2020", "Genesis block", "0");
}
const getLatestBlock = () => chain[chain.length - 1];
function addBlock(newBlock: PublicBlock) {
newBlock.setPreviousHash(getLatestBlock().blockState.hash);
newBlock.recalculateHash();
chain.push(newBlock);
}
function isChainValid() {
let isValid = true;
const [, ...chainWithoutFirstBlock] = chain;
chainWithoutFirstBlock.forEach((currentBlock, index) => {
const previousBlock = chain[index];
if (currentBlock.blockState.hash !== currentBlock.calculateHash()) {
console.log("hash");
isValid = false;
}
if (currentBlock.blockState.previousHash !== previousBlock.blockState.hash) {
isValid = false;
}
});
return isValid;
}
return {
addBlock,
chain,
isChainValid,
}
}
const myCoin = Blockchain();
myCoin.addBlock(Block(1, "10/07/2020", { amount: 4 }));
myCoin.addBlock(Block(2, "12/07/2020", { amount: 10 }));
console.log(myCoin.chain);
console.log("Is blockchain valid? ", myCoin.isChainValid());
myCoin.chain[1].blockState.data = { amount: 100 };
console.log("Is blockchain valid? ", myCoin.isChainValid());
console.log(JSON.stringify(myCoin, null, 4));