TiddlyBit
A tiddly JavaScript library for constructing, parsing and serializing Bitcoin scripts.
When you need to construct and serialize a Bitcoin script for use in something like Money Button, you could just use bsv.js and add 336kb to your website payload... or you could use TiddlyBit which is 96.4% more tiddly!.
- TiddlyBit parses and serializes Bitcoin scripts to and from Buffers, Typed Arrays, and hex or ASM encoded strings.
- Weighing in at just 12kb minimised (4kb when gziped), TiddlyBit is a much leaner option for use on the Web.
- TiddlyBit uses Buffers in Node.js, but in the browser uses native Typed Arrays which is much more performant than putting binary data in Objects.
- Super simple, dev-friendly API.
Installation
When using NPM:
npm install tiddlybit
const Script OpCode =
Or using in the browser via CDN:
const Script OpCode = TiddlyBit
Usage
Constructing a new script:
const Script OpCode = const script = OpCodeOP_FALSE OpCodeOP_RETURN 'hello' 'world' script// => '0 OP_RETURN 68656c6c6f 776f726c64' script// => '006a0568656c6c6f05776f726c64'
Parsing a script:
const Script = const script = Script// => Script {// chunks: [// <OpCode 118 OP_DUP>,// <OpCode 169 OP_HASH160>,// <Buffer 6a fc 0d 6b b5 78 28 2a c0 f6 ad 5c 5a f2 29 4c 19 71 21 08>,// <OpCode 136 OP_EQUALVERIFY>,// <OpCode 172 OP_CHECKSIG>// ]// }
The OpCode
object has properties with cached instances for every valid code. If needed, new OpCode
instances can be created using the contructor function:
const OpCode = OpCodeOP_RETURN// => <OpCode 106 OP_RETURN> // If needed you can find an OpCode by it's byte numberOpCode// => <OpCode 106 OP_RETURN> // Alternatively you can initialise a new OpCode'OP_RETURN'// => <OpCode 106 OP_RETURN>
TiddlyBit also exposes a binary helper for cross-platform binary operations. See the bops documentation for API details:
const binary Script OpCode = const buf = binarybinary const script = OpCodeOP_RETURN buf// => Script {// chunks: [// <OpCode 106 OP_RETURN>,// <Buffer 78 56 34 12>// ]// }
API
Script class
new Script(array)
Creates a script instance from the given array of data chunks.
Script.fromASM(str)
Parses an ASM string and returns a script instance.
Script.fromBuffer(buf)
Parses a binary transaction and returns a script instance.
Script.fromHex(str)
Parses a hex encoded script and returns a script instance.
Script instance
Script#push(array)
Push a single or array of chunks onto the script.
Script#toASM()
Searializes the script into an ASM encoded string.
Script#toBuffer()
Searializes the script into a buffer.
Script#toHex()
Searializes the script into a hex encoded string.
OpCode class
new OpCode(string)
Creates an OpCode instance from the given string.
OpCode.byByte(str)
Find OpCode instance by byte number.
OpCode instance
OpCode#toBuffer()
Searializes the Op Code into a buffer.
License
MIT License.
© Copyright 2020 libitx.