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 356 : 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 627 : void WalletInit::AddWalletOptions(ArgsManager& argsman) const
59 : {
60 627 : 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 627 : 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 627 : argsman.AddArg("-createwalletbackups=<n>", strprintf("Number of automatic wallet backups (default: %u)", nWalletBackups), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
63 627 : argsman.AddArg("-disablewallet", "Do not load the wallet and disable wallet RPC calls", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
64 : #if HAVE_SYSTEM
65 627 : 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 627 : 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 627 : argsman.AddArg("-rescan=<mode>", "Rescan the block chain for missing wallet transactions on startup"
69 627 : " (1 = start from wallet creation time, 2 = start from genesis block)", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
70 : #ifdef ENABLE_EXTERNAL_SIGNER
71 627 : argsman.AddArg("-signer=<cmd>", "External signing tool, see doc/external-signer.md", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
72 : #endif
73 627 : argsman.AddArg("-spendzeroconfchange", strprintf("Spend unconfirmed change when sending transactions (default: %u)", DEFAULT_SPEND_ZEROCONF_CHANGE), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
74 627 : argsman.AddArg("-dustprotectionthreshold=<n>",
75 627 : 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 627 : ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
80 627 : 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 627 : argsman.AddArg("-walletbackupsdir=<dir>", "Specify full path to directory for automatic wallet backups (must exist)", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
82 627 : argsman.AddArg("-walletbroadcast", strprintf("Make the wallet broadcast transactions (default: %u)", DEFAULT_WALLETBROADCAST), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
83 627 : 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 627 : 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 627 : 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 1254 : CURRENCY_UNIT, FormatMoney(DEFAULT_DISCARD_FEE)), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_FEE);
91 627 : 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 1254 : CURRENCY_UNIT, FormatMoney(DEFAULT_FALLBACK_FEE)), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_FEE);
93 627 : 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 627 : 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 1254 : CURRENCY_UNIT, FormatMoney(DEFAULT_TRANSACTION_MAXFEE)), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
97 627 : argsman.AddArg("-mintxfee=<amt>", strprintf("Fee rates (in %s/kB) smaller than this are considered zero fee for transaction creation (default: %s)",
98 1254 : CURRENCY_UNIT, FormatMoney(DEFAULT_TRANSACTION_MINFEE)), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_FEE);
99 1254 : argsman.AddArg("-paytxfee=<amt>", strprintf("Fee rate (in %s/kB) to add to transactions you send (default: %s)",
100 1254 : CURRENCY_UNIT, FormatMoney(CFeeRate{DEFAULT_PAY_TX_FEE}.GetFeePerK())), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_FEE);
101 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : argsman.AddArg("-coinjoinautostart", strprintf("Start CoinJoin automatically (0-1, default: %u)", DEFAULT_COINJOIN_AUTOSTART), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET_COINJOIN);
112 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : 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 627 : }
135 :
136 627 : bool WalletInit::ParameterInteraction() const
137 : {
138 : #ifdef USE_BDB
139 627 : 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 627 : if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
144 0 : for (const std::string& wallet : gArgs.GetArgs("-wallet")) {
145 0 : LogPrintf("%s: parameter interaction: -disablewallet -> ignoring -wallet=%s\n", __func__, wallet);
146 : }
147 :
148 0 : return true;
149 627 : } else if (gArgs.IsArgSet("-masternodeblsprivkey")) {
150 0 : return InitError(_("You can not start a masternode with wallet enabled."));
151 : }
152 :
153 627 : 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 627 : int rescan_mode = gArgs.GetIntArg("-rescan", 0);
158 627 : 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 627 : 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 627 : 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 627 : 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 627 : if (CMnemonic::Generate(gArgs.GetIntArg("-mnemonicbits", CHDChain::DEFAULT_MNEMONIC_BITS)) == SecureString()) {
182 0 : return InitError(strprintf(_("Invalid '%s'. Allowed values: 128, 160, 192, 224, 256."), "-mnemonicbits"));
183 : }
184 :
185 627 : return true;
186 627 : }
187 :
188 0 : void WalletInit::Construct(NodeContext& node) const
189 : {
190 0 : ArgsManager& args = *Assert(node.args);
191 0 : if (args.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
192 0 : LogPrintf("Wallet disabled!\n");
193 0 : return;
194 : }
195 0 : node.coinjoin_loader = node.init->makeCoinJoinLoader();
196 0 : auto wallet_loader = node.init->makeWalletLoader(*node.chain, *node.coinjoin_loader);
197 0 : node.wallet_loader = wallet_loader.get();
198 0 : node.chain_clients.emplace_back(std::move(wallet_loader));
199 0 : }
200 :
201 :
202 0 : void WalletInit::AutoLockMasternodeCollaterals(interfaces::WalletLoader& wallet_loader) const
203 : {
204 : // we can't do this before DIP3 is fully initialized
205 0 : for (const auto& wallet : wallet_loader.getWallets()) {
206 0 : wallet->autoLockMasternodeCollaterals();
207 : }
208 0 : }
209 :
210 26 : void WalletInit::InitCoinJoinSettings(interfaces::CoinJoin::Loader& coinjoin_loader, interfaces::WalletLoader& wallet_loader) const
211 : {
212 26 : const auto& wallets{wallet_loader.getWallets()};
213 26 : CCoinJoinClientOptions::SetEnabled(!wallets.empty() ? gArgs.GetBoolArg("-enablecoinjoin", true) : false);
214 26 : if (!CCoinJoinClientOptions::IsEnabled()) {
215 26 : return;
216 : }
217 0 : bool fAutoStart = gArgs.GetBoolArg("-coinjoinautostart", DEFAULT_COINJOIN_AUTOSTART);
218 0 : for (auto& wallet : wallets) {
219 0 : auto manager = Assert(coinjoin_loader.GetClient(wallet->getWalletName()));
220 0 : if (wallet->isLocked(/*fForMixing=*/false)) {
221 0 : manager->stopMixing();
222 0 : LogPrintf("CoinJoin: Mixing stopped for locked wallet \"%s\"\n", wallet->getWalletName());
223 0 : } else if (fAutoStart) {
224 0 : manager->startMixing();
225 0 : LogPrintf("CoinJoin: Automatic mixing started for wallet \"%s\"\n", wallet->getWalletName());
226 0 : }
227 0 : }
228 0 : 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 26 : }
235 :
236 0 : void WalletInit::InitAutoBackup() const
237 : {
238 0 : CWallet::InitAutoBackup();
239 0 : }
240 : } // namespace wallet
241 :
242 146 : const WalletInitInterface& g_wallet_init_interface = wallet::WalletInit();
|