Creates a new BtcTransactionTemplate instance.
Configuration options for the transaction template
Calculates the total change amount. (Total Inputs Amount - Total Non-change (non-malleable) Outputs Amount )
The total change amount in satoshis (as a string)
Calculates the current fee of the transaction.
The current fee in satoshis (as a string)
Calculates the estimated fee rate of the transaction.
The estimated fee rate in satoshis per vbyte
Estimates the virtual size of the transaction.
The estimated virtual size in vbytes
Checks if the current fee rate meets or exceeds the target fee rate.
True if the fee rate is satisfied, false otherwise
Gets the global xpubs of the transaction.
A read-only array of global xpubs
Sets global xpubs for the transaction, replacing any existing ones.
Array of global xpubs to set
Gets the inputs of the transaction.
A read-only array of inputs
Gets the malleable outputs of the transaction. Malleable outputs are all those that can have their amount changed, e.g. change outputs.
An array of malleable outputs
Checks if the transaction needs a change output.
True if there's enough leftover funds for a change output above the dust threshold.
Determines if a change output is needed.
True if a change output is needed, false otherwise
Gets the outputs of the transaction.
A read-only array of outputs
Calculates the target fees to pay based on the estimated size and target fee rate.
The target fees in satoshis (as a string)
Calculates the total input amount.
The total input amount in satoshis (as a string)
Calculates the total output amount.
The total output amount in satoshis (as a string)
Adds a global xpub to the transaction.
The global xpub to add
Adds an output to the transaction.
The output to add
Adjusts the change output of the transaction. This method calculates a new change amount based on the current inputs, non-change outputs, and the target fee. It then updates the change output or removes it if the new amount is below the dust threshold.
Key behaviors:
The new change amount in satoshis as a string, or null if no adjustment was made or the change output was removed.
Checks if the current fees are sufficient to meet the target fee rate.
True if the fees are paid, false otherwise
Removes an output from the transaction.
The index of the output to remove
Converts the transaction template to a base64-encoded PSBT (Partially Signed Bitcoin Transaction) string. This method creates a new PSBT, adds all valid inputs and outputs from the template, and then serializes the PSBT to a base64 string.
By default, it validates the entire transaction before creating the PSBT. This validation can be optionally skipped for partial or in-progress transactions.
The method performs the following steps:
validate() method to ensure
the transaction is valid.txid, which is internally stored in big-endian
(human-readable) format, is converted to little-endian format as required by
Bitcoin’s serialization rules.Optionalvalidated: boolean = trueWhether to validate the transaction before creating the PSBT. Set to false to skip validation for partial transactions.
A base64-encoded string representation of the PSBT.
isInputValid and isOutputValid checks are included.txids are stored in big-endian format internally for readability and compatibility with common UTXO sources,
but are converted to little-endian here, during PSBT serialization as per Bitcoin protocol requirements.Validates the entire transaction template.
This method performs a comprehensive check of the transaction, including:
True if the transaction is valid according to all checks, false otherwise.
StaticfromCreates a BtcTransactionTemplate from a raw PSBT hex string. This method parses the PSBT, extracts input and output information, and creates a new BtcTransactionTemplate instance.
The raw PSBT {PsbtV2 | string | Buffer}
Additional options for creating the template
A new BtcTransactionTemplate instance
Represents a Bitcoin transaction template. This class is used to construct and manipulate Bitcoin transactions.