mirror of
https://github.com/wownero/wownero-cpp.git
synced 2026-10-17 15:21:30 +02:00
support subaddress lookahead when creating full wallet
create wallets with wallet config instead of params
This commit is contained in:
@@ -1063,56 +1063,76 @@ namespace monero {
|
||||
return wallet;
|
||||
}
|
||||
|
||||
monero_wallet_full* monero_wallet_full::create_wallet_random(const std::string& path, const std::string& password, const monero_network_type network_type, const monero_rpc_connection& daemon_connection, const std::string& language, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
MTRACE("create_wallet_random(...)");
|
||||
if (!monero_utils::is_valid_language(language)) throw std::runtime_error("Unknown language: " + language);
|
||||
monero_wallet_full* wallet = new monero_wallet_full();
|
||||
if (http_client_factory == nullptr) wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(network_type), 1, true));
|
||||
else wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(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;
|
||||
wallet->m_w2->generate(path, password, secret_key, false, false);
|
||||
wallet->init_common();
|
||||
if (wallet->is_connected_to_daemon()) wallet->m_w2->set_refresh_from_block_height(wallet->get_daemon_height());
|
||||
return wallet;
|
||||
monero_wallet_full* monero_wallet_full::create_wallet(const monero_wallet_config& config, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
MTRACE("create_wallet(config)");
|
||||
|
||||
// validate and normalize config
|
||||
monero_wallet_config config_normalized = config.copy();
|
||||
if (config.m_path == boost::none) config_normalized.m_path = std::string("");
|
||||
if (config.m_password == boost::none) config_normalized.m_password = std::string("");
|
||||
if (config.m_language == boost::none) config_normalized.m_language = std::string("");
|
||||
if (config.m_mnemonic == boost::none) config_normalized.m_mnemonic = std::string("");
|
||||
if (config.m_primary_address == boost::none) config_normalized.m_primary_address = std::string("");
|
||||
if (config.m_private_spend_key == boost::none) config_normalized.m_private_spend_key = std::string("");
|
||||
if (config.m_private_view_key == boost::none) config_normalized.m_private_view_key = std::string("");
|
||||
if (config.m_seed_offset == boost::none) config_normalized.m_seed_offset = std::string("");
|
||||
if (config.m_account_lookahead != boost::none && config.m_subaddress_lookahead == boost::none) throw std::runtime_error("No subaddress lookahead provided with account lookahead");
|
||||
if (config.m_account_lookahead == boost::none && config.m_subaddress_lookahead != boost::none) throw std::runtime_error("No account lookahead provided with subaddress lookahead");
|
||||
|
||||
// create wallet
|
||||
if (!config_normalized.m_mnemonic.get().empty()) {
|
||||
if (!config_normalized.m_language.get().empty()) throw std::runtime_error("Cannot specify language when creating wallet from mnemonic");
|
||||
return create_wallet_from_mnemonic(config_normalized, std::move(http_client_factory));
|
||||
} else if (!config_normalized.m_primary_address.get().empty() || !config_normalized.m_private_spend_key.get().empty()) {
|
||||
if (!config_normalized.m_seed_offset.get().empty()) throw std::runtime_error("Cannot specify seed offset when creating wallet from keys");
|
||||
if (config_normalized.m_language.get().empty()) config_normalized.m_language = std::string("English");
|
||||
if (!monero_utils::is_valid_language(config_normalized.m_language.get())) throw std::runtime_error("Unknown language: " + config_normalized.m_language.get());
|
||||
return create_wallet_from_keys(config_normalized, std::move(http_client_factory));
|
||||
} else {
|
||||
if (!config_normalized.m_seed_offset.get().empty()) throw std::runtime_error("Cannot specify seed offset when creating wallet from keys");
|
||||
if (config_normalized.m_restore_height != boost::none) throw std::runtime_error("Cannot specify restore height when creating random wallet");
|
||||
if (config_normalized.m_language.get().empty()) config_normalized.m_language = std::string("English");
|
||||
if (!monero_utils::is_valid_language(config_normalized.m_language.get())) throw std::runtime_error("Unknown language: " + config_normalized.m_language.get());
|
||||
return monero_wallet_full::create_wallet_random(config_normalized, std::move(http_client_factory));
|
||||
}
|
||||
}
|
||||
|
||||
monero_wallet_full* monero_wallet_full::create_wallet_from_mnemonic(const std::string& path, const std::string& password, const monero_network_type network_type, const std::string& mnemonic, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const std::string& seed_offset, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
MTRACE("create_wallet_from_mnemonic(path, password, mnemonic, network_type, daemon_connection, restore_height)");
|
||||
monero_wallet_full* monero_wallet_full::create_wallet_from_mnemonic(const monero_wallet_config& config, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
MTRACE("create_wallet_from_mnemonic(...)");
|
||||
monero_wallet_full* wallet = new monero_wallet_full();
|
||||
|
||||
// validate mnemonic and get recovery key and language
|
||||
crypto::secret_key recovery_key;
|
||||
std::string language;
|
||||
bool is_valid = crypto::ElectrumWords::words_to_bytes(mnemonic, recovery_key, language);
|
||||
bool is_valid = crypto::ElectrumWords::words_to_bytes(config.m_mnemonic.get(), recovery_key, language);
|
||||
if (!is_valid) throw std::runtime_error("Invalid mnemonic");
|
||||
if (language == crypto::ElectrumWords::old_language_name) language = Language::English().get_language_name();
|
||||
|
||||
// apply offset if given
|
||||
if (!seed_offset.empty()) recovery_key = cryptonote::decrypt_key(recovery_key, seed_offset);
|
||||
if (!config.m_seed_offset.get().empty()) recovery_key = cryptonote::decrypt_key(recovery_key, config.m_seed_offset.get());
|
||||
|
||||
// initialize wallet
|
||||
if (http_client_factory == nullptr) wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(network_type), 1, true));
|
||||
else wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(network_type), 1, true, std::move(http_client_factory)));
|
||||
wallet->set_daemon_connection(daemon_connection);
|
||||
if (http_client_factory == nullptr) wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(config.m_network_type.get()), 1, true));
|
||||
else wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(config.m_network_type.get()), 1, true, std::move(http_client_factory)));
|
||||
wallet->set_daemon_connection(config.get_server());
|
||||
wallet->m_w2->set_seed_language(language);
|
||||
wallet->m_w2->generate(path, password, recovery_key, true, false);
|
||||
wallet->m_w2->set_refresh_from_block_height(restore_height);
|
||||
if (config.m_account_lookahead != boost::none) wallet->m_w2->set_subaddress_lookahead(config.m_account_lookahead.get(), config.m_subaddress_lookahead.get());
|
||||
wallet->m_w2->generate(config.m_path.get(), config.m_password.get(), recovery_key, true, false);
|
||||
wallet->m_w2->set_refresh_from_block_height(config.m_restore_height.get());
|
||||
wallet->init_common();
|
||||
return wallet;
|
||||
}
|
||||
|
||||
monero_wallet_full* monero_wallet_full::create_wallet_from_keys(const std::string& path, const std::string& password, const monero_network_type network_type, const std::string& address, const std::string& view_key, const std::string& spend_key, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const std::string& language, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
MTRACE("create_wallet_from_keys(path, password, address, view_key, spend_key, network_type, daemon_connection, restore_height, language)");
|
||||
monero_wallet_full* monero_wallet_full::create_wallet_from_keys(const monero_wallet_config& config, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
MTRACE("create_wallet_from_keys(...)");
|
||||
monero_wallet_full* wallet = new monero_wallet_full();
|
||||
|
||||
// parse and validate private spend key
|
||||
crypto::secret_key spend_key_sk;
|
||||
bool has_spend_key = false;
|
||||
if (!spend_key.empty()) {
|
||||
if (!config.m_private_spend_key.get().empty()) {
|
||||
cryptonote::blobdata spend_key_data;
|
||||
if (!epee::string_tools::parse_hexstr_to_binbuff(spend_key, spend_key_data) || spend_key_data.size() != sizeof(crypto::secret_key)) {
|
||||
if (!epee::string_tools::parse_hexstr_to_binbuff(config.m_private_spend_key.get(), spend_key_data) || spend_key_data.size() != sizeof(crypto::secret_key)) {
|
||||
throw std::runtime_error("failed to parse secret spend key");
|
||||
}
|
||||
has_spend_key = true;
|
||||
@@ -1122,13 +1142,13 @@ namespace monero {
|
||||
// parse and validate private view key
|
||||
bool has_view_key = true;
|
||||
crypto::secret_key view_key_sk;
|
||||
if (view_key.empty()) {
|
||||
if (config.m_private_view_key.get().empty()) {
|
||||
if (has_spend_key) has_view_key = false;
|
||||
else throw std::runtime_error("Neither spend key nor view key supplied");
|
||||
}
|
||||
if (has_view_key) {
|
||||
cryptonote::blobdata view_key_data;
|
||||
if (!epee::string_tools::parse_hexstr_to_binbuff(view_key, view_key_data) || view_key_data.size() != sizeof(crypto::secret_key)) {
|
||||
if (!epee::string_tools::parse_hexstr_to_binbuff(config.m_private_view_key.get(), view_key_data) || view_key_data.size() != sizeof(crypto::secret_key)) {
|
||||
throw std::runtime_error("failed to parse secret view key");
|
||||
}
|
||||
view_key_sk = *reinterpret_cast<const crypto::secret_key*>(view_key_data.data());
|
||||
@@ -1136,10 +1156,10 @@ namespace monero {
|
||||
|
||||
// parse and validate address
|
||||
cryptonote::address_parse_info address_info;
|
||||
if (address.empty()) {
|
||||
if (has_view_key) throw std::runtime_error("must provide address if providing private view key");
|
||||
if (config.m_primary_address.get().empty()) {
|
||||
if (has_view_key) throw std::runtime_error("must provide primary address if providing private view key");
|
||||
} else {
|
||||
if (!get_account_address_from_str(address_info, static_cast<cryptonote::network_type>(network_type), address)) throw std::runtime_error("failed to parse address");
|
||||
if (!get_account_address_from_str(address_info, static_cast<cryptonote::network_type>(config.m_network_type.get()), config.m_primary_address.get())) throw std::runtime_error("failed to parse address");
|
||||
|
||||
// check the spend and view keys match the given address
|
||||
crypto::public_key pkey;
|
||||
@@ -1154,27 +1174,84 @@ namespace monero {
|
||||
}
|
||||
|
||||
// validate language
|
||||
if (!monero_utils::is_valid_language(language)) throw std::runtime_error("Unknown language: " + language);
|
||||
if (!monero_utils::is_valid_language(config.m_language.get())) throw std::runtime_error("Unknown language: " + config.m_language.get());
|
||||
|
||||
// initialize wallet
|
||||
if (http_client_factory == nullptr) wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(network_type), 1, true));
|
||||
else wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(network_type), 1, true, std::move(http_client_factory)));
|
||||
if (has_spend_key && has_view_key) wallet->m_w2->generate(path, password, address_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, address_info.address, view_key_sk);
|
||||
wallet->set_daemon_connection(daemon_connection);
|
||||
wallet->m_w2->set_refresh_from_block_height(restore_height);
|
||||
wallet->m_w2->set_seed_language(language);
|
||||
if (http_client_factory == nullptr) wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(config.m_network_type.get()), 1, true));
|
||||
else wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(config.m_network_type.get()), 1, true, std::move(http_client_factory)));
|
||||
if (config.m_account_lookahead != boost::none) wallet->m_w2->set_subaddress_lookahead(config.m_account_lookahead.get(), config.m_subaddress_lookahead.get());
|
||||
if (has_spend_key && has_view_key) wallet->m_w2->generate(config.m_path.get(), config.m_password.get(), address_info.address, spend_key_sk, view_key_sk);
|
||||
else if (has_spend_key) wallet->m_w2->generate(config.m_path.get(), config.m_password.get(), spend_key_sk, true, false);
|
||||
else wallet->m_w2->generate(config.m_path.get(), config.m_password.get(), address_info.address, view_key_sk);
|
||||
wallet->set_daemon_connection(config.get_server());
|
||||
wallet->m_w2->set_refresh_from_block_height(config.m_restore_height.get());
|
||||
wallet->m_w2->set_seed_language(config.m_language.get());
|
||||
wallet->init_common();
|
||||
return wallet;
|
||||
}
|
||||
|
||||
monero_wallet_full* monero_wallet_full::create_wallet_random(const monero_wallet_config& config, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
MTRACE("create_wallet_random(...)");
|
||||
monero_wallet_full* wallet = new monero_wallet_full();
|
||||
if (http_client_factory == nullptr) wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(config.m_network_type.get()), 1, true));
|
||||
else wallet->m_w2 = std::unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(config.m_network_type.get()), 1, true, std::move(http_client_factory)));
|
||||
wallet->set_daemon_connection(config.get_server());
|
||||
wallet->m_w2->set_seed_language(config.m_language.get());
|
||||
crypto::secret_key secret_key;
|
||||
if (config.m_account_lookahead != boost::none) wallet->m_w2->set_subaddress_lookahead(config.m_account_lookahead.get(), config.m_subaddress_lookahead.get());
|
||||
wallet->m_w2->generate(config.m_path.get(), config.m_password.get(), secret_key, false, false);
|
||||
wallet->init_common();
|
||||
if (wallet->is_connected_to_daemon()) wallet->m_w2->set_refresh_from_block_height(wallet->get_daemon_height());
|
||||
return wallet;
|
||||
}
|
||||
|
||||
std::vector<std::string> monero_wallet_full::get_mnemonic_languages() {
|
||||
std::vector<std::string> languages;
|
||||
crypto::ElectrumWords::get_language_list(languages, true);
|
||||
return languages;
|
||||
}
|
||||
|
||||
// ------------------------------- Deprecated -------------------------------
|
||||
|
||||
monero_wallet_full* monero_wallet_full::create_wallet_random(const std::string& path, const std::string& password, const monero_network_type network_type, const monero_rpc_connection& daemon_connection, const std::string& language, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
std::cout << "Warning: monero_wallet_full::create_wallet_random() is deprecated and will be removed soon. Use monero_wallet_full::create_wallet(config) instead" << std::endl;
|
||||
monero_wallet_config config;
|
||||
config.m_path = path;
|
||||
config.m_password = password;
|
||||
config.m_network_type = network_type;
|
||||
config.set_server(daemon_connection);
|
||||
config.m_language = language;
|
||||
return create_wallet(config, std::move(http_client_factory));
|
||||
}
|
||||
|
||||
monero_wallet_full* monero_wallet_full::create_wallet_from_mnemonic(const std::string& path, const std::string& password, const monero_network_type network_type, const std::string& mnemonic, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const std::string& seed_offset, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
std::cout << "Warning: monero_wallet_full::create_wallet_from_mnemonic() is deprecated and will be removed soon. Use monero_wallet_full::create_wallet(config) instead" << std::endl;
|
||||
monero_wallet_config config;
|
||||
config.m_path = path;
|
||||
config.m_password = password;
|
||||
config.m_network_type = network_type;
|
||||
config.m_mnemonic = mnemonic;
|
||||
config.set_server(daemon_connection);
|
||||
config.m_restore_height = restore_height;
|
||||
config.m_seed_offset = seed_offset;
|
||||
return create_wallet(config, std::move(http_client_factory));
|
||||
}
|
||||
|
||||
monero_wallet_full* monero_wallet_full::create_wallet_from_keys(const std::string& path, const std::string& password, const monero_network_type network_type, const std::string& address, const std::string& view_key, const std::string& spend_key, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const std::string& language, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory) {
|
||||
std::cout << "Warning: monero_wallet_full::create_wallet_from_keys() is deprecated and will be removed soon. Use monero_wallet_full::create_wallet(config) instead" << std::endl;
|
||||
monero_wallet_config config;
|
||||
config.m_path = path;
|
||||
config.m_password = password;
|
||||
config.m_network_type = network_type;
|
||||
config.m_primary_address = address;
|
||||
config.m_private_view_key = view_key;
|
||||
config.m_private_spend_key = spend_key;
|
||||
config.set_server(daemon_connection);
|
||||
config.m_restore_height = restore_height;
|
||||
config.m_language = language;
|
||||
return create_wallet(config, std::move(http_client_factory));
|
||||
}
|
||||
|
||||
// ----------------------------- WALLET METHODS -----------------------------
|
||||
|
||||
monero_wallet_full::~monero_wallet_full() {
|
||||
|
||||
@@ -109,6 +109,15 @@ namespace monero {
|
||||
*/
|
||||
static monero_wallet_full* open_wallet_data(const std::string& password, const monero_network_type, const std::string& keys_data, const std::string& cache_data, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory = nullptr);
|
||||
|
||||
/**
|
||||
* Create a new wallet with the given configuration.
|
||||
*
|
||||
* @param config is the wallet configuration
|
||||
* @param http_client_factory allows use of custom http clients
|
||||
* @return a pointer to the wallet instance
|
||||
*/
|
||||
static monero_wallet_full* create_wallet(const monero_wallet_config& config, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory = nullptr);
|
||||
|
||||
/**
|
||||
* Create a new wallet with a randomly generated seed.
|
||||
*
|
||||
@@ -120,6 +129,7 @@ namespace monero {
|
||||
* @param http_client_factory allows use of custom http clients
|
||||
* @return a pointer to the wallet instance
|
||||
*/
|
||||
[[deprecated("monero_wallet_full::create_wallet_random() is deprecated and will be removed soon. Use monero_wallet_full::create_wallet(config) instead")]]
|
||||
static monero_wallet_full* create_wallet_random(const std::string& path, const std::string& password, const monero_network_type network_type, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), const std::string& language = "English", std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory = nullptr);
|
||||
|
||||
/**
|
||||
@@ -134,6 +144,7 @@ namespace monero {
|
||||
* @param http_client_factory allows use of custom http clients
|
||||
* @return a pointer to the wallet instance
|
||||
*/
|
||||
[[deprecated("monero_wallet_full::create_wallet_from_mnemonic() is deprecated and will be removed soon. Use monero_wallet_full::create_wallet(config) instead")]]
|
||||
static monero_wallet_full* create_wallet_from_mnemonic(const std::string& path, const std::string& password, const monero_network_type network_type, const std::string& mnemonic, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), uint64_t restore_height = 0, const std::string& seed_offset = "", std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory = nullptr);
|
||||
|
||||
/**
|
||||
@@ -151,6 +162,7 @@ namespace monero {
|
||||
* @param http_client_factory allows use of custom http clients
|
||||
* @return a pointer to the wallet instance
|
||||
*/
|
||||
[[deprecated("monero_wallet_full::create_wallet_from_keys() is deprecated and will be removed soon. Use monero_wallet_full::create_wallet(config) instead")]]
|
||||
static monero_wallet_full* create_wallet_from_keys(const std::string& path, const std::string& password, const monero_network_type network_type, const std::string& address, const std::string& view_key, const std::string& spend_key, const monero_rpc_connection& daemon_connection = monero_rpc_connection(), uint64_t restore_height = 0, const std::string& language = "English", std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory = nullptr);
|
||||
|
||||
/**
|
||||
@@ -286,7 +298,7 @@ namespace monero {
|
||||
std::string get_keys_file_buffer(const epee::wipeable_string& password, bool view_only) const;
|
||||
std::string get_cache_file_buffer(const epee::wipeable_string& password) const;
|
||||
|
||||
// --------------------------------- PRIVATE --------------------------------
|
||||
// ---------------------------------- PRIVATE ---------------------------------
|
||||
|
||||
private:
|
||||
friend struct wallet2_listener;
|
||||
@@ -294,6 +306,10 @@ namespace monero {
|
||||
std::unique_ptr<wallet2_listener> m_w2_listener; // internal wallet implementation listener
|
||||
std::set<monero_wallet_listener*> m_listeners; // external wallet listeners
|
||||
|
||||
static monero_wallet_full* create_wallet_from_mnemonic(const monero_wallet_config& config, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory);
|
||||
static monero_wallet_full* create_wallet_from_keys(const monero_wallet_config& config, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory);
|
||||
static monero_wallet_full* create_wallet_random(const monero_wallet_config& config, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory);
|
||||
|
||||
void init_common();
|
||||
std::vector<monero_subaddress> get_subaddresses_aux(uint32_t account_idx, const std::vector<uint32_t>& subaddress_indices, const std::vector<tools::wallet2::transfer_details>& transfers) const;
|
||||
std::vector<std::shared_ptr<monero_transfer>> get_transfers_aux(const monero_transfer_query& query) const;
|
||||
|
||||
@@ -90,6 +90,117 @@ namespace monero {
|
||||
return block;
|
||||
}
|
||||
|
||||
// --------------------------- MONERO WALLET CONFIG -----------------------------
|
||||
|
||||
monero_wallet_config::monero_wallet_config(const monero_wallet_config& config) {
|
||||
m_path = config.m_path;
|
||||
m_password = config.m_password;
|
||||
m_network_type = config.m_network_type;
|
||||
m_server_uri = config.m_server_uri;
|
||||
m_server_username = config.m_server_username;
|
||||
m_server_password = config.m_server_password;
|
||||
m_mnemonic = config.m_mnemonic;
|
||||
m_seed_offset = config.m_seed_offset;
|
||||
m_primary_address = config.m_primary_address;
|
||||
m_private_view_key = config.m_private_view_key;
|
||||
m_private_spend_key = config.m_private_spend_key;
|
||||
m_restore_height = config.m_restore_height;
|
||||
m_language = config.m_language;
|
||||
m_save_current = config.m_save_current;
|
||||
m_account_lookahead = config.m_account_lookahead;
|
||||
m_subaddress_lookahead = config.m_subaddress_lookahead;
|
||||
}
|
||||
|
||||
monero_wallet_config monero_wallet_config::copy() const {
|
||||
return monero_wallet_config(*this);
|
||||
}
|
||||
|
||||
rapidjson::Value monero_wallet_config::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const {
|
||||
|
||||
// create root
|
||||
rapidjson::Value root(rapidjson::kObjectType);
|
||||
|
||||
// set num values
|
||||
rapidjson::Value value_num(rapidjson::kNumberType);
|
||||
if (m_network_type != boost::none) monero_utils::add_json_member("networkType", m_network_type.get(), allocator, root, value_num);
|
||||
if (m_restore_height != boost::none) monero_utils::add_json_member("restoreHeight", m_restore_height.get(), allocator, root, value_num);
|
||||
if (m_account_lookahead != boost::none) monero_utils::add_json_member("accountLookahead", m_account_lookahead.get(), allocator, root, value_num);
|
||||
if (m_subaddress_lookahead != boost::none) monero_utils::add_json_member("subaddressLookahead", m_subaddress_lookahead.get(), allocator, root, value_num);
|
||||
|
||||
// set string values
|
||||
rapidjson::Value value_str(rapidjson::kStringType);
|
||||
if (m_path != boost::none) monero_utils::add_json_member("path", m_path.get(), allocator, root, value_str);
|
||||
if (m_password != boost::none) monero_utils::add_json_member("password", m_password.get(), allocator, root, value_str);
|
||||
if (m_server_uri != boost::none) monero_utils::add_json_member("serverUri", m_server_uri.get(), allocator, root, value_str);
|
||||
if (m_server_username != boost::none) monero_utils::add_json_member("serverUsername", m_server_username.get(), allocator, root, value_str);
|
||||
if (m_server_password != boost::none) monero_utils::add_json_member("serverPassword", m_server_password.get(), allocator, root, value_str);
|
||||
if (m_mnemonic != boost::none) monero_utils::add_json_member("mnemonic", m_mnemonic.get(), allocator, root, value_str);
|
||||
if (m_seed_offset != boost::none) monero_utils::add_json_member("seedOffset", m_seed_offset.get(), allocator, root, value_str);
|
||||
if (m_primary_address != boost::none) monero_utils::add_json_member("primaryAddress", m_primary_address.get(), allocator, root, value_str);
|
||||
if (m_private_view_key != boost::none) monero_utils::add_json_member("privateViewKey", m_private_view_key.get(), allocator, root, value_str);
|
||||
if (m_private_spend_key != boost::none) monero_utils::add_json_member("privateSpendKey", m_private_spend_key.get(), allocator, root, value_str);
|
||||
if (m_language != boost::none) monero_utils::add_json_member("language", m_language.get(), allocator, root, value_str);
|
||||
if (m_account_lookahead != boost::none) monero_utils::add_json_member("accountLookahead", m_account_lookahead.get(), allocator, root, value_str);
|
||||
if (m_subaddress_lookahead != boost::none) monero_utils::add_json_member("subaddressLookahead", m_subaddress_lookahead.get(), allocator, root, value_str);
|
||||
|
||||
// set bool values
|
||||
if (m_save_current != boost::none) monero_utils::add_json_member("saveCurrent", m_save_current.get(), allocator, root);
|
||||
|
||||
// return root
|
||||
return root;
|
||||
}
|
||||
|
||||
std::shared_ptr<monero_wallet_config> monero_wallet_config::deserialize(const std::string& config_json) {
|
||||
|
||||
// deserialize config json to property node
|
||||
std::istringstream iss = config_json.empty() ? std::istringstream() : std::istringstream(config_json);
|
||||
boost::property_tree::ptree node;
|
||||
boost::property_tree::read_json(iss, node);
|
||||
|
||||
// convert config property tree to monero_wallet_config
|
||||
std::shared_ptr<monero_wallet_config> config = std::make_shared<monero_wallet_config>();
|
||||
for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) {
|
||||
std::string key = it->first;
|
||||
if (key == std::string("path")) config->m_path = it->second.data();
|
||||
else if (key == std::string("password")) config->m_password = it->second.data();
|
||||
else if (key == std::string("networkType")) {
|
||||
uint32_t network_type_num = it->second.get_value<uint32_t>();
|
||||
if (network_type_num == 0) config->m_network_type = monero_network_type::MAINNET;
|
||||
else if (network_type_num == 1) config->m_network_type = monero_network_type::TESTNET;
|
||||
else if (network_type_num == 2) config->m_network_type = monero_network_type::STAGENET;
|
||||
else throw std::runtime_error("Invalid network type number: " + std::to_string(network_type_num));
|
||||
}
|
||||
else if (key == std::string("serverUri")) config->m_server_uri = it->second.data();
|
||||
else if (key == std::string("serverUsername")) config->m_server_username = it->second.data();
|
||||
else if (key == std::string("serverPassword")) config->m_server_password = it->second.data();
|
||||
else if (key == std::string("mnemonic")) config->m_mnemonic = it->second.data();
|
||||
else if (key == std::string("seedOffset")) config->m_seed_offset = it->second.data();
|
||||
else if (key == std::string("primaryAddress")) config->m_primary_address = it->second.data();
|
||||
else if (key == std::string("privateViewKey")) config->m_private_view_key = it->second.data();
|
||||
else if (key == std::string("privateSpendKey")) config->m_private_spend_key = it->second.data();
|
||||
else if (key == std::string("restoreHeight")) config->m_restore_height = it->second.get_value<uint64_t>();
|
||||
else if (key == std::string("language")) config->m_language = it->second.data();
|
||||
else if (key == std::string("saveCurrent")) config->m_save_current = it->second.get_value<bool>();
|
||||
else if (key == std::string("accountLookahead")) config->m_account_lookahead = it->second.get_value<uint64_t>();
|
||||
else if (key == std::string("subaddressLookahead")) config->m_subaddress_lookahead = it->second.get_value<uint64_t>();
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
void monero_wallet_config::set_server(const monero_rpc_connection& server) {
|
||||
m_server_uri = server.m_uri;
|
||||
m_server_username = server.m_username;
|
||||
m_server_password = server.m_password;
|
||||
}
|
||||
|
||||
monero_rpc_connection monero_wallet_config::get_server() const {
|
||||
return monero_rpc_connection(
|
||||
std::string(m_server_uri == boost::none ? "" : m_server_uri.get()),
|
||||
std::string(m_server_username == boost::none ? "" : m_server_username.get()),
|
||||
std::string(m_server_password == boost::none ? "" : m_server_password.get()));
|
||||
}
|
||||
|
||||
// -------------------------- MONERO SYNC RESULT ----------------------------
|
||||
|
||||
rapidjson::Value monero_sync_result::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const {
|
||||
|
||||
@@ -61,6 +61,36 @@ using namespace monero;
|
||||
*/
|
||||
namespace monero {
|
||||
|
||||
/**
|
||||
* Configures a wallet to create.
|
||||
*/
|
||||
struct monero_wallet_config : public serializable_struct {
|
||||
boost::optional<std::string> m_path;
|
||||
boost::optional<std::string> m_password;
|
||||
boost::optional<monero_network_type> m_network_type;
|
||||
boost::optional<std::string> m_server_uri;
|
||||
boost::optional<std::string> m_server_username;
|
||||
boost::optional<std::string> m_server_password;
|
||||
boost::optional<std::string> m_mnemonic;
|
||||
boost::optional<std::string> m_seed_offset;
|
||||
boost::optional<std::string> m_primary_address;
|
||||
boost::optional<std::string> m_private_view_key;
|
||||
boost::optional<std::string> m_private_spend_key;
|
||||
boost::optional<uint64_t> m_restore_height;
|
||||
boost::optional<std::string> m_language;
|
||||
boost::optional<bool> m_save_current;
|
||||
boost::optional<uint64_t> m_account_lookahead;
|
||||
boost::optional<uint64_t> m_subaddress_lookahead;
|
||||
|
||||
monero_wallet_config() {}
|
||||
monero_wallet_config(const monero_wallet_config& config);
|
||||
monero_wallet_config copy() const;
|
||||
rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const;
|
||||
static std::shared_ptr<monero_wallet_config> deserialize(const std::string& config_json);
|
||||
void set_server(const monero_rpc_connection& server);
|
||||
monero_rpc_connection get_server() const;
|
||||
};
|
||||
|
||||
/**
|
||||
* Models a result of syncing a wallet.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user