Coin selection

From claude-dev-suite/claude-dev-suite · View interactive version

Coin selection is an AI agent skill from the GitHub repository claude-dev-suite/claude-dev-suite. Coin selection algorithms: branch-and-bound (BnB), knapsack, single random draw (SRD), Murch's waste metric, change avoidance. Determines which UTXOs to spend for a given target. USE WHEN: implementing wallet send flow, choosing which UTXOs to consolidate, optimizing fees, designing privacy-preservi

How to use this skill

Skill file

Coin Selection

Coin selection chooses which subset of available UTXOs to spend

for a target amount + fee. Trade-offs: fee minimization, privacy,

change-output dust avoidance, UTXO-set hygiene.

Inputs to the algorithm

Algorithm 1: Branch and Bound (BnB)

Bitcoin Core's primary algorithm (Murch 2017).

Goal: find a subset that sums exactly to T + fee + change_buffer,

avoiding change output entirely.

branch_and_bound(utxos, target):
  utxos.sort_by(value, descending)
  best = None
  recurse(included=[], remaining=utxos, sum=0):
    if sum > target + threshold: return  # exceed
    if abs(sum - target) <= waste_metric:
      best = included; return
    if remaining empty: return
    # branch: include / exclude next utxo
    recurse(included + [head], tail, sum + head.value)
    recurse(included, tail, sum)

Backtracking with pruning. If BnB finds an exact-match solution

(within rounding), no change output needed → **saves the cost of a

change output** (~31 vB for P2WPKH change).

If BnB fails (no exact match), wallet falls back to knapsack or SRD.

Algorithm 2: Knapsack

Heuristic: iterate UTXOs, include each if it fits, else continue.

Simple but may select more inputs than necessary. Pre-Murch default.

Algorithm 3: Single Random Draw (SRD)

Randomly draw UTXOs until sum ≥ target + fee. Privacy-friendly: no

heuristic-readable patterns from chain analysis.

srd(utxos, target):
  shuffle(utxos)
  selected = []; sum = 0
  for u in utxos:
    selected.push(u); sum += u.value
    if sum >= target + fee_for_count(len(selected)): break
  return selected

Waste metric (Murch)

Defines "best" selection as one that minimizes:

waste = excess + change_cost + (input_count - 1) * fee_per_input

change output) + future cost to spend that change later.

Wallets use this to compare candidate solutions.

Change avoidance

If sum(selected) is within ~$change_threshold$ of target (typically

~1000 sats above), spend everything as fee instead of creating dusty

change. Algorithmic threshold = change_cost.

UTXO consolidation

When fee rates are low, batch many UTXOs into one output. Saves

future tx fees but reveals on-chain history correlation.

one identifiable cluster.

consolidate within a single account that's already correlated.

Privacy-aware selection

Heuristics chain analysts use:

likely owned by same entity. Mitigation: include only one UTXO per

tx when feasible.

the recipient. Mitigation: avoid round amounts.

one wallet style. Mitigation: vary script types across wallets.

Privacy-focused wallets (Wasabi, Sparrow with PayJoin) use SRD to

avoid heuristic-friendly patterns.

Implementations

| Wallet | Algorithm |

|--------|-----------|

| Bitcoin Core | BnB primary, SRD fallback, knapsack legacy |

| BDK | BnB + SRD configurable |

| Sparrow | BnB + SRD + privacy-aware |

| Wasabi | Privacy-first with random draws + CoinJoin pre-step |

Common bugs

high fees.

fee → tx stuck.

spendable in the future → unspendable, just dead weight.

See also

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.