Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2021 The Bitcoin Core developers
3 : // Distributed under the MIT software license, see the accompanying
4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 :
6 : #include <chainparams.h>
7 : #include <init.h>
8 : #include <interfaces/chain.h>
9 : #include <interfaces/coinjoin.h>
10 : #include <interfaces/init.h>
11 : #include <interfaces/wallet.h>
12 : #include <net.h>
13 : #include <node/context.h>
14 : #include <node/interface_ui.h>
15 : #include <univalue.h>
16 : #include <util/check.h>
17 : #include <util/error.h>
18 : #include <util/system.h>
19 : #include <util/moneystr.h>
20 : #include <util/translation.h>
21 : #include <validation.h>
22 : #ifdef USE_BDB
23 : #include <wallet/bdb.h>
24 : #endif
25 : #include <wallet/bip39.h>
26 : #include <wallet/coincontrol.h>
27 : #include <wallet/hdchain.h>
28 : #include <wallet/wallet.h>
29 : #include <walletinitinterface.h>
30 :
31 : #include <coinjoin/client.h>
32 : #include <coinjoin/options.h>
33 :
34 : using node::NodeContext;
35 :
36 : namespace wallet {
37 : class WalletInit : public WalletInitInterface
38 : {
39 : public:
40 : //! Was the wallet component compiled in.
41 12439 : bool HasWalletSupport() const override {return true;}
42 :
43 : //! Return the wallets help message.
44 : void AddWalletOptions(ArgsManager& argsman) const override;
45 :
46 : //! Wallets parameter interaction
47 : bool ParameterInteraction() const override;
48 :
49 : //! Add wallets that should be opened to list of chain clients.
50 : void Construct(NodeContext& node) const override;
51 :
52 : // Dash Specific Wallet Init
53 : void AutoLockMasternodeCollaterals(interfaces::WalletLoader& wallet_loader) const override;
54 : void InitCoinJoinSettings(interfaces::CoinJoin::Loader& coinjoin_loader, interfaces::WalletLoader& wallet_loader) const override;
55 : void InitAutoBackup() const override;
56 : };
57 :
58 3789 : void WalletInit::AddWalletOptions(ArgsManager& argsman) const
59 : {
60 3789 : argsman.AddArg("-avoidpartialspends", strprintf("Group outputs by address, selecting many (possibly all) or none, instead of selecting on a per-output basis. Privacy is improved as addresses are mostly swept with fewer transactions and outputs are aggregated in clean change addresses. It may result in higher fees due to less optimal coin selection caused by this added limitation and possibly a larger-than-necessary number of inputs being used. Always enabled for wallets with \"avoid_reuse\" enabled, otherwise default: %u.", DEFAULT_AVOIDPARTIALSPENDS), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
61 3789 : argsman.AddArg("-consolidatefeerate=<amt>", strprintf("The maximum feerate (in %s/kvB) at which transaction building may use more inputs than strictly necessary so that the wallet's UTXO pool can be reduced (default: %s).", CURRENCY_UNIT, FormatMoney(DEFAULT_CONSOLIDATE_FEERATE)), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
62 3789 : argsman.AddArg("-createwalletbackups=<n>", strprintf("Number of automatic wallet backups (default: %u)", nWalletBackups), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
63 3789 : argsman.AddArg("-disablewallet", "Do not load the wallet and disable wallet RPC calls", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
64 : #if HAVE_SYSTEM
65 3789 : argsman.AddArg("-instantsendnotify=<cmd>", "Execute command when a wallet InstantSend transaction is successfully locked. %s in cmd is replaced by TxID and %w is replaced by wallet name. %w is not currently implemented on Windows. On systems where %w is supported, it should NOT be quoted because this would break shell escaping used to invoke the command.", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
66 : #endif
67 3789 : argsman.AddArg("-keypool=<n>", strprintf("Set key pool size to <n> (default: %u). Warning: Smaller sizes may increase the risk of losing funds when restoring from an old backup, if none of the addresses in the original keypool have been used.", DEFAULT_KEYPOOL_SIZE), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
68 3789 : argsman.AddArg("-rescan=<mode>", "Rescan the block chain for missing wallet transactions on startup"
69 3789 : " (1 = start from wallet creation time, 2 = start from genesis block)", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
70 : #ifdef ENABLE_EXTERNAL_SIGNER
71 3789 : argsman.AddArg("-signer=<cmd>", "External signing tool, see doc/external-signer.md", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
72 : #endif
73 3789 : argsman.AddArg("-spendzeroconfchange", strprintf("Spend unconfirmed change when sending transactions (default: %u)", DEFAULT_SPEND_ZEROCONF_CHANGE), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
74 3789 : argsman.AddArg("-dustprotectionthreshold=<n>",
75 3789 : strprintf("Automatically lock UTXOs from incoming external transactions at or below <n> duffs "
76 : "to protect against dust attacks. Locked UTXOs persist across restarts and are not "
77 : "automatically unlocked when threshold changes; use lockunspent RPC to unlock manually "
78 : "(0 = disabled, default: %d, max: %d)", DEFAULT_DUST_PROTECTION_THRESHOLD, MAX_DUST_PROTECTION_THRESHOLD),
79 3789 : ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
80 3789 : argsman.AddArg("-wallet=<path>", "Specify wallet path to load at startup. Can be used multiple times to load multiple wallets. Path is to a directory containing wallet data and log files. If the path is not absolute, it is interpreted relative to <walletdir>. This only loads existing wallets and does not create new ones. For backwards compatibility this also accepts names of existing top-level data files in <walletdir>.", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::WALLET);
81 3789 : argsman.AddArg("-walletbackupsdir=<dir>", "Specify full path to directory for automatic wallet backups (must exist)", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
82 3789 : argsman.AddArg("-walletbroadcast", strprintf("Make the wallet broadcast transactions (default: %u)", DEFAULT_WALLETBROADCAST), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
83 3789 : argsman.AddArg("-walletdir=<dir>", "Specify directory to hold wallets (default: <datadir>/wallets if it exists, otherwise <datadir>)", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::WALLET);
84 : #if HAVE_SYSTEM
85 3789 : argsman.AddArg("-walletnotify=<cmd>", "Execute command when a wallet transaction changes. %s in cmd is replaced by TxID, %w is replaced by wallet name, %b is replaced by the hash of the block including the transaction (set to 'unconfirmed' if the transaction is not included) and %h is replaced by the block height (-1 if not included). %w is not currently implemented on windows. On systems where %w is supported, it should NOT be quoted because this would break shell escaping used to invoke the command.", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
86 : #endif
87 :
88 3789 : argsman.AddArg("-discardfee=<amt>", strprintf("The fee rate (in %s/kB) that indicates your tolerance for discarding change by adding it to the fee (default: %s). "
89 : "Note: An output is discarded if it is dust at this rate, but we will always discard up to the dust relay fee and a discard fee above that is limited by the fee estimate for the longest target",
90 7578 : CURRENCY_UNIT, FormatMoney(DEFAULT_DISCARD_FEE)), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_FEE);
91 3789 : argsman.AddArg("-fallbackfee=<amt>", strprintf("A fee rate (in %s/kB) that will be used when fee estimation has insufficient data. 0 to entirely disable the fallbackfee feature. (default: %s)",
92 7578 : CURRENCY_UNIT, FormatMoney(DEFAULT_FALLBACK_FEE)), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_FEE);
93 3789 : argsman.AddArg("-maxapsfee=<n>", strprintf("Spend up to this amount in additional (absolute) fees (in %s) if it allows the use of partial spend avoidance (default: %s)", CURRENCY_UNIT, FormatMoney(DEFAULT_MAX_AVOIDPARTIALSPEND_FEE)), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
94 :
95 3789 : argsman.AddArg("-maxtxfee=<amt>", strprintf("Maximum total fees (in %s) to use in a single wallet transaction; setting this too low may abort large transactions (default: %s)",
96 7578 : CURRENCY_UNIT, FormatMoney(DEFAULT_TRANSACTION_MAXFEE)), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
97 3789 : argsman.AddArg("-mintxfee=<amt>", strprintf("Fee rates (in %s/kB) smaller than this are considered zero fee for transaction creation (default: %s)",
98 7578 : CURRENCY_UNIT, FormatMoney(DEFAULT_TRANSACTION_MINFEE)), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_FEE);
99 7578 : argsman.AddArg("-paytxfee=<amt>", strprintf("Fee rate (in %s/kB) to add to transactions you send (default: %s)",
100 7578 : CURRENCY_UNIT, FormatMoney(CFeeRate{DEFAULT_PAY_TX_FEE}.GetFeePerK())), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_FEE);
101 3789 : argsman.AddArg("-txconfirmtarget=<n>", strprintf("If paytxfee is not set, include enough fee so transactions begin confirmation on average within n blocks (default: %u)", DEFAULT_TX_CONFIRM_TARGET), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_FEE);
102 :
103 3789 : argsman.AddArg("-hdseed=<hex>", "User defined seed for HD wallet (should be in hex). Only has effect during wallet creation/first start (default: randomly generated)", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::WALLET_HD);
104 3789 : argsman.AddArg("-mnemonic=<text>", "User defined mnemonic for HD wallet (bip39). Only has effect during wallet creation/first start (default: randomly generated)", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::WALLET_HD);
105 3789 : argsman.AddArg("-mnemonicbits=<n>", strprintf("User defined mnemonic security for HD wallet in bits (BIP39). Only has effect during wallet creation/first start (allowed values: 128, 160, 192, 224, 256; default: %u)", CHDChain::DEFAULT_MNEMONIC_BITS), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_HD);
106 3789 : argsman.AddArg("-mnemonicpassphrase=<text>", "User defined mnemonic passphrase for HD wallet (BIP39). Only has effect during wallet creation/first start (default: empty string)", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::WALLET_HD);
107 3789 : argsman.AddArg("-usehd", strprintf("Use hierarchical deterministic key generation (HD) after BIP39/BIP44. Only has effect during wallet creation/first start (default: %u)", DEFAULT_USE_HD_WALLET), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_HD);
108 :
109 3789 : argsman.AddArg("-enablecoinjoin", strprintf("Enable use of CoinJoin for funds stored in this wallet (0-1, default: %u)", 0), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_COINJOIN);
110 3789 : argsman.AddArg("-coinjoinamount=<n>", strprintf("Target CoinJoin balance (%u-%u, default: %u)", MIN_COINJOIN_AMOUNT, MAX_COINJOIN_AMOUNT, DEFAULT_COINJOIN_AMOUNT), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_COINJOIN);
111 3789 : argsman.AddArg("-coinjoinautostart", strprintf("Start CoinJoin automatically (0-1, default: %u)", DEFAULT_COINJOIN_AUTOSTART), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_COINJOIN);
112 3789 : argsman.AddArg("-coinjoindenomsgoal=<n>", strprintf("Try to create at least N inputs of each denominated amount (%u-%u, default: %u)", MIN_COINJOIN_DENOMS_GOAL, MAX_COINJOIN_DENOMS_GOAL, DEFAULT_COINJOIN_DENOMS_GOAL), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_COINJOIN);
113 3789 : argsman.AddArg("-coinjoindenomshardcap=<n>", strprintf("Create up to N inputs of each denominated amount (%u-%u, default: %u)", MIN_COINJOIN_DENOMS_HARDCAP, MAX_COINJOIN_DENOMS_HARDCAP, DEFAULT_COINJOIN_DENOMS_HARDCAP), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_COINJOIN);
114 3789 : argsman.AddArg("-coinjoinmultisession", strprintf("Enable multiple CoinJoin mixing sessions per block, experimental (0-1, default: %u)", DEFAULT_COINJOIN_MULTISESSION), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_COINJOIN);
115 3789 : argsman.AddArg("-coinjoinrounds=<n>", strprintf("Use N separate masternodes for each denominated input to mix funds (%u-%u, default: %u)", MIN_COINJOIN_ROUNDS, MAX_COINJOIN_ROUNDS, DEFAULT_COINJOIN_ROUNDS), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_COINJOIN);
116 3789 : argsman.AddArg("-coinjoinsessions=<n>", strprintf("Use N separate masternodes in parallel to mix funds (%u-%u, default: %u)", MIN_COINJOIN_SESSIONS, MAX_COINJOIN_SESSIONS, DEFAULT_COINJOIN_SESSIONS), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_COINJOIN);
117 :
118 : #ifdef USE_BDB
119 3789 : argsman.AddArg("-dblogsize=<n>", strprintf("Flush wallet database activity from memory to disk log every <n> megabytes (default: %u)", DatabaseOptions().max_log_mb), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
120 3789 : argsman.AddArg("-flushwallet", strprintf("Run a thread to flush wallet periodically (default: %u)", DEFAULT_FLUSHWALLET), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
121 3789 : argsman.AddArg("-privdb", strprintf("Sets the DB_PRIVATE flag in the wallet db environment (default: %u)", !DatabaseOptions().use_shared_memory), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
122 : #else
123 : argsman.AddHiddenArgs({"-dblogsize", "-flushwallet", "-privdb"});
124 : #endif
125 :
126 : #ifdef USE_SQLITE
127 3789 : argsman.AddArg("-unsafesqlitesync", "Set SQLite synchronous=OFF to disable waiting for the database to sync to disk. This is unsafe and can cause data loss and corruption. This option is only used by tests to improve their performance (default: false)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
128 : #else
129 : argsman.AddHiddenArgs({"-unsafesqlitesync"});
130 : #endif
131 :
132 3789 : argsman.AddArg("-walletrejectlongchains", strprintf("Wallet will not create transactions that violate mempool chain limits (default: %u)", DEFAULT_WALLET_REJECT_LONG_CHAINS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
133 3789 : argsman.AddArg("-walletcrosschain", strprintf("Allow reusing wallet files across chains (default: %u)", DEFAULT_WALLETCROSSCHAIN), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
134 3789 : }
135 :
136 3709 : bool WalletInit::ParameterInteraction() const
137 : {
138 : #ifdef USE_BDB
139 3709 : if (!BerkeleyDatabaseSanityCheck()) {
140 0 : return InitError(Untranslated("A version conflict was detected between the run-time BerkeleyDB library and the one used during compilation."));
141 : }
142 : #endif
143 3709 : if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
144 1602 : for (const std::string& wallet : gArgs.GetArgs("-wallet")) {
145 4 : LogPrintf("%s: parameter interaction: -disablewallet -> ignoring -wallet=%s\n", __func__, wallet);
146 : }
147 :
148 1598 : return true;
149 2111 : } else if (gArgs.IsArgSet("-masternodeblsprivkey")) {
150 0 : return InitError(_("You can not start a masternode with wallet enabled."));
151 : }
152 :
153 2111 : if (gArgs.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY) && gArgs.SoftSetBoolArg("-walletbroadcast", false)) {
154 0 : LogPrintf("%s: parameter interaction: -blocksonly=1 -> setting -walletbroadcast=0\n", __func__);
155 0 : }
156 :
157 2111 : int rescan_mode = gArgs.GetIntArg("-rescan", 0);
158 2111 : if (rescan_mode < 0 || rescan_mode > 2) {
159 0 : LogPrintf("%s: Warning: incorrect -rescan mode, falling back to default value.\n", __func__);
160 0 : InitWarning(_("Incorrect -rescan mode, falling back to default value"));
161 0 : gArgs.ForceRemoveArg("rescan");
162 0 : }
163 :
164 2111 : if (gArgs.IsArgSet("-walletbackupsdir")) {
165 0 : if (!fs::is_directory(gArgs.GetArg("-walletbackupsdir", ""))) {
166 0 : InitWarning(strprintf(_("Warning: incorrect parameter %s, path must exist! Using default path."), "-walletbackupsdir"));
167 0 : gArgs.ForceRemoveArg("walletbackupsdir");
168 0 : }
169 0 : }
170 :
171 2111 : if (gArgs.IsArgSet("-hdseed") && IsHex(gArgs.GetArg("-hdseed", "not hex")) && (gArgs.IsArgSet("-mnemonic") || gArgs.IsArgSet("-mnemonicpassphrase"))) {
172 0 : InitWarning(strprintf(_("Warning: can't use %s and %s together, will prefer %s"), "-hdseed", "-mnemonic/-mnemonicpassphrase", "-hdseed"));
173 0 : gArgs.ForceRemoveArg("mnemonic");
174 0 : gArgs.ForceRemoveArg("mnemonicpassphrase");
175 0 : }
176 :
177 2111 : if (gArgs.GetIntArg("-coinjoindenomshardcap", DEFAULT_COINJOIN_DENOMS_HARDCAP) < gArgs.GetIntArg("-coinjoindenomsgoal", DEFAULT_COINJOIN_DENOMS_GOAL)) {
178 0 : return InitError(strprintf(_("%s can't be lower than %s"), "-coinjoindenomshardcap", "-coinjoindenomsgoal"));
179 : }
180 :
181 2111 : if (CMnemonic::Generate(gArgs.GetIntArg("-mnemonicbits", CHDChain::DEFAULT_MNEMONIC_BITS)) == SecureString()) {
182 4 : return InitError(strprintf(_("Invalid '%s'. Allowed values: 128, 160, 192, 224, 256."), "-mnemonicbits"));
183 : }
184 :
185 2107 : return true;
186 3709 : }
187 :
188 3023 : void WalletInit::Construct(NodeContext& node) const
189 : {
190 3023 : ArgsManager& args = *Assert(node.args);
191 3023 : if (args.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
192 1587 : LogPrintf("Wallet disabled!\n");
193 1587 : return;
194 : }
195 1436 : node.coinjoin_loader = node.init->makeCoinJoinLoader();
196 1436 : auto wallet_loader = node.init->makeWalletLoader(*node.chain, *node.coinjoin_loader);
197 1436 : node.wallet_loader = wallet_loader.get();
198 1436 : node.chain_clients.emplace_back(std::move(wallet_loader));
199 3023 : }
200 :
201 :
202 1328 : void WalletInit::AutoLockMasternodeCollaterals(interfaces::WalletLoader& wallet_loader) const
203 : {
204 : // we can't do this before DIP3 is fully initialized
205 1872 : for (const auto& wallet : wallet_loader.getWallets()) {
206 544 : wallet->autoLockMasternodeCollaterals();
207 : }
208 1328 : }
209 :
210 6563 : void WalletInit::InitCoinJoinSettings(interfaces::CoinJoin::Loader& coinjoin_loader, interfaces::WalletLoader& wallet_loader) const
211 : {
212 6563 : const auto& wallets{wallet_loader.getWallets()};
213 6563 : CCoinJoinClientOptions::SetEnabled(!wallets.empty() ? gArgs.GetBoolArg("-enablecoinjoin", true) : false);
214 6563 : if (!CCoinJoinClientOptions::IsEnabled()) {
215 2534 : return;
216 : }
217 4029 : bool fAutoStart = gArgs.GetBoolArg("-coinjoinautostart", DEFAULT_COINJOIN_AUTOSTART);
218 16654 : for (auto& wallet : wallets) {
219 12625 : auto manager = Assert(coinjoin_loader.GetClient(wallet->getWalletName()));
220 12625 : if (wallet->isLocked(/*fForMixing=*/false)) {
221 692 : manager->stopMixing();
222 692 : LogPrintf("CoinJoin: Mixing stopped for locked wallet \"%s\"\n", wallet->getWalletName());
223 12625 : } else if (fAutoStart) {
224 0 : manager->startMixing();
225 0 : LogPrintf("CoinJoin: Automatic mixing started for wallet \"%s\"\n", wallet->getWalletName());
226 0 : }
227 12625 : }
228 4029 : LogPrintf("CoinJoin: autostart=%d, multisession=%d," /* Continued */
229 : "sessions=%d, rounds=%d, amount=%d, denoms_goal=%d, denoms_hardcap=%d\n",
230 : fAutoStart, CCoinJoinClientOptions::IsMultiSessionEnabled(),
231 : CCoinJoinClientOptions::GetSessions(), CCoinJoinClientOptions::GetRounds(),
232 : CCoinJoinClientOptions::GetAmount(), CCoinJoinClientOptions::GetDenomsGoal(),
233 : CCoinJoinClientOptions::GetDenomsHardCap());
234 6563 : }
235 :
236 3011 : void WalletInit::InitAutoBackup() const
237 : {
238 3011 : CWallet::InitAutoBackup();
239 3011 : }
240 : } // namespace wallet
241 :
242 3308 : const WalletInitInterface& g_wallet_init_interface = wallet::WalletInit();
|