forked from mtcscratch/miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminer.js
152 lines (88 loc) · 2.61 KB
/
miner.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
const request = require('request');
const crypto = require('crypto');
const fs = require('fs')
require('http').createServer().listen(3000)
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
console.log(config.username)
function hasher(s){
return new Promise(function(resolve, reject){
const val = crypto.randomBytes(32).toString('hex')
const hash = crypto.createHash('sha256').update(val + s).digest('hex');
resolve({'val': val + s, 'hash': hash})
});
}
function post(url, jsonObject){
return new Promise(function(resolve, reject){
var options = {
uri: url,
method: 'POST',
json: jsonObject
};
request(options, function (error, response, body) {
if (!error) {
resolve(response.body);
}else{
reject(error)
}
});
})
}
function get(url){
return new Promise(function(resolve, reject){
let object = {};
request(
{ uri: url },
function(error, response, body) {
if (!error) {
resolve(JSON.parse(response.body))
}else{
reject(error)
}
}
);
})
}
let hashCount = 0;
let minerKey = null;
let salt = null;
let lastSecond = null;
async function loop(){
let serverConfig = null;
let e = null;
while (true){
if (lastSecond + config.checkTimeout < Date.now()){
lastSecond = Date.now();
console.log(`# Checking block key integrity at ${Date.now()}ms`)
serverConfig = await get(`${config.mattcoinApiUrl}/config`)
if (minerKey != serverConfig.key){
minerKey = serverConfig.key;
salt = serverConfig.salt
console.log(`# Block key integrity nullified! New key ${minerKey} registered.`)
}
}
let hashObject = await hasher(salt);
hashCount++;
if(hashObject.hash.startsWith(minerKey)){
console.log(`# Found hash that satisfies block key`)
console.log(hashObject.hash)
console.log(`# Attempting hash verification`)
e = await post(`${config.mattcoinApiUrl}/submission`, {'value': hashObject.val, 'user': config.username} );
serverConfig = await get(`${config.mattcoinApiUrl}/config`)
if(e.response){
console.log(`# Authored block ${serverConfig.blockCount - 1} with success!`)
minerKey = serverConfig.key;
salt = serverConfig.salt
}else{
console.log(`# An error occured with the provided hash, mining has continued.`)
minerKey = serverConfig.key;
salt = serverConfig.salt
}
}
}
}
get(`${config.mattcoinApiUrl}/config`).then(function(serverConfig){
minerKey = serverConfig.key;
console.log(`Starting miner with key ${minerKey} as first block key.`)
lastSecond = Date.now()
loop()
})