aboutsummaryrefslogtreecommitdiff
path: root/src/util
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/util
parentf8f0d9893d7969bdaa870fadb94ec5d0dfa8334d (diff)
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/util')
-rw-r--r--src/util/message.cpp26
-rw-r--r--src/util/message.h9
2 files changed, 23 insertions, 12 deletions
diff --git a/src/util/message.cpp b/src/util/message.cpp
index 22ace2dd75..17603a43d2 100644
--- a/src/util/message.cpp
+++ b/src/util/message.cpp
@@ -15,7 +15,11 @@
#include <string>
#include <vector>
-const std::string strMessageMagic = "Bitcoin Signed Message:\n";
+/**
+ * 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,
@@ -37,12 +41,8 @@ MessageVerificationResult MessageVerify(
return MessageVerificationResult::ERR_MALFORMED_SIGNATURE;
}
- CHashWriter ss(SER_GETHASH, 0);
- ss << strMessageMagic;
- ss << message;
-
CPubKey pubkey;
- if (!pubkey.RecoverCompact(ss.GetHash(), signature_bytes)) {
+ if (!pubkey.RecoverCompact(MessageHash(message), signature_bytes)) {
return MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED;
}
@@ -58,13 +58,9 @@ bool MessageSign(
const std::string& message,
std::string& signature)
{
- CHashWriter ss(SER_GETHASH, 0);
- ss << strMessageMagic;
- ss << message;
-
std::vector<unsigned char> signature_bytes;
- if (!privkey.SignCompact(ss.GetHash(), signature_bytes)) {
+ if (!privkey.SignCompact(MessageHash(message), signature_bytes)) {
return false;
}
@@ -72,3 +68,11 @@ bool MessageSign(
return true;
}
+
+uint256 MessageHash(const std::string& message)
+{
+ CHashWriter hasher(SER_GETHASH, 0);
+ hasher << MESSAGE_MAGIC << message;
+
+ return hasher.GetHash();
+}
diff --git a/src/util/message.h b/src/util/message.h
index 1a1ba88daf..01fd14ce2d 100644
--- a/src/util/message.h
+++ b/src/util/message.h
@@ -7,10 +7,11 @@
#define BITCOIN_UTIL_MESSAGE_H
#include <key.h> // For CKey
+#include <uint256.h>
#include <string>
-extern const std::string strMessageMagic;
+extern const std::string MESSAGE_MAGIC;
/** The result of a signed message verification.
* Message verification takes as an input:
@@ -58,4 +59,10 @@ bool MessageSign(
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);
+
#endif // BITCOIN_UTIL_MESSAGE_H