aboutsummaryrefslogtreecommitdiff
path: root/src/simplewallet
diff options
context:
space:
mode:
Diffstat (limited to 'src/simplewallet')
-rw-r--r--src/simplewallet/simplewallet.cpp310
-rw-r--r--src/simplewallet/simplewallet.h3
2 files changed, 215 insertions, 98 deletions
diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp
index ba950f803..088d5e2ac 100644
--- a/src/simplewallet/simplewallet.cpp
+++ b/src/simplewallet/simplewallet.cpp
@@ -46,6 +46,7 @@
#include "common/i18n.h"
#include "common/command_line.h"
#include "common/util.h"
+#include "common/dns_utils.h"
#include "p2p/net_node.h"
#include "cryptonote_protocol/cryptonote_protocol_handler.h"
#include "simplewallet.h"
@@ -563,6 +564,7 @@ simple_wallet::simple_wallet()
m_cmd_binder.set_handler("set_log", boost::bind(&simple_wallet::set_log, this, _1), tr("set_log <level> - Change current log detail level, <0-4>"));
m_cmd_binder.set_handler("address", boost::bind(&simple_wallet::print_address, this, _1), tr("Show current wallet public address"));
m_cmd_binder.set_handler("integrated_address", boost::bind(&simple_wallet::print_integrated_address, this, _1), tr("integrated_address [PID] - Encode a payment ID into an integrated address for the current wallet public address (no argument uses a random payment ID), or decode an integrated address to standard address and payment ID"));
+ m_cmd_binder.set_handler("address_book", boost::bind(&simple_wallet::address_book, this, _1), tr("address_book [(add (<address> [pid <long or short payment id>])|<integrated address> [<description possibly with whitespaces>])|(delete <index>)] - Print all entries in the address book, optionally adding/deleting an entry to/from it"));
m_cmd_binder.set_handler("save", boost::bind(&simple_wallet::save, this, _1), tr("Save wallet data"));
m_cmd_binder.set_handler("save_watch_only", boost::bind(&simple_wallet::save_watch_only, this, _1), tr("Save a watch-only keys file"));
m_cmd_binder.set_handler("viewkey", boost::bind(&simple_wallet::viewkey, this, _1), tr("Display private view key"));
@@ -573,6 +575,7 @@ simple_wallet::simple_wallet()
m_cmd_binder.set_handler("get_tx_key", boost::bind(&simple_wallet::get_tx_key, this, _1), tr("Get transaction key (r) for a given <txid>"));
m_cmd_binder.set_handler("check_tx_key", boost::bind(&simple_wallet::check_tx_key, this, _1), tr("Check amount going to <address> in <txid>"));
m_cmd_binder.set_handler("show_transfers", boost::bind(&simple_wallet::show_transfers, this, _1), tr("show_transfers [in|out|pending|failed|pool] [<min_height> [<max_height>]] - Show incoming/outgoing transfers within an optional height range"));
+ m_cmd_binder.set_handler("unspent_outputs", boost::bind(&simple_wallet::unspent_outputs, this, _1), tr("unspent_outputs [<min_amount> <max_amount>] - Show unspent outputs within an optional amount range)"));
m_cmd_binder.set_handler("rescan_bc", boost::bind(&simple_wallet::rescan_blockchain, this, _1), tr("Rescan blockchain from scratch"));
m_cmd_binder.set_handler("set_tx_note", boost::bind(&simple_wallet::set_tx_note, this, _1), tr("Set an arbitrary string note for a txid"));
m_cmd_binder.set_handler("get_tx_note", boost::bind(&simple_wallet::get_tx_note, this, _1), tr("Get a string note for a txid"));
@@ -612,9 +615,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
}
else if (args[1] == "language")
{
- std::vector<std::string> local_args = args;
- local_args.erase(local_args.begin(), local_args.begin()+2);
- seed_set_language(local_args);
+ seed_set_language(args);
return true;
}
}
@@ -627,9 +628,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
}
else
{
- std::vector<std::string> local_args = args;
- local_args.erase(local_args.begin(), local_args.begin()+2);
- set_always_confirm_transfers(local_args);
+ set_always_confirm_transfers(args);
return true;
}
}
@@ -642,9 +641,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
}
else
{
- std::vector<std::string> local_args = args;
- local_args.erase(local_args.begin(), local_args.begin()+2);
- set_store_tx_info(local_args);
+ set_store_tx_info(args);
return true;
}
}
@@ -657,9 +654,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
}
else
{
- std::vector<std::string> local_args = args;
- local_args.erase(local_args.begin(), local_args.begin()+2);
- set_default_mixin(local_args);
+ set_default_mixin(args);
return true;
}
}
@@ -672,9 +667,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
}
else
{
- std::vector<std::string> local_args = args;
- local_args.erase(local_args.begin(), local_args.begin()+2);
- set_auto_refresh(local_args);
+ set_auto_refresh(args);
return true;
}
}
@@ -688,9 +681,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
}
else
{
- std::vector<std::string> local_args = args;
- local_args.erase(local_args.begin(), local_args.begin()+2);
- set_refresh_type(local_args);
+ set_refresh_type(args);
return true;
}
}
@@ -703,9 +694,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
}
else
{
- std::vector<std::string> local_args = args;
- local_args.erase(local_args.begin(), local_args.begin()+2);
- set_default_priority(local_args);
+ set_default_priority(args);
return true;
}
}
@@ -718,9 +707,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
}
else
{
- std::vector<std::string> local_args = args;
- local_args.erase(local_args.begin(), local_args.begin()+2);
- set_confirm_missing_payment_id(local_args);
+ set_confirm_missing_payment_id(args);
return true;
}
}
@@ -1878,75 +1865,6 @@ bool simple_wallet::rescan_spent(const std::vector<std::string> &args)
return true;
}
//----------------------------------------------------------------------------------------------------
-bool simple_wallet::get_address_from_str(const std::string &str, cryptonote::account_public_address &address, bool &has_payment_id, crypto::hash8 &payment_id)
-{
- if(!get_account_integrated_address_from_str(address, has_payment_id, payment_id, m_wallet->testnet(), str))
- {
- // if treating as an address fails, try as url
- bool dnssec_ok = false;
- std::string url = str;
-
- // attempt to get address from dns query
- auto addresses_from_dns = tools::wallet2::addresses_from_url(url, dnssec_ok);
-
- // for now, move on only if one address found
- if (addresses_from_dns.size() == 1)
- {
- if (get_account_integrated_address_from_str(address, has_payment_id, payment_id, m_wallet->testnet(), addresses_from_dns[0]))
- {
- // if it was an address, prompt user for confirmation.
- // inform user of DNSSEC validation status as well.
-
- std::string dnssec_str;
- if (dnssec_ok)
- {
- dnssec_str = tr("DNSSEC validation passed");
- }
- else
- {
- dnssec_str = tr("WARNING: DNSSEC validation was unsuccessful, this address may not be correct!");
- }
- std::stringstream prompt;
- prompt << tr("For URL: ") << url
- << ", " << dnssec_str << std::endl
- << tr(" Monero Address = ") << addresses_from_dns[0]
- << std::endl
- << tr("Is this OK? (Y/n) ")
- ;
-
- // prompt the user for confirmation given the dns query and dnssec status
- std::string confirm_dns_ok = command_line::input_line(prompt.str());
- if (std::cin.eof())
- {
- return false;
- }
- if (!command_line::is_yes(confirm_dns_ok))
- {
- fail_msg_writer() << tr("you have cancelled the transfer request");
- return false;
- }
- }
- else
- {
- fail_msg_writer() << tr("failed to get a Monero address from: ") << url;
- return false;
- }
- }
- else if (addresses_from_dns.size() > 1)
- {
- fail_msg_writer() << tr("not yet supported: Multiple Monero addresses found for given URL: ") << url;
- return false;
- }
- else
- {
- fail_msg_writer() << tr("wrong address: ") << url;
- return false;
- }
- }
-
- return true;
-}
-//----------------------------------------------------------------------------------------------------
bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::string> &args_)
{
if (!try_connect_to_daemon())
@@ -2039,7 +1957,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
cryptonote::tx_destination_entry de;
bool has_payment_id;
crypto::hash8 new_payment_id;
- if (!get_address_from_str(local_args[i], de.addr, has_payment_id, new_payment_id))
+ if (!tools::dns_utils::get_account_address_from_str_or_url(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), local_args[i]))
return true;
if (has_payment_id)
@@ -2527,7 +2445,7 @@ bool simple_wallet::sweep_all(const std::vector<std::string> &args_)
bool has_payment_id;
crypto::hash8 new_payment_id;
cryptonote::account_public_address address;
- if (!get_address_from_str(local_args[0], address, has_payment_id, new_payment_id))
+ if (!tools::dns_utils::get_account_address_from_str_or_url(address, has_payment_id, new_payment_id, m_wallet->testnet(), local_args[0]))
return true;
if (has_payment_id)
@@ -3075,7 +2993,7 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_)
cryptonote::account_public_address address;
bool has_payment_id;
crypto::hash8 payment_id;
- if(!get_account_integrated_address_from_str(address, has_payment_id, payment_id, m_wallet->testnet(), local_args[2]))
+ if(!tools::dns_utils::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), local_args[2]))
{
fail_msg_writer() << tr("failed to parse address");
return true;
@@ -3383,6 +3301,124 @@ bool simple_wallet::show_transfers(const std::vector<std::string> &args_)
return true;
}
//----------------------------------------------------------------------------------------------------
+bool simple_wallet::unspent_outputs(const std::vector<std::string> &args_)
+{
+ if(!args_.empty() && args_.size() != 2) {
+ fail_msg_writer() << tr("usage: unspent_outputs [<min_amount> <max_amount>]");
+ return true;
+ }
+ uint64_t min_amount = 0;
+ uint64_t max_amount = std::numeric_limits<uint64_t>::max();
+ if (args_.size() == 2)
+ {
+ if (!cryptonote::parse_amount(min_amount, args_[0]) || !cryptonote::parse_amount(max_amount, args_[1]))
+ {
+ fail_msg_writer() << tr("amount is wrong: ") << args_[0] << ' ' << args_[1] <<
+ ", " << tr("expected number from 0 to ") << print_money(std::numeric_limits<uint64_t>::max());
+ return true;
+ }
+ if (min_amount > max_amount)
+ {
+ fail_msg_writer() << tr("<min_amount> should be smaller than <max_amount>");
+ return true;
+ }
+ }
+ tools::wallet2::transfer_container transfers;
+ m_wallet->get_transfers(transfers);
+ if (transfers.empty())
+ {
+ success_msg_writer() << "There is no unspent output in this wallet.";
+ return true;
+ }
+ std::map<uint64_t, tools::wallet2::transfer_container> amount_to_tds;
+ uint64_t min_height = std::numeric_limits<uint64_t>::max();
+ uint64_t max_height = 0;
+ uint64_t found_min_amount = std::numeric_limits<uint64_t>::max();
+ uint64_t found_max_amount = 0;
+ uint64_t count = 0;
+ for (const auto& td : transfers)
+ {
+ uint64_t amount = td.amount();
+ if (td.m_spent || amount < min_amount || amount > max_amount)
+ continue;
+ amount_to_tds[amount].push_back(td);
+ if (min_height > td.m_block_height) min_height = td.m_block_height;
+ if (max_height < td.m_block_height) max_height = td.m_block_height;
+ if (found_min_amount > amount) found_min_amount = amount;
+ if (found_max_amount < amount) found_max_amount = amount;
+ ++count;
+ }
+ for (const auto& amount_tds : amount_to_tds)
+ {
+ auto& tds = amount_tds.second;
+ success_msg_writer() << tr("\nAmount: ") << print_money(amount_tds.first) << tr(", number of keys: ") << tds.size();
+ for (size_t i = 0; i < tds.size(); )
+ {
+ std::ostringstream oss;
+ for (size_t j = 0; j < 8 && i < tds.size(); ++i, ++j)
+ oss << tds[i].m_block_height << tr(" ");
+ success_msg_writer() << oss.str();
+ }
+ }
+ success_msg_writer()
+ << tr("\nMin block height: ") << min_height
+ << tr("\nMax block height: ") << max_height
+ << tr("\nMin amount found: ") << print_money(found_min_amount)
+ << tr("\nMax amount found: ") << print_money(found_max_amount)
+ << tr("\nTotal count: ") << count;
+ const size_t histogram_height = 10;
+ const size_t histogram_width = 50;
+ double bin_size = (max_height - min_height + 1.0) / histogram_width;
+ size_t max_bin_count = 0;
+ std::vector<size_t> histogram(histogram_width, 0);
+ for (const auto& amount_tds : amount_to_tds)
+ {
+ for (auto& td : amount_tds.second)
+ {
+ uint64_t bin_index = (td.m_block_height - min_height + 1) / bin_size;
+ if (bin_index >= histogram_width)
+ bin_index = histogram_width - 1;
+ histogram[bin_index]++;
+ if (max_bin_count < histogram[bin_index])
+ max_bin_count = histogram[bin_index];
+ }
+ }
+ for (size_t x = 0; x < histogram_width; ++x)
+ {
+ double bin_count = histogram[x];
+ if (max_bin_count > histogram_height)
+ bin_count *= histogram_height / (double)max_bin_count;
+ if (histogram[x] > 0 && bin_count < 1.0)
+ bin_count = 1.0;
+ histogram[x] = bin_count;
+ }
+ std::vector<std::string> histogram_line(histogram_height, std::string(histogram_width, ' '));
+ for (size_t y = 0; y < histogram_height; ++y)
+ {
+ for (size_t x = 0; x < histogram_width; ++x)
+ {
+ if (y < histogram[x])
+ histogram_line[y][x] = '*';
+ }
+ }
+ double count_per_star = max_bin_count / (double)histogram_height;
+ if (count_per_star < 1)
+ count_per_star = 1;
+ success_msg_writer()
+ << tr("\nBin size: ") << bin_size
+ << tr("\nOutputs per *: ") << count_per_star;
+ ostringstream histogram_str;
+ histogram_str << tr("count\n ^\n");
+ for (size_t y = histogram_height; y > 0; --y)
+ histogram_str << tr(" |") << histogram_line[y - 1] << tr("|\n");
+ histogram_str
+ << tr(" +") << std::string(histogram_width, '-') << tr("+--> block height\n")
+ << tr(" ^") << std::string(histogram_width - 2, ' ') << tr("^\n")
+ << tr(" ") << min_height << std::string(histogram_width - 8, ' ') << max_height;
+ success_msg_writer() << histogram_str.str();
+ return true;
+}
+//----------------------------------------------------------------------------------------------------
bool simple_wallet::rescan_blockchain(const std::vector<std::string> &args_)
{
return refresh_main(0, true);
@@ -3490,6 +3526,86 @@ bool simple_wallet::print_integrated_address(const std::vector<std::string> &arg
return true;
}
//----------------------------------------------------------------------------------------------------
+bool simple_wallet::address_book(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
+{
+ if (args.size() == 0)
+ {
+ }
+ else if (args.size() == 1 || (args[0] != "add" && args[0] != "delete"))
+ {
+ fail_msg_writer() << tr("usage: address_book [(add (<address> [pid <long or short payment id>])|<integrated address> [<description possibly with whitespaces>])|(delete <index>)]");
+ return true;
+ }
+ else if (args[0] == "add")
+ {
+ cryptonote::account_public_address address;
+ bool has_payment_id;
+ crypto::hash8 payment_id8;
+ if (!get_address_from_str(args[1], address, has_payment_id, payment_id8))
+ {
+ fail_msg_writer() << tr("failed to parse address");
+ return true;
+ }
+ crypto::hash payment_id = null_hash;
+ size_t description_start = 2;
+ if (has_payment_id)
+ {
+ memcpy(payment_id.data, payment_id8.data, 8);
+ }
+ else if (!has_payment_id && args.size() >= 4 && args[2] == "pid")
+ {
+ if (tools::wallet2::parse_long_payment_id(args[3], payment_id))
+ {
+ description_start += 2;
+ }
+ else if (tools::wallet2::parse_short_payment_id(args[3], payment_id8))
+ {
+ memcpy(payment_id.data, payment_id8.data, 8);
+ description_start += 2;
+ }
+ else
+ {
+ fail_msg_writer() << tr("failed to parse payment ID");
+ return true;
+ }
+ }
+ std::string description;
+ for (size_t i = description_start; i < args.size(); ++i)
+ {
+ if (i > description_start)
+ description += " ";
+ description += args[i];
+ }
+ m_wallet->add_address_book_row(address, payment_id, description);
+ }
+ else
+ {
+ size_t row_id;
+ if(!epee::string_tools::get_xtype_from_string(row_id, args[1]))
+ {
+ fail_msg_writer() << tr("failed to parse index");
+ return true;
+ }
+ m_wallet->delete_address_book_row(row_id);
+ }
+ auto address_book = m_wallet->get_address_book();
+ if (address_book.empty())
+ {
+ success_msg_writer() << tr("Address book is empty.");
+ }
+ else
+ {
+ for (size_t i = 0; i < address_book.size(); ++i) {
+ auto& row = address_book[i];
+ success_msg_writer() << tr("Index: ") << i;
+ success_msg_writer() << tr("Address: ") << get_account_address_as_str(m_wallet->testnet(), row.m_address);
+ success_msg_writer() << tr("Payment ID: ") << row.m_payment_id;
+ success_msg_writer() << tr("Description: ") << row.m_description << "\n";
+ }
+ }
+ return true;
+}
+//----------------------------------------------------------------------------------------------------
bool simple_wallet::set_tx_note(const std::vector<std::string> &args)
{
if (args.size() == 0)
@@ -3615,7 +3731,7 @@ bool simple_wallet::verify(const std::vector<std::string> &args)
cryptonote::account_public_address address;
bool has_payment_id;
crypto::hash8 payment_id;
- if(!get_account_integrated_address_from_str(address, has_payment_id, payment_id, m_wallet->testnet(), address_string))
+ if(!tools::dns_utils::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), address_string))
{
fail_msg_writer() << tr("failed to parse address");
return true;
diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h
index 05e8356e9..753bca74d 100644
--- a/src/simplewallet/simplewallet.h
+++ b/src/simplewallet/simplewallet.h
@@ -135,6 +135,7 @@ namespace cryptonote
);
bool print_address(const std::vector<std::string> &args = std::vector<std::string>());
bool print_integrated_address(const std::vector<std::string> &args = std::vector<std::string>());
+ bool address_book(const std::vector<std::string> &args = std::vector<std::string>());
bool save(const std::vector<std::string> &args);
bool save_watch_only(const std::vector<std::string> &args);
bool set_variable(const std::vector<std::string> &args);
@@ -143,6 +144,7 @@ namespace cryptonote
bool get_tx_key(const std::vector<std::string> &args);
bool check_tx_key(const std::vector<std::string> &args);
bool show_transfers(const std::vector<std::string> &args);
+ bool unspent_outputs(const std::vector<std::string> &args);
bool rescan_blockchain(const std::vector<std::string> &args);
bool refresh_main(uint64_t start_height, bool reset = false);
bool set_tx_note(const std::vector<std::string> &args);
@@ -163,7 +165,6 @@ namespace cryptonote
bool accept_loaded_tx(const std::function<size_t()> get_num_txes, const std::function<const tools::wallet2::tx_construction_data&(size_t)> &get_tx, const std::string &extra_message = std::string());
bool accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs);
bool accept_loaded_tx(const tools::wallet2::signed_tx_set &txs);
- bool get_address_from_str(const std::string &str, cryptonote::account_public_address &address, bool &has_payment_id, crypto::hash8 &payment_id);
/*!
* \brief Prints the seed with a nice message