-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.js
192 lines (158 loc) · 4.41 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const EC = require('elliptic').ec;
const fs = require('fs');
const crypto = require('crypto');
const { path } = require('express/lib/application');
const ec = new EC('secp256k1');
class Transaction {
constructor(dataType, dataPath, address, { pid, fullName, gender, dob }) {
this.address = address;
this.timestamp = Date.now();
this.dataType = dataType;
this.dataPath = dataPath;
this.data = fs.readFileSync(dataPath);
this.patient = {
pid,
fullName,
gender,
dob,
};
}
calculateHash() {
return crypto
.createHash('sha512')
.update(
this.address +
this.timestamp +
this.dataType +
this.dataPath +
this.data +
JSON.stringify(this.patient)
)
.digest('hex');
}
signTransaction(signingKey) {
if (signingKey.getPublic('hex') !== this.address) {
throw new Error('You cannot sign transactions for other users!');
}
const hashTx = this.calculateHash();
const sig = signingKey.sign(hashTx, 'base64');
this.signature = sig.toDER('hex');
console.log('tx signed');
}
isValid() {
if (!this.signature || this.signature.length === 0) {
throw new Error('No signature in this transaction');
}
const publicKey = ec.keyFromPublic(this.address, 'hex');
return publicKey.verify(this.calculateHash(), this.signature);
}
}
class Block {
constructor(prevHash, timestamp, transactions) {
this.prevHash = prevHash;
this.timestamp = timestamp;
this.transactions = transactions;
this.nonce = 0;
this.hash = this.calculateHash();
}
calculateHash() {
return crypto
.createHash('sha512')
.update(
this.prevHash +
this.timestamp +
JSON.stringify(this.transactions) +
this.nonce
)
.digest('hex');
}
mineBlock(numZeros) {
while (this.hash.substring(0, numZeros) !== Array(numZeros + 1).join('0')) {
this.nonce++;
this.hash = this.calculateHash();
}
}
hasValidTransactions() {
for (const tx of this.transactions) {
if (!tx.isValid()) {
return false;
}
}
return true;
}
}
class Blockchain {
constructor({ chain, numZeros, pendingTransactions }, blkExists) {
this.chain = blkExists ? chain : [this.createGenesisBlock()];
console.log(this.chain, blkExists);
this.numZeros = numZeros;
this.pendingTransactions = pendingTransactions;
}
createGenesisBlock() {
const genBlk = new Block(0, Date.parse('2022-01-01'), []);
console.log('genesis trigg', genBlk);
return genBlk;
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
minePendingTransactions() {
const block = new Block(
this.getLatestBlock().hash,
Date.now(),
this.pendingTransactions
);
block.mineBlock(this.numZeros);
console.log('Block successfully mined!');
this.chain.push(block);
this.pendingTransactions = [];
}
addTx(transaction) {
if (!transaction.address) {
throw new Error('Transaction must include address');
}
if (!transaction.isValid()) {
throw new Error('Cannot add invalid transaction to chain');
}
const pendingTxForWallet = this.pendingTransactions.filter(
tx => tx.address === transaction.address
);
this.pendingTransactions.push(transaction);
console.log('transaction added: %s', transaction);
}
getAllTransactionsForWallet(address) {
const txs = [];
for (const block of this.chain) {
for (const tx of block.transactions) {
if (tx.address === address) {
txs.push(tx);
}
}
}
console.log(`get transactions for wallet count: ${txs.length}`);
return txs;
}
isChainValid() {
const realGenesis = JSON.stringify(this.createGenesisBlock());
if (realGenesis !== JSON.stringify(this.chain[0])) {
return false;
}
for (let i = 1; i < this.chain.length; i++) {
const currentBlock = this.chain[i];
const previousBlock = this.chain[i - 1];
if (previousBlock.hash !== currentBlock.previousHash) {
return false;
}
if (!currentBlock.hasValidTransactions()) {
return false;
}
if (currentBlock.hash !== currentBlock.calculateHash()) {
return false;
}
}
return true;
}
}
module.exports.Blockchain = Blockchain;
module.exports.Block = Block;
module.exports.Transaction = Transaction;