diff --git a/src/clientversion.h b/src/clientversion.h --- a/src/clientversion.h +++ b/src/clientversion.h @@ -49,6 +49,8 @@ extern const std::string CLIENT_BUILD; std::string FormatFullVersion(); +std::string FormatSubVersionUserAgent(const std::string &userAgent, + const std::vector &comments); std::string FormatSubVersion(const std::string &name, int nClientVersion, const std::vector &comments); diff --git a/src/clientversion.cpp b/src/clientversion.cpp --- a/src/clientversion.cpp +++ b/src/clientversion.cpp @@ -60,14 +60,15 @@ } /** - * Format the subversion field according to BIP 14 spec + * Format the subversion field according to BIP 14 spec using given userAgent. * (https://github.com/bitcoin/bips/blob/master/bip-0014.mediawiki) */ -std::string FormatSubVersion(const std::string &name, int nClientVersion, - const std::vector &comments) { +std::string +FormatSubVersionUserAgent(const std::string &userAgent, + const std::vector &comments) { std::ostringstream ss; ss << "/"; - ss << name << ":" << FormatVersion(nClientVersion); + ss << userAgent; if (!comments.empty()) { std::vector::const_iterator it(comments.begin()); ss << "(" << *it; @@ -79,3 +80,14 @@ ss << "/"; return ss.str(); } + +/** + * Format the subversion field according to BIP 14 spec + * (https://github.com/bitcoin/bips/blob/master/bip-0014.mediawiki) + */ +std::string FormatSubVersion(const std::string &name, int nClientVersion, + const std::vector &comments) { + std::ostringstream ss; + ss << name << ":" << FormatVersion(nClientVersion); + return FormatSubVersionUserAgent(ss.str(), comments); +} diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1852,6 +1852,25 @@ std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/")); } +BOOST_AUTO_TEST_CASE(test_FormatSubVersionUserAgent) { + std::vector comments; + comments.push_back(std::string("comment1")); + std::vector comments2; + comments2.push_back(std::string("comment1")); + // Semicolon is discouraged but not forbidden by BIP-0014 + comments2.push_back(SanitizeString( + std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), + SAFE_CHARS_UA_COMMENT)); + BOOST_CHECK_EQUAL( + FormatSubVersionUserAgent("Test:0.9.99", std::vector()), + std::string("/Test:0.9.99/")); + BOOST_CHECK_EQUAL(FormatSubVersionUserAgent("Test:0.9.99", comments), + std::string("/Test:0.9.99(comment1)/")); + BOOST_CHECK_EQUAL( + FormatSubVersionUserAgent("Test:0.9.99", comments2), + std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/")); +} + BOOST_AUTO_TEST_CASE(test_ParseFixedPoint) { int64_t amount = 0; BOOST_CHECK(ParseFixedPoint("0", 8, &amount));