forked from wallet42/node-forknote-util
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathindex.js
More file actions
295 lines (255 loc) · 10.6 KB
/
Copy pathindex.js
File metadata and controls
295 lines (255 loc) · 10.6 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
module.exports = require('bindings')('blocktemplate.node');
const SHA3 = require('sha3');
const varuint = require('varuint-bitcoin');
const crypto = require('crypto');
const fastMerkleRoot = require('merkle-lib/fastRoot');
const { BASE_DIFF, BASE_RAVEN_DIFF, difficultyToFloat, parsePositiveBigInt } = require('./bigint');
const bitcoinUtils = require('./bitcoin_utils');
const rtm = require('./rtm');
const MAX_TEMPLATE_TRANSACTIONS = 5000;
function scriptCompile(addrHash) {
return bitcoinUtils.p2pkhScript(addrHash);
}
function reverseBuffer(buff) {
const reversed = Buffer.alloc(buff.length);
for (let i = buff.length - 1; i >= 0; i--) reversed[buff.length - i - 1] = buff[i];
return reversed;
}
function sha256(buffer) {
return crypto.createHash('sha256').update(buffer).digest();
};
function hash256(buffer) {
return sha256(sha256(buffer));
};
function sha256_3(buffer) {
return crypto.createHash('sha3-256').update(buffer).digest();
};
function hash256_3(buffer) {
return sha256_3(sha256_3(buffer));
};
function transaction_hash(transaction, forWitness) {
if (forWitness && transaction.isCoinbase()) return Buffer.alloc(32, 0);
return hash256(transaction.__toBuffer(undefined, undefined, forWitness));
}
function transaction_hash3(transaction, forWitness) {
if (forWitness && transaction.isCoinbase()) return Buffer.alloc(32, 0);
return hash256_3(transaction.__toBuffer(undefined, undefined, forWitness));
}
function getMerkleRoot(transactions, transaction_hash_func, merkle_hash_func) {
if (transactions.length === 0) return Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
const hashes = transactions.map(transaction => transaction_hash_func(transaction, false));
return fastMerkleRoot(hashes, merkle_hash_func);
}
let last_epoch_number;
let last_seed_hash;
module.exports.baseDiff = function() {
return BASE_DIFF;
};
module.exports.baseRavenDiff = function() {
return BASE_RAVEN_DIFF;
};
module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
const poolAddrHash = bitcoinUtils.base58AddressToHash160(poolAddress);
const coinbaseOutputs = [];
let serializedBlockHeight;
let bytesHeight;
{ // input for coinbase tx
let blockHeightSerial = rpcData.height.toString(16).length % 2 === 0 ?
rpcData.height.toString(16) :
`0${ rpcData.height.toString(16)}`;
bytesHeight = Math.ceil((rpcData.height * 2).toString(2).length / 8);
const lengthDiff = blockHeightSerial.length/2 - bytesHeight;
for (let i = 0; i < lengthDiff; i++) blockHeightSerial = `${blockHeightSerial }00`;
serializedBlockHeight = Buffer.concat([
Buffer.from(`0${ bytesHeight}`, 'hex'),
reverseBuffer(Buffer.from(blockHeightSerial, 'hex')),
Buffer.from('00', 'hex') // OP_0
]);
coinbaseOutputs.push({
value: Math.floor(rpcData.coinbasevalue),
script: scriptCompile(poolAddrHash)
});
if (rpcData.CommunityAutonomousAddress && rpcData.CommunityAutonomousValue) {
coinbaseOutputs.push({
value: Math.floor(rpcData.CommunityAutonomousValue),
script: scriptCompile(bitcoinUtils.base58AddressToHash160(rpcData.CommunityAutonomousAddress))
});
}
if (rpcData.default_witness_commitment) {
coinbaseOutputs.push({
value: 0,
script: Buffer.from(rpcData.default_witness_commitment, 'hex')
});
}
}
let header = Buffer.alloc(80);
{ let position = 0;
header.writeUInt32BE(rpcData.height, position); // height offset 0
header.write(rpcData.bits, position += 4, 4, 'hex'); // bits offset 4
header.writeUInt32BE(rpcData.curtime, position += 4); // nTime offset 8
header.write('DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD', position += 4, 32, 'hex'); // merkleRoot (placeholder, filled in later, offset 12)
header.write(rpcData.previousblockhash, position += 32, 32, 'hex'); // prevblockhash offset 44
header.writeUInt32BE(rpcData.version, position += 32); // version offset 76
header = reverseBuffer(header);
}
let blob = Buffer.concat([
header, // 80 bytes
Buffer.from('AAAAAAAAAAAAAAAA', 'hex'), // 8 bytes
Buffer.from('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB', 'hex'), // 32 bytes
varuint.encode(rpcData.transactions.length + 1, Buffer.alloc(varuint.encodingLength(rpcData.transactions.length + 1)), 0)
]);
const offset1 = blob.length;
const txCoinbase = bitcoinUtils.serializeTransaction({
version: 1,
inputs: [{
// will be used for our reserved_offset extra_nonce
hash: Buffer.alloc(32, 0),
index: 0xffffffff,
sequence: 0xffffffff,
script: Buffer.concat([serializedBlockHeight, Buffer.alloc(17, 0xCC)]) // 17 bytes
}],
outputs: coinbaseOutputs,
locktime: 0
});
blob = Buffer.concat([ blob, txCoinbase ]);
rpcData.transactions.forEach(function (value) {
blob = Buffer.concat([ blob, Buffer.from(value.data, 'hex') ]);
});
const EPOCH_LENGTH = 7500;
const epoch_number = Math.floor(rpcData.height / EPOCH_LENGTH);
if (last_epoch_number !== epoch_number) {
const sha3 = new SHA3.SHA3Hash(256);
if (last_epoch_number && last_epoch_number + 1 === epoch_number) {
last_seed_hash = sha3.update(last_seed_hash).digest();
} else {
last_seed_hash = Buffer.alloc(32, 0);
for (let i = 0; i < epoch_number; i++) {
last_seed_hash = sha3.update(last_seed_hash).digest();
sha3.reset();
}
}
last_epoch_number = epoch_number;
}
const difficulty = difficultyToFloat(module.exports.baseRavenDiff(), rpcData.target, 16, 'Raven target');
return {
blocktemplate_blob: blob.toString('hex'),
// reserved_offset to CCCCCC....
reserved_offset: offset1 + 4 /* txCoinbase.version */ + 1 /* vinLen */ + 32 /* hash */ + 4 /* index */ +
1 /* vScript len */ + 1 /* coinbase height len */ + bytesHeight + 1 /* trailing zero byte */,
seed_hash: last_seed_hash.toString('hex'),
difficulty,
height: rpcData.height,
bits: rpcData.bits,
prev_hash: rpcData.previousblockhash,
};
};
function update_merkle_root_hash(offsetArg, payload, blob_in, blob_out, transaction_hash_func, merkle_hash_func = hash256) {
let offset = offsetArg;
const nTransactions = varuint.decode(blob_in, offset);
offset += varuint.decode.bytes;
if (nTransactions < 1 || nTransactions > MAX_TEMPLATE_TRANSACTIONS) {
throw new Error('Invalid transaction count in block template');
}
const transactions = [];
for (let i = 0; i < nTransactions; ++i) {
if (offset >= blob_in.length) {
throw new Error('Invalid transaction offset in block template');
}
let tx;
if (payload) {
let parsed;
try {
parsed = rtm.readTransaction(blob_in, offset, true);
} catch (_err) {
throw new Error('Unable to parse transaction from block template');
}
if (!parsed || parsed.offset <= offset || parsed.offset > blob_in.length) {
throw new Error('Invalid transaction size in block template');
}
tx = parsed.transaction;
offset = parsed.offset;
if (i + 1 < nTransactions) {
const nextOffset = rtm.findRecoverableTransactionOffset(blob_in, offset, true);
if (nextOffset !== offset) {
tx = rtm.extendTransactionRaw(tx, blob_in.slice(offset, nextOffset));
offset = nextOffset;
}
}
} else {
const parsed = bitcoinUtils.readRawBitcoinTransaction(blob_in, offset);
tx = parsed.transaction;
offset = parsed.offset;
}
transactions.push(tx);
}
if (offset !== blob_in.length) {
throw new Error('Unexpected data after block template transactions');
}
getMerkleRoot(transactions, transaction_hash_func, merkle_hash_func).copy(blob_out, 4 + 32);
};
module.exports.blockHashBuff = function(blobBuffer) {
return reverseBuffer(hash256(blobBuffer));
};
module.exports.blockHashBuff3 = function(blobBuffer) {
return reverseBuffer(hash256_3(blobBuffer));
};
module.exports.convertRavenBlob = function(blobBuffer) {
const header = blobBuffer.slice(0, 80);
update_merkle_root_hash(80 + 8 + 32, false, blobBuffer, header, transaction_hash);
return module.exports.blockHashBuff(header);
};
module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) {
update_merkle_root_hash(80 + 8 + 32, false, blockTemplate, blockTemplate, transaction_hash);
nonceBuff.copy (blockTemplate, 80, 0, 8);
mixhashBuff.copy(blockTemplate, 88, 0, 32);
return blockTemplate;
};
module.exports.constructNewDeroBlob = function(blockTemplate, nonceBuff) {
nonceBuff.copy(blockTemplate, 39, 0, 4);
return blockTemplate;
};
module.exports.EthBlockTemplate = function(rpcData) {
const difficulty = Number(module.exports.baseDiff() / parsePositiveBigInt(stripHexPrefix(rpcData[2], 'ETH target'), 16, 'ETH target'));
return {
hash: rpcData[0].substr(2),
seed_hash: rpcData[1].substr(2),
difficulty,
height: parseInt(rpcData[3])
};
};
module.exports.ErgBlockTemplate = function(rpcData) {
const difficulty = Number(module.exports.baseDiff() / parsePositiveBigInt(rpcData.b, undefined, 'ERG target'));
return {
hash: rpcData.msg,
hash2: rpcData.pk,
difficulty,
height: parseInt(rpcData.h)
};
};
module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
return rtm.RtmBlockTemplate(rpcData, poolAddress);
};
module.exports.convertRtmBlob = function(blobBuffer) {
const header = blobBuffer.slice(0, 80);
update_merkle_root_hash(80, true, blobBuffer, header, transaction_hash);
return header;
};
module.exports.convertKcnBlob = function(blobBuffer) {
const header = blobBuffer.slice(0, 80);
update_merkle_root_hash(80, false, blobBuffer, header, transaction_hash3, hash256_3);
return header;
};
module.exports.constructNewRtmBlob = function(blockTemplate, nonceBuff) {
update_merkle_root_hash(80, true, blockTemplate, blockTemplate, transaction_hash);
nonceBuff.copy(blockTemplate, 76, 0, 4);
return blockTemplate;
};
module.exports.constructNewKcnBlob = function(blockTemplate, nonceBuff) {
update_merkle_root_hash(80, false, blockTemplate, blockTemplate, transaction_hash3, hash256_3);
nonceBuff.copy(blockTemplate, 76, 0, 4);
return blockTemplate;
};
function stripHexPrefix(value, label) {
if (typeof value !== 'string') throw new Error(`Invalid ${label}`);
return value.startsWith('0x') ? value.slice(2) : value;
}