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