Branch data Line data Source code
1 : : // Copyright (c) 2017-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <wallet/walletutil.h>
6 : :
7 : : #include <chainparams.h>
8 : : #include <common/args.h>
9 : : #include <common/settings.h>
10 : : #include <key_io.h>
11 : : #include <util/check.h>
12 : : #include <univalue.h>
13 : : #include <util/log.h>
14 : :
15 : : namespace wallet {
16 : 2603 : fs::path GetWalletDir()
17 : : {
18 : 2603 : fs::path path;
19 : :
20 [ + - + - : 2603 : if (gArgs.IsArgSet("-walletdir")) {
+ + ]
21 [ + - + - ]: 57 : path = gArgs.GetPathArg("-walletdir");
22 [ + - - + ]: 19 : if (!fs::is_directory(path)) {
23 : : // If the path specified doesn't exist, we return the deliberately
24 : : // invalid empty string.
25 [ # # ]: 0 : path = "";
26 : : }
27 : : } else {
28 [ + - ]: 5168 : path = gArgs.GetDataDirNet();
29 : : // If a wallets directory exists, use that, otherwise default to GetDataDir
30 [ + - + - : 10336 : if (fs::is_directory(path / "wallets")) {
+ - + + ]
31 [ + - ]: 1945 : path /= "wallets";
32 : : }
33 : : }
34 : :
35 : 2603 : return path;
36 : 0 : }
37 : :
38 : 1684 : util::Result<fs::path> GetWalletPath(const std::string& name)
39 : : {
40 : 1684 : const fs::path name_path = fs::PathFromString(name);
41 : :
42 : : // 'name' must be a normalized path, i.e. no . or .. except at the root
43 [ + - + + ]: 5052 : if (name_path != name_path.lexically_normal()) {
44 [ + - + - ]: 48 : return util::Error{Untranslated("Wallet name given as a path must be normalized")};
45 : : }
46 : :
47 : : // 'name' cannot begin with ./ or ../
48 [ + + + - : 7106 : if (!name_path.empty() && (*name_path.begin() == fs::PathFromString(".") || *name_path.begin() == fs::PathFromString(".."))) {
+ - + + +
+ + - + -
+ + + + +
+ + + + +
+ + - - -
- - - ]
49 [ + - + - ]: 18 : return util::Error{Untranslated("Wallet name given as a relative path cannot begin with ./ or ../, for wallets not in the walletdir, please use an absolute path.")};
50 : : }
51 : :
52 : : // Disallow path at root
53 [ + + + - : 1697 : if (name_path.has_root_path() && name_path.root_path() == name_path) {
+ + + + ]
54 [ + - + - ]: 6 : return util::Error{Untranslated("Wallet name cannot be the root path")};
55 : : }
56 : :
57 : : // Do some checking on wallet path. It should be either a:
58 : : //
59 : : // 1. Path where a directory can be created.
60 : : // 2. Path to an existing directory.
61 : : // 3. Path to a symlink to a directory.
62 : : // 4. For backwards compatibility, the name of a data file in -walletdir.
63 [ + - + - ]: 1660 : const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), name_path);
64 [ + - ]: 1660 : fs::file_type path_type = fs::symlink_status(wallet_path).type();
65 [ + + + + : 1678 : if (!(path_type == fs::file_type::not_found || path_type == fs::file_type::directory ||
+ + + + ]
66 [ + - + + ]: 16 : (path_type == fs::file_type::symlink && fs::is_directory(wallet_path)) ||
67 [ + - - + ]: 4 : (path_type == fs::file_type::regular && name_path.filename() == name_path))) {
68 [ + - + - ]: 8 : return util::Error{Untranslated(strprintf(
69 : : "Invalid -wallet path '%s'. -wallet path should point to a directory where wallet.dat and "
70 : : "database/log.?????????? files can be stored, a location where such a directory could be created, "
71 : : "or (for backwards compatibility) the name of an existing data file in -walletdir (%s)",
72 [ + - + - ]: 20 : name, fs::quoted(fs::PathToString(GetWalletDir()))))};
73 : : }
74 [ + - ]: 3312 : return wallet_path;
75 : 3344 : }
76 : :
77 : 193 : bool AddWalletSetting(interfaces::Chain& chain, const std::string& wallet_name)
78 : : {
79 : 386 : const auto update_function = [&wallet_name](common::SettingsValue& setting_value) {
80 [ + + ]: 193 : if (!setting_value.isArray()) setting_value.setArray();
81 [ + + ]: 361 : for (const auto& value : setting_value.getValues()) {
82 [ + - + + ]: 169 : if (value.isStr() && value.get_str() == wallet_name) return interfaces::SettingsAction::SKIP_WRITE;
83 : : }
84 [ + - ]: 192 : setting_value.push_back(wallet_name);
85 : 192 : return interfaces::SettingsAction::WRITE;
86 : 193 : };
87 [ + - + - ]: 193 : return chain.updateRwSetting("wallet", update_function);
88 : : }
89 : :
90 : 23 : bool RemoveWalletSetting(interfaces::Chain& chain, const std::string& wallet_name)
91 : : {
92 : 46 : const auto update_function = [&wallet_name](common::SettingsValue& setting_value) {
93 [ + + ]: 23 : if (!setting_value.isArray()) {
94 [ + + + - ]: 3 : if (wallet_name.empty() && setting_value.isNull()) {
95 : : // Empty setting suppresses backwards-compatible default wallet autoload.
96 : 1 : setting_value.setArray();
97 : 1 : return interfaces::SettingsAction::WRITE;
98 : : }
99 : : return interfaces::SettingsAction::SKIP_WRITE;
100 : : }
101 : 20 : common::SettingsValue new_value(common::SettingsValue::VARR);
102 [ + - + + ]: 83 : for (const auto& value : setting_value.getValues()) {
103 [ + - + - : 63 : if (!value.isStr() || value.get_str() != wallet_name) new_value.push_back(value);
+ + + - +
- ]
104 : : }
105 [ - + - + : 20 : if (new_value.size() == setting_value.size()) return interfaces::SettingsAction::SKIP_WRITE;
+ + ]
106 : 12 : setting_value = std::move(new_value);
107 : 12 : return interfaces::SettingsAction::WRITE;
108 : 20 : };
109 [ + - + - ]: 23 : return chain.updateRwSetting("wallet", update_function);
110 : : }
111 : :
112 : 1959 : void UpdateWalletSetting(interfaces::Chain& chain,
113 : : const std::string& wallet_name,
114 : : std::optional<bool> load_on_startup,
115 : : std::vector<bilingual_str>& warnings)
116 : : {
117 [ + + ]: 1959 : if (!load_on_startup) return;
118 [ + + + + ]: 206 : if (load_on_startup.value() && !AddWalletSetting(chain, wallet_name)) {
119 [ + - + - ]: 4 : warnings.emplace_back(Untranslated("Wallet load on startup setting could not be updated, so wallet may not be loaded next node startup."));
120 [ + + + + ]: 204 : } else if (!load_on_startup.value() && !RemoveWalletSetting(chain, wallet_name)) {
121 [ + - + - ]: 2 : warnings.emplace_back(Untranslated("Wallet load on startup setting could not be updated, so wallet may still be loaded next node startup."));
122 : : }
123 : : }
124 : :
125 : 4194 : WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& addr_type, bool internal)
126 : : {
127 : 4194 : int64_t creation_time = GetTime();
128 : :
129 : 4194 : std::string xpub = EncodeExtPubKey(master_key);
130 : :
131 : : // Build descriptor string
132 [ + - ]: 4194 : std::string desc_prefix;
133 [ + - ]: 4194 : std::string desc_suffix = "/*)";
134 [ + + + + : 4194 : switch (addr_type) {
- - ]
135 : 1046 : case OutputType::LEGACY: {
136 [ + - ]: 2092 : desc_prefix = "pkh(" + xpub + "/44h";
137 : 1046 : break;
138 : : }
139 : 1042 : case OutputType::P2SH_SEGWIT: {
140 [ + - ]: 2084 : desc_prefix = "sh(wpkh(" + xpub + "/49h";
141 [ + - ]: 1042 : desc_suffix += ')';
142 : : break;
143 : : }
144 : 1046 : case OutputType::BECH32: {
145 [ + - ]: 2092 : desc_prefix = "wpkh(" + xpub + "/84h";
146 : 1046 : break;
147 : : }
148 : 1060 : case OutputType::BECH32M: {
149 [ + - ]: 2120 : desc_prefix = "tr(" + xpub + "/86h";
150 : 1060 : break;
151 : : }
152 : 0 : case OutputType::UNKNOWN: {
153 : : // We should never have a DescriptorScriptPubKeyMan for an UNKNOWN OutputType,
154 : : // so if we get to this point something is wrong
155 : 0 : AssertUnreachable();
156 : : }
157 : : } // no default case, so the compiler can warn about missing cases
158 [ - + ]: 4194 : assert(!desc_prefix.empty());
159 : :
160 : : // Mainnet derives at 0', testnet and regtest derive at 1'
161 [ + - + + ]: 4194 : if (Params().IsTestChain()) {
162 [ + - ]: 3858 : desc_prefix += "/1h";
163 : : } else {
164 [ + - ]: 336 : desc_prefix += "/0h";
165 : : }
166 : :
167 [ + + + - ]: 10485 : std::string internal_path = internal ? "/1" : "/0";
168 [ + - + - : 12582 : std::string desc_str = desc_prefix + "/0h" + internal_path + desc_suffix;
+ - ]
169 : :
170 : : // Make the descriptor
171 : 8388 : FlatSigningProvider keys;
172 [ - + ]: 8388 : std::string error;
173 [ - + + - ]: 8388 : std::vector<std::unique_ptr<Descriptor>> desc = Parse(desc_str, keys, error, false);
174 [ + - + - : 4194 : WalletDescriptor w_desc(std::move(desc.at(0)), creation_time, 0, 0, 0);
+ - ]
175 : 4194 : return w_desc;
176 : 4194 : }
177 : :
178 : 28 : void WalletDescriptor::UpdateFrom(const WalletDescriptor& other)
179 : : {
180 [ + - ]: 28 : if (!IsCanonicallyEquivalent(other)) {
181 : : return;
182 : : }
183 : 28 : range_start = other.range_start;
184 : 28 : next_index = other.next_index;
185 : 28 : range_end = other.range_end;
186 : 28 : creation_time = other.creation_time;
187 : 28 : cache = other.cache;
188 : : }
189 : :
190 : 9592 : uint256 WalletDescriptor::GetCanonicalHash() const
191 : : {
192 [ + + ]: 9592 : if (!m_canonical_hash) {
193 : 3042 : m_canonical_hash.emplace();
194 : 3042 : std::string canonical = descriptor->ToCanonicalString();
195 [ + - + - : 6084 : CSHA256().Write((unsigned char*)canonical.data(), canonical.size()).Finalize(m_canonical_hash->begin());
+ - ]
196 : 3042 : }
197 : 9592 : return *m_canonical_hash;
198 : : }
199 : :
200 : 4796 : bool WalletDescriptor::IsCanonicallyEquivalent(const WalletDescriptor& other) const
201 : : {
202 [ + + ]: 4796 : return GetCanonicalHash() == other.GetCanonicalHash();
203 : : }
204 : :
205 : : } // namespace wallet
|