Debian Codegraph @

monero/src/wallet/api/wallet_manager.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31
32 #include "wallet_manager.h"
33 #include "wallet.h"
34 #include "common_defines.h"
35 #include "common/dns_utils.h"
36 #include "common/util.h"
37 #include "common/updates.h"
38 #include "version.h"
39 #include "net/http_client.h"
40 #include <boost/filesystem.hpp>
41 #include <boost/regex.hpp>
42
43 #undef MONERO_DEFAULT_LOG_CATEGORY
44 #define MONERO_DEFAULT_LOG_CATEGORY "WalletAPI"
45
46 namespace epee {
47 unsigned int g_test_dbg_lock_sleep = 0;
48 }
49
50 namespace Monero {
51
52 WalletManagerImpl::WalletManagerImpl()
53 {
54 tools::set_strict_default_file_permissions(true);
55 }
56
57 Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password,
58 const std::string &language, NetworkType nettype, uint64_t kdf_rounds)
59 {
60 WalletImpl * wallet = new WalletImpl(nettype, kdf_rounds);
61 wallet->create(path, password, language);
62 return wallet;
63 }
64
65 Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password, NetworkType nettype, uint64_t kdf_rounds, const std::string &ringDatabasePath, WalletListener * listener)
66 {
67 WalletImpl * wallet = new WalletImpl(nettype, kdf_rounds);
68 wallet->setListener(listener);
69 if (listener){
70 listener->onSetWallet(wallet);
71 }
72
73 wallet->open(path, password, ringDatabasePath);
74 return wallet;
75 }
76
77 Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &mnemonic, NetworkType nettype, uint64_t restoreHeight)
78 {
79 return recoveryWallet(path, "", mnemonic, nettype, restoreHeight);
80 }
81
82 Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path,
83 const std::string &language,
84 NetworkType nettype,
85 uint64_t restoreHeight,
86 const std::string &addressString,
87 const std::string &viewKeyString,
88 const std::string &spendKeyString)
89 {
90 return createWalletFromKeys(path, "", language, nettype, restoreHeight,
91 addressString, viewKeyString, spendKeyString);
92 }
93
94 Wallet *WalletManagerImpl::recoveryWallet(const std::string &path,
95 const std::string &password,
96 const std::string &mnemonic,
97 NetworkType nettype,
98 uint64_t restoreHeight,
99 uint64_t kdf_rounds,
100 const std::string &seed_offset,/* = {}*/
101 const std::string &subaddressLookahead)
102 {
103 WalletImpl * wallet = new WalletImpl(nettype, kdf_rounds);
104 if(restoreHeight > 0){
105 wallet->setRefreshFromBlockHeight(restoreHeight);
106 }
107 auto lookahead = tools::parse_subaddress_lookahead(subaddressLookahead);
108 if (lookahead)
109 {
110 wallet->setSubaddressLookahead(lookahead->first, lookahead->second);
111 }
112 wallet->recover(path, password, mnemonic, seed_offset);
113 return wallet;
114 }
115
116 Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path,
117 const std::string &password,
118 const std::string &language,
119 NetworkType nettype,
120 uint64_t restoreHeight,
121 const std::string &addressString,
122 const std::string &viewKeyString,
123 const std::string &spendKeyString,
124 uint64_t kdf_rounds,
125 const std::string &subaddressLookahead)
126 {
127 WalletImpl * wallet = new WalletImpl(nettype, kdf_rounds);
128 if(restoreHeight > 0){
129 wallet->setRefreshFromBlockHeight(restoreHeight);
130 }
131 auto lookahead = tools::parse_subaddress_lookahead(subaddressLookahead);
132 if (lookahead)
133 {
134 wallet->setSubaddressLookahead(lookahead->first, lookahead->second);
135 }
136 wallet->recoverFromKeysWithPassword(path, password, language, addressString, viewKeyString, spendKeyString);
137 return wallet;
138 }
139
140 Wallet *WalletManagerImpl::createDeterministicWalletFromSpendKey(const std::string &path,
141 const std::string &password,
142 const std::string &language,
143 NetworkType nettype,
144 uint64_t restoreHeight,
145 const std::string &spendkey_string,
146 uint64_t kdf_rounds,
147 const std::string &offset_passphrase,
148 const std::string &subaddressLookahead)
149 {
150 WalletImpl * wallet = new WalletImpl(nettype, kdf_rounds);
151 if(restoreHeight > 0){
152 wallet->setRefreshFromBlockHeight(restoreHeight);
153 }
154 auto lookahead = tools::parse_subaddress_lookahead(subaddressLookahead);
155 if (lookahead)
156 {
157 wallet->setSubaddressLookahead(lookahead->first, lookahead->second);
158 }
159 wallet->recoverDeterministicWalletFromSpendKey(path, password, language, spendkey_string, offset_passphrase);
160 return wallet;
161 }
162
163 Wallet *WalletManagerImpl::createWalletFromDevice(const std::string &path,
164 const std::string &password,
165 NetworkType nettype,
166 const std::string &deviceName,
167 uint64_t restoreHeight,
168 const std::string &subaddressLookahead,
169 uint64_t kdf_rounds,
170 WalletListener * listener)
171 {
172 WalletImpl * wallet = new WalletImpl(nettype, kdf_rounds);
173 wallet->setListener(listener);
174 if (listener){
175 listener->onSetWallet(wallet);
176 }
177
178 if(restoreHeight > 0){
179 wallet->setRefreshFromBlockHeight(restoreHeight);
180 } else {
181 wallet->setRefreshFromBlockHeight(wallet->estimateBlockChainHeight());
182 }
183 auto lookahead = tools::parse_subaddress_lookahead(subaddressLookahead);
184 if (lookahead)
185 {
186 wallet->setSubaddressLookahead(lookahead->first, lookahead->second);
187 }
188 wallet->recoverFromDevice(path, password, deviceName);
189 return wallet;
190 }
191
192 bool WalletManagerImpl::closeWallet(Wallet *wallet, bool store)
193 {
194 WalletImpl * wallet_ = dynamic_cast<WalletImpl*>(wallet);
195 if (!wallet_)
196 return false;
197 bool result = wallet_->close(store);
198 if (!result) {
199 m_errorString = wallet_->errorString();
200 } else {
201 delete wallet_;
202 }
203 return result;
204 }
205
206 bool WalletManagerImpl::walletExists(const std::string &path)
207 {
208 bool keys_file_exists;
209 bool wallet_file_exists;
210 tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists);
211 if(keys_file_exists){
212 return true;
213 }
214 return false;
215 }
216
217 bool WalletManagerImpl::verifyWalletPassword(const std::string &keys_file_name, const std::string &password, bool no_spend_key, uint64_t kdf_rounds) const
218 {
219 return tools::wallet2::verify_password(keys_file_name, password, no_spend_key, hw::get_device("default"), kdf_rounds);
220 }
221
222 bool WalletManagerImpl::queryWalletDevice(Wallet::Device& device_type, const std::string &keys_file_name, const std::string &password, uint64_t kdf_rounds) const
223 {
224 hw::device::device_type type;
225 bool r = tools::wallet2::query_device(type, keys_file_name, password, kdf_rounds);
226 device_type = static_cast<Wallet::Device>(type);
227 return r;
228 }
229
230 std::vector<std::string> WalletManagerImpl::findWallets(const std::string &path)
231 {
232 std::vector<std::string> result;
233 boost::filesystem::path work_dir(path);
234 // return empty result if path doesn't exist
235 if(!boost::filesystem::is_directory(path)){
236 return result;
237 }
238 const boost::regex wallet_rx("(.*)\\.(keys)$"); // searching for <wallet_name>.keys files
239 boost::filesystem::recursive_directory_iterator end_itr; // Default ctor yields past-the-end
240 for (boost::filesystem::recursive_directory_iterator itr(path); itr != end_itr; ++itr) {
241 // Skip if not a file
242 if (!boost::filesystem::is_regular_file(itr->status()))
243 continue;
244 boost::smatch what;
245 std::string filename = itr->path().filename().string();
246
247 LOG_PRINT_L3("Checking filename: " << filename);
248
249 bool matched = boost::regex_match(filename, what, wallet_rx);
250 if (matched) {
251 // if keys file found, checking if there's wallet file itself
252 std::string wallet_file = (itr->path().parent_path() /= what[1].str()).string();
253 if (boost::filesystem::exists(wallet_file)) {
254 LOG_PRINT_L3("Found wallet: " << wallet_file);
255 result.push_back(wallet_file);
256 }
257 }
258 }
259 return result;
260 }
261
262 std::string WalletManagerImpl::errorString() const
263 {
264 return m_errorString;
265 }
266
267 void WalletManagerImpl::setDaemonAddress(const std::string &address)
268 {
269 m_http_client.set_server(address, boost::none);
270 }
271
272 bool WalletManagerImpl::connected(uint32_t *version)
273 {
274 epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_VERSION::request> req_t = AUTO_VAL_INIT(req_t);
275 epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_VERSION::response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
276 req_t.jsonrpc = "2.0";
277 req_t.id = epee::serialization::storage_entry(0);
278 req_t.method = "get_version";
279 if (!epee::net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client))
280 return false;
281
282 if (version)
283 *version = resp_t.result.version;
284 return true;
285 }
286
287 uint64_t WalletManagerImpl::blockchainHeight()
288 {
289 cryptonote::COMMAND_RPC_GET_INFO::request ireq;
290 cryptonote::COMMAND_RPC_GET_INFO::response ires;
291
292 if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client))
293 return 0;
294 return ires.height;
295 }
296
297 uint64_t WalletManagerImpl::blockchainTargetHeight()
298 {
299 cryptonote::COMMAND_RPC_GET_INFO::request ireq;
300 cryptonote::COMMAND_RPC_GET_INFO::response ires;
301
302 if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client))
303 return 0;
304 return ires.target_height >= ires.height ? ires.target_height : ires.height;
305 }
306
307 uint64_t WalletManagerImpl::networkDifficulty()
308 {
309 cryptonote::COMMAND_RPC_GET_INFO::request ireq;
310 cryptonote::COMMAND_RPC_GET_INFO::response ires;
311
312 if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client))
313 return 0;
314 return ires.difficulty;
315 }
316
317 double WalletManagerImpl::miningHashRate()
318 {
319 cryptonote::COMMAND_RPC_MINING_STATUS::request mreq;
320 cryptonote::COMMAND_RPC_MINING_STATUS::response mres;
321
322 epee::net_utils::http::http_simple_client http_client;
323 if (!epee::net_utils::invoke_http_json("/mining_status", mreq, mres, m_http_client))
324 return 0.0;
325 if (!mres.active)
326 return 0.0;
327 return mres.speed;
328 }
329
330 uint64_t WalletManagerImpl::blockTarget()
331 {
332 cryptonote::COMMAND_RPC_GET_INFO::request ireq;
333 cryptonote::COMMAND_RPC_GET_INFO::response ires;
334
335 if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client))
336 return 0;
337 return ires.target;
338 }
339
340 bool WalletManagerImpl::isMining()
341 {
342 cryptonote::COMMAND_RPC_MINING_STATUS::request mreq;
343 cryptonote::COMMAND_RPC_MINING_STATUS::response mres;
344
345 if (!epee::net_utils::invoke_http_json("/mining_status", mreq, mres, m_http_client))
346 return false;
347 return mres.active;
348 }
349
350 bool WalletManagerImpl::startMining(const std::string &address, uint32_t threads, bool background_mining, bool ignore_battery)
351 {
352 cryptonote::COMMAND_RPC_START_MINING::request mreq;
353 cryptonote::COMMAND_RPC_START_MINING::response mres;
354
355 mreq.miner_address = address;
356 mreq.threads_count = threads;
357 mreq.ignore_battery = ignore_battery;
358 mreq.do_background_mining = background_mining;
359
360 if (!epee::net_utils::invoke_http_json("/start_mining", mreq, mres, m_http_client))
361 return false;
362 return mres.status == CORE_RPC_STATUS_OK;
363 }
364
365 bool WalletManagerImpl::stopMining()
366 {
367 cryptonote::COMMAND_RPC_STOP_MINING::request mreq;
368 cryptonote::COMMAND_RPC_STOP_MINING::response mres;
369
370 if (!epee::net_utils::invoke_http_json("/stop_mining", mreq, mres, m_http_client))
371 return false;
372 return mres.status == CORE_RPC_STATUS_OK;
373 }
374
375 std::string WalletManagerImpl::resolveOpenAlias(const std::string &address, bool &dnssec_valid) const
376 {
377 std::vector<std::string> addresses = tools::dns_utils::addresses_from_url(address, dnssec_valid);
378 if (addresses.empty())
379 return "";
380 return addresses.front();
381 }
382
383 std::tuple<bool, std::string, std::string, std::string, std::string> WalletManager::checkUpdates(
384 const std::string &software,
385 std::string subdir,
386 const char *buildtag/* = nullptr*/,
387 const char *current_version/* = nullptr*/)
388 {
389 if (buildtag == nullptr)
390 {
391 #ifdef BUILD_TAG
392 static const char buildtag_default[] = BOOST_PP_STRINGIZE(BUILD_TAG);
393 #else
394 static const char buildtag_default[] = "source";
395 // Override the subdir string when built from source
396 subdir = "source";
397 #endif
398 buildtag = buildtag_default;
399 }
400
401 std::string version, hash;
402 MDEBUG("Checking for a new " << software << " version for " << buildtag);
403 if (!tools::check_updates(software, buildtag, version, hash))
404 return std::make_tuple(false, "", "", "", "");
405
406 if (tools::vercmp(version.c_str(), current_version != nullptr ? current_version : MONERO_VERSION) > 0)
407 {
408 std::string user_url = tools::get_update_url(software, subdir, buildtag, version, true);
409 std::string auto_url = tools::get_update_url(software, subdir, buildtag, version, false);
410 MGINFO("Version " << version << " of " << software << " for " << buildtag << " is available: " << user_url << ", SHA256 hash " << hash);
411 return std::make_tuple(true, version, hash, user_url, auto_url);
412 }
413 return std::make_tuple(false, "", "", "", "");
414 }
415
416 bool WalletManagerImpl::setProxy(const std::string &address)
417 {
418 return m_http_client.set_proxy(address);
419 }
420
421 ///////////////////// WalletManagerFactory implementation //////////////////////
422 WalletManager *WalletManagerFactory::getWalletManager()
423 {
424
425 static WalletManagerImpl * g_walletManager = nullptr;
426
427 if (!g_walletManager) {
428 g_walletManager = new WalletManagerImpl();
429 }
430
431 return g_walletManager;
432 }
433
434 void WalletManagerFactory::setLogLevel(int level)
435 {
436 mlog_set_log_level(level);
437 }
438
439 void WalletManagerFactory::setLogCategories(const std::string &categories)
440 {
441 mlog_set_log(categories.c_str());
442 }
443
444
445
446 }