The bitcoin transaction format is the most efficient way of sending the information that is needed for a Bitcoin node to validate. The BIP for this format is BIP-239
Install the Bitcoin Extended Format library into your project:
$ npm install bitcoin-ef
or, with yarn
$ yarn add bitcoin-ef
or, install as a global cli tool
$ npm install -g bitcoin-ef
Here's the getting started with the Extended Format
import { StandardToExtended, ExtendedToStandard } from 'bitcoin-ef/bsv';
// hex encoded standard transaction
const tx = "..."; // hex or buffer
// the missing information from the inputs, in the correct order (!)
const inputs = [
{
lockingScript: "...", // hex or buffer
satoshis: 1234
},
{
lockingScript: "...", // hex or buffer
satoshis: 5423
}
];
// The StandardToExtended function accepts both a hex string or a buffer, and will return the same format as was given
const extendTransaction = StandardToExtended(tx, inputs);
// The ExtendedToStandard function accepts both a hex string or a buffer, and will return the same format as was given
const standardTransaction = ExtendedToStandard(extendTransaction);
import bsv from 'bsv';
import 'bitcoin-ef/bsv';
const tx = new bsv.Transaction()
.from(utxo)
.to(toAddress, 50000)
.fee(150)
.change(changeAddress)
.sign(privateKey)
const txBuffer = tx.toExtended();
const txHex = tx.toExtended('hex');
or if you have problems with the above:
import bsv from 'bsv';
import { BSVToExtended } from 'bitcoin-ef/bsv';
const tx = new bsv.Transaction()
.from(utxo)
.to(toAddress, 50000)
.fee(150)
.change(changeAddress)
.sign(privateKey)
const txBuffer = BSVToExtended(tx);
const txHex = BSVToExtended(tx, 'hex');
Or using common JS:
const bsv = require('bsv')
require('bitcoin-ef/bsv')
/*
const utxo = {
txId: '83c6307ba22838e469545db27a193934576e753ff4c0288537cece4a06ad3d87',
outputIndex: 0,
script: '76a914117af07edf84bcd40950f46a8254f7f78d85243088ac',
satoshis: 836486
}
const child = bsv.HDPrivateKey('xprv....')
.deriveChild('m/0/0')
const privateKey = child.privateKey
console.log(privateKey.toString())
const toAddress = privateKey.toAddress().toString()
console.log(toAddress)
const tx = new bsv.Transaction()
.from(utxo)
.change(toAddress)
.sign(privateKey)
*/
// This is the hex of the signed transaction that does NOT contain the previous output script and satoshis.
const tx = bsv.Transaction('0100000001873dad064acece378528c0f43f756e573439197ab25d5469e43828a27b30c683000000006b483045022100ff923348df29deedf3c08fcb1bb898f7304344931ba131a03f86beae43f67af7022049fff7207c1f1ee0074de743066e4d3c5aa636d6d019187879327c9c9f6b3cbf412102e7cf3fce2bc6bf4b9e8ef59fd2e4df7f79b5fd8d84cc6b05b8cb9066fdd81575ffffffff0126c30c00000000001976a914117af07edf84bcd40950f46a8254f7f78d85243088ac00000000')
// The following code is not necessary if you have created and signed the transaction using the bsv library.
tx.inputs[0].output = {
script: bsv.Script('76a914117af07edf84bcd40950f46a8254f7f78d85243088ac'),
satoshis: 836486
}
console.log(tx.toExtended('hex'))
After installing the cli tool, you can use it like this:
$ bitcoin-ef --help
To convert an extended transaction to a standard transaction:
$ bitcoin-ef --to-standard <hex encoded extended transaction>
To convert a standard transaction to an extended transaction, you need to pass the missing information from the inputs, in the correct order (!):
$ bitcoin-ef --to-extended <hex encoded standard transaction> <json encoded inputs>
To automatically convert a standard transaction to an extended transaction, using the WhatsOnChain API for lookups. This is the default behaviour of the cli tool:
$ bitcoin-ef --enrich-standard <hex encoded standard transaction>
or
$ bitcoin-ef <hex encoded standard transaction>
All kinds of contributions are welcome π! The most basic way to show your support is to star π the project, or to raise issues π¬. You can also support this project by becoming a sponsor on GitHub π
Thank you to these wonderful people (emoji key):
Simon Ordish π π» π‘οΈ |
Siggi π π» π‘οΈ |
This project follows the all-contributors specification.