validateAndSelectUtxos function

List<BtcUtxo> validateAndSelectUtxos(
  1. List<BtcUtxo> allUtxos,
  2. int amountInSatoshis,
  3. int feeEstimate,
  4. int selectedTotal,
  5. List<BtcUtxo> selectedUtxos,
)

Ports validateAndSelectUTXOs.ts: if the largest-first selection already covers amount+fee, keep it; otherwise widen to ALL UTXOs if that's enough, else throw. Deliberately never partially widens beyond "everything" — matches the JS SDK's own fallback exactly.

Implementation

List<BtcUtxo> validateAndSelectUtxos(
  List<BtcUtxo> allUtxos,
  int amountInSatoshis,
  int feeEstimate,
  int selectedTotal,
  List<BtcUtxo> selectedUtxos,
) {
  final requiredAmount = amountInSatoshis + feeEstimate;
  if (selectedTotal >= requiredAmount) return selectedUtxos;
  if (selectedUtxos.length < allUtxos.length) {
    final allTotal = utxoTotal(allUtxos);
    if (allTotal < requiredAmount) {
      throw StateError(
          'insufficient funds: have $allTotal, need $requiredAmount satoshis');
    }
    return allUtxos;
  }
  throw StateError(
      'insufficient funds: have $selectedTotal, need $requiredAmount satoshis');
}