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/util/message.cpp | |
parent | 2ce3447eb1e25ec7aec4b300dabf6c1e394f1906 (diff) | |
download | bitcoin-f8f0d9893d7969bdaa870fadb94ec5d0dfa8334d.tar.xz |
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/util/message.cpp')
-rw-r--r-- | src/util/message.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/util/message.cpp b/src/util/message.cpp index 33e9c3384b..22ace2dd75 100644 --- a/src/util/message.cpp +++ b/src/util/message.cpp @@ -4,6 +4,7 @@ // 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 @@ -51,3 +52,23 @@ MessageVerificationResult MessageVerify( return MessageVerificationResult::OK; } + +bool MessageSign( + const CKey& privkey, + 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)) { + return false; + } + + signature = EncodeBase64(signature_bytes.data(), signature_bytes.size()); + + return true; +} |