aboutsummaryrefslogtreecommitdiff
path: root/src/test/util_tests.cpp
diff options
context:
space:
mode:
authorJeffrey Czyz <jkczyz@gmail.com>2019-12-07 20:52:38 +0100
committerVasil Dimov <vd@FreeBSD.org>2020-02-14 10:45:41 +0100
commite193a84fb28068e38d5f54fbfd6208428c5bb655 (patch)
tree3d9e920c26f874da2d9c06da07843d3fd15f4458 /src/test/util_tests.cpp
parentf8f0d9893d7969bdaa870fadb94ec5d0dfa8334d (diff)
downloadbitcoin-e193a84fb28068e38d5f54fbfd6208428c5bb655.tar.xz
Refactor message hashing into a utility function
And add unit test for it. The purpose of using a preamble or "magic" text as part of signing and verifying a message was not given when the code was repeated in a few locations. Make a test showing how it is used to prevent inadvertently signing a transaction.
Diffstat (limited to 'src/test/util_tests.cpp')
-rw-r--r--src/test/util_tests.cpp21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index 8a2553617b..f86e713676 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -5,12 +5,14 @@
#include <util/system.h>
#include <clientversion.h>
+#include <hash.h> // For Hash()
#include <key.h> // For CKey
#include <optional.h>
#include <sync.h>
#include <test/util/setup_common.h>
#include <test/util/str.h>
-#include <util/message.h> // For MessageSign(), MessageVerify()
+#include <uint256.h>
+#include <util/message.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
#include <util/moneystr.h>
#include <util/strencodings.h>
#include <util/string.h>
@@ -2116,4 +2118,21 @@ BOOST_AUTO_TEST_CASE(message_verify)
MessageVerificationResult::OK);
}
+BOOST_AUTO_TEST_CASE(message_hash)
+{
+ const std::string unsigned_tx = "...";
+ const std::string prefixed_message =
+ std::string(1, (char)MESSAGE_MAGIC.length()) +
+ MESSAGE_MAGIC +
+ std::string(1, (char)unsigned_tx.length()) +
+ unsigned_tx;
+
+ const uint256 signature_hash = Hash(unsigned_tx.begin(), unsigned_tx.end());
+ const uint256 message_hash1 = Hash(prefixed_message.begin(), prefixed_message.end());
+ const uint256 message_hash2 = MessageHash(unsigned_tx);
+
+ BOOST_CHECK_EQUAL(message_hash1, message_hash2);
+ BOOST_CHECK_NE(message_hash1, signature_hash);
+}
+
BOOST_AUTO_TEST_SUITE_END()