| 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, 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); |
| 74 | //Refresh addressBook |
| 75 | wallet->addressBook()->refresh(); |
| 76 | return wallet; |
| 77 | } |
| 78 | |
| 79 | Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &mnemonic, NetworkType nettype, uint64_t restoreHeight) |
| 80 | { |
| 81 | return recoveryWallet(path, "", mnemonic, nettype, restoreHeight); |
| 82 | } |
| 83 | |
| 84 | Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path, |
| 85 | const std::string &language, |
| 86 | NetworkType nettype, |
| 87 | uint64_t restoreHeight, |
| 88 | const std::string &addressString, |
| 89 | const std::string &viewKeyString, |
| 90 | const std::string &spendKeyString) |
| 91 | { |
| 92 | return createWalletFromKeys(path, "", language, nettype, restoreHeight, |
| 93 | addressString, viewKeyString, spendKeyString); |
| 94 | } |
| 95 | |
| 96 | Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, |
| 97 | const std::string &password, |
| 98 | const std::string &mnemonic, |
| 99 | NetworkType nettype, |
| 100 | uint64_t restoreHeight, |
| 101 | uint64_t kdf_rounds, |
| 102 | const std::string &seed_offset/* = {}*/) |
| 103 | { |
| 104 | WalletImpl * wallet = new WalletImpl(nettype, kdf_rounds); |
| 105 | if(restoreHeight > 0){ |
| 106 | wallet->setRefreshFromBlockHeight(restoreHeight); |
| 107 | } |
| 108 | wallet->recover(path, password, mnemonic, seed_offset); |
| 109 | return wallet; |
| 110 | } |
| 111 | |
| 112 | Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path, |
| 113 | const std::string &password, |
| 114 | const std::string &language, |
| 115 | NetworkType nettype, |
| 116 | uint64_t restoreHeight, |
| 117 | const std::string &addressString, |
| 118 | const std::string &viewKeyString, |
| 119 | const std::string &spendKeyString, |
| 120 | uint64_t kdf_rounds) |
| 121 | { |
| 122 | WalletImpl * wallet = new WalletImpl(nettype, kdf_rounds); |
| 123 | if(restoreHeight > 0){ |
| 124 | wallet->setRefreshFromBlockHeight(restoreHeight); |
| 125 | } |
| 126 | wallet->recoverFromKeysWithPassword(path, password, language, addressString, viewKeyString, spendKeyString); |
| 127 | return wallet; |
| 128 | } |
| 129 | |
| 130 | Wallet *WalletManagerImpl::createWalletFromDevice(const std::string &path, |
| 131 | const std::string &password, |
| 132 | NetworkType nettype, |
| 133 | const std::string &deviceName, |
| 134 | uint64_t restoreHeight, |
| 135 | const std::string &subaddressLookahead, |
| 136 | uint64_t kdf_rounds, |
| 137 | WalletListener * listener) |
| 138 | { |
| 139 | WalletImpl * wallet = new WalletImpl(nettype, kdf_rounds); |
| 140 | wallet->setListener(listener); |
| 141 | if (listener){ |
| 142 | listener->onSetWallet(wallet); |
| 143 | } |
| 144 | |
| 145 | if(restoreHeight > 0){ |
| 146 | wallet->setRefreshFromBlockHeight(restoreHeight); |
| 147 | } else { |
| 148 | wallet->setRefreshFromBlockHeight(wallet->estimateBlockChainHeight()); |
| 149 | } |
| 150 | auto lookahead = tools::parse_subaddress_lookahead(subaddressLookahead); |
| 151 | if (lookahead) |
| 152 | { |
| 153 | wallet->setSubaddressLookahead(lookahead->first, lookahead->second); |
| 154 | } |
| 155 | wallet->recoverFromDevice(path, password, deviceName); |
| 156 | return wallet; |
| 157 | } |
| 158 | |
| 159 | bool WalletManagerImpl::closeWallet(Wallet *wallet, bool store) |
| 160 | { |
| 161 | WalletImpl * wallet_ = dynamic_cast<WalletImpl*>(wallet); |
| 162 | if (!wallet_) |
| 163 | return false; |
| 164 | bool result = wallet_->close(store); |
| 165 | if (!result) { |
| 166 | m_errorString = wallet_->errorString(); |
| 167 | } else { |
| 168 | delete wallet_; |
| 169 | } |
| 170 | return result; |
| 171 | } |
| 172 | |
| 173 | bool WalletManagerImpl::walletExists(const std::string &path) |
| 174 | { |
| 175 | bool keys_file_exists; |
| 176 | bool wallet_file_exists; |
| 177 | tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists); |
| 178 | if(keys_file_exists){ |
| 179 | return true; |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | bool WalletManagerImpl::verifyWalletPassword(const std::string &keys_file_name, const std::string &password, bool no_spend_key, uint64_t kdf_rounds) const |
| 185 | { |
| 186 | return tools::wallet2::verify_password(keys_file_name, password, no_spend_key, hw::get_device("default"), kdf_rounds); |
| 187 | } |
| 188 | |
| 189 | bool WalletManagerImpl::queryWalletDevice(Wallet::Device& device_type, const std::string &keys_file_name, const std::string &password, uint64_t kdf_rounds) const |
| 190 | { |
| 191 | hw::device::device_type type; |
| 192 | bool r = tools::wallet2::query_device(type, keys_file_name, password, kdf_rounds); |
| 193 | device_type = static_cast<Wallet::Device>(type); |
| 194 | return r; |
| 195 | } |
| 196 | |
| 197 | std::vector<std::string> WalletManagerImpl::findWallets(const std::string &path) |
| 198 | { |
| 199 | std::vector<std::string> result; |
| 200 | boost::filesystem::path work_dir(path); |
| 201 | // return empty result if path doesn't exist |
| 202 | if(!boost::filesystem::is_directory(path)){ |
| 203 | return result; |
| 204 | } |
| 205 | const boost::regex wallet_rx("(.*)\\.(keys)$"); // searching for <wallet_name>.keys files |
| 206 | boost::filesystem::recursive_directory_iterator end_itr; // Default ctor yields past-the-end |
| 207 | for (boost::filesystem::recursive_directory_iterator itr(path); itr != end_itr; ++itr) { |
| 208 | // Skip if not a file |
| 209 | if (!boost::filesystem::is_regular_file(itr->status())) |
| 210 | continue; |
| 211 | boost::smatch what; |
| 212 | std::string filename = itr->path().filename().string(); |
| 213 | |
| 214 | LOG_PRINT_L3("Checking filename: " << filename); |
| 215 | |
| 216 | bool matched = boost::regex_match(filename, what, wallet_rx); |
| 217 | if (matched) { |
| 218 | // if keys file found, checking if there's wallet file itself |
| 219 | std::string wallet_file = (itr->path().parent_path() /= what[1].str()).string(); |
| 220 | if (boost::filesystem::exists(wallet_file)) { |
| 221 | LOG_PRINT_L3("Found wallet: " << wallet_file); |
| 222 | result.push_back(wallet_file); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | return result; |
| 227 | } |
| 228 | |
| 229 | std::string WalletManagerImpl::errorString() const |
| 230 | { |
| 231 | return m_errorString; |
| 232 | } |
| 233 | |
| 234 | void WalletManagerImpl::setDaemonAddress(const std::string &address) |
| 235 | { |
| 236 | m_http_client.set_server(address, boost::none); |
| 237 | } |
| 238 | |
| 239 | bool WalletManagerImpl::connected(uint32_t *version) |
| 240 | { |
| 241 | epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_VERSION::request> req_t = AUTO_VAL_INIT(req_t); |
| 242 | epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_VERSION::response, std::string> resp_t = AUTO_VAL_INIT(resp_t); |
| 243 | req_t.jsonrpc = "2.0"; |
| 244 | req_t.id = epee::serialization::storage_entry(0); |
| 245 | req_t.method = "get_version"; |
| 246 | if (!epee::net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client)) |
| 247 | return false; |
| 248 | |
| 249 | if (version) |
| 250 | *version = resp_t.result.version; |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | uint64_t WalletManagerImpl::blockchainHeight() |
| 255 | { |
| 256 | cryptonote::COMMAND_RPC_GET_INFO::request ireq; |
| 257 | cryptonote::COMMAND_RPC_GET_INFO::response ires; |
| 258 | |
| 259 | if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client)) |
| 260 | return 0; |
| 261 | return ires.height; |
| 262 | } |
| 263 | |
| 264 | uint64_t WalletManagerImpl::blockchainTargetHeight() |
| 265 | { |
| 266 | cryptonote::COMMAND_RPC_GET_INFO::request ireq; |
| 267 | cryptonote::COMMAND_RPC_GET_INFO::response ires; |
| 268 | |
| 269 | if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client)) |
| 270 | return 0; |
| 271 | return ires.target_height >= ires.height ? ires.target_height : ires.height; |
| 272 | } |
| 273 | |
| 274 | uint64_t WalletManagerImpl::networkDifficulty() |
| 275 | { |
| 276 | cryptonote::COMMAND_RPC_GET_INFO::request ireq; |
| 277 | cryptonote::COMMAND_RPC_GET_INFO::response ires; |
| 278 | |
| 279 | if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client)) |
| 280 | return 0; |
| 281 | return ires.difficulty; |
| 282 | } |
| 283 | |
| 284 | double WalletManagerImpl::miningHashRate() |
| 285 | { |
| 286 | cryptonote::COMMAND_RPC_MINING_STATUS::request mreq; |
| 287 | cryptonote::COMMAND_RPC_MINING_STATUS::response mres; |
| 288 | |
| 289 | epee::net_utils::http::http_simple_client http_client; |
| 290 | if (!epee::net_utils::invoke_http_json("/mining_status", mreq, mres, m_http_client)) |
| 291 | return 0.0; |
| 292 | if (!mres.active) |
| 293 | return 0.0; |
| 294 | return mres.speed; |
| 295 | } |
| 296 | |
| 297 | uint64_t WalletManagerImpl::blockTarget() |
| 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; |
| 305 | } |
| 306 | |
| 307 | bool WalletManagerImpl::isMining() |
| 308 | { |
| 309 | cryptonote::COMMAND_RPC_MINING_STATUS::request mreq; |
| 310 | cryptonote::COMMAND_RPC_MINING_STATUS::response mres; |
| 311 | |
| 312 | if (!epee::net_utils::invoke_http_json("/mining_status", mreq, mres, m_http_client)) |
| 313 | return false; |
| 314 | return mres.active; |
| 315 | } |
| 316 | |
| 317 | bool WalletManagerImpl::startMining(const std::string &address, uint32_t threads, bool background_mining, bool ignore_battery) |
| 318 | { |
| 319 | cryptonote::COMMAND_RPC_START_MINING::request mreq; |
| 320 | cryptonote::COMMAND_RPC_START_MINING::response mres; |
| 321 | |
| 322 | mreq.miner_address = address; |
| 323 | mreq.threads_count = threads; |
| 324 | mreq.ignore_battery = ignore_battery; |
| 325 | mreq.do_background_mining = background_mining; |
| 326 | |
| 327 | if (!epee::net_utils::invoke_http_json("/start_mining", mreq, mres, m_http_client)) |
| 328 | return false; |
| 329 | return mres.status == CORE_RPC_STATUS_OK; |
| 330 | } |
| 331 | |
| 332 | bool WalletManagerImpl::stopMining() |
| 333 | { |
| 334 | cryptonote::COMMAND_RPC_STOP_MINING::request mreq; |
| 335 | cryptonote::COMMAND_RPC_STOP_MINING::response mres; |
| 336 | |
| 337 | if (!epee::net_utils::invoke_http_json("/stop_mining", mreq, mres, m_http_client)) |
| 338 | return false; |
| 339 | return mres.status == CORE_RPC_STATUS_OK; |
| 340 | } |
| 341 | |
| 342 | std::string WalletManagerImpl::resolveOpenAlias(const std::string &address, bool &dnssec_valid) const |
| 343 | { |
| 344 | std::vector<std::string> addresses = tools::dns_utils::addresses_from_url(address, dnssec_valid); |
| 345 | if (addresses.empty()) |
| 346 | return ""; |
| 347 | return addresses.front(); |
| 348 | } |
| 349 | |
| 350 | std::tuple<bool, std::string, std::string, std::string, std::string> WalletManager::checkUpdates( |
| 351 | const std::string &software, |
| 352 | std::string subdir, |
| 353 | const char *buildtag/* = nullptr*/, |
| 354 | const char *current_version/* = nullptr*/) |
| 355 | { |
| 356 | if (buildtag == nullptr) |
| 357 | { |
| 358 | #ifdef BUILD_TAG |
| 359 | static const char buildtag_default[] = BOOST_PP_STRINGIZE(BUILD_TAG); |
| 360 | #else |
| 361 | static const char buildtag_default[] = "source"; |
| 362 | // Override the subdir string when built from source |
| 363 | subdir = "source"; |
| 364 | #endif |
| 365 | buildtag = buildtag_default; |
| 366 | } |
| 367 | |
| 368 | std::string version, hash; |
| 369 | MDEBUG("Checking for a new " << software << " version for " << buildtag); |
| 370 | if (!tools::check_updates(software, buildtag, version, hash)) |
| 371 | return std::make_tuple(false, "", "", "", ""); |
| 372 | |
| 373 | if (tools::vercmp(version.c_str(), current_version != nullptr ? current_version : MONERO_VERSION) > 0) |
| 374 | { |
| 375 | std::string user_url = tools::get_update_url(software, subdir, buildtag, version, true); |
| 376 | std::string auto_url = tools::get_update_url(software, subdir, buildtag, version, false); |
| 377 | MGINFO("Version " << version << " of " << software << " for " << buildtag << " is available: " << user_url << ", SHA256 hash " << hash); |
| 378 | return std::make_tuple(true, version, hash, user_url, auto_url); |
| 379 | } |
| 380 | return std::make_tuple(false, "", "", "", ""); |
| 381 | } |
| 382 | |
| 383 | bool WalletManagerImpl::setProxy(const std::string &address) |
| 384 | { |
| 385 | return m_http_client.set_proxy(address); |
| 386 | } |
| 387 | |
| 388 | ///////////////////// WalletManagerFactory implementation ////////////////////// |
| 389 | WalletManager *WalletManagerFactory::getWalletManager() |
| 390 | { |
| 391 | |
| 392 | static WalletManagerImpl * g_walletManager = nullptr; |
| 393 | |
| 394 | if (!g_walletManager) { |
| 395 | g_walletManager = new WalletManagerImpl(); |
| 396 | } |
| 397 | |
| 398 | return g_walletManager; |
| 399 | } |
| 400 | |
| 401 | void WalletManagerFactory::setLogLevel(int level) |
| 402 | { |
| 403 | mlog_set_log_level(level); |
| 404 | } |
| 405 | |
| 406 | void WalletManagerFactory::setLogCategories(const std::string &categories) |
| 407 | { |
| 408 | mlog_set_log(categories.c_str()); |
| 409 | } |
| 410 | |
| 411 | |
| 412 | |
| 413 | } |