Skip to content

Commit

Permalink
Wallet: Option to export data to ASCII
Browse files Browse the repository at this point in the history
New CLI wallet variable: export-format with options "binary" (the default),
or "ascii". "Binary" behaves as before, "ascii" forces the wallet to convert
data to ASCII using base64.

Reading files from the disk tries to auto detect what format has been
used (using a magic string added when exporting the data).

Implements #2859
  • Loading branch information
tmoravec committed Aug 21, 2019
1 parent c9df9d6 commit bb542f5
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 37 deletions.
65 changes: 49 additions & 16 deletions src/simplewallet/simplewallet.cpp
Expand Up @@ -1248,7 +1248,7 @@ bool simple_wallet::export_multisig_main(const std::vector<std::string> &args, b
}
else
{
bool r = epee::file_io_utils::save_string_to_file(filename, ciphertext);
bool r = m_wallet->save_to_file(filename, ciphertext);
if (!r)
{
fail_msg_writer() << tr("failed to save file ") << filename;
Expand Down Expand Up @@ -1309,7 +1309,7 @@ bool simple_wallet::import_multisig_main(const std::vector<std::string> &args, b
{
const std::string &filename = args[n];
std::string data;
bool r = epee::file_io_utils::load_file_to_string(filename, data);
bool r = m_wallet->load_from_file(filename, data);
if (!r)
{
fail_msg_writer() << tr("failed to read file ") << filename;
Expand Down Expand Up @@ -1620,7 +1620,7 @@ bool simple_wallet::export_raw_multisig(const std::vector<std::string> &args)
if (!filenames.empty())
filenames += ", ";
filenames += filename;
if (!epee::file_io_utils::save_string_to_file(filename, cryptonote::tx_to_blob(ptx.tx)))
if (!m_wallet->save_to_file(filename, cryptonote::tx_to_blob(ptx.tx)))
{
fail_msg_writer() << tr("Failed to export multisig transaction to file ") << filename;
return true;
Expand Down Expand Up @@ -2706,6 +2706,35 @@ bool simple_wallet::set_device_name(const std::vector<std::string> &args/* = std
return true;
}

bool simple_wallet::set_export_format(const std::vector<std::string> &args/* = std::vector<std::string()*/)
{
if (args.size() < 2)
{
fail_msg_writer() << tr("Export format not specified");
return true;
}

if (boost::algorithm::iequals(args[1], "ascii"))
{
m_wallet->set_export_format(tools::wallet2::ExportFormat::Ascii);
}
else if (boost::algorithm::iequals(args[1], "binary"))
{
m_wallet->set_export_format(tools::wallet2::ExportFormat::Binary);
}
else
{
fail_msg_writer() << tr("Export format not recognized.");
return true;
}
const auto pwd_container = get_and_verify_password();
if (pwd_container)
{
m_wallet->rewrite(m_wallet_file, pwd_container->password());
}
return true;
}

bool simple_wallet::help(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{
if(args.empty())
Expand Down Expand Up @@ -2893,7 +2922,9 @@ simple_wallet::simple_wallet()
" Set the lookahead sizes for the subaddress hash table.\n "
" Set this if you are not sure whether you will spend on a key reusing Monero fork later.\n "
"segregation-height <n>\n "
" Set to the height of a key reusing fork you want to use, 0 to use default."));
" Set to the height of a key reusing fork you want to use, 0 to use default.\n"
"export-format <\"binary\"|\"ascii\">\n"
" Save all exported files as binary (cannot be copied and pasted) or ascii (can be).\n"));
m_cmd_binder.set_handler("encrypted_seed",
boost::bind(&simple_wallet::encrypted_seed, this, _1),
tr("Display the encrypted Electrum-style mnemonic seed."));
Expand Down Expand Up @@ -3259,6 +3290,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
success_msg_writer() << "track-uses = " << m_wallet->track_uses();
success_msg_writer() << "setup-background-mining = " << setup_background_mining_string + tr(" (set this to support the network and to get a chance to receive new monero)");
success_msg_writer() << "device_name = " << m_wallet->device_name();
success_msg_writer() << "export-format = " << (m_wallet->export_format() == tools::wallet2::ExportFormat::Ascii ? "ascii" : "binary");
return true;
}
else
Expand Down Expand Up @@ -3317,6 +3349,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
CHECK_SIMPLE_VARIABLE("track-uses", set_track_uses, tr("0 or 1"));
CHECK_SIMPLE_VARIABLE("setup-background-mining", set_setup_background_mining, tr("1/yes or 0/no"));
CHECK_SIMPLE_VARIABLE("device-name", set_device_name, tr("<device_name[:device_spec]>"));
CHECK_SIMPLE_VARIABLE("export-format", set_export_format, tr("\"binary\" or \"ascii\""));
}
fail_msg_writer() << tr("set: unrecognized argument(s)");
return true;
Expand Down Expand Up @@ -7293,7 +7326,7 @@ bool simple_wallet::get_tx_proof(const std::vector<std::string> &args)
{
std::string sig_str = m_wallet->get_tx_proof(txid, info.address, info.is_subaddress, args.size() == 3 ? args[2] : "");
const std::string filename = "monero_tx_proof";
if (epee::file_io_utils::save_string_to_file(filename, sig_str))
if (m_wallet->save_to_file(filename, sig_str, true))
success_msg_writer() << tr("signature file saved to: ") << filename;
else
fail_msg_writer() << tr("failed to save signature file");
Expand Down Expand Up @@ -7421,7 +7454,7 @@ bool simple_wallet::check_tx_proof(const std::vector<std::string> &args)

// read signature file
std::string sig_str;
if (!epee::file_io_utils::load_file_to_string(args[2], sig_str))
if (!m_wallet->load_from_file(args[2], sig_str))
{
fail_msg_writer() << tr("failed to load signature file");
return true;
Expand Down Expand Up @@ -7505,7 +7538,7 @@ bool simple_wallet::get_spend_proof(const std::vector<std::string> &args)
{
const std::string sig_str = m_wallet->get_spend_proof(txid, args.size() == 2 ? args[1] : "");
const std::string filename = "monero_spend_proof";
if (epee::file_io_utils::save_string_to_file(filename, sig_str))
if (m_wallet->save_to_file(filename, sig_str, true))
success_msg_writer() << tr("signature file saved to: ") << filename;
else
fail_msg_writer() << tr("failed to save signature file");
Expand Down Expand Up @@ -7535,7 +7568,7 @@ bool simple_wallet::check_spend_proof(const std::vector<std::string> &args)
return true;

std::string sig_str;
if (!epee::file_io_utils::load_file_to_string(args[1], sig_str))
if (!m_wallet->load_from_file(args[1], sig_str))
{
fail_msg_writer() << tr("failed to load signature file");
return true;
Expand Down Expand Up @@ -7594,7 +7627,7 @@ bool simple_wallet::get_reserve_proof(const std::vector<std::string> &args)
{
const std::string sig_str = m_wallet->get_reserve_proof(account_minreserve, args.size() == 2 ? args[1] : "");
const std::string filename = "monero_reserve_proof";
if (epee::file_io_utils::save_string_to_file(filename, sig_str))
if (m_wallet->save_to_file(filename, sig_str, true))
success_msg_writer() << tr("signature file saved to: ") << filename;
else
fail_msg_writer() << tr("failed to save signature file");
Expand Down Expand Up @@ -7629,7 +7662,7 @@ bool simple_wallet::check_reserve_proof(const std::vector<std::string> &args)
}

std::string sig_str;
if (!epee::file_io_utils::load_file_to_string(args[1], sig_str))
if (!m_wallet->load_from_file(args[1], sig_str))
{
fail_msg_writer() << tr("failed to load signature file");
return true;
Expand Down Expand Up @@ -8992,7 +9025,7 @@ bool simple_wallet::sign(const std::vector<std::string> &args)

std::string filename = args[0];
std::string data;
bool r = epee::file_io_utils::load_file_to_string(filename, data);
bool r = m_wallet->load_from_file(filename, data);
if (!r)
{
fail_msg_writer() << tr("failed to read file ") << filename;
Expand All @@ -9018,7 +9051,7 @@ bool simple_wallet::verify(const std::vector<std::string> &args)
std::string signature= args[2];

std::string data;
bool r = epee::file_io_utils::load_file_to_string(filename, data);
bool r = m_wallet->load_from_file(filename, data);
if (!r)
{
fail_msg_writer() << tr("failed to read file ") << filename;
Expand Down Expand Up @@ -9217,7 +9250,7 @@ bool simple_wallet::export_outputs(const std::vector<std::string> &args)
try
{
std::string data = m_wallet->export_outputs_to_str();
bool r = epee::file_io_utils::save_string_to_file(filename, data);
bool r = m_wallet->save_to_file(filename, data);
if (!r)
{
fail_msg_writer() << tr("failed to save file ") << filename;
Expand Down Expand Up @@ -9250,7 +9283,7 @@ bool simple_wallet::import_outputs(const std::vector<std::string> &args)
std::string filename = args[0];

std::string data;
bool r = epee::file_io_utils::load_file_to_string(filename, data);
bool r = m_wallet->load_from_file(filename, data);
if (!r)
{
fail_msg_writer() << tr("failed to read file ") << filename;
Expand Down Expand Up @@ -9451,7 +9484,7 @@ void simple_wallet::commit_or_save(std::vector<tools::wallet2::pending_tx>& ptx_
tx_to_blob(ptx.tx, blob);
const std::string blob_hex = epee::string_tools::buff_to_hex_nodelimer(blob);
const std::string filename = "raw_monero_tx" + (ptx_vector.size() == 1 ? "" : ("_" + std::to_string(i++)));
if (epee::file_io_utils::save_string_to_file(filename, blob_hex))
if (m_wallet->save_to_file(filename, blob_hex, true))
success_msg_writer(true) << tr("Transaction successfully saved to ") << filename << tr(", txid ") << txid;
else
fail_msg_writer() << tr("Failed to save transaction to ") << filename << tr(", txid ") << txid;
Expand Down Expand Up @@ -10236,7 +10269,7 @@ void simple_wallet::mms_export(const std::vector<std::string> &args)
if (valid_id)
{
const std::string filename = "mms_message_content";
if (epee::file_io_utils::save_string_to_file(filename, m.content))
if (m_wallet->save_to_file(filename, m.content))
{
success_msg_writer() << tr("Message content saved to: ") << filename;
}
Expand Down
1 change: 1 addition & 0 deletions src/simplewallet/simplewallet.h
Expand Up @@ -145,6 +145,7 @@ namespace cryptonote
bool set_track_uses(const std::vector<std::string> &args = std::vector<std::string>());
bool set_setup_background_mining(const std::vector<std::string> &args = std::vector<std::string>());
bool set_device_name(const std::vector<std::string> &args = std::vector<std::string>());
bool set_export_format(const std::vector<std::string> &args = std::vector<std::string>());
bool help(const std::vector<std::string> &args = std::vector<std::string>());
bool start_mining(const std::vector<std::string> &args);
bool stop_mining(const std::vector<std::string> &args);
Expand Down

0 comments on commit bb542f5

Please sign in to comment.