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
This commit is contained in:
parent
c06c372b79
commit
cec5fb5357
6 changed files with 432 additions and 181 deletions
44
src/buffer_writer.js
Normal file
44
src/buffer_writer.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
'use strict';
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
const bufferutils = require('./bufferutils');
|
||||
const types = require('./types');
|
||||
const typeforce = require('typeforce');
|
||||
const varuint = require('varuint-bitcoin');
|
||||
/**
|
||||
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
|
||||
*/
|
||||
class BufferWriter {
|
||||
constructor(buffer, offset = 0) {
|
||||
typeforce(types.tuple(types.Buffer, types.UInt32), [buffer, offset]);
|
||||
this.buffer = buffer;
|
||||
this.offset = offset;
|
||||
}
|
||||
writeUInt8(i) {
|
||||
this.offset = this.buffer.writeUInt8(i, this.offset);
|
||||
}
|
||||
writeInt32(i) {
|
||||
this.offset = this.buffer.writeInt32LE(i, this.offset);
|
||||
}
|
||||
writeUInt32(i) {
|
||||
this.offset = this.buffer.writeUInt32LE(i, this.offset);
|
||||
}
|
||||
writeUInt64(i) {
|
||||
this.offset = bufferutils.writeUInt64LE(this.buffer, i, this.offset);
|
||||
}
|
||||
writeVarInt(i) {
|
||||
varuint.encode(i, this.buffer, this.offset);
|
||||
this.offset += varuint.encode.bytes;
|
||||
}
|
||||
writeSlice(slice) {
|
||||
this.offset += slice.copy(this.buffer, this.offset);
|
||||
}
|
||||
writeVarSlice(slice) {
|
||||
this.writeVarInt(slice.length);
|
||||
this.writeSlice(slice);
|
||||
}
|
||||
writeVector(vector) {
|
||||
this.writeVarInt(vector.length);
|
||||
vector.forEach(buf => this.writeVarSlice(buf));
|
||||
}
|
||||
}
|
||||
exports.BufferWriter = BufferWriter;
|
|
@ -1,5 +1,6 @@
|
|||
'use strict';
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
const buffer_writer_1 = require('./buffer_writer');
|
||||
const bufferutils = require('./bufferutils');
|
||||
const bufferutils_1 = require('./bufferutils');
|
||||
const bcrypto = require('./crypto');
|
||||
|
@ -296,33 +297,16 @@ class Transaction {
|
|||
arguments,
|
||||
);
|
||||
let tbuffer = Buffer.from([]);
|
||||
let toffset = 0;
|
||||
function writeSlice(slice) {
|
||||
toffset += slice.copy(tbuffer, toffset);
|
||||
}
|
||||
function writeUInt32(i) {
|
||||
toffset = tbuffer.writeUInt32LE(i, toffset);
|
||||
}
|
||||
function writeUInt64(i) {
|
||||
toffset = bufferutils.writeUInt64LE(tbuffer, i, toffset);
|
||||
}
|
||||
function writeVarInt(i) {
|
||||
varuint.encode(i, tbuffer, toffset);
|
||||
toffset += varuint.encode.bytes;
|
||||
}
|
||||
function writeVarSlice(slice) {
|
||||
writeVarInt(slice.length);
|
||||
writeSlice(slice);
|
||||
}
|
||||
let bufferWriter;
|
||||
let hashOutputs = ZERO;
|
||||
let hashPrevouts = ZERO;
|
||||
let hashSequence = ZERO;
|
||||
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
|
||||
tbuffer = Buffer.allocUnsafe(36 * this.ins.length);
|
||||
toffset = 0;
|
||||
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
|
||||
this.ins.forEach(txIn => {
|
||||
writeSlice(txIn.hash);
|
||||
writeUInt32(txIn.index);
|
||||
bufferWriter.writeSlice(txIn.hash);
|
||||
bufferWriter.writeUInt32(txIn.index);
|
||||
});
|
||||
hashPrevouts = bcrypto.hash256(tbuffer);
|
||||
}
|
||||
|
@ -332,9 +316,9 @@ class Transaction {
|
|||
(hashType & 0x1f) !== Transaction.SIGHASH_NONE
|
||||
) {
|
||||
tbuffer = Buffer.allocUnsafe(4 * this.ins.length);
|
||||
toffset = 0;
|
||||
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
|
||||
this.ins.forEach(txIn => {
|
||||
writeUInt32(txIn.sequence);
|
||||
bufferWriter.writeUInt32(txIn.sequence);
|
||||
});
|
||||
hashSequence = bcrypto.hash256(tbuffer);
|
||||
}
|
||||
|
@ -346,10 +330,10 @@ class Transaction {
|
|||
return sum + 8 + varSliceSize(output.script);
|
||||
}, 0);
|
||||
tbuffer = Buffer.allocUnsafe(txOutsSize);
|
||||
toffset = 0;
|
||||
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
|
||||
this.outs.forEach(out => {
|
||||
writeUInt64(out.value);
|
||||
writeVarSlice(out.script);
|
||||
bufferWriter.writeUInt64(out.value);
|
||||
bufferWriter.writeVarSlice(out.script);
|
||||
});
|
||||
hashOutputs = bcrypto.hash256(tbuffer);
|
||||
} else if (
|
||||
|
@ -358,25 +342,25 @@ class Transaction {
|
|||
) {
|
||||
const output = this.outs[inIndex];
|
||||
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script));
|
||||
toffset = 0;
|
||||
writeUInt64(output.value);
|
||||
writeVarSlice(output.script);
|
||||
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
|
||||
bufferWriter.writeUInt64(output.value);
|
||||
bufferWriter.writeVarSlice(output.script);
|
||||
hashOutputs = bcrypto.hash256(tbuffer);
|
||||
}
|
||||
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript));
|
||||
toffset = 0;
|
||||
bufferWriter = new buffer_writer_1.BufferWriter(tbuffer, 0);
|
||||
const input = this.ins[inIndex];
|
||||
writeUInt32(this.version);
|
||||
writeSlice(hashPrevouts);
|
||||
writeSlice(hashSequence);
|
||||
writeSlice(input.hash);
|
||||
writeUInt32(input.index);
|
||||
writeVarSlice(prevOutScript);
|
||||
writeUInt64(value);
|
||||
writeUInt32(input.sequence);
|
||||
writeSlice(hashOutputs);
|
||||
writeUInt32(this.locktime);
|
||||
writeUInt32(hashType);
|
||||
bufferWriter.writeUInt32(this.version);
|
||||
bufferWriter.writeSlice(hashPrevouts);
|
||||
bufferWriter.writeSlice(hashSequence);
|
||||
bufferWriter.writeSlice(input.hash);
|
||||
bufferWriter.writeUInt32(input.index);
|
||||
bufferWriter.writeVarSlice(prevOutScript);
|
||||
bufferWriter.writeUInt64(value);
|
||||
bufferWriter.writeUInt32(input.sequence);
|
||||
bufferWriter.writeSlice(hashOutputs);
|
||||
bufferWriter.writeUInt32(this.locktime);
|
||||
bufferWriter.writeUInt32(hashType);
|
||||
return bcrypto.hash256(tbuffer);
|
||||
}
|
||||
getHash(forWitness) {
|
||||
|
@ -404,64 +388,41 @@ class Transaction {
|
|||
}
|
||||
__toBuffer(buffer, initialOffset, _ALLOW_WITNESS = false) {
|
||||
if (!buffer) buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS));
|
||||
let offset = initialOffset || 0;
|
||||
function writeSlice(slice) {
|
||||
offset += slice.copy(buffer, offset);
|
||||
}
|
||||
function writeUInt8(i) {
|
||||
offset = buffer.writeUInt8(i, offset);
|
||||
}
|
||||
function writeUInt32(i) {
|
||||
offset = buffer.writeUInt32LE(i, offset);
|
||||
}
|
||||
function writeInt32(i) {
|
||||
offset = buffer.writeInt32LE(i, offset);
|
||||
}
|
||||
function writeUInt64(i) {
|
||||
offset = bufferutils.writeUInt64LE(buffer, i, offset);
|
||||
}
|
||||
function writeVarInt(i) {
|
||||
varuint.encode(i, buffer, offset);
|
||||
offset += varuint.encode.bytes;
|
||||
}
|
||||
function writeVarSlice(slice) {
|
||||
writeVarInt(slice.length);
|
||||
writeSlice(slice);
|
||||
}
|
||||
function writeVector(vector) {
|
||||
writeVarInt(vector.length);
|
||||
vector.forEach(writeVarSlice);
|
||||
}
|
||||
writeInt32(this.version);
|
||||
const bufferWriter = new buffer_writer_1.BufferWriter(
|
||||
buffer,
|
||||
initialOffset || 0,
|
||||
);
|
||||
bufferWriter.writeInt32(this.version);
|
||||
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
|
||||
if (hasWitnesses) {
|
||||
writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
|
||||
writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG);
|
||||
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
|
||||
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG);
|
||||
}
|
||||
writeVarInt(this.ins.length);
|
||||
bufferWriter.writeVarInt(this.ins.length);
|
||||
this.ins.forEach(txIn => {
|
||||
writeSlice(txIn.hash);
|
||||
writeUInt32(txIn.index);
|
||||
writeVarSlice(txIn.script);
|
||||
writeUInt32(txIn.sequence);
|
||||
bufferWriter.writeSlice(txIn.hash);
|
||||
bufferWriter.writeUInt32(txIn.index);
|
||||
bufferWriter.writeVarSlice(txIn.script);
|
||||
bufferWriter.writeUInt32(txIn.sequence);
|
||||
});
|
||||
writeVarInt(this.outs.length);
|
||||
bufferWriter.writeVarInt(this.outs.length);
|
||||
this.outs.forEach(txOut => {
|
||||
if (isOutput(txOut)) {
|
||||
writeUInt64(txOut.value);
|
||||
bufferWriter.writeUInt64(txOut.value);
|
||||
} else {
|
||||
writeSlice(txOut.valueBuffer);
|
||||
bufferWriter.writeSlice(txOut.valueBuffer);
|
||||
}
|
||||
writeVarSlice(txOut.script);
|
||||
bufferWriter.writeVarSlice(txOut.script);
|
||||
});
|
||||
if (hasWitnesses) {
|
||||
this.ins.forEach(input => {
|
||||
writeVector(input.witness);
|
||||
bufferWriter.writeVector(input.witness);
|
||||
});
|
||||
}
|
||||
writeUInt32(this.locktime);
|
||||
bufferWriter.writeUInt32(this.locktime);
|
||||
// avoid slicing unless necessary
|
||||
if (initialOffset !== undefined) return buffer.slice(initialOffset, offset);
|
||||
if (initialOffset !== undefined)
|
||||
return buffer.slice(initialOffset, bufferWriter.offset);
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue