The Output class is the central component for managing descriptors. It facilitates the creation of outputs to receive funds and enables the signing and finalization of PSBTs (Partially Signed Bitcoin Transactions) for spending UTXOs (Unspent Transaction Outputs).

Hierarchy

  • Output

Constructors

  • Parameters

    • options: {
          descriptor: string;
          index?: number;
          checksumRequired?: boolean;
          allowMiniscriptInP2SH?: boolean;
          network?: Network;
          preimages?: Preimage[];
          signersPubKeys?: Buffer[];
      }
      • descriptor: string

        The descriptor string in ASCII format. It may include a "*" to denote an arbitrary index (aka ranged descriptors).

      • Optional index?: number

        The descriptor's index in the case of a range descriptor (must be an integer >=0).

      • Optional checksumRequired?: boolean

        An optional flag indicating whether the descriptor is required to include a checksum.

        Default Value

        false
        
      • Optional allowMiniscriptInP2SH?: boolean

        A flag indicating whether this instance can parse and generate script satisfactions for sh(miniscript) top-level expressions of miniscripts. This is not recommended.

        Default Value

        false
        
      • Optional network?: Network

        One of bitcoinjs-lib networks (or another one following the same interface).

        Default Value

        networks.bitcoin
        
      • Optional preimages?: Preimage[]

        An array of preimages if the miniscript-based descriptor uses them.

        This info is necessary to finalize Psbts. Leave it undefined if your miniscript-based descriptor does not use preimages or you don't know or don't wanto use them.

        You can also leave it undefined if only need to generate the scriptPubKey or address for a descriptor.

        Default Value

        []

      • Optional signersPubKeys?: Buffer[]

        An array of the public keys used for signing the transaction when spending the previous output associated with this descriptor.

        This parameter is only used if the descriptor object is being used to finalize a transaction. It is necessary to specify the spending path when working with miniscript-based expressions that have multiple spending paths.

        Set this parameter to an array containing the public keys involved in the desired spending path. Leave it undefined if you only need to generate the scriptPubKey or address for a descriptor, or if all the public keys involved in the descriptor will sign the transaction. In the latter case, the satisfier will automatically choose the most optimal spending path (if more than one is available).

        For more details on using this parameter, refer to this Stack Exchange answer.

    Returns Output

    Throws

    • when descriptor is invalid

Properties

#payment: Payment
#preimages: Preimage[] = []
#signersPubKeys: Buffer[]
#miniscript?: string
#witnessScript?: Buffer
#redeemScript?: Buffer
#isSegwit?: boolean
#expandedExpression?: string
#expandedMiniscript?: string
#expansionMap?: ExpansionMap
#network: Network

Methods

  • Gets the TimeConstraints (nSequence and nLockTime) of the miniscript descriptor as passed in the constructor, just using the expression, the signersPubKeys and preimages.

    We just need to know which will be the signatures that will be used (signersPubKeys) but final signatures are not necessary for obtaning nLockTime and nSequence.

    Remember: nSequence and nLockTime are part of the hash that is signed. Thus, they must not change after computing the signatures. When running getScriptSatisfaction, using the final signatures, satisfyMiniscript verifies that the time constraints did not change.

    Returns undefined | TimeConstraints

  • Creates and returns an instance of bitcoinjs-lib Payment's interface with the scriptPubKey of this Output.

    Returns Payment

  • Returns the Bitcoin Address of this Output.

    Returns string

  • Returns this Output's scriptPubKey.

    Returns Buffer

  • Returns the compiled Script Satisfaction if this Output was created using a miniscript-based descriptor.

    The Satisfaction is the unlocking script that fulfills (satisfies) this Output and it is derived using the Safisfier algorithm described here.

    Important: As mentioned above, note that this function only applies to miniscript descriptors.

    Parameters

    • signatures: PartialSig[] | "DANGEROUSLY_USE_FAKE_SIGNATURES"

      An array with all the signatures needed to build the Satisfaction of this miniscript-based Output.

      signatures must be passed using this format (pairs of pubKey/signature): interface PartialSig { pubkey: Buffer; signature: Buffer; }

      • Alternatively, if you do not have the signatures, you can use the option 'DANGEROUSLY_USE_FAKE_SIGNATURES'. This will generate script satisfactions using 72-byte zero-padded signatures. While this can be useful in modules like coinselector that require estimating transaction size before signing, it is critical to understand the risks:
      • Using this option generales invalid unlocking scripts.
      • It should NEVER be used with real transactions.
      • Its primary use is for testing and size estimation purposes only.

      ⚠️ Warning: Misuse of 'DANGEROUSLY_USE_FAKE_SIGNATURES' can lead to security vulnerabilities, including but not limited to invalid transaction generation. Ensure you fully understand the implications before use.

    Returns Buffer

  • Gets the nSequence required to fulfill this Output.

    Returns undefined | number

  • Gets the nLockTime required to fulfill this Output.

    Returns undefined | number

  • Gets the witnessScript required to fulfill this Output. Only applies to Segwit outputs.

    Returns undefined | Buffer

  • Gets the redeemScript required to fullfill this Output. Only applies to SH outputs: sh(wpkh), sh(wsh), sh(lockingScript).

    Returns undefined | Buffer

  • Whether this Output is Segwit.

    Returns undefined | boolean

  • Sets this output as an input of the provided psbt and updates the psbt locktime if required by the descriptor.

    psbt and vout are mandatory. Include txHex as well. The pair vout and txHex define the transaction and output number this instance pertains to.

    Though not advised, for Segwit inputs you can pass txId and value in lieu of txHex. If doing so, ensure value accuracy to avoid potential fee attacks - See this issue.

    Note: Hardware wallets need the full txHex for Segwit.

    When unsure, always use txHex, and skip txId and value for safety.

    Parameters

    • __namedParameters: {
          psbt: Psbt;
          txHex?: string;
          txId?: string;
          value?: number;
          vout: number;
      }
      • psbt: Psbt
      • Optional txHex?: string
      • Optional txId?: string
      • Optional value?: number
      • vout: number

    Returns ((__namedParameters) => void)

    A finalizer function to be used after signing the psbt. This function ensures that this input is properly finalized. The finalizer has this signature:

    ( { psbt, validate = true } : { psbt: Psbt; validate: boolean | undefined } ) => void

      • (__namedParameters): void
      • Parameters

        • __namedParameters: {
              psbt: Psbt;
              validate?: boolean;
          }
          • psbt: Psbt
          • Optional validate?: boolean

            Runs further test on the validity of the signatures. It speeds down the finalization process but makes sure the psbt will be valid.

            Default

            true
            

        Returns void

  • Adds this output as an output of the provided psbt with the given value.

    Parameters

    • __namedParameters: {
          psbt: Psbt;
          value: number;
      }
      • psbt: Psbt
      • value: number

    Returns void

  • Parameters

    • __namedParameters: {
          psbt: Psbt;
          index: number;
      }
      • psbt: Psbt
      • index: number

    Returns void

  • Finalizes a PSBT input by adding the necessary unlocking script that satisfies this Output's spending conditions.

    🔴 IMPORTANT 🔴 It is STRONGLY RECOMMENDED to use the finalizer function returned by updatePsbtAsInput instead of calling this method directly. This approach eliminates the need to manage the Output instance and the input's index, simplifying the process.

    The finalizePsbtInput method completes a PSBT input by adding the unlocking script (scriptWitness or scriptSig) that satisfies this Output's spending conditions. Bear in mind that both scriptSig and scriptWitness incorporate signatures. As such, you should complete all necessary signing operations before calling this method.

    For each unspent output from a previous transaction that you're referencing in a psbt as an input to be spent, apply this method as follows: output.finalizePsbtInput({ index, psbt }).

    It's essential to specify the exact position (or index) of the input in the psbt that references this unspent Output. This index should align with the value returned by the updatePsbtAsInput method. Note: The index corresponds to the position of the input in the psbt. To get this index, right after calling updatePsbtAsInput(), use: index = psbt.data.inputs.length - 1.

    Parameters

    • __namedParameters: {
          index: number;
          psbt: Psbt;
          validate?: boolean;
      }
      • index: number
      • psbt: Psbt
      • Optional validate?: boolean

        Runs further test on the validity of the signatures. It speeds down the finalization process but makes sure the psbt will be valid.

        Default

        true
        

    Returns void

  • Decomposes the descriptor used to form this Output into its elemental parts. See ExpansionMap for a detailed explanation.

    Returns {
        expandedExpression?: string;
        miniscript?: string;
        expandedMiniscript?: string;
        expansionMap?: ExpansionMap;
    }

    • Optional expandedExpression?: string
    • Optional miniscript?: string
    • Optional expandedMiniscript?: string
    • Optional expansionMap?: ExpansionMap