1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-present The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 6 #ifndef BITCOIN_COMMON_SIGNMESSAGE_H
7 #define BITCOIN_COMMON_SIGNMESSAGE_H
8 9 #include <uint256.h>
10 11 #include <string>
12 13 class CKey;
14 15 extern const std::string MESSAGE_MAGIC;
16 17 /** The result of a signed message verification.
18 * Message verification takes as an input:
19 * - address (with whose private key the message is supposed to have been signed)
20 * - signature
21 * - message
22 */
23 enum class MessageVerificationResult {
24 //! The provided address is invalid.
25 ERR_INVALID_ADDRESS,
26 27 //! The provided address is valid but does not refer to a public key.
28 ERR_ADDRESS_NO_KEY,
29 30 //! The provided signature couldn't be parsed (maybe invalid base64).
31 ERR_MALFORMED_SIGNATURE,
32 33 //! A public key could not be recovered from the provided signature and message.
34 ERR_PUBKEY_NOT_RECOVERED,
35 36 //! The message was not signed with the private key of the provided address.
37 ERR_NOT_SIGNED,
38 39 //! The message verification was successful.
40 OK
41 };
42 43 enum class SigningResult {
44 OK, //!< No error
45 PRIVATE_KEY_NOT_AVAILABLE,
46 SIGNING_FAILED,
47 };
48 49 /** Verify a signed message.
50 * @param[in] address Signer's bitcoin address, it must refer to a public key.
51 * @param[in] signature The signature in base64 format.
52 * @param[in] message The message that was signed.
53 * @return result code */
54 MessageVerificationResult MessageVerify(
55 const std::string& address,
56 const std::string& signature,
57 const std::string& message);
58 59 /** Sign a message.
60 * @param[in] privkey Private key to sign with.
61 * @param[in] message The message to sign.
62 * @param[out] signature Signature, base64 encoded, only set if true is returned.
63 * @return true if signing was successful. */
64 bool MessageSign(
65 const CKey& privkey,
66 const std::string& message,
67 std::string& signature);
68 69 /**
70 * Hashes a message for signing and verification in a manner that prevents
71 * inadvertently signing a transaction.
72 */
73 uint256 MessageHash(const std::string& message);
74 75 std::string SigningResultString(SigningResult res);
76 77 #endif // BITCOIN_COMMON_SIGNMESSAGE_H
78