What is bip174?
The bip174 npm package is a library for handling Partially Signed Bitcoin Transactions (PSBT) as defined by BIP 174. It provides tools to create, modify, and finalize PSBTs, which are useful for multi-party transaction signing and other advanced Bitcoin transaction workflows.
What are bip174's main functionalities?
Create a PSBT
This feature allows you to create a new PSBT by adding inputs and outputs. The code sample demonstrates how to initialize a PSBT, add an input with a transaction hash and index, and add an output with a recipient address and value.
const { Psbt } = require('bip174');
const psbt = new Psbt();
psbt.addInput({
hash: 'transactionHash',
index: 0,
nonWitnessUtxo: Buffer.from('hexString', 'hex')
});
psbt.addOutput({
address: 'recipientAddress',
value: 1000
});
console.log(psbt.toBase64());
Sign a PSBT
This feature allows you to sign a PSBT. The code sample demonstrates how to load a PSBT from a base64 string, create a key pair, and sign the first input of the PSBT.
const { Psbt } = require('bip174');
const psbt = Psbt.fromBase64('base64PsbtString');
const keyPair = { publicKey: Buffer.from('publicKeyHex', 'hex'), privateKey: Buffer.from('privateKeyHex', 'hex') };
psbt.signInput(0, keyPair);
console.log(psbt.toBase64());
Finalize a PSBT
This feature allows you to finalize a PSBT and extract the final transaction. The code sample demonstrates how to load a PSBT from a base64 string, finalize all inputs, and extract the final transaction in hexadecimal format.
const { Psbt } = require('bip174');
const psbt = Psbt.fromBase64('base64PsbtString');
psbt.finalizeAllInputs();
const tx = psbt.extractTransaction();
console.log(tx.toHex());
Other packages similar to bip174
bitcoinjs-lib
The bitcoinjs-lib package is a comprehensive library for Bitcoin-related operations, including transaction creation, signing, and verification. It provides more general functionality compared to bip174, which is specifically focused on PSBT handling.
bcoin
The bcoin package is a full Bitcoin node implementation written in JavaScript. It includes extensive functionality for transaction creation, signing, and network communication. While it supports PSBT, it is a more heavyweight solution compared to the focused bip174 package.
bitcore-lib
The bitcore-lib package is a library for Bitcoin and blockchain-related operations. It provides tools for creating and signing transactions, as well as interacting with the Bitcoin network. It offers broader functionality than bip174, which is specialized for PSBT.