Branch data Line data Source code
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 : : #include <common/signmessage.h>
7 : :
8 : : #include <addresstype.h>
9 : : #include <hash.h>
10 : : #include <key.h>
11 : : #include <key_io.h>
12 : : #include <pubkey.h>
13 : : #include <uint256.h>
14 : : #include <util/check.h>
15 : : #include <util/strencodings.h>
16 : :
17 : : #include <optional>
18 : : #include <span>
19 : : #include <string>
20 : : #include <variant>
21 : : #include <vector>
22 : :
23 : : /**
24 : : * Text used to signify that a signed message follows and to prevent
25 : : * inadvertently signing a transaction.
26 : : */
27 : : const std::string MESSAGE_MAGIC = "Bitcoin Signed Message:\n";
28 : :
29 : 7 : MessageVerificationResult MessageVerify(
30 : : const std::string& address,
31 : : const std::string& signature,
32 : : const std::string& message)
33 : : {
34 : 7 : CTxDestination destination = DecodeDestination(address);
35 [ + - + + ]: 7 : if (!IsValidDestination(destination)) {
36 : : return MessageVerificationResult::ERR_INVALID_ADDRESS;
37 : : }
38 : :
39 [ + + ]: 12 : if (std::get_if<PKHash>(&destination) == nullptr) {
40 : : return MessageVerificationResult::ERR_ADDRESS_NO_KEY;
41 : : }
42 : :
43 [ - + + - ]: 5 : auto signature_bytes = DecodeBase64(signature);
44 [ + + ]: 5 : if (!signature_bytes) {
45 : : return MessageVerificationResult::ERR_MALFORMED_SIGNATURE;
46 : : }
47 : :
48 [ + - ]: 4 : CPubKey pubkey;
49 [ + - + - : 4 : if (!pubkey.RecoverCompact(MessageHash(message), *signature_bytes)) {
+ + ]
50 : : return MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED;
51 : : }
52 : :
53 [ + - + - : 6 : if (!(PKHash(pubkey) == *std::get_if<PKHash>(&destination))) {
+ + ]
54 : 1 : return MessageVerificationResult::ERR_NOT_SIGNED;
55 : : }
56 : :
57 : : return MessageVerificationResult::OK;
58 : 12 : }
59 : :
60 : 2 : bool MessageSign(
61 : : const CKey& privkey,
62 : : const std::string& message,
63 : : std::string& signature)
64 : : {
65 : 2 : std::vector<unsigned char> signature_bytes;
66 : :
67 [ + - + - : 2 : if (!privkey.SignCompact(MessageHash(message), signature_bytes)) {
+ + ]
68 : : return false;
69 : : }
70 : :
71 [ - + + - ]: 1 : signature = EncodeBase64(signature_bytes);
72 : :
73 : 1 : return true;
74 : 2 : }
75 : :
76 : 7 : uint256 MessageHash(const std::string& message)
77 : : {
78 : 7 : HashWriter hasher{};
79 : 7 : hasher << MESSAGE_MAGIC << message;
80 : :
81 : 7 : return hasher.GetHash();
82 : : }
83 : :
84 : 0 : std::string SigningResultString(const SigningResult res)
85 : : {
86 [ # # # # ]: 0 : switch (res) {
87 : 0 : case SigningResult::OK:
88 : 0 : return "No error";
89 : 0 : case SigningResult::PRIVATE_KEY_NOT_AVAILABLE:
90 : 0 : return "Private key not available";
91 : 0 : case SigningResult::SIGNING_FAILED:
92 : 0 : return "Sign failed";
93 : : } // no default case, so the compiler can warn about missing cases
94 : 0 : assert(false);
95 : : }
|