-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.js
238 lines (208 loc) · 6 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* ===== SHA256 with Crypto-js ===============================
| Learn more: Crypto-js: https://github.com/brix/crypto-js |
| =========================================================*/
const SHA256 = require('crypto-js/sha256');
const { Block } = require('./block.js');
const db = require('level')('./data/chain');
const util = require('./util.js');
/* ===== Level Helpers ==============================
| Pulling in Helpers for level DB |
| Copied from levelSandbox |
| ===============================================*/
// Add data to levelDB with key/value pair
addLevelDBData = async (key, value) => {
return new Promise((resolve, reject) => {
db.put(key, value)
.then(() => {
db.get(key)
.then(block => {
resolve(block);
})
.catch(err => {
reject(err);
});
})
.catch(err => {
reject(err);
});
});
};
// Get data from levelDB with key
getLevelDBData = key => {
return new Promise((resolve, reject) => {
db.get(key, (err, value) => {
if (err) {
console.log('Not found!', err);
reject(err);
} else {
// console.log('Value = ' + value) // DEBUG
resolve(value);
}
});
});
};
getBlockHeight = () => {
return new Promise((resolve, reject) => {
let height = -1;
db.createReadStream()
.on('data', data => {
height++;
})
.on('error', error => {
reject(error);
})
.on('close', () => {
resolve(height);
});
});
};
/* ===== Blockchain Class ==========================
| Class with a constructor for new blockchain |
| ================================================*/
class Blockchain {
constructor() {
this.getBlockHeight().then(height => {
if (height === -1) {
let genesisBlock = new Block('Genesis Block');
this.addBlock(genesisBlock)
.then(block => {
console.log('-------- THE GENESIS BLOCK --------');
console.log(block);
})
.catch(err => {
console.log(err);
});
}
});
}
// Helpers
async getBlockHeight() {
var height = await getBlockHeight();
return height;
}
async getBlock(blockHeight) {
let block = JSON.parse(await getLevelDBData(blockHeight));
// verify if ins't the Genesis block
if (parseInt(block.height) > 0) {
block.body.star.storyDecoded = Buffer.from(
block.body.star.story,
'hex'
).toString();
}
return block; // returns block at blockHeight as a JSON object
}
// get block by address
async getBlocksByAddress(address) {
const blocks = [];
let block = {};
return new Promise((resolve, reject) => {
db.createReadStream()
.on('data', data => {
if (data.key !== 0) {
block = JSON.parse(data.value);
if (block.body.address === address) {
block.body.star.storyDecoded = Buffer.from(
block.body.star.story,
'hex'
).toString();
blocks.push(block);
}
}
})
.on('error', error => {
return reject(error);
})
.on('close', () => {
return resolve(blocks);
});
});
}
// get block by address
async getBlockByHash(hash) {
let block = {};
return new Promise((resolve, reject) => {
db.createReadStream()
.on('data', data => {
block = JSON.parse(data.value);
if (util.isStringChainEquals(block.hash, hash)) {
if (data.key !== 0) {
block.body.star.storyDecoded = Buffer.from(
block.body.star.story,
'hex'
).toString();
return resolve(block);
} else {
return resolve(block);
}
}
})
.on('error', error => {
return reject(error);
})
.on('close', () => {
return reject('Block not found');
});
});
}
// Add new block
async addBlock(newBlock) {
let previousBlockHeight = parseInt(await this.getBlockHeight());
// Block height
newBlock.height = previousBlockHeight + 1;
// UTC timestamp
newBlock.time = new Date()
.getTime()
.toString()
.slice(0, -3);
// previous block hash
if (newBlock.height > 0) {
let previousBlock = await this.getBlock(previousBlockHeight);
newBlock.previousBlockHash = previousBlock.hash;
}
// Block hash with SHA256 using newBlock and converting to a string
newBlock.hash = SHA256(JSON.stringify(newBlock)).toString();
// Adding block object to levelDB
await addLevelDBData(newBlock.height, JSON.stringify(newBlock));
// return the new block
return newBlock;
}
// validate block
async validateBlock(blockHeight) {
let block = await this.getBlock(blockHeight);
block = JSON.parse(block);
let blockHash = block.hash;
block.hash = '';
let validBlockHash = SHA256(JSON.stringify(block)).toString();
if (blockHash === validBlockHash) {
return true;
} else {
console.log('Block # ' + blockHeight + ' invalid hash:');
return false;
}
}
// Validate blockchain
async validateChain() {
let previousHash = '';
let hasError = false;
const height = await this.getBlockHeight();
for (let i = 0; i <= height; i++) {
this.getBlock(i).then(async block => {
block = JSON.parse(block);
const isValidBlock = await this.validateBlock(block.height);
if (!isValidBlock) {
console.log('error on block: ' + i);
hasError = true;
}
if (block.previousBlockHash !== previousHash) {
console.log('error previous block hash on block: ' + i);
hasError = true;
}
previousHash = block.hash;
if (!hasError) {
console.log('BLOCKCHAIN VALIDATED');
}
});
}
}
}
module.exports = { Blockchain };