diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/blockchain_db/blockchain_db.h | 14 | ||||
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.cpp | 14 | ||||
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.h | 2 | ||||
-rw-r--r-- | src/common/dns_utils.cpp | 143 | ||||
-rw-r--r-- | src/common/dns_utils.h | 19 | ||||
-rw-r--r-- | src/crypto/tree-hash.c | 38 | ||||
-rw-r--r-- | src/cryptonote_core/blockchain.cpp | 10 | ||||
-rw-r--r-- | src/cryptonote_core/tx_pool.cpp | 2 | ||||
-rw-r--r-- | src/daemon/command_parser_executor.cpp | 32 | ||||
-rw-r--r-- | src/rpc/core_rpc_server_commands_defs.h | 2 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.cpp | 278 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.h | 3 | ||||
-rw-r--r-- | src/wallet/api/wallet_manager.cpp | 36 | ||||
-rw-r--r-- | src/wallet/api/wallet_manager.h | 4 | ||||
-rw-r--r-- | src/wallet/wallet2.cpp | 87 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 4 | ||||
-rw-r--r-- | src/wallet/wallet2_api.h | 6 |
17 files changed, 508 insertions, 186 deletions
diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h index 91c388de6..b39cb1801 100644 --- a/src/blockchain_db/blockchain_db.h +++ b/src/blockchain_db/blockchain_db.h @@ -1020,6 +1020,20 @@ public: virtual transaction get_tx(const crypto::hash& h) const = 0; /** + * @brief fetches the transaction with the given hash + * + * The subclass should return the transaction stored which has the given + * hash. + * + * If the transaction does not exist, the subclass should return false. + * + * @param h the hash to look for + * + * @return true iff the transaction was found + */ + virtual bool get_tx(const crypto::hash& h, transaction &tx) const = 0; + + /** * @brief fetches the total number of transactions ever * * The subclass should return a count of all the transactions from diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index 1ad9876ac..ba2cb60bd 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -1791,7 +1791,7 @@ uint64_t BlockchainLMDB::get_tx_unlock_time(const crypto::hash& h) const return ret; } -transaction BlockchainLMDB::get_tx(const crypto::hash& h) const +bool BlockchainLMDB::get_tx(const crypto::hash& h, transaction &tx) const { LOG_PRINT_L3("BlockchainLMDB::" << __func__); check_open(); @@ -1810,19 +1810,27 @@ transaction BlockchainLMDB::get_tx(const crypto::hash& h) const get_result = mdb_cursor_get(m_cur_txs, &val_tx_id, &result, MDB_SET); } if (get_result == MDB_NOTFOUND) - throw1(TX_DNE(std::string("tx with hash ").append(epee::string_tools::pod_to_hex(h)).append(" not found in db").c_str())); + return false; else if (get_result) throw0(DB_ERROR(lmdb_error("DB error attempting to fetch tx from hash", get_result).c_str())); blobdata bd; bd.assign(reinterpret_cast<char*>(result.mv_data), result.mv_size); - transaction tx; if (!parse_and_validate_tx_from_blob(bd, tx)) throw0(DB_ERROR("Failed to parse tx from blob retrieved from the db")); TXN_POSTFIX_RDONLY(); + return true; +} + +transaction BlockchainLMDB::get_tx(const crypto::hash& h) const +{ + transaction tx; + + if (!get_tx(h, tx)) + throw1(TX_DNE(std::string("tx with hash ").append(epee::string_tools::pod_to_hex(h)).append(" not found in db").c_str())); return tx; } diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h index 6db5abca1..b0d8d9d0a 100644 --- a/src/blockchain_db/lmdb/db_lmdb.h +++ b/src/blockchain_db/lmdb/db_lmdb.h @@ -209,6 +209,8 @@ public: virtual transaction get_tx(const crypto::hash& h) const; + virtual bool get_tx(const crypto::hash& h, transaction &tx) const; + virtual uint64_t get_tx_count() const; virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const; diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp index e6e53a5c0..35fb9fe6c 100644 --- a/src/common/dns_utils.cpp +++ b/src/common/dns_utils.cpp @@ -26,7 +26,10 @@ // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include "common/command_line.h" +#include "common/i18n.h" #include "common/dns_utils.h" +#include "cryptonote_core/cryptonote_basic_impl.h" #include <cstring> #include <sstream> // check local first (in the event of static or in-source compilation of libunbound) @@ -323,4 +326,144 @@ bool DNSResolver::check_address_syntax(const char *addr) const return true; } +namespace dns_utils +{ + +const char *tr(const char *str) { return i18n_translate(str, "tools::dns_utils"); } + +//----------------------------------------------------------------------- +// TODO: parse the string in a less stupid way, probably with regex +std::string address_from_txt_record(const std::string& s) +{ + // make sure the txt record has "oa1:xmr" and find it + auto pos = s.find("oa1:xmr"); + if (pos == std::string::npos) + return {}; + // search from there to find "recipient_address=" + pos = s.find("recipient_address=", pos); + if (pos == std::string::npos) + return {}; + pos += 18; // move past "recipient_address=" + // find the next semicolon + auto pos2 = s.find(";", pos); + if (pos2 != std::string::npos) + { + // length of address == 95, we can at least validate that much here + if (pos2 - pos == 95) + { + return s.substr(pos, 95); + } + else if (pos2 - pos == 106) // length of address == 106 --> integrated address + { + return s.substr(pos, 106); + } + } + return {}; +} +/** + * @brief gets a monero address from the TXT record of a DNS entry + * + * gets the monero address from the TXT record of the DNS entry associated + * with <url>. If this lookup fails, or the TXT record does not contain an + * XMR address in the correct format, returns an empty string. <dnssec_valid> + * will be set true or false according to whether or not the DNS query passes + * DNSSEC validation. + * + * @param url the url to look up + * @param dnssec_valid return-by-reference for DNSSEC status of query + * + * @return a monero address (as a string) or an empty string + */ +std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid) +{ + std::vector<std::string> addresses; + // get txt records + bool dnssec_available, dnssec_isvalid; + std::string oa_addr = DNSResolver::instance().get_dns_format_from_oa_address(url); + auto records = DNSResolver::instance().get_txt_record(oa_addr, dnssec_available, dnssec_isvalid); + + // TODO: update this to allow for conveying that dnssec was not available + if (dnssec_available && dnssec_isvalid) + { + dnssec_valid = true; + } + else dnssec_valid = false; + + // for each txt record, try to find a monero address in it. + for (auto& rec : records) + { + std::string addr = address_from_txt_record(rec); + if (addr.size()) + { + addresses.push_back(addr); + } + } + return addresses; +} + +std::string get_account_address_as_str_from_url(const std::string& url, bool& dnssec_valid) +{ + // attempt to get address from dns query + auto addresses = addresses_from_url(url, dnssec_valid); + if (addresses.empty()) + { + std::cout << tr("wrong address: ") << url; + return {}; + } + // for now, move on only if one address found + if (addresses.size() > 1) + { + std::cout << tr("not yet supported: Multiple Monero addresses found for given URL: ") << url; + return {}; + } + // prompt user for confirmation. + // inform user of DNSSEC validation status as well. + std::string dnssec_str; + if (dnssec_valid) + { + 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[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 {}; + } + if (!command_line::is_yes(confirm_dns_ok)) + { + std::cout << tr("you have cancelled the transfer request") << std::endl; + return {}; + } + return addresses[0]; +} + +bool get_account_address_from_str_or_url( + cryptonote::account_public_address& address + , bool& has_payment_id + , crypto::hash8& payment_id + , bool testnet + , const std::string& str_or_url + ) +{ + if (cryptonote::get_account_integrated_address_from_str(address, has_payment_id, payment_id, testnet, str_or_url)) + return true; + bool dnssec_valid; + std::string address_str = get_account_address_as_str_from_url(str_or_url, dnssec_valid); + return !address_str.empty() && + cryptonote::get_account_integrated_address_from_str(address, has_payment_id, payment_id, testnet, address_str); +} + +} // namespace tools::dns_utils + } // namespace tools diff --git a/src/common/dns_utils.h b/src/common/dns_utils.h index 2e87fb01f..5fe1d4775 100644 --- a/src/common/dns_utils.h +++ b/src/common/dns_utils.h @@ -25,9 +25,11 @@ // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#pragma once #include <vector> #include <string> +#include "cryptonote_core/cryptonote_basic.h" namespace tools { @@ -155,4 +157,21 @@ private: DNSResolverData *m_data; }; // class DNSResolver +namespace dns_utils +{ + +std::string address_from_txt_record(const std::string& s); +std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid); + +std::string get_account_address_as_str_from_url(const std::string& url, bool& dnssec_valid); +bool get_account_address_from_str_or_url( + cryptonote::account_public_address& address + , bool& has_payment_id + , crypto::hash8& payment_id + , bool testnet + , const std::string& str_or_url + ); + +} // namespace tools::dns_utils + } // namespace tools diff --git a/src/crypto/tree-hash.c b/src/crypto/tree-hash.c index d73f0d959..5cdaa8c94 100644 --- a/src/crypto/tree-hash.c +++ b/src/crypto/tree-hash.c @@ -40,27 +40,28 @@ #include <stdlib.h> #endif -/// Quick check if this is power of two (use on unsigned types; in this case for size_t only) -bool ispowerof2_size_t(size_t x) { - return x && !(x & (x - 1)); -} - /*** * Round to power of two, for count>=3 and for count being not too large (as reasonable for tree hash calculations) */ size_t tree_hash_cnt(size_t count) { - assert( count >= 3); // cases for 0,1,2 are handled elsewhere - // Round down the count size: fun(2**n)= 2**(n-1) to round down to power of two - size_t tmp = count - 1; - size_t jj = 1; - for (jj=1 ; tmp != 0 ; ++jj) { - tmp /= 2; // dividing by 2 until to get how many powers of 2 fits size_to tmp - } - size_t cnt = 1 << (jj-2); // cnt is the count, but rounded down to power of two - // printf("count=%zu cnt=%zu jj=%zu tmp=%zu \n" , count,cnt,jj,tmp); - assert( cnt > 0 ); assert( cnt >= count/2 ); assert( cnt <= count ); - assert( ispowerof2_size_t( cnt )); - return cnt; + // This algo has some bad history but all we are doing is 1 << floor(log2(count)) + // There are _many_ ways to do log2, for some reason the one selected was the most obscure one, + // and fixing it made it even more obscure. + // + // Iterative method implemented below aims for clarity over speed, if performance is needed + // then my advice is to use the BSR instruction on x86 + // + // All the paranoid asserts have been removed since it is trivial to mathematically prove that + // the return will always be a power of 2. + // Problem space has been defined as 3 <= count <= 2^28. Of course quarter of a billion transactions + // is not a sane upper limit for a block, so there will be tighter limits in other parts of the code + + assert( count >= 3 ); // cases for 0,1,2 are handled elsewhere + assert( count <= 0x10000000 ); // sanity limit to 2^28, MSB=1 will cause an inf loop + + size_t pow = 2; + while(pow < count) pow <<= 1; + return pow >> 1; } void tree_hash(const char (*hashes)[HASH_SIZE], size_t count, char *root_hash) { @@ -86,9 +87,6 @@ void tree_hash(const char (*hashes)[HASH_SIZE], size_t count, char *root_hash) { size_t i, j; size_t cnt = tree_hash_cnt( count ); - size_t max_size_t = (size_t) -1; // max allowed value of size_t - assert( cnt < max_size_t/2 ); // reasonable size to avoid any overflows. /2 is extra; Anyway should be limited much stronger by logical code - // as we have sane limits on transactions counts in blockchain rules char (*ints)[HASH_SIZE]; size_t ints_size = cnt * HASH_SIZE; diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index f1d08cc49..11dbb2fe6 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -1896,11 +1896,11 @@ bool Blockchain::get_transactions(const t_ids_container& txs_ids, t_tx_container { try { - txs.push_back(m_db->get_tx(tx_hash)); - } - catch (const TX_DNE& e) - { - missed_txs.push_back(tx_hash); + transaction tx; + if (m_db->get_tx(tx_hash, tx)) + txs.push_back(std::move(tx)); + else + missed_txs.push_back(tx_hash); } catch (const std::exception& e) { diff --git a/src/cryptonote_core/tx_pool.cpp b/src/cryptonote_core/tx_pool.cpp index 4cfd61f9f..ca4b6d6f5 100644 --- a/src/cryptonote_core/tx_pool.cpp +++ b/src/cryptonote_core/tx_pool.cpp @@ -189,6 +189,8 @@ namespace cryptonote txd_p.first->second.fee = fee; txd_p.first->second.max_used_block_id = null_hash; txd_p.first->second.max_used_block_height = 0; + txd_p.first->second.last_failed_height = 0; + txd_p.first->second.last_failed_id = null_hash; txd_p.first->second.kept_by_block = kept_by_block; txd_p.first->second.receive_time = time(nullptr); txd_p.first->second.last_relayed_time = time(NULL); diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index f07ef1616..7381dd06f 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -26,7 +26,7 @@ // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "cryptonote_core/cryptonote_basic_impl.h" +#include "common/dns_utils.h" #include "daemon/command_parser_executor.h" namespace daemonize { @@ -238,17 +238,35 @@ bool t_command_parser_executor::start_mining(const std::vector<std::string>& arg } cryptonote::account_public_address adr; + bool has_payment_id; + crypto::hash8 payment_id; bool testnet = false; - if(!cryptonote::get_account_address_from_str(adr, false, args.front())) + if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, false, args.front())) { - if(!cryptonote::get_account_address_from_str(adr, true, args.front())) + if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, true, args.front())) { - std::cout << "target account address has wrong format" << std::endl; - return true; + bool dnssec_valid; + std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(args.front(), dnssec_valid); + if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, false, address_str)) + { + if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, true, address_str)) + { + std::cout << "target account address has wrong format" << std::endl; + return true; + } + else + { + testnet = true; + } + } + } + else + { + testnet = true; } - testnet = true; - std::cout << "Mining to a testnet address, make sure this is intentional!" << std::endl; } + if(testnet) + std::cout << "Mining to a testnet address, make sure this is intentional!" << std::endl; uint64_t threads_count = 1; if(args.size() > 2) { diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index b09cad235..6060cee3a 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -919,7 +919,7 @@ namespace cryptonote KV_SERIALIZE(last_failed_id_hash) KV_SERIALIZE(receive_time) KV_SERIALIZE(relayed) - KV_SERIALIZE(last_failed_id_hash) + KV_SERIALIZE(last_relayed_time) END_KV_SERIALIZE_MAP() }; diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index d03f3e7be..57d1d459d 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")); @@ -1877,75 +1880,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()) @@ -2038,7 +1972,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) @@ -2526,7 +2460,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) @@ -3074,7 +3008,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; @@ -3382,6 +3316,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); @@ -3489,6 +3541,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) @@ -3614,7 +3746,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 c3e14a8cc..a807e2cda 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); @@ -162,7 +164,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 diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index 4ee5ab8df..6b48caf1d 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -32,6 +32,7 @@ #include "wallet_manager.h" #include "wallet.h" #include "common_defines.h" +#include "common/dns_utils.h" #include "net/http_client.h" #include <boost/filesystem.hpp> @@ -137,7 +138,7 @@ void WalletManagerImpl::setDaemonAddress(const std::string &address) m_daemonAddress = address; } -bool WalletManagerImpl::connected(uint32_t *version = NULL) const +bool WalletManagerImpl::connected(uint32_t *version) const { epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_VERSION::request> req_t = AUTO_VAL_INIT(req_t); epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_VERSION::response, std::string> resp_t = AUTO_VAL_INIT(resp_t); @@ -352,9 +353,40 @@ double WalletManagerImpl::miningHashRate() const return mres.speed; } +void WalletManagerImpl::hardForkInfo(uint8_t &version, uint64_t &earliest_height) const +{ + epee::json_rpc::request<cryptonote::COMMAND_RPC_HARD_FORK_INFO::request> req_t = AUTO_VAL_INIT(req_t); + epee::json_rpc::response<cryptonote::COMMAND_RPC_HARD_FORK_INFO::response, std::string> resp_t = AUTO_VAL_INIT(resp_t); + + version = 0; + earliest_height = 0; + + epee::net_utils::http::http_simple_client http_client; + req_t.jsonrpc = "2.0"; + req_t.id = epee::serialization::storage_entry(0); + req_t.method = "hard_fork_info"; + req_t.params.version = 0; + bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/json_rpc", req_t, resp_t, http_client); + if (!r || resp_t.result.status != CORE_RPC_STATUS_OK) + return; + version = resp_t.result.version; + earliest_height = resp_t.result.earliest_height; +} + +uint64_t WalletManagerImpl::blockTarget() const +{ + cryptonote::COMMAND_RPC_GET_INFO::request ireq; + cryptonote::COMMAND_RPC_GET_INFO::response ires; + + epee::net_utils::http::http_simple_client http_client; + if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + return 0; + return ires.target; +} + std::string WalletManagerImpl::resolveOpenAlias(const std::string &address, bool &dnssec_valid) const { - std::vector<std::string> addresses = tools::wallet2::addresses_from_url(address, dnssec_valid); + std::vector<std::string> addresses = tools::dns_utils::addresses_from_url(address, dnssec_valid); if (addresses.empty()) return ""; return addresses.front(); diff --git a/src/wallet/api/wallet_manager.h b/src/wallet/api/wallet_manager.h index 214afc3fa..01752f69b 100644 --- a/src/wallet/api/wallet_manager.h +++ b/src/wallet/api/wallet_manager.h @@ -46,12 +46,14 @@ public: std::vector<std::string> findWallets(const std::string &path); std::string errorString() const; void setDaemonAddress(const std::string &address); - bool connected(uint32_t *version) const; + bool connected(uint32_t *version = NULL) const; bool checkPayment(const std::string &address, const std::string &txid, const std::string &txkey, const std::string &daemon_address, uint64_t &received, uint64_t &height, std::string &error) const; uint64_t blockchainHeight() const; uint64_t blockchainTargetHeight() const; uint64_t networkDifficulty() const; double miningHashRate() const; + void hardForkInfo(uint8_t &version, uint64_t &earliest_height) const; + uint64_t blockTarget() const; std::string resolveOpenAlias(const std::string &address, bool &dnssec_valid) const; private: diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 221dd8e0b..e0a3a23bd 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -51,7 +51,6 @@ using namespace epee; #include "cryptonote_protocol/blobdatatype.h" #include "mnemonics/electrum-words.h" #include "common/i18n.h" -#include "common/dns_utils.h" #include "common/util.h" #include "rapidjson/document.h" #include "rapidjson/writer.h" @@ -1632,7 +1631,7 @@ void wallet2::refresh(uint64_t start_height, uint64_t & blocks_fetched, bool& re process_blocks(blocks_start_height, blocks, o_indices, added_blocks); blocks_fetched += added_blocks; pull_thread.join(); - if(!added_blocks) + if(blocks_start_height == next_blocks_start_height) break; // switch to the new blocks from the daemon @@ -2854,74 +2853,7 @@ std::vector<std::vector<cryptonote::tx_destination_entry>> split_amounts( return retVal; } } // anonymous namespace - -/** - * @brief gets a monero address from the TXT record of a DNS entry - * - * gets the monero address from the TXT record of the DNS entry associated - * with <url>. If this lookup fails, or the TXT record does not contain an - * XMR address in the correct format, returns an empty string. <dnssec_valid> - * will be set true or false according to whether or not the DNS query passes - * DNSSEC validation. - * - * @param url the url to look up - * @param dnssec_valid return-by-reference for DNSSEC status of query - * - * @return a monero address (as a string) or an empty string - */ -std::vector<std::string> wallet2::addresses_from_url(const std::string& url, bool& dnssec_valid) -{ - std::vector<std::string> addresses; - // get txt records - bool dnssec_available, dnssec_isvalid; - std::string oa_addr = tools::DNSResolver::instance().get_dns_format_from_oa_address(url); - auto records = tools::DNSResolver::instance().get_txt_record(oa_addr, dnssec_available, dnssec_isvalid); - - // TODO: update this to allow for conveying that dnssec was not available - if (dnssec_available && dnssec_isvalid) - { - dnssec_valid = true; - } - else dnssec_valid = false; - - // for each txt record, try to find a monero address in it. - for (auto& rec : records) - { - std::string addr = address_from_txt_record(rec); - if (addr.size()) - { - addresses.push_back(addr); - } - } - - return addresses; -} - //---------------------------------------------------------------------------------------------------- -// TODO: parse the string in a less stupid way, probably with regex -std::string wallet2::address_from_txt_record(const std::string& s) -{ - // make sure the txt record has "oa1:xmr" and find it - auto pos = s.find("oa1:xmr"); - - // search from there to find "recipient_address=" - pos = s.find("recipient_address=", pos); - - pos += 18; // move past "recipient_address=" - - // find the next semicolon - auto pos2 = s.find(";", pos); - if (pos2 != std::string::npos) - { - // length of address == 95, we can at least validate that much here - if (pos2 - pos == 95) - { - return s.substr(pos, 95); - } - } - return std::string(); -} - crypto::hash wallet2::get_payment_id(const pending_tx &ptx) const { std::vector<tx_extra_field> tx_extra_fields; @@ -3521,6 +3453,23 @@ void wallet2::get_outs(std::vector<std::vector<entry>> &outs, const std::list<si outs.back().reserve(fake_outputs_count + 1); const rct::key mask = td.is_rct() ? rct::commit(td.amount(), td.m_mask) : rct::zeroCommit(td.amount()); + // make sure the real outputs we asked for are really included, along + // with the correct key and mask: this guards against an active attack + // where the node sends dummy data for all outputs, and we then send + // the real one, which the node can then tell from the fake outputs, + // as it has different data than the dummy data it had sent earlier + bool real_out_found = false; + for (size_t n = 0; n < requested_outputs_count; ++n) + { + size_t i = base + n; + if (req.outputs[i].index == td.m_global_output_index) + if (daemon_resp.outs[i].key == boost::get<txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key) + if (daemon_resp.outs[i].mask == mask) + real_out_found = true; + } + THROW_WALLET_EXCEPTION_IF(!real_out_found, error::wallet_internal_error, + "Daemon response did not include the requested real output"); + // pick real out first (it will be sorted when done) outs.back().push_back(std::make_tuple(td.m_global_output_index, boost::get<txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key, mask)); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 54e26008b..b69c477e9 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -503,10 +503,6 @@ namespace tools static bool parse_short_payment_id(const std::string& payment_id_str, crypto::hash8& payment_id); static bool parse_payment_id(const std::string& payment_id_str, crypto::hash& payment_id); - static std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid); - - static std::string address_from_txt_record(const std::string& s); - bool always_confirm_transfers() const { return m_always_confirm_transfers; } void always_confirm_transfers(bool always) { m_always_confirm_transfers = always; } bool store_tx_info() const { return m_store_tx_info; } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 2e1d95b58..a7162afcb 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -562,6 +562,12 @@ struct WalletManager //! returns current mining hash rate (0 if not mining) virtual double miningHashRate() const = 0; + //! returns current hard fork info + virtual void hardForkInfo(uint8_t &version, uint64_t &earliest_height) const = 0; + + //! returns current block target + virtual uint64_t blockTarget() const = 0; + //! resolves an OpenAlias address to a monero address virtual std::string resolveOpenAlias(const std::string &address, bool &dnssec_valid) const = 0; }; |