• Selects UTXOs for a Bitcoin transaction.

    Sorts UTXOs by their descending net value (each UTXO's value minus the fees needed to spend it).

    It initially attempts to find a solution using the avoidChange algorithm, which aims to select UTXOs such that no change is required. If this is not possible, it then applies the addUntilReach algorithm, which adds UTXOs until the total value exceeds the target value plus fees. Change is added only if it's above the dustThreshold).

    UTXOs that do not provide enough value to cover their respective fee contributions are automatically excluded.

    NOTE: When the descriptor in an input is addr(address), it is assumed that any addr(SH_TYPE_ADDRESS) is in fact a Segwit SH_WPKH (Script Hash-Witness Public Key Hash). For inputs using arbitrary scripts (not standard addresses), use a descriptor in the format sh(MINISCRIPT).

    Parameters

    • __namedParameters: {
          utxos: OutputWithValue[];
          targets: OutputWithValue[];
          remainder: {};
          feeRate: number;
          dustRelayFeeRate?: number;
      }
      • utxos: OutputWithValue[]

        Array of UTXOs for the transaction. Each UTXO includes an OutputInstance and its value.

      • targets: OutputWithValue[]

        Array of transaction targets. If specified, remainder is used as the change address.

      • remainder: {}

        OutputInstance used as the change address when targets are specified, or as the recipient address for maximum fund transactions.

        • feeRate: number
        • Optional dustRelayFeeRate?: number

          Fee rate used to calculate the dust threshold for transaction outputs. Defaults to standard dust relay fee rate if not specified.

          Default Value

          3
          

      Returns undefined | {
          utxos: OutputWithValue[];
          targets: OutputWithValue[];
          fee: number;
          vsize: number;
      }

      Object with selected UTXOs, targets, fee, and estimated vsize, or undefined if no solution is found.

      Example

      const { utxos, targets, fee, vsize } = coinselect({
      utxos: [
      { output: new Output({ descriptor: 'addr(...)' }), value: 2000 },
      { output: new Output({ descriptor: 'addr(...)' }), value: 4000 }
      ],
      targets: [
      { output: new Output({ descriptor: 'addr(...)' }), value: 3000 }
      ],
      remainder: new Output({ descriptor: 'addr(...)' }),
      feeRate: 1.34
      });

      See

      https://bitcoinerlab.com/modules/descriptors for descriptor info.