forked from bitcoinjs/bitcoinjs-lib
-
Notifications
You must be signed in to change notification settings - Fork 3
/
block_gold.js
279 lines (279 loc) · 9.95 KB
/
block_gold.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
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
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const bufferutils_1 = require('./bufferutils');
const bcrypto = require('./crypto');
const networks = require('./networks');
const transaction_1 = require('./transaction');
const types = require('./types');
const eq = require('equihashjs-verify');
const lwma_1 = require('./lwma');
const fastMerkleRoot = require('merkle-lib/fastRoot');
const typeforce = require('typeforce');
const varuint = require('varuint-bitcoin');
const errorMerkleNoTxes = new TypeError(
'Cannot compute merkle root for zero transactions',
);
const errorWitnessNotSegwit = new TypeError(
'Cannot compute witness commit for non-segwit block',
);
class Block {
constructor() {
this.version = 1;
this.prevHash = undefined;
this.merkleRoot = undefined;
this.height = 0;
this.reserved = undefined;
this.timestamp = 0;
this.witnessCommit = undefined;
this.bits = 0;
this.nonce = undefined;
this.solutionLength = 0;
this.solution = undefined;
this.transactions = undefined;
}
static fromBuffer(buffer) {
if (buffer.length < 140) throw new Error('Buffer too small (< 140 bytes)');
const bufferReader = new bufferutils_1.BufferReader(buffer);
const block = new Block();
block.version = bufferReader.readInt32();
block.prevHash = bufferReader.readSlice(32);
block.merkleRoot = bufferReader.readSlice(32);
block.height = bufferReader.readUInt32();
block.reserved = bufferReader.readSlice(28);
block.timestamp = bufferReader.readUInt32();
block.bits = bufferReader.readUInt32();
block.nonce = bufferReader.readSlice(32);
block.solutionLength = bufferReader.readVarInt();
block.solution = bufferReader.readSlice(block.solutionLength);
if (buffer.length === bufferReader.offset) return block;
const readTransaction = () => {
const tx = transaction_1.Transaction.fromBuffer(
bufferReader.buffer.slice(bufferReader.offset),
true,
);
bufferReader.offset += tx.byteLength();
return tx;
};
const nTransactions = bufferReader.readVarInt();
block.transactions = [];
for (let i = 0; i < nTransactions; ++i) {
const tx = readTransaction();
block.transactions.push(tx);
}
const witnessCommit = block.getWitnessCommit();
// This Block contains a witness commit
if (witnessCommit) block.witnessCommit = witnessCommit;
return block;
}
static fromHex(hex) {
return Block.fromBuffer(Buffer.from(hex, 'hex'));
}
static calculateTarget(bits) {
const exponent = ((bits & 0xff000000) >> 24) - 3;
const mantissa = bits & 0x007fffff;
const target = Buffer.alloc(32, 0);
target.writeUIntBE(mantissa, 29 - exponent, 3);
return target;
}
static calculateMerkleRoot(transactions, forWitness) {
typeforce([{ getHash: types.Function }], transactions);
if (transactions.length === 0) throw errorMerkleNoTxes;
if (forWitness && !txesHaveWitnessCommit(transactions))
throw errorWitnessNotSegwit;
const hashes = transactions.map(transaction =>
transaction.getHash(forWitness),
);
const rootHash = fastMerkleRoot(hashes, bcrypto.hash256);
return forWitness
? bcrypto.hash256(
Buffer.concat([rootHash, transactions[0].ins[0].witness[0]]),
)
: rootHash;
}
getWitnessCommit() {
if (!txesHaveWitnessCommit(this.transactions)) return null;
// The merkle root for the witness data is in an OP_RETURN output.
// There is no rule for the index of the output, so use filter to find it.
// The root is prepended with 0xaa21a9ed so check for 0x6a24aa21a9ed
// If multiple commits are found, the output with highest index is assumed.
const witnessCommits = this.transactions[0].outs
.filter(out =>
out.script.slice(0, 6).equals(Buffer.from('6a24aa21a9ed', 'hex')),
)
.map(out => out.script.slice(6, 38));
if (witnessCommits.length === 0) return null;
// Use the commit with the highest output (should only be one though)
const result = witnessCommits[witnessCommits.length - 1];
if (!(result instanceof Buffer && result.length === 32)) return null;
return result;
}
hasWitnessCommit() {
if (
this.witnessCommit instanceof Buffer &&
this.witnessCommit.length === 32
)
return true;
if (this.getWitnessCommit() !== null) return true;
return false;
}
hasWitness() {
return anyTxHasWitness(this.transactions);
}
weight() {
const base = this.byteLength(false, false);
const total = this.byteLength(false, true);
return base * 3 + total;
}
byteLength(headersOnly, allowWitness = true, useLegacyFormat = false) {
// Solution can have different size, for regtest/testnet is arround 140-170, for mainnet 1400-1500
let headerSize;
if (useLegacyFormat) {
headerSize = 80;
} else {
headerSize =
140 +
varuint.encodingLength(this.solutionLength) +
this.solution.length;
}
if (headersOnly || !this.transactions) return headerSize;
return (
headerSize +
varuint.encodingLength(this.transactions.length) +
this.transactions.reduce((a, x) => a + x.byteLength(allowWitness), 0)
);
}
getHash(network = networks.bitcoingold) {
const useLegacyFormat = this.height < network.forkHeight;
console.warn({ useLegacyFormat });
return bcrypto.hash256(this.toBuffer(true, useLegacyFormat));
}
getId(network = networks.bitcoingold) {
return bufferutils_1.reverseBuffer(this.getHash(network)).toString('hex');
}
getUTCDate() {
const date = new Date(0); // epoch
date.setUTCSeconds(this.timestamp);
return date;
}
// TODO: buffer, offset compatibility
toBuffer(headersOnly, useLegacyFormat = false) {
const buffer = Buffer.allocUnsafe(
this.byteLength(headersOnly, undefined, useLegacyFormat),
);
const bufferWriter = new bufferutils_1.BufferWriter(buffer);
bufferWriter.writeInt32(this.version);
bufferWriter.writeSlice(this.prevHash);
bufferWriter.writeSlice(this.merkleRoot);
if (useLegacyFormat) {
bufferWriter.writeUInt32(this.timestamp);
bufferWriter.writeUInt32(this.bits);
bufferWriter.writeSlice(this.nonce.slice(0, 4));
} else {
bufferWriter.writeInt32(this.height);
bufferWriter.writeSlice(this.reserved);
bufferWriter.writeUInt32(this.timestamp);
bufferWriter.writeUInt32(this.bits);
bufferWriter.writeSlice(this.nonce);
bufferWriter.writeVarInt(this.solutionLength);
bufferWriter.writeSlice(this.solution);
}
if (headersOnly || !this.transactions) return buffer;
varuint.encode(this.transactions.length, buffer, bufferWriter.offset);
bufferWriter.offset += varuint.encode.bytes;
this.transactions.forEach(tx => {
const txSize = tx.byteLength(); // TODO: extract from toBuffer?
tx.toBuffer(buffer, bufferWriter.offset);
bufferWriter.offset += txSize;
});
return buffer;
}
toHex(headersOnly, useLegacyFormat = false) {
return this.toBuffer(headersOnly, useLegacyFormat).toString('hex');
}
checkTxRoots() {
// If the Block has segwit transactions but no witness commit,
// there's no way it can be valid, so fail the check.
const hasWitnessCommit = this.hasWitnessCommit();
if (!hasWitnessCommit && this.hasWitness()) return false;
return (
this.__checkMerkleRoot() &&
(hasWitnessCommit ? this.__checkWitnessCommit() : true)
);
}
checkProofOfWork(validateSolution, network) {
const hash = bufferutils_1.reverseBuffer(this.getHash());
const target = Block.calculateTarget(this.bits);
const validTarget = hash.compare(target) <= 0;
if (this.height < network.forkHeight || !validateSolution || !validTarget) {
console.log(
'### checkProofOfWork prefork or not required to validate solutions',
{ height: this.height, validateSolution, validTarget },
);
return validTarget;
}
let equihashNetwork;
if (
network.equihashForkHeight &&
this.height < network.equihashForkHeight
) {
equihashNetwork = network.equihashLegacy;
} else {
equihashNetwork = network.equihash || eq.networks.bitcoingold;
}
const equihash = new eq.Equihash(equihashNetwork);
const header = this.toHex(true);
return equihash.verify(Buffer.from(header, 'hex'), this.solution);
}
checkTargetBits(network, previousBlocks) {
// Testnet with old lwma params are not supported yet, if needed to validate such blocks
// - add new network in Network.js
if (!network.lwma || this.height < network.lwma.enableHeight) {
return true;
}
const bits = lwma_1.calcNextBits(this, previousBlocks, network.lwma);
return this.bits === bits;
}
__checkMerkleRoot() {
if (!this.transactions) throw errorMerkleNoTxes;
const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions);
return this.merkleRoot.compare(actualMerkleRoot) === 0;
}
__checkWitnessCommit() {
if (!this.transactions) throw errorMerkleNoTxes;
if (!this.hasWitnessCommit()) throw errorWitnessNotSegwit;
const actualWitnessCommit = Block.calculateMerkleRoot(
this.transactions,
true,
);
return this.witnessCommit.compare(actualWitnessCommit) === 0;
}
}
exports.Block = Block;
function txesHaveWitnessCommit(transactions) {
return (
transactions instanceof Array &&
transactions[0] &&
transactions[0].ins &&
transactions[0].ins instanceof Array &&
transactions[0].ins[0] &&
transactions[0].ins[0].witness &&
transactions[0].ins[0].witness instanceof Array &&
transactions[0].ins[0].witness.length > 0
);
}
function anyTxHasWitness(transactions) {
return (
transactions instanceof Array &&
transactions.some(
tx =>
typeof tx === 'object' &&
tx.ins instanceof Array &&
tx.ins.some(
input =>
typeof input === 'object' &&
input.witness instanceof Array &&
input.witness.length > 0,
),
)
);
}