Caravan Documentation
    Preparing search index...

    Class BtcTransactionTemplate

    Represents a Bitcoin transaction template. This class is used to construct and manipulate Bitcoin transactions.

    Index

    Constructors

    Accessors

    Methods

    • 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:

      1. If there are multiple outputs and the change becomes dust, it removes the change output.
      2. If there's only one output (which must be the change output) and it becomes dust, it keeps the output to maintain a valid transaction structure.
      3. It calculates the difference between the new and current change amount.
      4. It ensures the transaction remains balanced after adjustment.

      Returns null | string

      The new change amount in satoshis as a string, or null if no adjustment was made or the change output was removed.

      If there's not enough input to satisfy non-change outputs and fees, or if the transaction doesn't balance after adjustment.

    • 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:

      1. If validation is enabled (default), it calls the validate() method to ensure the transaction is valid.
      2. Creates a new PsbtV2 instance.
      3. Adds all inputs from the template to the PSBT, including UTXO information.
        • During this step, each input's txid, which is internally stored in big-endian (human-readable) format, is converted to little-endian format as required by Bitcoin’s serialization rules.
      4. Adds all outputs from the template to the PSBT.
      5. Serializes the PSBT to a base64-encoded string.

      Parameters

      • Optionalvalidated: boolean = true

        Whether to validate the transaction before creating the PSBT. Set to false to skip validation for partial transactions.

      Returns string

      A base64-encoded string representation of the PSBT.

      If validation is enabled and the transaction fails validation checks.

      If an invalid address is encountered when creating an output script.

      If there's an issue with input or output data that prevents PSBT creation.

      If serialization of the PSBT fails.

      • Only inputs and outputs that pass the isInputValid and isOutputValid checks are included.
      • Input amounts are not included in the PSBT. If needed, they should be added separately.
      • Output amounts are converted from string to integer (satoshis) when added to the PSBT.
      • The resulting PSBT is not signed and may require further processing (e.g., signing) before it can be broadcast.
      • Input 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:

      1. Validation of all inputs:
        • Checks if each input has the required fields for PSBT creation.
        • Validates each input's general structure and data.
      2. Validation of all outputs:
        • Ensures each output has a valid address and amount.
      3. Verification that the current fee meets or exceeds the target fee
      4. Check that the fee rate is not absurdly high
      5. Check that the absolute fee is not absurdly high

      Returns boolean

      True if the transaction is valid according to all checks, false otherwise.

      If any validation check encounters an unexpected error.

      const txTemplate = new BtcTransactionTemplate(options);
      if (txTemplate.validate()) {
      console.log("Transaction is valid");
      } else {
      console.log("Transaction is invalid");
      }