diff --git a/src/wallet/monero_wallet_core.cpp b/src/wallet/monero_wallet_core.cpp index 17df5d1..35e1e5c 100644 --- a/src/wallet/monero_wallet_core.cpp +++ b/src/wallet/monero_wallet_core.cpp @@ -809,10 +809,23 @@ namespace monero { return wallet; } - monero_wallet_core* monero_wallet_core::create_wallet_random(const string& path, const string& password, const monero_network_type network_type, const monero_rpc_connection& daemon_connection, const string& language) { - MTRACE("create_wallet_random(path, password, network_type, daemon_connection, language)"); + monero_wallet_core* monero_wallet_core::open_wallet_data(const string& password, const monero_network_type network_type, const string& keys_data, const string& cache_data, const monero_rpc_connection& daemon_connection, unique_ptr http_client_factory) { + MTRACE("open_wallet_data(...)"); monero_wallet_core* wallet = new monero_wallet_core(); - wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true)); + if (http_client_factory == nullptr) wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true)); + else wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true, std::move(http_client_factory))); + wallet->m_w2->load("", password, keys_data, cache_data); + wallet->m_w2->init(""); + wallet->set_daemon_connection(daemon_connection); + wallet->init_common(); + return wallet; + } + + monero_wallet_core* monero_wallet_core::create_wallet_random(const string& path, const string& password, const monero_network_type network_type, const monero_rpc_connection& daemon_connection, const string& language, unique_ptr http_client_factory) { + MTRACE("create_wallet_random(...)"); + monero_wallet_core* wallet = new monero_wallet_core(); + if (http_client_factory == nullptr) wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true)); + else wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true, std::move(http_client_factory))); wallet->set_daemon_connection(daemon_connection); wallet->m_w2->set_seed_language(language); crypto::secret_key secret_key; @@ -822,7 +835,7 @@ namespace monero { return wallet; } - monero_wallet_core* monero_wallet_core::create_wallet_from_mnemonic(const string& path, const string& password, const monero_network_type network_type, const string& mnemonic, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const string& seed_offset) { + monero_wallet_core* monero_wallet_core::create_wallet_from_mnemonic(const string& path, const string& password, const monero_network_type network_type, const string& mnemonic, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const string& seed_offset, unique_ptr http_client_factory) { MTRACE("create_wallet_from_mnemonic(path, password, mnemonic, network_type, daemon_connection, restore_height)"); monero_wallet_core* wallet = new monero_wallet_core(); @@ -837,7 +850,8 @@ namespace monero { if (!seed_offset.empty()) recovery_key = cryptonote::decrypt_key(recovery_key, seed_offset); // initialize wallet - wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true)); + if (http_client_factory == nullptr) wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true)); + else wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true, std::move(http_client_factory))); wallet->set_daemon_connection(daemon_connection); wallet->m_w2->set_seed_language(language); wallet->m_w2->generate(path, password, recovery_key, true, false); @@ -846,7 +860,7 @@ namespace monero { return wallet; } - monero_wallet_core* monero_wallet_core::create_wallet_from_keys(const string& path, const string& password, const monero_network_type network_type, const string& address, const string& view_key, const string& spend_key, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const string& language) { + monero_wallet_core* monero_wallet_core::create_wallet_from_keys(const string& path, const string& password, const monero_network_type network_type, const string& address, const string& view_key, const string& spend_key, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const string& language, unique_ptr http_client_factory) { MTRACE("create_wallet_from_keys(path, password, address, view_key, spend_key, network_type, daemon_connection, restore_height, language)"); monero_wallet_core* wallet = new monero_wallet_core(); @@ -893,7 +907,8 @@ namespace monero { } // initialize wallet - wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true)); + if (http_client_factory == nullptr) wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true)); + else wallet->m_w2 = unique_ptr(new tools::wallet2(static_cast(network_type), 1, true, std::move(http_client_factory))); if (has_spend_key && has_view_key) wallet->m_w2->generate(path, password, info.address, spend_key_sk, view_key_sk); else if (has_spend_key) wallet->m_w2->generate(path, password, spend_key_sk, true, false); else wallet->m_w2->generate(path, password, info.address, view_key_sk); @@ -3184,6 +3199,20 @@ namespace monero { m_w2->store_to(path, password); } + std::string monero_wallet_core::get_keys_file_buffer(const epee::wipeable_string& password, bool watch_only) const { + boost::optional keys_file_data = m_w2->get_keys_file_data(password, watch_only); + std::string buf; + ::serialization::dump_binary(keys_file_data.get(), buf); + return buf; + } + + std::string monero_wallet_core::get_cache_file_buffer(const epee::wipeable_string& password) const { + boost::optional cache_file_data = m_w2->get_cache_file_data(password); + std::string buf; + ::serialization::dump_binary(cache_file_data.get(), buf); + return buf; + } + void monero_wallet_core::close(bool save) { MTRACE("close()"); if (save) this->save(); diff --git a/src/wallet/monero_wallet_core.h b/src/wallet/monero_wallet_core.h index b413bf8..bb03810 100644 --- a/src/wallet/monero_wallet_core.h +++ b/src/wallet/monero_wallet_core.h @@ -98,6 +98,19 @@ namespace monero { */ static monero_wallet_core* open_wallet(const string& path, const string& password, const monero_network_type network_type); + /** + * Open an in-memory wallet from existing data buffers. + * + * @param password is the password of the wallet file to open + * @param network_type is the wallet's network type + * @param keys_data contains the contents of the ".keys" file + * @param cache_data contains the contents of the wallet cache file (no extension) + * @param daemon_connection is connection information to a daemon (default = an unconnected wallet) + * @param http_client_factory allows use of custom http clients + * @return a pointer to the wallet instance + */ + static monero_wallet_core* open_wallet_data(const string& password, const monero_network_type, const string& keys_data, const string& cache_data, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), unique_ptr http_client_factory = nullptr); + /** * Create a new wallet with a randomly generated seed. * @@ -106,9 +119,10 @@ namespace monero { * @param network_type is the wallet's network type's * @param daemon_connection is connection information to a daemon (defaults to an unconnected wallet) * @param language is the wallet and mnemonic's language (defaults to "English") + * @param http_client_factory allows use of custom http clients * @return a pointer to the wallet instance */ - static monero_wallet_core* create_wallet_random(const string& path, const string& password, const monero_network_type network_type, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), const string& language = "English"); + static monero_wallet_core* create_wallet_random(const string& path, const string& password, const monero_network_type network_type, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), const string& language = "English", unique_ptr http_client_factory = nullptr); /** * Create a wallet from an existing mnemonic phrase. @@ -119,9 +133,10 @@ namespace monero { * @param daemon_connection is connection information to a daemon (defaults to an unconnected wallet) * @param restore_height is the block height to restore from (default = 0) * @param seed_offset is the offset used to derive a new seed from the given mnemonic to recover a secret wallet from the mnemonic phrase (default = "") + * @param http_client_factory allows use of custom http clients * @return a pointer to the wallet instance */ - static monero_wallet_core* create_wallet_from_mnemonic(const string& path, const string& password, const monero_network_type network_type, const string& mnemonic, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), uint64_t restore_height = 0, const string& seed_offset = ""); + static monero_wallet_core* create_wallet_from_mnemonic(const string& path, const string& password, const monero_network_type network_type, const string& mnemonic, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), uint64_t restore_height = 0, const string& seed_offset = "", unique_ptr http_client_factory = nullptr); /** * Create a wallet from an address, view key, and spend key. @@ -135,9 +150,10 @@ namespace monero { * @param daemon_connection is connection information to a daemon (defaults to an unconnected wallet) * @param restore_height is the block height to restore (i.e. scan the chain) from (default = 0) * @param language is the wallet and mnemonic's language (defaults to "English") + * @param http_client_factory allows use of custom http clients * @return a pointer to the wallet instance */ - static monero_wallet_core* create_wallet_from_keys(const string& path, const string& password, const monero_network_type network_type, const string& address, const string& view_key, const string& spend_key, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), uint64_t restore_height = 0, const string& language = "English"); + static monero_wallet_core* create_wallet_from_keys(const string& path, const string& password, const monero_network_type network_type, const string& address, const string& view_key, const string& spend_key, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), uint64_t restore_height = 0, const string& language = "English", unique_ptr http_client_factory = nullptr); /** * Get a list of available languages for the wallet's mnemonic phrase. @@ -270,6 +286,12 @@ namespace monero { void move_to(string path, string password); void close(bool save = false); + /** + * Wallet import and export using buffers and not the file system. + */ + std::string get_keys_file_buffer(const epee::wipeable_string& password, bool watch_only) const; + std::string get_cache_file_buffer(const epee::wipeable_string& password) const; + // --------------------------------- PRIVATE -------------------------------- private: