bitcoinjs/bitcoinjs-lib — pull request #170 by dcousens (2014-05-04 07:44:57+00:00)
Title: BufferExt and Transaction serialization to use Buffers

The following pull request introduces a `BufferExt` class for utility functions that are necessary to migrate away from `convert.js` and towards using `Buffer` consistently (as part of #106).
These functions are then used in `Transaction.serialize` and `Transaction.deserialize`, offering a significant performance boost when redundant buffer copies are eventually removed (`Script.buffer` is not presently a `Buffer` object).

It introduces the functions:
* readUInt64LE,
* readVarInt,
* writeUInt64LE,
* writeVarInt, and
* varIntSize

Their functionality is straight forward enough, and replaces their `convert.js` counter parts `numToBytes`/`bytesToNum` and `varIntToNum`/`numToVarInt`.

To make this transition simpler, `txin.sequence` was changed to always be a `Number` instead of a byte array; requiring only 4 bytes, it readily fits within the full precision range of a typical Javascript `Number`.

In preparation for implementing `Transaction.serialize`, `fakeTxHash` was changed to represent actual hex data as `new Buffer(hex, 'hex')` requires a valid hex string,

Finally, the previous comments about the endianness of the hash's was incorrect, as a `txHash` is big-endian when represented in hex.
This can be seen in the `uint256` implementation (which is typically little-endian) in [bitcoin/bitcoin](https://github.com/bitcoin/bitcoin/blob/ab086e0bd39b6d010c040c9bf4d781400fc7b9f8/src/uint256.h#L297-L303).