From fee8424938a6403bdd12d568037a3305cfcf5341 Mon Sep 17 00:00:00 2001 From: warptangent Date: Tue, 19 May 2015 02:33:30 -0700 Subject: [PATCH] Allow name@domain.tld for OpenAlias lookups Based on tewinget's update. Make OpenAlias address format independent of existing DNS functions. Add tests. Test: make debug-test cd build/debug/tests/unit_tests # test that regular DNS functions work, including IPv4 lookups. # also test function that converts OpenAlias address format make && ./unit_tests --gtest_filter=DNSResolver* # test that OpenAlias addresses like donate@getmonero.org work from # wallet tools make && ./unit_tests --gtest_filter=AddressFromURL.Success --- src/common/dns_utils.cpp | 13 +++++++++++++ src/common/dns_utils.h | 12 ++++++++++++ src/wallet/wallet2.cpp | 3 ++- tests/unit_tests/address_from_url.cpp | 8 ++++++++ tests/unit_tests/dns_resolver.cpp | 8 ++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp index ea7f1078b..98e3d0ea6 100644 --- a/src/common/dns_utils.cpp +++ b/src/common/dns_utils.cpp @@ -304,6 +304,19 @@ std::vector DNSResolver::get_txt_record(const std::string& url, boo return records; } +std::string DNSResolver::get_dns_format_from_oa_address(const std::string& oa_addr) +{ + std::string addr(oa_addr); + auto first_at = addr.find("@"); + if (first_at == std::string::npos) + return addr; + + // convert name@domain.tld to name.domain.tld + addr.replace(first_at, 1, "."); + + return addr; +} + DNSResolver& DNSResolver::instance() { static DNSResolver* staticInstance = NULL; diff --git a/src/common/dns_utils.h b/src/common/dns_utils.h index a16c7eff7..d98523a30 100644 --- a/src/common/dns_utils.h +++ b/src/common/dns_utils.h @@ -100,6 +100,18 @@ public: // TODO: modify this to accomodate DNSSEC std::vector get_txt_record(const std::string& url, bool& dnssec_available, bool& dnssec_valid); + /** + * @brief Gets a DNS address from OpenAlias format + * + * If the address looks good, but contains one @ symbol, replace that with a . + * e.g. donate@getmonero.org becomes donate.getmonero.org + * + * @param oa_addr OpenAlias address + * + * @return dns_addr DNS address + */ + std::string get_dns_format_from_oa_address(const std::string& oa_addr); + /** * @brief Gets the singleton instance of DNSResolver * diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 6698e7296..7928e1c62 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1016,7 +1016,8 @@ std::vector wallet2::addresses_from_url(const std::string& url, boo std::vector addresses; // get txt records bool dnssec_available, dnssec_isvalid; - auto records = tools::DNSResolver::instance().get_txt_record(url, 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) diff --git a/tests/unit_tests/address_from_url.cpp b/tests/unit_tests/address_from_url.cpp index fe2f072de..99df75d74 100644 --- a/tests/unit_tests/address_from_url.cpp +++ b/tests/unit_tests/address_from_url.cpp @@ -93,6 +93,14 @@ TEST(AddressFromURL, Success) { EXPECT_STREQ(addr.c_str(), addresses[0].c_str()); } + + // OpenAlias address with an @ instead of first . + addresses = tools::wallet2::addresses_from_url("donate@getmonero.org", dnssec_result); + EXPECT_EQ(1, addresses.size()); + if (addresses.size() == 1) + { + EXPECT_STREQ(addr.c_str(), addresses[0].c_str()); + } } TEST(AddressFromURL, Failure) diff --git a/tests/unit_tests/dns_resolver.cpp b/tests/unit_tests/dns_resolver.cpp index 6717e990a..0709a773f 100644 --- a/tests/unit_tests/dns_resolver.cpp +++ b/tests/unit_tests/dns_resolver.cpp @@ -148,4 +148,12 @@ TEST(DNSResolver, GetTXTRecord) { std::cout << "TXT record for donate.getmonero.org: " << rec << std::endl; } + + // replace first @ with . + std::string addr = tools::DNSResolver::instance().get_dns_format_from_oa_address("donate@getmonero.org"); + EXPECT_STREQ("donate.getmonero.org", addr.c_str()); + + // no change + addr = tools::DNSResolver::instance().get_dns_format_from_oa_address("donate.getmonero.org"); + EXPECT_STREQ("donate.getmonero.org", addr.c_str()); }