diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/message.cpp | 92 | ||||
-rw-r--r-- | src/util/message.h | 76 | ||||
-rw-r--r-- | src/util/moneystr.cpp | 24 | ||||
-rw-r--r-- | src/util/moneystr.h | 2 | ||||
-rw-r--r-- | src/util/string.h | 14 | ||||
-rw-r--r-- | src/util/system.cpp | 10 | ||||
-rw-r--r-- | src/util/system.h | 3 | ||||
-rw-r--r-- | src/util/time.cpp | 37 | ||||
-rw-r--r-- | src/util/time.h | 4 | ||||
-rw-r--r-- | src/util/validation.cpp | 25 | ||||
-rw-r--r-- | src/util/validation.h | 18 |
11 files changed, 222 insertions, 83 deletions
diff --git a/src/util/message.cpp b/src/util/message.cpp new file mode 100644 index 0000000000..1e7128d225 --- /dev/null +++ b/src/util/message.cpp @@ -0,0 +1,92 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2020 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include <hash.h> // For CHashWriter +#include <key.h> // For CKey +#include <key_io.h> // For DecodeDestination() +#include <pubkey.h> // For CPubKey +#include <script/standard.h> // For CTxDestination, IsValidDestination(), PKHash +#include <serialize.h> // For SER_GETHASH +#include <util/message.h> +#include <util/strencodings.h> // For DecodeBase64() + +#include <string> +#include <vector> + +/** + * Text used to signify that a signed message follows and to prevent + * inadvertently signing a transaction. + */ +const std::string MESSAGE_MAGIC = "Bitcoin Signed Message:\n"; + +MessageVerificationResult MessageVerify( + const std::string& address, + const std::string& signature, + const std::string& message) +{ + CTxDestination destination = DecodeDestination(address); + if (!IsValidDestination(destination)) { + return MessageVerificationResult::ERR_INVALID_ADDRESS; + } + + if (boost::get<PKHash>(&destination) == nullptr) { + return MessageVerificationResult::ERR_ADDRESS_NO_KEY; + } + + bool invalid = false; + std::vector<unsigned char> signature_bytes = DecodeBase64(signature.c_str(), &invalid); + if (invalid) { + return MessageVerificationResult::ERR_MALFORMED_SIGNATURE; + } + + CPubKey pubkey; + if (!pubkey.RecoverCompact(MessageHash(message), signature_bytes)) { + return MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED; + } + + if (!(CTxDestination(PKHash(pubkey)) == destination)) { + return MessageVerificationResult::ERR_NOT_SIGNED; + } + + return MessageVerificationResult::OK; +} + +bool MessageSign( + const CKey& privkey, + const std::string& message, + std::string& signature) +{ + std::vector<unsigned char> signature_bytes; + + if (!privkey.SignCompact(MessageHash(message), signature_bytes)) { + return false; + } + + signature = EncodeBase64(signature_bytes.data(), signature_bytes.size()); + + return true; +} + +uint256 MessageHash(const std::string& message) +{ + CHashWriter hasher(SER_GETHASH, 0); + hasher << MESSAGE_MAGIC << message; + + return hasher.GetHash(); +} + +std::string SigningResultString(const SigningResult res) +{ + switch (res) { + case SigningResult::OK: + return "No error"; + case SigningResult::PRIVATE_KEY_NOT_AVAILABLE: + return "Private key not available"; + case SigningResult::SIGNING_FAILED: + return "Sign failed"; + // no default case, so the compiler can warn about missing cases + } + assert(false); +} diff --git a/src/util/message.h b/src/util/message.h new file mode 100644 index 0000000000..b31c5f5761 --- /dev/null +++ b/src/util/message.h @@ -0,0 +1,76 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2020 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_UTIL_MESSAGE_H +#define BITCOIN_UTIL_MESSAGE_H + +#include <key.h> // For CKey +#include <uint256.h> + +#include <string> + +extern const std::string MESSAGE_MAGIC; + +/** The result of a signed message verification. + * Message verification takes as an input: + * - address (with whose private key the message is supposed to have been signed) + * - signature + * - message + */ +enum class MessageVerificationResult { + //! The provided address is invalid. + ERR_INVALID_ADDRESS, + + //! The provided address is valid but does not refer to a public key. + ERR_ADDRESS_NO_KEY, + + //! The provided signature couldn't be parsed (maybe invalid base64). + ERR_MALFORMED_SIGNATURE, + + //! A public key could not be recovered from the provided signature and message. + ERR_PUBKEY_NOT_RECOVERED, + + //! The message was not signed with the private key of the provided address. + ERR_NOT_SIGNED, + + //! The message verification was successful. + OK +}; + +enum class SigningResult { + OK, //!< No error + PRIVATE_KEY_NOT_AVAILABLE, + SIGNING_FAILED, +}; + +/** Verify a signed message. + * @param[in] address Signer's bitcoin address, it must refer to a public key. + * @param[in] signature The signature in base64 format. + * @param[in] message The message that was signed. + * @return result code */ +MessageVerificationResult MessageVerify( + const std::string& address, + const std::string& signature, + const std::string& message); + +/** Sign a message. + * @param[in] privkey Private key to sign with. + * @param[in] message The message to sign. + * @param[out] signature Signature, base64 encoded, only set if true is returned. + * @return true if signing was successful. */ +bool MessageSign( + const CKey& privkey, + const std::string& message, + std::string& signature); + +/** + * Hashes a message for signing and verification in a manner that prevents + * inadvertently signing a transaction. + */ +uint256 MessageHash(const std::string& message); + +std::string SigningResultString(const SigningResult res); + +#endif // BITCOIN_UTIL_MESSAGE_H diff --git a/src/util/moneystr.cpp b/src/util/moneystr.cpp index 2797f450ca..544cfb58f9 100644 --- a/src/util/moneystr.cpp +++ b/src/util/moneystr.cpp @@ -31,21 +31,19 @@ std::string FormatMoney(const CAmount& n) } -bool ParseMoney(const std::string& str, CAmount& nRet) +bool ParseMoney(const std::string& money_string, CAmount& nRet) { - if (!ValidAsCString(str)) { + if (!ValidAsCString(money_string)) { + return false; + } + const std::string str = TrimString(money_string); + if (str.empty()) { return false; } - return ParseMoney(str.c_str(), nRet); -} -bool ParseMoney(const char* pszIn, CAmount& nRet) -{ std::string strWhole; int64_t nUnits = 0; - const char* p = pszIn; - while (IsSpace(*p)) - p++; + const char* p = str.c_str(); for (; *p; p++) { if (*p == '.') @@ -60,14 +58,14 @@ bool ParseMoney(const char* pszIn, CAmount& nRet) break; } if (IsSpace(*p)) - break; + return false; if (!IsDigit(*p)) return false; strWhole.insert(strWhole.end(), *p); } - for (; *p; p++) - if (!IsSpace(*p)) - return false; + if (*p) { + return false; + } if (strWhole.size() > 10) // guard against 63 bit overflow return false; if (nUnits < 0 || nUnits > COIN) diff --git a/src/util/moneystr.h b/src/util/moneystr.h index 027c7e2e53..d8b08adc24 100644 --- a/src/util/moneystr.h +++ b/src/util/moneystr.h @@ -18,7 +18,7 @@ * JSON but use AmountFromValue and ValueFromAmount for that. */ std::string FormatMoney(const CAmount& n); +/** Parse an amount denoted in full coins. E.g. "0.0034" supplied on the command line. **/ NODISCARD bool ParseMoney(const std::string& str, CAmount& nRet); -NODISCARD bool ParseMoney(const char* pszIn, CAmount& nRet); #endif // BITCOIN_UTIL_MONEYSTR_H diff --git a/src/util/string.h b/src/util/string.h index 3db8fc8b98..694f0a1ca4 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -8,6 +8,8 @@ #include <attributes.h> #include <cstring> +#include <locale> +#include <sstream> #include <string> #include <vector> @@ -52,4 +54,16 @@ NODISCARD inline bool ValidAsCString(const std::string& str) noexcept return str.size() == strlen(str.c_str()); } +/** + * Locale-independent version of std::to_string + */ +template <typename T> +std::string ToString(const T& t) +{ + std::ostringstream oss; + oss.imbue(std::locale::classic()); + oss << t; + return oss.str(); +} + #endif // BITCOIN_UTIL_STRENCODINGS_H diff --git a/src/util/system.cpp b/src/util/system.cpp index 13ff99c663..b0a538b527 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -63,6 +63,7 @@ #include <malloc.h> #endif +#include <boost/algorithm/string/replace.hpp> #include <thread> #include <typeinfo> #include <univalue.h> @@ -1047,6 +1048,15 @@ fs::path GetSpecialFolderPath(int nFolder, bool fCreate) } #endif +#ifndef WIN32 +std::string ShellEscape(const std::string& arg) +{ + std::string escaped = arg; + boost::replace_all(escaped, "'", "'\"'\"'"); + return "'" + escaped + "'"; +} +#endif + #if HAVE_SYSTEM void runCommand(const std::string& strCommand) { diff --git a/src/util/system.h b/src/util/system.h index bb69181de9..3138522b5c 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -81,6 +81,9 @@ fs::path GetConfigFile(const std::string& confPath); #ifdef WIN32 fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true); #endif +#ifndef WIN32 +std::string ShellEscape(const std::string& arg); +#endif #if HAVE_SYSTEM void runCommand(const std::string& strCommand); #endif diff --git a/src/util/time.cpp b/src/util/time.cpp index 2afff2626b..14937b985e 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -11,10 +11,13 @@ #include <atomic> #include <boost/date_time/posix_time/posix_time.hpp> -#include <boost/thread.hpp> #include <ctime> +#include <thread> + #include <tinyformat.h> +void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); } + static std::atomic<int64_t> nMockTime(0); //!< For unit testing int64_t GetTime() @@ -72,32 +75,16 @@ int64_t GetSystemTimeInSeconds() return GetTimeMicros()/1000000; } -void MilliSleep(int64_t n) -{ - -/** - * Boost's sleep_for was uninterruptible when backed by nanosleep from 1.50 - * until fixed in 1.52. Use the deprecated sleep method for the broken case. - * See: https://svn.boost.org/trac/boost/ticket/7238 - */ -#if defined(HAVE_WORKING_BOOST_SLEEP_FOR) - boost::this_thread::sleep_for(boost::chrono::milliseconds(n)); -#elif defined(HAVE_WORKING_BOOST_SLEEP) - boost::this_thread::sleep(boost::posix_time::milliseconds(n)); -#else -//should never get here -#error missing boost sleep implementation -#endif -} - std::string FormatISO8601DateTime(int64_t nTime) { struct tm ts; time_t time_val = nTime; #ifdef _MSC_VER - gmtime_s(&ts, &time_val); + if (gmtime_s(&ts, &time_val) != 0) { #else - gmtime_r(&time_val, &ts); + if (gmtime_r(&time_val, &ts) == nullptr) { #endif + return {}; + } return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec); } @@ -105,10 +92,12 @@ std::string FormatISO8601Date(int64_t nTime) { struct tm ts; time_t time_val = nTime; #ifdef _MSC_VER - gmtime_s(&ts, &time_val); + if (gmtime_s(&ts, &time_val) != 0) { #else - gmtime_r(&time_val, &ts); + if (gmtime_r(&time_val, &ts) == nullptr) { #endif + return {}; + } return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday); } @@ -124,4 +113,4 @@ int64_t ParseISO8601DateTime(const std::string& str) if (ptime.is_not_a_date_time() || epoch > ptime) return 0; return (ptime - epoch).total_seconds(); -}
\ No newline at end of file +} diff --git a/src/util/time.h b/src/util/time.h index af4390aa1c..77de1e047d 100644 --- a/src/util/time.h +++ b/src/util/time.h @@ -10,6 +10,8 @@ #include <string> #include <chrono> +void UninterruptibleSleep(const std::chrono::microseconds& n); + /** * Helper to count the seconds of a duration. * @@ -36,8 +38,6 @@ void SetMockTime(int64_t nMockTimeIn); /** For testing */ int64_t GetMockTime(); -void MilliSleep(int64_t n); - /** Return system time (or mocked time, if set) */ template <typename T> T GetTime(); diff --git a/src/util/validation.cpp b/src/util/validation.cpp deleted file mode 100644 index 89bc6665a4..0000000000 --- a/src/util/validation.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2020 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#include <util/validation.h> - -#include <consensus/validation.h> -#include <tinyformat.h> - -std::string FormatStateMessage(const ValidationState &state) -{ - if (state.IsValid()) { - return "Valid"; - } - - const std::string debug_message = state.GetDebugMessage(); - if (!debug_message.empty()) { - return strprintf("%s, %s", state.GetRejectReason(), debug_message); - } - - return state.GetRejectReason(); -} - -const std::string strMessageMagic = "Bitcoin Signed Message:\n"; diff --git a/src/util/validation.h b/src/util/validation.h deleted file mode 100644 index da2cf9f102..0000000000 --- a/src/util/validation.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2019 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_UTIL_VALIDATION_H -#define BITCOIN_UTIL_VALIDATION_H - -#include <string> - -class ValidationState; - -/** Convert ValidationState to a human-readable message for logging */ -std::string FormatStateMessage(const ValidationState &state); - -extern const std::string strMessageMagic; - -#endif // BITCOIN_UTIL_VALIDATION_H |