wallet: rejig to avoid prompting in wallet2

wallet2 is a library, and should not prompt for stdin. Instead,
pass a function so simplewallet can prompt on stdin, and a GUI
might display a window, etc.
This commit is contained in:
moneromooo-monero 2017-10-28 19:13:42 +01:00
parent ec5135e5b7
commit e89994e98f
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
8 changed files with 97 additions and 78 deletions

View file

@ -238,9 +238,6 @@ namespace tools
boost::optional<password_container> password_container::prompt(const bool verify, const char *message) boost::optional<password_container> password_container::prompt(const bool verify, const char *message)
{ {
#ifdef HAVE_READLINE
rdln::suspend_readline pause_readline;
#endif
password_container pass1{}; password_container pass1{};
password_container pass2{}; password_container pass2{};
if (is_cin_tty() ? read_from_tty(verify, message, pass1.m_password, pass2.m_password) : read_from_file(pass1.m_password)) if (is_cin_tty() ? read_from_tty(verify, message, pass1.m_password, pass2.m_password) : read_from_file(pass1.m_password))
@ -249,7 +246,7 @@ namespace tools
return boost::none; return boost::none;
} }
boost::optional<login> login::parse(std::string&& userpass, bool verify, const char* message) boost::optional<login> login::parse(std::string&& userpass, bool verify, const std::function<boost::optional<password_container>(bool)> &prompt)
{ {
login out{}; login out{};
password_container wipe{std::move(userpass)}; password_container wipe{std::move(userpass)};
@ -257,7 +254,7 @@ namespace tools
const auto loc = wipe.password().find(':'); const auto loc = wipe.password().find(':');
if (loc == std::string::npos) if (loc == std::string::npos)
{ {
auto result = tools::password_container::prompt(verify, message); auto result = prompt(verify);
if (!result) if (!result)
return boost::none; return boost::none;

View file

@ -82,7 +82,7 @@ namespace tools
\return The username and password, or boost::none if \return The username and password, or boost::none if
`password_container::prompt` fails. `password_container::prompt` fails.
*/ */
static boost::optional<login> parse(std::string&& userpass, bool verify, const char* message = "Password"); static boost::optional<login> parse(std::string&& userpass, bool verify, const std::function<boost::optional<password_container>(bool)> &prompt);
login(const login&) = delete; login(const login&) = delete;
login(login&&) = default; login(login&&) = default;

View file

@ -247,7 +247,12 @@ int main(int argc, char const * argv[])
if (command_line::has_arg(vm, arg.rpc_login)) if (command_line::has_arg(vm, arg.rpc_login))
{ {
login = tools::login::parse( login = tools::login::parse(
command_line::get_arg(vm, arg.rpc_login), false, "Daemon client password" command_line::get_arg(vm, arg.rpc_login), false, [](bool verify) {
#ifdef HAVE_READLINE
rdln::suspend_readline pause_readline;
#endif
return tools::password_container::prompt(verify, "Daemon client password");
}
); );
if (!login) if (!login)
{ {

View file

@ -83,7 +83,9 @@ namespace cryptonote
if (command_line::has_arg(vm, arg.rpc_login)) if (command_line::has_arg(vm, arg.rpc_login))
{ {
config.login = tools::login::parse(command_line::get_arg(vm, arg.rpc_login), true, "RPC server password"); config.login = tools::login::parse(command_line::get_arg(vm, arg.rpc_login), true, [](bool verify) {
return tools::password_container::prompt(verify, "RPC server password");
});
if (!config.login) if (!config.login)
return boost::none; return boost::none;

View file

@ -138,6 +138,24 @@ namespace
return epee::string_tools::trim(buf); return epee::string_tools::trim(buf);
} }
boost::optional<tools::password_container> password_prompter(const char *prompt, bool verify)
{
#ifdef HAVE_READLINE
rdln::suspend_readline pause_readline;
#endif
auto pwd_container = tools::password_container::prompt(verify, prompt);
if (!pwd_container)
{
tools::fail_msg_writer() << tr("failed to read wallet password");
}
return pwd_container;
}
boost::optional<tools::password_container> default_password_prompter(bool verify)
{
return password_prompter(verify ? tr("Enter new wallet password") : tr("Wallet password"), verify);
}
inline std::string interpret_rpc_response(bool ok, const std::string& status) inline std::string interpret_rpc_response(bool ok, const std::string& status)
{ {
std::string err; std::string err;
@ -283,7 +301,7 @@ namespace
<< tr("Is this OK? (Y/n) ") << tr("Is this OK? (Y/n) ")
; ;
// prompt the user for confirmation given the dns query and dnssec status // prompt the user for confirmation given the dns query and dnssec status
std::string confirm_dns_ok = command_line::input_line(prompt.str()); std::string confirm_dns_ok = input_line(prompt.str());
if (std::cin.eof()) if (std::cin.eof())
{ {
return {}; return {};
@ -461,7 +479,7 @@ bool simple_wallet::change_password(const std::vector<std::string> &args)
} }
// prompts for a new password, pass true to verify the password // prompts for a new password, pass true to verify the password
const auto pwd_container = tools::wallet2::password_prompt(true); const auto pwd_container = default_password_prompter(true);
try try
{ {
@ -1033,7 +1051,7 @@ bool simple_wallet::ask_wallet_create_if_needed()
do{ do{
LOG_PRINT_L3("User asked to specify wallet file name."); LOG_PRINT_L3("User asked to specify wallet file name.");
wallet_path = command_line::input_line( wallet_path = input_line(
tr(m_restoring ? "Specify a new wallet file name for your restored wallet (e.g., MyWallet).\n" tr(m_restoring ? "Specify a new wallet file name for your restored wallet (e.g., MyWallet).\n"
"Wallet file name (or Ctrl-C to quit): " : "Wallet file name (or Ctrl-C to quit): " :
"Specify wallet file name (e.g., MyWallet). If the wallet doesn't exist, it will be created.\n" "Specify wallet file name (e.g., MyWallet). If the wallet doesn't exist, it will be created.\n"
@ -1084,7 +1102,7 @@ bool simple_wallet::ask_wallet_create_if_needed()
if (!m_restoring) if (!m_restoring)
{ {
message_writer() << tr("No wallet found with that name. Confirm creation of new wallet named: ") << wallet_path; message_writer() << tr("No wallet found with that name. Confirm creation of new wallet named: ") << wallet_path;
confirm_creation = command_line::input_line(tr("(Y/Yes/N/No): ")); confirm_creation = input_line(tr("(Y/Yes/N/No): "));
if(std::cin.eof()) if(std::cin.eof())
{ {
LOG_ERROR("Unexpected std::cin.eof() - Exited simple_wallet::ask_wallet_create_if_needed()"); LOG_ERROR("Unexpected std::cin.eof() - Exited simple_wallet::ask_wallet_create_if_needed()");
@ -1168,7 +1186,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
do do
{ {
const char *prompt = m_electrum_seed.empty() ? "Specify Electrum seed: " : "Electrum seed continued: "; const char *prompt = m_electrum_seed.empty() ? "Specify Electrum seed: " : "Electrum seed continued: ";
std::string electrum_seed = command_line::input_line(prompt); std::string electrum_seed = input_line(prompt);
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (electrum_seed.empty()) if (electrum_seed.empty())
@ -1197,7 +1215,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
{ {
m_wallet_file = m_generate_from_view_key; m_wallet_file = m_generate_from_view_key;
// parse address // parse address
std::string address_string = command_line::input_line("Standard address: "); std::string address_string = input_line("Standard address: ");
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (address_string.empty()) { if (address_string.empty()) {
@ -1217,7 +1235,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
} }
// parse view secret key // parse view secret key
std::string viewkey_string = command_line::input_line("View key: "); std::string viewkey_string = input_line("View key: ");
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (viewkey_string.empty()) { if (viewkey_string.empty()) {
@ -1271,7 +1289,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
{ {
m_wallet_file = m_generate_from_keys; m_wallet_file = m_generate_from_keys;
// parse address // parse address
std::string address_string = command_line::input_line("Standard address: "); std::string address_string = input_line("Standard address: ");
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (address_string.empty()) { if (address_string.empty()) {
@ -1291,7 +1309,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
} }
// parse spend secret key // parse spend secret key
std::string spendkey_string = command_line::input_line("Secret spend key: "); std::string spendkey_string = input_line("Secret spend key: ");
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (spendkey_string.empty()) { if (spendkey_string.empty()) {
@ -1307,7 +1325,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
crypto::secret_key spendkey = *reinterpret_cast<const crypto::secret_key*>(spendkey_data.data()); crypto::secret_key spendkey = *reinterpret_cast<const crypto::secret_key*>(spendkey_data.data());
// parse view secret key // parse view secret key
std::string viewkey_string = command_line::input_line("Secret view key: "); std::string viewkey_string = input_line("Secret view key: ");
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (viewkey_string.empty()) { if (viewkey_string.empty()) {
@ -1354,7 +1372,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
unsigned int multisig_n; unsigned int multisig_n;
// parse multisig type // parse multisig type
std::string multisig_type_string = command_line::input_line("Multisig type (input as M/N with M <= N and M > 1): "); std::string multisig_type_string = input_line("Multisig type (input as M/N with M <= N and M > 1): ");
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (multisig_type_string.empty()) if (multisig_type_string.empty())
@ -1380,7 +1398,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
message_writer() << boost::format(tr("Generating master wallet from %u of %u multisig wallet keys")) % multisig_m % multisig_n; message_writer() << boost::format(tr("Generating master wallet from %u of %u multisig wallet keys")) % multisig_m % multisig_n;
// parse multisig address // parse multisig address
std::string address_string = command_line::input_line("Multisig wallet address: "); std::string address_string = input_line("Multisig wallet address: ");
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (address_string.empty()) { if (address_string.empty()) {
@ -1395,7 +1413,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
} }
// parse secret view key // parse secret view key
std::string viewkey_string = command_line::input_line("Secret view key: "); std::string viewkey_string = input_line("Secret view key: ");
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (viewkey_string.empty()) if (viewkey_string.empty())
@ -1435,7 +1453,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
// get N secret spend keys from user // get N secret spend keys from user
for(unsigned int i=0; i<multisig_n; ++i) for(unsigned int i=0; i<multisig_n; ++i)
{ {
spendkey_string = command_line::input_line(tr((boost::format(tr("Secret spend key (%u of %u):")) % (i+i) % multisig_m).str().c_str())); spendkey_string = input_line(tr((boost::format(tr("Secret spend key (%u of %u):")) % (i+i) % multisig_m).str().c_str()));
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (spendkey_string.empty()) if (spendkey_string.empty())
@ -1483,7 +1501,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
else if (!m_generate_from_json.empty()) else if (!m_generate_from_json.empty())
{ {
m_wallet_file = m_generate_from_json; m_wallet_file = m_generate_from_json;
m_wallet = tools::wallet2::make_from_json(vm, m_wallet_file); m_wallet = tools::wallet2::make_from_json(vm, m_wallet_file, password_prompter);
if (!m_wallet) if (!m_wallet)
return false; return false;
} }
@ -1505,9 +1523,9 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
{ {
std::string heightstr; std::string heightstr;
if (!connected || version < MAKE_CORE_RPC_VERSION(1, 6)) if (!connected || version < MAKE_CORE_RPC_VERSION(1, 6))
heightstr = command_line::input_line("Restore from specific blockchain height (optional, default 0): "); heightstr = input_line("Restore from specific blockchain height (optional, default 0): ");
else else
heightstr = command_line::input_line("Restore from specific blockchain height (optional, default 0),\nor alternatively from specific date (YYYY-MM-DD): "); heightstr = input_line("Restore from specific blockchain height (optional, default 0),\nor alternatively from specific date (YYYY-MM-DD): ");
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if (heightstr.empty()) if (heightstr.empty())
@ -1543,7 +1561,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
day = boost::lexical_cast<uint16_t>(heightstr.substr(8,2)); day = boost::lexical_cast<uint16_t>(heightstr.substr(8,2));
m_restore_height = m_wallet->get_blockchain_height_by_date(year, month, day); m_restore_height = m_wallet->get_blockchain_height_by_date(year, month, day);
success_msg_writer() << tr("Restore height is: ") << m_restore_height; success_msg_writer() << tr("Restore height is: ") << m_restore_height;
std::string confirm = command_line::input_line(tr("Is this okay? (Y/Yes/N/No): ")); std::string confirm = input_line(tr("Is this okay? (Y/Yes/N/No): "));
if (std::cin.eof()) if (std::cin.eof())
return false; return false;
if(command_line::is_yes(confirm)) if(command_line::is_yes(confirm))
@ -1674,7 +1692,7 @@ std::string simple_wallet::get_mnemonic_language()
} }
while (language_number < 0) while (language_number < 0)
{ {
language_choice = command_line::input_line(tr("Enter the number corresponding to the language of your choice: ")); language_choice = input_line(tr("Enter the number corresponding to the language of your choice: "));
if (std::cin.eof()) if (std::cin.eof())
return std::string(); return std::string();
try try
@ -1696,7 +1714,7 @@ std::string simple_wallet::get_mnemonic_language()
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
boost::optional<tools::password_container> simple_wallet::get_and_verify_password() const boost::optional<tools::password_container> simple_wallet::get_and_verify_password() const
{ {
auto pwd_container = tools::wallet2::password_prompt(m_wallet_file.empty()); auto pwd_container = default_password_prompter(m_wallet_file.empty());
if (!pwd_container) if (!pwd_container)
return boost::none; return boost::none;
@ -1711,7 +1729,7 @@ boost::optional<tools::password_container> simple_wallet::get_and_verify_passwor
bool simple_wallet::new_wallet(const boost::program_options::variables_map& vm, bool simple_wallet::new_wallet(const boost::program_options::variables_map& vm,
const crypto::secret_key& recovery_key, bool recover, bool two_random, const std::string &old_language) const crypto::secret_key& recovery_key, bool recover, bool two_random, const std::string &old_language)
{ {
auto rc = tools::wallet2::make_new(vm); auto rc = tools::wallet2::make_new(vm, password_prompter);
m_wallet = std::move(rc.first); m_wallet = std::move(rc.first);
if (!m_wallet) if (!m_wallet)
{ {
@ -1792,7 +1810,7 @@ bool simple_wallet::new_wallet(const boost::program_options::variables_map& vm,
const cryptonote::account_public_address& address, const boost::optional<crypto::secret_key>& spendkey, const cryptonote::account_public_address& address, const boost::optional<crypto::secret_key>& spendkey,
const crypto::secret_key& viewkey) const crypto::secret_key& viewkey)
{ {
auto rc = tools::wallet2::make_new(vm); auto rc = tools::wallet2::make_new(vm, password_prompter);
m_wallet = std::move(rc.first); m_wallet = std::move(rc.first);
if (!m_wallet) if (!m_wallet)
{ {
@ -1834,7 +1852,7 @@ bool simple_wallet::open_wallet(const boost::program_options::variables_map& vm)
std::string password; std::string password;
try try
{ {
auto rc = tools::wallet2::make_from_file(vm, m_wallet_file); auto rc = tools::wallet2::make_from_file(vm, m_wallet_file, password_prompter);
m_wallet = std::move(rc.first); m_wallet = std::move(rc.first);
password = std::move(rc.second).password(); password = std::move(rc.second).password();
if (!m_wallet) if (!m_wallet)
@ -2724,7 +2742,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
// prompt is there is no payment id and confirmation is required // prompt is there is no payment id and confirmation is required
if (!payment_id_seen && m_wallet->confirm_missing_payment_id()) if (!payment_id_seen && m_wallet->confirm_missing_payment_id())
{ {
std::string accepted = command_line::input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No): ")); std::string accepted = input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No): "));
if (std::cin.eof()) if (std::cin.eof())
return true; return true;
if (!command_line::is_yes(accepted)) if (!command_line::is_yes(accepted))
@ -2808,7 +2826,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
std::string prompt_str = prompt.str(); std::string prompt_str = prompt.str();
if (!prompt_str.empty()) if (!prompt_str.empty())
{ {
std::string accepted = command_line::input_line(prompt_str); std::string accepted = input_line(prompt_str);
if (std::cin.eof()) if (std::cin.eof())
return true; return true;
if (!command_line::is_yes(accepted)) if (!command_line::is_yes(accepted))
@ -2879,7 +2897,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
} }
prompt << ENDL << tr("Is this okay? (Y/Yes/N/No): "); prompt << ENDL << tr("Is this okay? (Y/Yes/N/No): ");
std::string accepted = command_line::input_line(prompt.str()); std::string accepted = input_line(prompt.str());
if (std::cin.eof()) if (std::cin.eof())
return true; return true;
if (!command_line::is_yes(accepted)) if (!command_line::is_yes(accepted))
@ -3059,7 +3077,7 @@ bool simple_wallet::sweep_unmixable(const std::vector<std::string> &args_)
print_money(total_unmixable) % print_money(total_unmixable) %
print_money(total_fee)).str(); print_money(total_fee)).str();
} }
std::string accepted = command_line::input_line(prompt_str); std::string accepted = input_line(prompt_str);
if (std::cin.eof()) if (std::cin.eof())
return true; return true;
if (!command_line::is_yes(accepted)) if (!command_line::is_yes(accepted))
@ -3303,7 +3321,7 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a
// prompt is there is no payment id and confirmation is required // prompt is there is no payment id and confirmation is required
if (!payment_id_seen && m_wallet->confirm_missing_payment_id()) if (!payment_id_seen && m_wallet->confirm_missing_payment_id())
{ {
std::string accepted = command_line::input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No): ")); std::string accepted = input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No): "));
if (std::cin.eof()) if (std::cin.eof())
return true; return true;
if (!command_line::is_yes(accepted)) if (!command_line::is_yes(accepted))
@ -3361,7 +3379,7 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a
print_money(total_sent) % print_money(total_sent) %
print_money(total_fee); print_money(total_fee);
} }
std::string accepted = command_line::input_line(prompt.str()); std::string accepted = input_line(prompt.str());
if (std::cin.eof()) if (std::cin.eof())
return true; return true;
if (!command_line::is_yes(accepted)) if (!command_line::is_yes(accepted))
@ -3657,7 +3675,7 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes,
uint64_t fee = amount - amount_to_dests; uint64_t fee = amount - amount_to_dests;
std::string prompt_str = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min ring size %lu, %s. %sIs this okay? (Y/Yes/N/No): ")) % (unsigned long)get_num_txes() % print_money(amount) % print_money(fee) % dest_string % change_string % (unsigned long)min_ring_size % payment_id_string % extra_message).str(); std::string prompt_str = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min ring size %lu, %s. %sIs this okay? (Y/Yes/N/No): ")) % (unsigned long)get_num_txes() % print_money(amount) % print_money(fee) % dest_string % change_string % (unsigned long)min_ring_size % payment_id_string % extra_message).str();
return command_line::is_yes(command_line::input_line(prompt_str)); return command_line::is_yes(input_line(prompt_str));
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
bool simple_wallet::accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs) bool simple_wallet::accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs)

View file

@ -135,7 +135,7 @@ uint64_t calculate_fee(uint64_t fee_per_kb, const cryptonote::blobdata &blob, ui
return calculate_fee(fee_per_kb, blob.size(), fee_multiplier); return calculate_fee(fee_per_kb, blob.size(), fee_multiplier);
} }
std::unique_ptr<tools::wallet2> make_basic(const boost::program_options::variables_map& vm, const options& opts) std::unique_ptr<tools::wallet2> make_basic(const boost::program_options::variables_map& vm, const options& opts, const std::function<boost::optional<tools::password_container>(const char *, bool)> &password_prompter)
{ {
const bool testnet = command_line::get_arg(vm, opts.testnet); const bool testnet = command_line::get_arg(vm, opts.testnet);
const bool restricted = command_line::get_arg(vm, opts.restricted); const bool restricted = command_line::get_arg(vm, opts.restricted);
@ -154,7 +154,9 @@ std::unique_ptr<tools::wallet2> make_basic(const boost::program_options::variabl
if (command_line::has_arg(vm, opts.daemon_login)) if (command_line::has_arg(vm, opts.daemon_login))
{ {
auto parsed = tools::login::parse( auto parsed = tools::login::parse(
command_line::get_arg(vm, opts.daemon_login), false, "Daemon client password" command_line::get_arg(vm, opts.daemon_login), false, [password_prompter](bool verify) {
return password_prompter("Daemon client password", verify);
}
); );
if (!parsed) if (!parsed)
return nullptr; return nullptr;
@ -178,7 +180,7 @@ std::unique_ptr<tools::wallet2> make_basic(const boost::program_options::variabl
return wallet; return wallet;
} }
boost::optional<tools::password_container> get_password(const boost::program_options::variables_map& vm, const options& opts, const bool verify) boost::optional<tools::password_container> get_password(const boost::program_options::variables_map& vm, const options& opts, const std::function<boost::optional<tools::password_container>(const char*, bool)> &password_prompter, const bool verify)
{ {
if (command_line::has_arg(vm, opts.password) && command_line::has_arg(vm, opts.password_file)) if (command_line::has_arg(vm, opts.password) && command_line::has_arg(vm, opts.password_file))
{ {
@ -207,10 +209,10 @@ boost::optional<tools::password_container> get_password(const boost::program_opt
return {tools::password_container{std::move(password)}}; return {tools::password_container{std::move(password)}};
} }
return tools::wallet2::password_prompt(verify); return password_prompter(verify ? tr("Enter new wallet password") : tr("Wallet password"), verify);
} }
std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, const boost::program_options::variables_map& vm, const options& opts) std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file, const boost::program_options::variables_map& vm, const options& opts, const std::function<boost::optional<tools::password_container>(const char *, bool)> &password_prompter)
{ {
const bool testnet = command_line::get_arg(vm, opts.testnet); const bool testnet = command_line::get_arg(vm, opts.testnet);
@ -359,7 +361,7 @@ std::unique_ptr<tools::wallet2> generate_from_json(const std::string& json_file,
return false; return false;
} }
wallet.reset(make_basic(vm, opts).release()); wallet.reset(make_basic(vm, opts, password_prompter).release());
wallet->set_refresh_from_block_height(field_scan_from_height); wallet->set_refresh_from_block_height(field_scan_from_height);
try try
@ -496,34 +498,22 @@ void wallet2::init_options(boost::program_options::options_description& desc_par
command_line::add_arg(desc_params, opts.restricted); command_line::add_arg(desc_params, opts.restricted);
} }
boost::optional<password_container> wallet2::password_prompt(const bool new_password) std::unique_ptr<wallet2> wallet2::make_from_json(const boost::program_options::variables_map& vm, const std::string& json_file, const std::function<boost::optional<tools::password_container>(const char *, bool)> &password_prompter)
{
auto pwd_container = tools::password_container::prompt(
new_password, (new_password ? tr("Enter new wallet password") : tr("Wallet password"))
);
if (!pwd_container)
{
tools::fail_msg_writer() << tr("failed to read wallet password");
}
return pwd_container;
}
std::unique_ptr<wallet2> wallet2::make_from_json(const boost::program_options::variables_map& vm, const std::string& json_file)
{ {
const options opts{}; const options opts{};
return generate_from_json(json_file, vm, opts); return generate_from_json(json_file, vm, opts, password_prompter);
} }
std::pair<std::unique_ptr<wallet2>, password_container> wallet2::make_from_file( std::pair<std::unique_ptr<wallet2>, password_container> wallet2::make_from_file(
const boost::program_options::variables_map& vm, const std::string& wallet_file) const boost::program_options::variables_map& vm, const std::string& wallet_file, const std::function<boost::optional<tools::password_container>(const char *, bool)> &password_prompter)
{ {
const options opts{}; const options opts{};
auto pwd = get_password(vm, opts, false); auto pwd = get_password(vm, opts, password_prompter, false);
if (!pwd) if (!pwd)
{ {
return {nullptr, password_container{}}; return {nullptr, password_container{}};
} }
auto wallet = make_basic(vm, opts); auto wallet = make_basic(vm, opts, password_prompter);
if (wallet) if (wallet)
{ {
wallet->load(wallet_file, pwd->password()); wallet->load(wallet_file, pwd->password());
@ -531,21 +521,21 @@ std::pair<std::unique_ptr<wallet2>, password_container> wallet2::make_from_file(
return {std::move(wallet), std::move(*pwd)}; return {std::move(wallet), std::move(*pwd)};
} }
std::pair<std::unique_ptr<wallet2>, password_container> wallet2::make_new(const boost::program_options::variables_map& vm) std::pair<std::unique_ptr<wallet2>, password_container> wallet2::make_new(const boost::program_options::variables_map& vm, const std::function<boost::optional<password_container>(const char *, bool)> &password_prompter)
{ {
const options opts{}; const options opts{};
auto pwd = get_password(vm, opts, true); auto pwd = get_password(vm, opts, password_prompter, true);
if (!pwd) if (!pwd)
{ {
return {nullptr, password_container{}}; return {nullptr, password_container{}};
} }
return {make_basic(vm, opts), std::move(*pwd)}; return {make_basic(vm, opts, password_prompter), std::move(*pwd)};
} }
std::unique_ptr<wallet2> wallet2::make_dummy(const boost::program_options::variables_map& vm) std::unique_ptr<wallet2> wallet2::make_dummy(const boost::program_options::variables_map& vm, const std::function<boost::optional<tools::password_container>(const char *, bool)> &password_prompter)
{ {
const options opts{}; const options opts{};
return make_basic(vm, opts); return make_basic(vm, opts, password_prompter);
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------

View file

@ -155,21 +155,18 @@ namespace tools
static bool has_testnet_option(const boost::program_options::variables_map& vm); static bool has_testnet_option(const boost::program_options::variables_map& vm);
static void init_options(boost::program_options::options_description& desc_params); static void init_options(boost::program_options::options_description& desc_params);
//! \return Password retrieved from prompt. Logs error on failure.
static boost::optional<password_container> password_prompt(const bool new_password);
//! Uses stdin and stdout. Returns a wallet2 if no errors. //! Uses stdin and stdout. Returns a wallet2 if no errors.
static std::unique_ptr<wallet2> make_from_json(const boost::program_options::variables_map& vm, const std::string& json_file); static std::unique_ptr<wallet2> make_from_json(const boost::program_options::variables_map& vm, const std::string& json_file, const std::function<boost::optional<password_container>(const char *, bool)> &password_prompter);
//! Uses stdin and stdout. Returns a wallet2 and password for `wallet_file` if no errors. //! Uses stdin and stdout. Returns a wallet2 and password for `wallet_file` if no errors.
static std::pair<std::unique_ptr<wallet2>, password_container> static std::pair<std::unique_ptr<wallet2>, password_container>
make_from_file(const boost::program_options::variables_map& vm, const std::string& wallet_file); make_from_file(const boost::program_options::variables_map& vm, const std::string& wallet_file, const std::function<boost::optional<password_container>(const char *, bool)> &password_prompter);
//! Uses stdin and stdout. Returns a wallet2 and password for wallet with no file if no errors. //! Uses stdin and stdout. Returns a wallet2 and password for wallet with no file if no errors.
static std::pair<std::unique_ptr<wallet2>, password_container> make_new(const boost::program_options::variables_map& vm); static std::pair<std::unique_ptr<wallet2>, password_container> make_new(const boost::program_options::variables_map& vm, const std::function<boost::optional<password_container>(const char *, bool)> &password_prompter);
//! Just parses variables. //! Just parses variables.
static std::unique_ptr<wallet2> make_dummy(const boost::program_options::variables_map& vm); static std::unique_ptr<wallet2> make_dummy(const boost::program_options::variables_map& vm, const std::function<boost::optional<password_container>(const char *, bool)> &password_prompter);
static bool verify_password(const std::string& keys_file_name, const std::string& password, bool watch_only); static bool verify_password(const std::string& keys_file_name, const std::string& password, bool watch_only);

View file

@ -60,6 +60,16 @@ namespace
const command_line::arg_descriptor<std::string> arg_wallet_dir = {"wallet-dir", "Directory for newly created wallets"}; const command_line::arg_descriptor<std::string> arg_wallet_dir = {"wallet-dir", "Directory for newly created wallets"};
constexpr const char default_rpc_username[] = "monero"; constexpr const char default_rpc_username[] = "monero";
boost::optional<tools::password_container> password_prompter(const char *prompt, bool verify)
{
auto pwd_container = tools::password_container::prompt(verify, prompt);
if (!pwd_container)
{
MERROR("failed to read wallet password");
}
return pwd_container;
}
} }
namespace tools namespace tools
@ -131,7 +141,7 @@ namespace tools
walvars = m_wallet; walvars = m_wallet;
else else
{ {
tmpwal = tools::wallet2::make_dummy(*m_vm); tmpwal = tools::wallet2::make_dummy(*m_vm, password_prompter);
walvars = tmpwal.get(); walvars = tmpwal.get();
} }
boost::optional<epee::net_utils::http::login> http_login{}; boost::optional<epee::net_utils::http::login> http_login{};
@ -1798,7 +1808,7 @@ namespace tools
command_line::add_arg(desc, arg_password); command_line::add_arg(desc, arg_password);
po::store(po::parse_command_line(argc, argv, desc), vm2); po::store(po::parse_command_line(argc, argv, desc), vm2);
} }
std::unique_ptr<tools::wallet2> wal = tools::wallet2::make_new(vm2).first; std::unique_ptr<tools::wallet2> wal = tools::wallet2::make_new(vm2, password_prompter).first;
if (!wal) if (!wal)
{ {
er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
@ -1872,7 +1882,7 @@ namespace tools
} }
std::unique_ptr<tools::wallet2> wal = nullptr; std::unique_ptr<tools::wallet2> wal = nullptr;
try { try {
wal = tools::wallet2::make_from_file(vm2, wallet_file).first; wal = tools::wallet2::make_from_file(vm2, wallet_file, password_prompter).first;
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
@ -2007,11 +2017,11 @@ int main(int argc, char** argv) {
LOG_PRINT_L0(tools::wallet_rpc_server::tr("Loading wallet...")); LOG_PRINT_L0(tools::wallet_rpc_server::tr("Loading wallet..."));
if(!wallet_file.empty()) if(!wallet_file.empty())
{ {
wal = tools::wallet2::make_from_file(*vm, wallet_file).first; wal = tools::wallet2::make_from_file(*vm, wallet_file, password_prompter).first;
} }
else else
{ {
wal = tools::wallet2::make_from_json(*vm, from_json); wal = tools::wallet2::make_from_json(*vm, from_json, password_prompter);
} }
if (!wal) if (!wal)
{ {