Skip to content

Commit cec5fb5

Browse files
Extract BufferWriter class
Move various write methods to a class `BufferWriter`. This allows increased code reuse for libraries that want to implement different serialization formats. Also de-duplicates some code paths and increases test coverage. Original concept by idea by https://github.com/argjv: https://github.com/BitGo/bitgo-utxo-lib/blob/master/src/bufferWriter.js
1 parent c06c372 commit cec5fb5

File tree

6 files changed

+432
-181
lines changed

6 files changed

+432
-181
lines changed

src/buffer_writer.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
Object.defineProperty(exports, '__esModule', { value: true });
3+
const bufferutils = require('./bufferutils');
4+
const types = require('./types');
5+
const typeforce = require('typeforce');
6+
const varuint = require('varuint-bitcoin');
7+
/**
8+
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
9+
*/
10+
class BufferWriter {
11+
constructor(buffer, offset = 0) {
12+
typeforce(types.tuple(types.Buffer, types.UInt32), [buffer, offset]);
13+
this.buffer = buffer;
14+
this.offset = offset;
15+
}
16+
writeUInt8(i) {
17+
this.offset = this.buffer.writeUInt8(i, this.offset);
18+
}
19+
writeInt32(i) {
20+
this.offset = this.buffer.writeInt32LE(i, this.offset);
21+
}
22+
writeUInt32(i) {
23+
this.offset = this.buffer.writeUInt32LE(i, this.offset);
24+
}
25+
writeUInt64(i) {
26+
this.offset = bufferutils.writeUInt64LE(this.buffer, i, this.offset);
27+
}
28+
writeVarInt(i) {
29+
varuint.encode(i, this.buffer, this.offset);
30+
this.offset += varuint.encode.bytes;
31+
}
32+
writeSlice(slice) {
33+
this.offset += slice.copy(this.buffer, this.offset);
34+
}
35+
writeVarSlice(slice) {
36+
this.writeVarInt(slice.length);
37+
this.writeSlice(slice);
38+
}
39+
writeVector(vector) {
40+
this.writeVarInt(vector.length);
41+
vector.forEach(buf => this.writeVarSlice(buf));
42+
}
43+
}
44+
exports.BufferWriter = BufferWriter;

src/transaction.js

Lines changed: 45 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3+
const buffer_writer_1 = require('./buffer_writer');
34
const bufferutils = require('./bufferutils');
45
const bufferutils_1 = require('./bufferutils');
56
const bcrypto = require('./crypto');
@@ -296,33 +297,16 @@ class Transaction {
296297
arguments,
297298
);
298299
let tbuffer = Buffer.from([]);
299-
let toffset = 0;
300-
function writeSlice(slice) {
301-
toffset += slice.copy(tbuffer, toffset);
302-
}
303-
function writeUInt32(i) {
304-
toffset = tbuffer.writeUInt32LE(i, toffset);
305-
}
306-
function writeUInt64(i) {
307-
toffset = bufferutils.writeUInt64LE(tbuffer, i, toffset);
308-
}
309-
function writeVarInt(i) {
310-
varuint.encode(i, tbuffer, toffset);
311-
toffset += varuint.encode.bytes;
312-
}
313-
function writeVarSlice(slice) {
314-
writeVarInt(slice.length);
315-
writeSlice(slice);
316-
}
300+
let bufferWriter;
317301
let hashOutputs = ZERO;
318302
let hashPrevouts = ZERO;
319303
let hashSequence = ZERO;
320304
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
321305
tbuffer = Buffer.allocUnsafe(36 * this.ins.length);
322-
toffset = 0;
306+
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
323307
this.ins.forEach(txIn => {
324-
writeSlice(txIn.hash);
325-
writeUInt32(txIn.index);
308+
bufferWriter.writeSlice(txIn.hash);
309+
bufferWriter.writeUInt32(txIn.index);
326310
});
327311
hashPrevouts = bcrypto.hash256(tbuffer);
328312
}
@@ -332,9 +316,9 @@ class Transaction {
332316
(hashType & 0x1f) !== Transaction.SIGHASH_NONE
333317
) {
334318
tbuffer = Buffer.allocUnsafe(4 * this.ins.length);
335-
toffset = 0;
319+
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
336320
this.ins.forEach(txIn => {
337-
writeUInt32(txIn.sequence);
321+
bufferWriter.writeUInt32(txIn.sequence);
338322
});
339323
hashSequence = bcrypto.hash256(tbuffer);
340324
}
@@ -346,10 +330,10 @@ class Transaction {
346330
return sum + 8 + varSliceSize(output.script);
347331
}, 0);
348332
tbuffer = Buffer.allocUnsafe(txOutsSize);
349-
toffset = 0;
333+
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
350334
this.outs.forEach(out => {
351-
writeUInt64(out.value);
352-
writeVarSlice(out.script);
335+
bufferWriter.writeUInt64(out.value);
336+
bufferWriter.writeVarSlice(out.script);
353337
});
354338
hashOutputs = bcrypto.hash256(tbuffer);
355339
} else if (
@@ -358,25 +342,25 @@ class Transaction {
358342
) {
359343
const output = this.outs[inIndex];
360344
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script));
361-
toffset = 0;
362-
writeUInt64(output.value);
363-
writeVarSlice(output.script);
345+
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
346+
bufferWriter.writeUInt64(output.value);
347+
bufferWriter.writeVarSlice(output.script);
364348
hashOutputs = bcrypto.hash256(tbuffer);
365349
}
366350
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript));
367-
toffset = 0;
351+
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
368352
const input = this.ins[inIndex];
369-
writeUInt32(this.version);
370-
writeSlice(hashPrevouts);
371-
writeSlice(hashSequence);
372-
writeSlice(input.hash);
373-
writeUInt32(input.index);
374-
writeVarSlice(prevOutScript);
375-
writeUInt64(value);
376-
writeUInt32(input.sequence);
377-
writeSlice(hashOutputs);
378-
writeUInt32(this.locktime);
379-
writeUInt32(hashType);
353+
bufferWriter.writeUInt32(this.version);
354+
bufferWriter.writeSlice(hashPrevouts);
355+
bufferWriter.writeSlice(hashSequence);
356+
bufferWriter.writeSlice(input.hash);
357+
bufferWriter.writeUInt32(input.index);
358+
bufferWriter.writeVarSlice(prevOutScript);
359+
bufferWriter.writeUInt64(value);
360+
bufferWriter.writeUInt32(input.sequence);
361+
bufferWriter.writeSlice(hashOutputs);
362+
bufferWriter.writeUInt32(this.locktime);
363+
bufferWriter.writeUInt32(hashType);
380364
return bcrypto.hash256(tbuffer);
381365
}
382366
getHash(forWitness) {
@@ -404,64 +388,41 @@ class Transaction {
404388
}
405389
__toBuffer(buffer, initialOffset, _ALLOW_WITNESS = false) {
406390
if (!buffer) buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS));
407-
let offset = initialOffset || 0;
408-
function writeSlice(slice) {
409-
offset += slice.copy(buffer, offset);
410-
}
411-
function writeUInt8(i) {
412-
offset = buffer.writeUInt8(i, offset);
413-
}
414-
function writeUInt32(i) {
415-
offset = buffer.writeUInt32LE(i, offset);
416-
}
417-
function writeInt32(i) {
418-
offset = buffer.writeInt32LE(i, offset);
419-
}
420-
function writeUInt64(i) {
421-
offset = bufferutils.writeUInt64LE(buffer, i, offset);
422-
}
423-
function writeVarInt(i) {
424-
varuint.encode(i, buffer, offset);
425-
offset += varuint.encode.bytes;
426-
}
427-
function writeVarSlice(slice) {
428-
writeVarInt(slice.length);
429-
writeSlice(slice);
430-
}
431-
function writeVector(vector) {
432-
writeVarInt(vector.length);
433-
vector.forEach(writeVarSlice);
434-
}
435-
writeInt32(this.version);
391+
const bufferWriter = new buffer_writer_1.BufferWriter(
392+
buffer,
393+
initialOffset || 0,
394+
);
395+
bufferWriter.writeInt32(this.version);
436396
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
437397
if (hasWitnesses) {
438-
writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
439-
writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG);
398+
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
399+
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG);
440400
}
441-
writeVarInt(this.ins.length);
401+
bufferWriter.writeVarInt(this.ins.length);
442402
this.ins.forEach(txIn => {
443-
writeSlice(txIn.hash);
444-
writeUInt32(txIn.index);
445-
writeVarSlice(txIn.script);
446-
writeUInt32(txIn.sequence);
403+
bufferWriter.writeSlice(txIn.hash);
404+
bufferWriter.writeUInt32(txIn.index);
405+
bufferWriter.writeVarSlice(txIn.script);
406+
bufferWriter.writeUInt32(txIn.sequence);
447407
});
448-
writeVarInt(this.outs.length);
408+
bufferWriter.writeVarInt(this.outs.length);
449409
this.outs.forEach(txOut => {
450410
if (isOutput(txOut)) {
451-
writeUInt64(txOut.value);
411+
bufferWriter.writeUInt64(txOut.value);
452412
} else {
453-
writeSlice(txOut.valueBuffer);
413+
bufferWriter.writeSlice(txOut.valueBuffer);
454414
}
455-
writeVarSlice(txOut.script);
415+
bufferWriter.writeVarSlice(txOut.script);
456416
});
457417
if (hasWitnesses) {
458418
this.ins.forEach(input => {
459-
writeVector(input.witness);
419+
bufferWriter.writeVector(input.witness);
460420
});
461421
}
462-
writeUInt32(this.locktime);
422+
bufferWriter.writeUInt32(this.locktime);
463423
// avoid slicing unless necessary
464-
if (initialOffset !== undefined) return buffer.slice(initialOffset, offset);
424+
if (initialOffset !== undefined)
425+
return buffer.slice(initialOffset, bufferWriter.offset);
465426
return buffer;
466427
}
467428
}

0 commit comments

Comments
 (0)