diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/message.cpp | 21 | ||||
-rw-r--r-- | src/util/message.h | 12 |
2 files changed, 33 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; +} diff --git a/src/util/message.h b/src/util/message.h index 6adda5d3ff..1a1ba88daf 100644 --- a/src/util/message.h +++ b/src/util/message.h @@ -6,6 +6,8 @@ #ifndef BITCOIN_UTIL_MESSAGE_H #define BITCOIN_UTIL_MESSAGE_H +#include <key.h> // For CKey + #include <string> extern const std::string strMessageMagic; @@ -46,4 +48,14 @@ MessageVerificationResult MessageVerify( 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); + #endif // BITCOIN_UTIL_MESSAGE_H |