diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2019-11-22 17:17:29 +0100 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2020-02-14 10:45:40 +0100 |
commit | f8f0d9893d7969bdaa870fadb94ec5d0dfa8334d (patch) | |
tree | 21484d5e11ebf6cf1f707805ce766b397637beba /src/test/util_tests.cpp | |
parent | 2ce3447eb1e25ec7aec4b300dabf6c1e394f1906 (diff) |
Deduplicate the message signing code
The logic of signing a message was duplicated in 3 places:
src/qt/signverifymessagedialog.cpp
SignVerifyMessageDialog::on_signMessageButton_SM_clicked()
src/rpc/misc.cpp
signmessagewithprivkey()
src/wallet/rpcwallet.cpp
signmessage()
Move the logic into
src/util/message.cpp
MessageSign()
and call it from all the 3 places.
Diffstat (limited to 'src/test/util_tests.cpp')
-rw-r--r-- | src/test/util_tests.cpp | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 0b40df0b37..8a2553617b 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -5,11 +5,12 @@ #include <util/system.h> #include <clientversion.h> +#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 MessageVerify() +#include <util/message.h> // For MessageSign(), MessageVerify() #include <util/moneystr.h> #include <util/strencodings.h> #include <util/string.h> @@ -17,6 +18,7 @@ #include <util/spanparsing.h> #include <util/vector.h> +#include <array> #include <stdint.h> #include <thread> #include <univalue.h> @@ -2026,6 +2028,42 @@ BOOST_AUTO_TEST_CASE(test_tracked_vector) BOOST_CHECK_EQUAL(v8[2].copies, 0); } +BOOST_AUTO_TEST_CASE(message_sign) +{ + const std::array<unsigned char, 32> privkey_bytes = { + // just some random data + // derived address from this private key: 15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs + 0xD9, 0x7F, 0x51, 0x08, 0xF1, 0x1C, 0xDA, 0x6E, + 0xEE, 0xBA, 0xAA, 0x42, 0x0F, 0xEF, 0x07, 0x26, + 0xB1, 0xF8, 0x98, 0x06, 0x0B, 0x98, 0x48, 0x9F, + 0xA3, 0x09, 0x84, 0x63, 0xC0, 0x03, 0x28, 0x66 + }; + + const std::string message = "Trust no one"; + + const std::string expected_signature = + "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk="; + + CKey privkey; + std::string generated_signature; + + BOOST_REQUIRE_MESSAGE(!privkey.IsValid(), + "Confirm the private key is invalid"); + + BOOST_CHECK_MESSAGE(!MessageSign(privkey, message, generated_signature), + "Sign with an invalid private key"); + + privkey.Set(privkey_bytes.begin(), privkey_bytes.end(), true); + + BOOST_REQUIRE_MESSAGE(privkey.IsValid(), + "Confirm the private key is valid"); + + BOOST_CHECK_MESSAGE(MessageSign(privkey, message, generated_signature), + "Sign with a valid private key"); + + BOOST_CHECK_EQUAL(expected_signature, generated_signature); +} + BOOST_AUTO_TEST_CASE(message_verify) { BOOST_CHECK_EQUAL( |