wallet2: allow empty wallet filename to avoid saving data

Useful to speed tests up and avoid unnecessary leftover files
This commit is contained in:
moneromooo-monero 2017-06-30 12:12:28 +01:00
parent b84b3565f3
commit 95a21a793d
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
2 changed files with 58 additions and 40 deletions

View File

@ -2594,9 +2594,12 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const epee::wip
clear(); clear();
prepare_file_names(wallet_); prepare_file_names(wallet_);
if (!wallet_.empty())
{
boost::system::error_code ignored_ec; boost::system::error_code ignored_ec;
THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, m_wallet_file); THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, m_wallet_file);
THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_keys_file, ignored_ec), error::file_exists, m_keys_file); THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_keys_file, ignored_ec), error::file_exists, m_keys_file);
}
crypto::secret_key retval = m_account.generate(recovery_param, recover, two_random); crypto::secret_key retval = m_account.generate(recovery_param, recover, two_random);
@ -2618,18 +2621,23 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const epee::wip
m_refresh_from_block_height = height >= blocks_per_month ? height - blocks_per_month : 0; m_refresh_from_block_height = height >= blocks_per_month ? height - blocks_per_month : 0;
} }
if (!wallet_.empty())
{
bool r = store_keys(m_keys_file, password, false); bool r = store_keys(m_keys_file, password, false);
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file); THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file);
r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet)); r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet));
if(!r) MERROR("String with address text not saved"); if(!r) MERROR("String with address text not saved");
}
cryptonote::block b; cryptonote::block b;
generate_genesis(b); generate_genesis(b);
m_blockchain.push_back(get_block_hash(b)); m_blockchain.push_back(get_block_hash(b));
add_subaddress_account(tr("Primary account")); add_subaddress_account(tr("Primary account"));
if (!wallet_.empty())
store(); store();
return retval; return retval;
} }
@ -2678,9 +2686,12 @@ void wallet2::generate(const std::string& wallet_, const epee::wipeable_string&
clear(); clear();
prepare_file_names(wallet_); prepare_file_names(wallet_);
if (!wallet_.empty())
{
boost::system::error_code ignored_ec; boost::system::error_code ignored_ec;
THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, m_wallet_file); THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, m_wallet_file);
THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_keys_file, ignored_ec), error::file_exists, m_keys_file); THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_keys_file, ignored_ec), error::file_exists, m_keys_file);
}
m_account.create_from_viewkey(account_public_address, viewkey); m_account.create_from_viewkey(account_public_address, viewkey);
m_account_public_address = account_public_address; m_account_public_address = account_public_address;
@ -2689,17 +2700,21 @@ void wallet2::generate(const std::string& wallet_, const epee::wipeable_string&
m_multisig_threshold = 0; m_multisig_threshold = 0;
m_multisig_total = 0; m_multisig_total = 0;
if (!wallet_.empty())
{
bool r = store_keys(m_keys_file, password, true); bool r = store_keys(m_keys_file, password, true);
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file); THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file);
r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet)); r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet));
if(!r) MERROR("String with address text not saved"); if(!r) MERROR("String with address text not saved");
}
cryptonote::block b; cryptonote::block b;
generate_genesis(b); generate_genesis(b);
m_blockchain.push_back(get_block_hash(b)); m_blockchain.push_back(get_block_hash(b));
add_subaddress_account(tr("Primary account")); add_subaddress_account(tr("Primary account"));
if (!wallet_.empty())
store(); store();
} }
@ -2717,9 +2732,12 @@ void wallet2::generate(const std::string& wallet_, const epee::wipeable_string&
clear(); clear();
prepare_file_names(wallet_); prepare_file_names(wallet_);
if (!wallet_.empty())
{
boost::system::error_code ignored_ec; boost::system::error_code ignored_ec;
THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, m_wallet_file); THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, m_wallet_file);
THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_keys_file, ignored_ec), error::file_exists, m_keys_file); THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_keys_file, ignored_ec), error::file_exists, m_keys_file);
}
m_account.create_from_keys(account_public_address, spendkey, viewkey); m_account.create_from_keys(account_public_address, spendkey, viewkey);
m_account_public_address = account_public_address; m_account_public_address = account_public_address;
@ -2728,16 +2746,20 @@ void wallet2::generate(const std::string& wallet_, const epee::wipeable_string&
m_multisig_threshold = 0; m_multisig_threshold = 0;
m_multisig_total = 0; m_multisig_total = 0;
if (!wallet_.empty())
{
bool r = store_keys(m_keys_file, password, false); bool r = store_keys(m_keys_file, password, false);
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file); THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file);
r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet)); r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet));
if(!r) MERROR("String with address text not saved"); if(!r) MERROR("String with address text not saved");
}
cryptonote::block b; cryptonote::block b;
generate_genesis(b); generate_genesis(b);
m_blockchain.push_back(get_block_hash(b)); m_blockchain.push_back(get_block_hash(b));
if (!wallet_.empty())
store(); store();
} }
@ -2789,17 +2811,21 @@ void wallet2::make_multisig(const epee::wipeable_string &password,
m_multisig_threshold = threshold; m_multisig_threshold = threshold;
m_multisig_total = spend_keys.size() + 1; m_multisig_total = spend_keys.size() + 1;
if (!m_wallet_file.empty())
{
bool r = store_keys(m_keys_file, password, false); bool r = store_keys(m_keys_file, password, false);
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file); THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file);
r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet)); r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet));
if(!r) MERROR("String with address text not saved"); if(!r) MERROR("String with address text not saved");
}
cryptonote::block b; cryptonote::block b;
generate_genesis(b); generate_genesis(b);
m_blockchain.push_back(get_block_hash(b)); m_blockchain.push_back(get_block_hash(b));
add_subaddress_account(tr("Primary account")); add_subaddress_account(tr("Primary account"));
if (!m_wallet_file.empty())
store(); store();
} }
@ -2889,6 +2915,8 @@ bool wallet2::has_multisig_partial_key_images() const
*/ */
void wallet2::rewrite(const std::string& wallet_name, const epee::wipeable_string& password) void wallet2::rewrite(const std::string& wallet_name, const epee::wipeable_string& password)
{ {
if (wallet_name.empty())
return;
prepare_file_names(wallet_name); prepare_file_names(wallet_name);
boost::system::error_code ignored_ec; boost::system::error_code ignored_ec;
THROW_WALLET_EXCEPTION_IF(!boost::filesystem::exists(m_keys_file, ignored_ec), error::file_not_found, m_keys_file); THROW_WALLET_EXCEPTION_IF(!boost::filesystem::exists(m_keys_file, ignored_ec), error::file_not_found, m_keys_file);

View File

@ -59,20 +59,10 @@ static void make_wallet(unsigned int idx, tools::wallet2 &wallet)
crypto::secret_key spendkey; crypto::secret_key spendkey;
epee::string_tools::hex_to_pod(test_addresses[idx].spendkey, spendkey); epee::string_tools::hex_to_pod(test_addresses[idx].spendkey, spendkey);
std::string basename = "/tmp/multisig-unit-test-" + std::to_string(idx);
try try
{ {
boost::filesystem::remove(basename + ".keys");
boost::filesystem::remove(basename + ".address.txt");
boost::filesystem::remove(basename);
wallet.init(""); wallet.init("");
wallet.generate(basename, "", spendkey, true, false); wallet.generate("", "", spendkey, true, false);
boost::filesystem::remove(basename + ".keys");
boost::filesystem::remove(basename + ".address.txt");
boost::filesystem::remove(basename);
ASSERT_TRUE(test_addresses[idx].address == wallet.get_account().get_public_address_str(true)); ASSERT_TRUE(test_addresses[idx].address == wallet.get_account().get_public_address_str(true));
} }
catch (const std::exception &e) catch (const std::exception &e)