-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
208 lines (172 loc) · 7.08 KB
/
index.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
const crypto = require('crypto');
const Client = require('bitcoin-core');
const { MerkleTree } = require('merkletreejs');
const SHA256 = require('crypto-js/sha256');
var childProcess = require('child_process');
const serviceAccount = require("./env.json");
const client = new Client({
host: 'localhost',
// network: 'testnet',
username: serviceAccount.login,
password: serviceAccount.passw,
// port: 18332
// port: 18445
port: 8332
});
let getBlockTemplate;
let versionLE;
let previousblockhashLE = '';
let target= '';
let rootLE;
let timeLE;
let bitsLE;
let txID = [];
let txData = '';
let heatherString = '';
let shaHeatherLE = '';
read_data().then(r => getWork());
async function read_data() {
let fee =0;
let sigops =0;
let weight =0;
let tx0= '';
getBlockTemplate = await client.getBlockTemplate({"rules": ["segwit"]});
let sigoplimit = getBlockTemplate.sigoplimit;
let weightlimit = getBlockTemplate.weightlimit;
let coinbasevalue = getBlockTemplate.coinbasevalue/100000000;
versionLE = reverseX(getBlockTemplate.version);
previousblockhashLE = reverseX(getBlockTemplate.previousblockhash);
target = getBlockTemplate.target;
//1
await client.createRawTransaction ([],
// {"tb1qlj6r6w7xnez3m8mlzfyc00npsx6srtwd424gwv":coinbasevalue}).then(help => {
{"bc1qhl2d9f932x4f76vdqgupmy0698767aahmcz95p":coinbasevalue}).then(help => {
tx0 = '02000000'+ //версия
'01'+ //кол входов
'0000000000000000000000000000000000000000000000000000000000000000'+ //адрес входа
'ffffffff'+ //адрес выхода
'13'+ //байт в coinbase
'03'+
byteToString(reverseX(getBlockTemplate.height))+
'4578787573206c6f7665204c696e61'+ //coinbase
'ffffffff'+ //sequence
help.slice(10);
txData = tx0
});
//console.log(tx0);
txID = [];
await client.decodeRawTransaction(tx0).then(help => {
txID.push(help.txid);
weight += help.weight;
});
//2
await getBlockTemplate.transactions.forEach(transactions => {
if (sigops+transactions.sigops < sigoplimit && weight+transactions.weight < weightlimit) {
sigops += transactions.sigops;
weight += transactions.weight;
fee += transactions.fee;
txID.push(transactions.txid);
txData = txData+transactions.data;
}
});
// console.clear();
console.log('Block: '+getBlockTemplate.height+' '+ new Date(Date.now()));
console.log('sigopslimit: '+sigoplimit+' sigops: '+sigops+' fee: '+fee/100000000+' txLeng: '+txID.length);
console.log('weightlimit: '+weightlimit+' weight: '+weight+' coinbase: '+coinbasevalue);
//3
const leaves = txID.map(x => Buffer.from(x, 'hex'));
const tree = new MerkleTree(leaves, SHA256, { isBitcoinTree: true });
rootLE = reverseX(tree.getRoot().toString('hex'));
//4 time
timeLE = reverseX(getBlockTemplate.curtime);
//5 bits
bitsLE = reverseX(getBlockTemplate.bits);
}
async function getWork() {
let count=0;
//6 nonce
//nonceLE = reverseX('00000000');
let nonceInt = 0;
do {
if (count === 5000000) {
await read_data();
count=0;
}
count++;
let nonceStr = nonceInt.toString(16);
while (nonceStr.length < 8) nonceStr ='0'+ nonceStr;
//shaHeather
let heatherByteLE = versionLE.
concat(previousblockhashLE).
concat(rootLE).
concat(timeLE).
concat(bitsLE).
concat(nonceStr);
heatherString = byteToString(heatherByteLE);
//sha target
let shaHeather = reverseX(dblsha(heatherString));
shaHeatherLE = byteToString(shaHeather);
if (nonceInt< 0xffffffff) nonceInt ++;
else nonceInt = 0;
if (shaHeatherLE.slice(0,8) === '00000000') {
console.log(nonceInt);
console.log('mask: 1------12--------------------------------------------------------------23--------------------------------------------------------------34------45------56------6');
console.log('heatherLE: '+heatherString);
console.log('shaHeatherLE: '+shaHeatherLE);
console.log('target: '+target+'\n');
// await submitBlock();
}
}
while (parseInt(shaHeatherLE, 16) > parseInt(target, 16));
await submitBlock();
// read_data().then(r => getWork());
function dblsha (data) {
let sha2 = crypto.createHash('sha256').update(Buffer.from(data, 'hex')).digest('hex');
return crypto.createHash('sha256').update(Buffer.from(sha2, 'hex')).digest('hex')
}
}
async function submitBlock() {
let info ='';
let txCount;
if (txID.length < 0xFD) txCount = byteToString(reverseX(txID.length));
else if (txID.length <= 0xFFFF) txCount = 'FD'+byteToString(reverseX(txID.length));
else if (txID.length <= 0xFFFFFF) txCount = 'FE'+byteToString(reverseX(txID.length))+'00';
else if (txID.length <= 0xFFFFFFFF) txCount = 'FE'+byteToString(reverseX(txID.length));
else if (txID.length <= 0xFFFFFFFFFF) txCount = 'FF'+byteToString(reverseX(txID.length))+'000000';
else if (txID.length <= 0xFFFFFFFFFFFF) txCount = 'FF'+byteToString(reverseX(txID.length))+'0000';
else if (txID.length <= 0xFFFFFFFFFFFFFF) txCount = 'FF'+byteToString(reverseX(txID.length))+'00';
else if (txID.length <= 0xFFFFFFFFFFFFFFFF) txCount = 'FF'+byteToString(reverseX(txID.length));
else console.log('txcount error!');
console.log('Result success!');
let block = heatherString + txCount + txData;
console.log('block '+block);
await client.submitBlock(block).then(x => info = x);
}
function reverseX (data){
data = data.toString(16);
if (data.length % 2) data='0'+data;
return data.match(/.{1,2}/g).reverse();
}
function byteToString(data) {
let tempStr = '';
data.forEach(x => tempStr += x);
return tempStr
}
function runScript(scriptPath, callback) {
// keep track of whether callback has been invoked to prevent multiple invocations
var invoked = false;
var process = childProcess.fork(scriptPath);
// listen for errors as they may prevent the exit event from firing
process.on('error', function (err) {
if (invoked) return;
invoked = true;
callback(err);
});
// execute the callback once the process has finished running
process.on('exit', function (code) {
if (invoked) return;
invoked = true;
var err = code === 0 ? null : new Error('exit code ' + code);
callback(err);
});
}