tx-decoder
Composable helpers for decoding blockchain transactions
WARNING: This package is deprecated. Use tx-builder which includes both transaction builder and decoder.
This package contains composable helpers for decoding a blockchain transaction and implements an example of bitcoin transaction decoder.
It can be useful for blockchain projects forked from BitcoinCore that want to add custom data to a transaction.
Every composable helper expects a buffer and returns a pair (array of two elements) of a result and a buffer left.
Compared to bitcoinjs-lib
which implements decoding in an imperative style where every helper uses buffer
of the outer scope and mutates outer offset
. Thus its helpers cannot be reused for a different transaction structure.
Example:
Here is how bitcoin transaction decoder is implemented (the main export of the package):
const Buffer = Bufferconst readInt32 readUInt32 = const compose addProp = const readInputs readInput readOutput = // Create a buffer from a transaction hex:const txHex = "0100000001545f6161d2be3bdfe71..." // see `test/fixture.js` for a full tx exampleconst buffer = Buffer // decodeTx :: Buffer -> [Object<version,vin,vout,locktime>, BufferLeft]const decodeTx = {} buffer console// > [{version: 1, vin: [...], vout: [...]}, <Buffer >]
Here is how readInput
is defined in the same composable manner:
const readSlice readVarSlice readUInt32 = const compose addProp = // readInput :: Buffer -> [Res, Buffer]const readInput = {} buffer
Checkout tests/manual-decode.js
if you are not familiar with how to decode a transaction. It has a step-by-step imperative-style example.
Checkout tests for exact examples of how to use the package helpers.
Upcoming
- decode script
- create a transaction builder (see tx-builder package)
Release Notes:
- 0.2.0 Added readHash helper
- Reverse the hash (tx id) after its read from hex.
- 0.1.2 Initial version
- buffer utils, compose helpers, bitcoin tx decode example.