Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_WALLET_COINCONTROL_H6#define BITCOIN_WALLET_COINCONTROL_H8#include <outputtype.h>9#include <policy/feerate.h>10#include <primitives/transaction.h>11#include <script/keyorigin.h>12#include <script/signingprovider.h>13#include <util/fees.h>15#include <algorithm>16#include <map>17#include <optional>18#include <set>20namespace wallet {21inline constexpr int DEFAULT_MIN_DEPTH = 0;22inline constexpr int DEFAULT_MAX_DEPTH = 9'999'999;24inline constexpr int DEFAULT_WALLET_TX_VERSION = CTransaction::CURRENT_VERSION;26//! Default for -avoidpartialspends27inline constexpr bool DEFAULT_AVOIDPARTIALSPENDS = false;29class PreselectedInput30{31private:32//! The previous output being spent by this input33std::optional<CTxOut> m_txout;34//! The input weight for spending this input35std::optional<int64_t> m_weight;36//! The sequence number for this input37std::optional<uint32_t> m_sequence;38//! The scriptSig for this input39std::optional<CScript> m_script_sig;40//! The scriptWitness for this input41std::optional<CScriptWitness> m_script_witness;42//! The position in the inputs vector for this input43std::optional<unsigned int> m_pos;45public:46/**47* Set the previous output for this input.48* Only necessary if the input is expected to be an external input.49*/50void SetTxOut(const CTxOut& txout);51/** Retrieve the previous output for this input. */52CTxOut GetTxOut() const;53/** Return whether the previous output is set for this input. */54bool HasTxOut() const;56/** Set the weight for this input. */57void SetInputWeight(int64_t weight);58/** Retrieve the input weight for this input. */59std::optional<int64_t> GetInputWeight() const;61/** Set the sequence for this input. */62void SetSequence(uint32_t sequence);63/** Retrieve the sequence for this input. */64std::optional<uint32_t> GetSequence() const;66/** Set the scriptSig for this input. */67void SetScriptSig(const CScript& script);68/** Set the scriptWitness for this input. */69void SetScriptWitness(const CScriptWitness& script_wit);70/** Return whether either the scriptSig or scriptWitness are set for this input. */71bool HasScripts() const;72/** Retrieve both the scriptSig and the scriptWitness. */73std::pair<std::optional<CScript>, std::optional<CScriptWitness>> GetScripts() const;75/** Store the position of this input. */76void SetPosition(unsigned int pos);77/** Retrieve the position of this input. */78std::optional<unsigned int> GetPosition() const;79};81/** Coin Control Features. */82class CCoinControl83{84public:85//! Custom change destination, if not set an address is generated86CTxDestination destChange = CNoDestination();87//! Override the default change type if set, ignored if destChange is set88std::optional<OutputType> m_change_type;89//! If false, only safe inputs will be used90bool m_include_unsafe_inputs = false;91//! If true, the selection process can add extra unselected inputs from the wallet92//! while requires all selected inputs be used93bool m_allow_other_inputs = true;94//! Override automatic min/max checks on fee, m_feerate must be set if true95bool fOverrideFeeRate = false;96//! Override the wallet's fee rate if set97std::optional<CFeeRate> m_feerate;98//! Override the default confirmation target if set99std::optional<unsigned int> m_confirm_target;100//! Override the wallet's m_signal_rbf if set101std::optional<bool> m_signal_bip125_rbf;102//! Avoid partial use of funds sent to a given address103bool m_avoid_partial_spends = DEFAULT_AVOIDPARTIALSPENDS;104//! Forbids inclusion of dirty (previously used) addresses105bool m_avoid_address_reuse = false;106//! Fee estimation mode.107FeeEstimateMode m_fee_mode = FeeEstimateMode::UNSET;108//! Minimum chain depth value for coin availability109int m_min_depth = DEFAULT_MIN_DEPTH;110//! Maximum chain depth value for coin availability111int m_max_depth = DEFAULT_MAX_DEPTH;112//! SigningProvider that has pubkeys and scripts to do spend size estimation for external inputs113FlatSigningProvider m_external_provider;114//! Version115uint32_t m_version = DEFAULT_WALLET_TX_VERSION;116//! Locktime117std::optional<uint32_t> m_locktime;118//! Caps weight of resulting tx119std::optional<int> m_max_tx_weight{std::nullopt};121CCoinControl();123/**124* Returns true if there are pre-selected inputs.125*/126bool HasSelected() const;127/**128* Returns true if the given output is pre-selected.129*/130bool IsSelected(const COutPoint& outpoint) const;131/**132* Returns true if the given output is selected as an external input.133*/134bool IsExternalSelected(const COutPoint& outpoint) const;135/**136* Returns the external output for the given outpoint if it exists.137*/138std::optional<CTxOut> GetExternalOutput(const COutPoint& outpoint) const;139/**140* Lock-in the given output for spending.141* The output will be included in the transaction even if it's not the most optimal choice.142*/143PreselectedInput& Select(const COutPoint& outpoint);144/**145* Unselects the given output.146*/147void UnSelect(const COutPoint& outpoint);148/**149* Unselects all outputs.150*/151void UnSelectAll();152/**153* List the selected inputs.154*/155std::vector<COutPoint> ListSelected() const;156/**157* Set an input's weight.158*/159void SetInputWeight(const COutPoint& outpoint, int64_t weight);160/**161* Returns the input weight.162*/163std::optional<int64_t> GetInputWeight(const COutPoint& outpoint) const;164/** Retrieve the sequence for an input */165std::optional<uint32_t> GetSequence(const COutPoint& outpoint) const;166/** Retrieves the scriptSig and scriptWitness for an input. */167std::pair<std::optional<CScript>, std::optional<CScriptWitness>> GetScripts(const COutPoint& outpoint) const;169bool HasSelectedOrder() const170{171return m_selection_pos > 0;172}174std::optional<unsigned int> GetSelectionPos(const COutPoint& outpoint) const175{176const auto it = m_selected.find(outpoint);177if (it == m_selected.end()) {178return std::nullopt;179}180return it->second.GetPosition();181}183private:184//! Selected inputs (inputs that will be used, regardless of whether they're optimal or not)185std::map<COutPoint, PreselectedInput> m_selected;186unsigned int m_selection_pos{0};187};188} // namespace wallet190#endif // BITCOIN_WALLET_COINCONTROL_H