-
Notifications
You must be signed in to change notification settings - Fork 0
/
proofofwork.js
71 lines (54 loc) · 1.7 KB
/
proofofwork.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
/*
Proof Of Work in NodeJs
usage:
$ node index.js <DIFFICULTY>
*/
const crypto = require('crypto');
const difficulty = Number(process.argv[2] ?? 4);
const blockHeader = 'Bl0ckH34d3r:';
const prefix = '0';
const humanCount = (count) =>
count < 1e3 ? count.toString() :
count < 1e6 ? (count / 1e3).toFixed(1) + 'K' :
count < 1e9 ? (count / 1e6).toFixed(1) + 'M' :
(count / 1e9).toFixed(1) + 'G';
const sha256sum = (img) => crypto.createHash('sha256').update(img).digest('hex');
const randomBlock = (len) => `${blockHeader}`+crypto.randomBytes((len-blockHeader.length) / 2).toString('hex');
function proofOfWork(data, dif) {
let nonce = 0;
let hash = '';
while (!hash.startsWith( prefix.repeat(dif) )) {
nonce++;
hash = sha256sum(data + nonce);
process.stdout.write(` Attempts: ${humanCount(nonce).padStart(10,' ')}\r`);
}
return { nonce, hash, difficulty };
}
function verifyProofOfWork(block, nonce, difficulty) {
const hash = sha256sum( block + nonce );
return hash.startsWith( prefix.repeat(difficulty) );
}
//New block
const block = randomBlock(64);
console.log(`
Proof of Work difficulty ${difficulty}
Block: "${block}"
`);
const startTimeW = Date.now()
, { nonce, hash } = proofOfWork(block, difficulty)
, endTimeW = Date.now()
, secs = Number(endTimeW - startTimeW) / 1000;
console.log(`
Nonce Found: ${nonce}
Hash: "${hash}"
Time to Work: ${secs} secs
`);
//Validation
const startTimeV = process.hrtime.bigint()
, valid = verifyProofOfWork(block, nonce, difficulty)
, endTimeV = process.hrtime.bigint()
, msecs = Number(endTimeV - startTimeV) / 1e6;
console.log(`
Validate Proof of work: ${valid}
Time to Validate: ${msecs} msecs
`);