Caravan Documentation
    Preparing search index...

    The PsbtV2 class is intended to represent an easily modifiable and serializable psbt of version 2 conforming to BIP0174. Getters exist for all BIP-defined keytypes. Very few setters and modifier methods exist. As they are added, they should enforce implied and documented rules and limitations.

    allowTxnVersion1: A Note A psbtv2 must have its transaction version GTE 2 to be bip370 compliant. If this class is instantiated with allowTxnVersion1 set to true, then a psbtv2 which has had its txn version forceably set to 1 (for example with PsbtV2.dangerouslySetGlobalTxVersion1) can be instantiated. This has, possibly dangerous implications concerning how the locktime might be interpreted.

    Defining BIPs: https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki

    Hierarchy

    • PsbtV2Maps
      • PsbtV2
    Index

    Constructors

    Properties

    globalMap: Map<string, Buffer> = ...
    inputMaps: Map<string, Buffer>[] = []
    outputMaps: Map<string, Buffer>[] = []

    Accessors

    • get isRBFSignaled(): boolean

      Checks if the transaction signals Replace-by-Fee (RBF).

      This method determines whether the transaction is eligible for RBF by examining the sequence numbers of all inputs. As per BIP125, a transaction is considered to have opted in to RBF if it contains at least one input with a sequence number less than (0xffffffff - 1).

      Return value:

      • true: If any input has a sequence number < 0xfffffffe, indicating RBF.
      • false: If all inputs have sequence numbers >= 0xfffffffe, indicating no RBF.

      This method is useful for wallets, block explorers, or any service that needs to determine if a transaction can potentially be replaced before confirmation.

      References:

      Returns boolean

    • get isReadyForConstructor(): boolean

      Returns true if the PsbtV2 is ready for an operator taking the Constructor role.

      This check assumes that the Creator used this class's constructor method to initialize the PsbtV2 without passing a psbt (constructor defaults were set).

      Returns boolean

    • get isReadyForTransactionExtractor(): boolean

      Returns true if the PsbtV2 is ready for an operator taking the Transaction Extractor role.

      If all the inputs have been finalized, then the psbt is ready for the Transaction Extractor. According to BIP 174, it's the responsibility of the Input Finalizer to add scriptSigs or scriptWitnesses and then remove other details besides the UTXO. This getter checks that the Input Finalizer has finished its job.

      Returns boolean

    • get isReadyForUpdater(): boolean

      Returns true if the PsbtV2 is ready for an operator taking the Updater role.

      Before signatures are added, but after an input is added, a PsbtV2 is likely to be ready for Constructor, ready for Updater, and ready for Signer simultaneously.

      According to BIP370, the Updater can modify the sequence number, but it is unclear if the Updater retains permissions provided in psbtv0 (BIP174). It is likely not the case that the Updater has the same permissions as previously because it seems to now be the realm of the Constructor to add inputs and outputs.

      Returns boolean

    Methods

    • Parameters

      • __namedParameters: {
            bip32Derivation?: {
                masterFingerprint: Buffer;
                path: string;
                pubkey: Buffer;
            }[];
            nonWitnessUtxo?: Buffer;
            outputIndex: number;
            previousTxId: string
            | Buffer;
            redeemScript?: Buffer;
            sequence?: number;
            sighashType?: SighashType;
            witnessScript?: Buffer;
            witnessUtxo?: { amount: number; script: Buffer };
        }

      Returns void

    • Parameters

      • __namedParameters: {
            amount: number;
            bip32Derivation?: {
                masterFingerprint: Buffer;
                path: string;
                pubkey: Buffer;
            }[];
            redeemScript?: Buffer;
            script: Buffer;
            witnessScript?: Buffer;
        }

      Returns void

    • Adds a signature for an input. Validates that the input is mapped and does not already have a signature for the pubkey. Also validates for sighash. Other validation is incomplete. Also validates for required args in case typescript is not being used to call the method.

      The Signer, when it creates a signature, must add the partial sig keypair to the psbt for the input which it is signing. In the case that a particular signer does not, this method can be used to add a signature to the psbt. This method assumes the Signer did the validation outlined in BIP0174 before creating a signature. https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#signer

      Parameters

      • inputIndex: number
      • pubkey: Buffer
      • sig: Buffer

      Returns void

    • Copies the maps in this PsbtV2Maps object to another PsbtV2Maps object.

      NOTE: This copy method is made available to achieve parity with the PSBT api required by ledger-bitcoin for creating merklized PSBTs. HOWEVER, it is not recommended to use this when avoidable as copying maps bypasses the validation defined in the constructor, so it could create a psbtv2 in an invalid psbt state. PsbtV2.serialize is preferable whenever possible.

      Parameters

      • to: PsbtV2Maps

      Returns void

    • This method is provided for compatibility issues and probably shouldn't be used since a PsbtV2 with PSBT_GLOBAL_TX_VERSION = 1 is BIP0370 non-compliant. No guarantees can be made here that a serialized PsbtV2 which used this method will be compatible with outside consumers.

      One may wish to instance this class from a partially signed PSBTv0 with a txn version 1 by using the static PsbtV2.FromV0. This method provides a way to override validation logic for the txn version and roles lifecycle defined for PsbtV2.

      Returns void

    • Removes all sigs for an input unless a pubkey is specified. Validates that the input exists. When providing a pubkey, this validates that a sig for the pubkey exists.

      Parameters

      • inputIndex: number
      • Optionalpubkey: Buffer

      Returns void

    • Return the current state of the psbt as a string in the specified format.

      Parameters

      • format: "hex" | "base64" = "base64"

      Returns string

    • Sets the sequence number for a specific input in the transaction.

      This private helper method is crucial for implementing RBF and other sequence-based transaction features. It writes the provided sequence number as a 32-bit little-endian unsigned integer and stores it in the appropriate input's map using the PSBT_IN_SEQUENCE key.

      The sequence number has multiple uses in Bitcoin transactions:

      1. Signaling RBF (values < 0xfffffffe)
      2. Enabling nLockTime (values < 0xffffffff)
      3. Relative timelock with BIP68 (if bit 31 is not set)

      According to BIP125 (Opt-in Full Replace-by-Fee Signaling):

      • For a transaction to be considered opt-in RBF, it must have at least one input with a sequence number < 0xfffffffe.
      • The recommended sequence for RBF is 0xffffffff-2 (0xfffffffd).

      Sequence number meanings:

      • = 0xffffffff: Then the transaction is final no matter the nLockTime.
      • < 0xfffffffe: Transaction signals for RBF.
      • < 0xefffffff : Then the transaction signals BIP68 relative locktime.

      For using nLocktime along with Opt-in RBF, the sequence value should be between 0xf0000000 and 0xfffffffd.

      Care should be taken when setting sequence numbers to ensure the desired transaction properties are correctly signaled. Improper use can lead to unexpected transaction behavior or rejection by the network.

      References:

      Parameters

      • inputIndex: number
      • sequence: number

      Returns void

    • Sets values on the proprietary keytype for a global, input, or output map. BIP 174 allows for proprietary values to be set on all maps with the keytype 0xFC. This method sets byte data to key values defined by the args.

      Args:

      • mapSelector selects which map to set the proprietary value. If this value is not "global", then a tuple must be provided with "inputs" or "outputs" as the first element and the index number on the second element representing which input or output map to set the value to. An example looks like ["inputs", 0]. If the map name doesn't match, the values will be set to the global map. If the index is missing on "inputs" or "outputs", then it will throw.
      • identifier should be the bytes identifier for the set of proprietary keytypes.
      • subkeyType accepts bytes proprietary keytype.
      • subkeyData accepts bytes proprietary keydata.
      • valueData accepts bytes which will be written as the proprietary value.

      From the provided args, a key with the following format will be generated: 0xFC<compact uint identifier length><bytes identifier><bytes subtype><bytes subkeydata>

      Parameters

      • mapSelector: MapSelectorType
      • identifier: Buffer
      • subkeyType: Buffer
      • subkeyData: Buffer
      • valueData: Buffer

      Returns void

    • Outputs a serialized PSBTv0 from a best-attempt conversion of the fields in this PSBTv2. Accepts optional desired format as a string (default base64).

      Parameters

      • Optionalformat: "hex" | "base64"

      Returns string

    • Attempts to return a PsbtV2 by converting from a PsbtV0 string or Buffer.

      This method first starts with a fresh PsbtV2 having just been created. It then takes the PsbtV2 through its operator saga and through the Input Finalizer role. In this sense, validation for each operator role will be performed as the Psbt saga is replayed.

      Parameters

      • psbt: string | Buffer
      • allowTxnVersion1: boolean = false

      Returns PsbtV2