Coin selection
Coin selection is the process of choosing which UTXOs (or “coins”) to use as inputs when making an on-chain bitcoin payment. For payments on the lightning network, coin selection is only relevant for the transaction that opens the lightning channel.
Coin selection can be automatic, and handled by the wallet application. Or it can be manual, letting the user control which coins are used.
Constraints #
There are a few constraints that you should have in mind when considering the coin-selection strategy for your product.
First, the transaction must have sufficient funding. For example, to send X, the transaction inputs must at least equate to X.
Second, as blockspace is limited, miners prioritize transactions by fee per byte to maximize revenue. The minimum relay fee must accompany transactions for confirmation (therefore, inputs must equal X + fee).
Third, transaction outputs must have a value of 500 sats or more. Anything less than this value is considered a “dust” output, and these transactions are often not relayed and confirmed by most nodes and miners in the network.
Finally, wallet owners need to label their incoming and outgoing (change) coins. Labels provide context to a wallet’s transaction history and coin provenance, making it easier to successfully employ coin control for privacy optimization.
Optimizations #
Whether an automated or manual process, coin selection is important when understanding how to optimize our bitcoin payments for either cost, speed, or privacy. Below is a breakdown of the three most common effects of coin selection.
Cost #
Miners select transactions based on their fee, measured by the fee rate of satoshis per byte. Therefore, it is cost-effective for users to minimize transaction size by minimizing the number of inputs.
Speed #
Higher fees will increase the likelihood of your transaction being prioritized by miners and included in the next block, thereby increasing the speed of your transaction being confirmed.
Privacy #
Because each UTXO can be traced backward on a public ledger, we can unearth the digital footprints of bitcoin payments. Privacy is often lost due to the exposure of unknown coins, addresses, balances, and economic activity to unrecognized trading partners. Privacy can be optimized by selecting transaction inputs from an anonymity set (CoinJoins or mixers) or a recognized label or cluster or reducing the size of change outputs.
Automatic coin selection #
This strategy has arguably become the most popular form of coin selection used in bitcoin wallets and applications. The application selects the coins on behalf of users to fulfill a payment request. As mentioned above, “fractions (of bitcoins) are retrieved by an algorithm to fulfill the payment amount”. This algorithm varies from wallet to wallet, and is dependent upon the desired outcome of optimisation.
Automatic selection strategies can provide a near frictionless user experience and are suitable for most introductory level bitcoin wallets.
How it works #
A user chooses to send a payment to one of their contacts. They enter the amount of bitcoin they wish to send, select their transaction fee rate, and approve the outgoing payment request. During this process, their wallet automatically selects which coins to use to fund the transaction’s inputs, ensuring it can fulfill the payment request.
Examples #
Below are some popular algorithms currently implemented by bitcoin wallets that optimize for speed and lower fees:
First In First Out (FIFO) / Last In, First Out (LIFO) #
The default strategy spends the oldest/youngest coins first.
Pruned FIFO #
Similar to FIFO, but the smallest coins are filtered out in the post-selection step.
High Priority First #
Coins selected by priority (calculated by value x age). Up until February 2016, a portion of each block (50kB) was reserved for high-priority transactions by default. This algorithm, therefore optimised for transaction speed.
Minimize Fees (Optimize Size) #
Spending the lowest number of coins to reduce the byte size of the transaction, resulting in a lower fee.
Minimize Future Fees (Merge Coins) #
Spending the maximum number of inputs to merge coins as a single change output for future use. This strategy can optimise for speed as the transaction size (and therefore cost) is increased. However, merging coins can also lead to a loss of privacy as coins, their addresses, balances, and historical transaction data are intertwined.
Below are algorithms that optimize for privacy
Target Sized Change #
Wallet aims to minimize the value difference of target input and change output.
Branch & Bound (BnB)/Exact Change #
Wallet finds an input set equal in value to the target, avoiding change outputs. If the wallet cannot find an exact match, it refers back to a “knapsack” solver which selects inputs that minimise the change output to within 0.01 BTC.
Blackjack #
Accumulates inputs until the target value (+fees) is matched, does not accumulate inputs that go over the target value (within a threshold).
Accumulative #
Accumulates inputs until the target value (+fees) is reached, skipping detrimental inputs.
Freezing Coins #
Freezes certain coins or clusters from their wallet’s UTXO pool to either prioritize or avoid using when funding outgoing payment requests. This technique aids automatic selection strategies to become more private but also relies on the practice of successfully labeling coins.
Pros
- Low friction
- Optimizes for cost, speed, or privacy
- Minimizes change outputs and decreases balance exposure
Cons
- Doesn’t optimize for all three features (speed, cost, privacy)
- Can select UTXOs associated with targeted dusting attacks, resulting in privacy loss
- UTXOs selected from unrecognized clusters can expose private data of wallet addresses, balances, and contact payment information.
- Reducing the inputs doesn’t usually optimise for minimal change outputs, exposing larger amounts of sensitive wallet data.
- Freezing UTXOs prevents coin selection algorithms from using them, so the resulting transaction may not be properly optimized.
Best practice #
When to use #
- New users or introductory level wallets
- Wallets specifically optimizing for either cost, speed, or privacy
When not to use #
- When privacy is a top priority to users
- When wanting to provide a more custom experience for bitcoin transactions
Products that use this scheme #
Products
Manual coin selection (a.k.a coin control) #
Manual coin selection gives users full control over exactly which coins or clusters they use to fund a transaction input for an outgoing payment request.
Manual coin control is often an “opt-in” strategy, agreed to by users as they enter “Private Mode” or select “Manual Coin Selection” when creating an outgoing payment request. However, it is also possible for bitcoin wallets to use manual coin control as their default coin selection strategy.
This strategy allows users the ability to choose what they wish to optimize for in bitcoin transactions (cost, speed, or privacy). However, it’s worth noting that manual coin control, whilst beneficial to many, could also lead to expensive, slow, or revealing transactions if misused. Manual coin selection is an advanced feature and should be used (and designed) with caution.
How it works #
A user chooses to send a payment to one of their contacts. They enter the amount of bitcoin they wish to send, select their transaction fee rate, “opt-in” for manual coin selection (or by default), select which UTXOs will fund the transaction input, and finally approve the outgoing payment request.
Pros
- Gives user full control over coin selection. They can choose to optimize for privacy, cost, or speed
- Good labeling allows users to prioritize recognized clusters and minimizes the exposure of data
- Can reduce dust attacks (avoiding choosing minute or unknown UTXOs for payment inputs)
Cons
- Inexperienced users may end up making mistakes that result in higher fees, slower transactions, or a loss of privacy
- Can lead to dust attacks if not careful selecting sizes of UTXO inputs
- Bad labeling of contacts and clusters can lead to sensitive information being exposed
Best practise #
When to use #
- Wallets-oriented towards advanced, privacy-focused bitcoiners
- When wanting to give users full control of selecting transaction inputs
- When paired with good labeling practices
When not to use #
- Introductory level bitcoin wallets
Products that use this scheme #
Products
Bitcoin Core, BTCPay Server, Chaincase, Samourai, and Wasabi
Next, let’s look at how liquidity works on the lightning network.