Coverage Report

Created: 2026-08-05 14:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/common/signmessage.cpp
Line
Count
Source
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
MessageVerificationResult MessageVerify(
30
    const std::string& address,
31
    const std::string& signature,
32
    const std::string& message)
33
20
{
34
20
    CTxDestination destination = DecodeDestination(address);
35
20
    if (!IsValidDestination(destination)) {
36
2
        return MessageVerificationResult::ERR_INVALID_ADDRESS;
37
2
    }
38
39
18
    if (std::get_if<PKHash>(&destination) == nullptr) {
40
3
        return MessageVerificationResult::ERR_ADDRESS_NO_KEY;
41
3
    }
42
43
15
    auto signature_bytes = DecodeBase64(signature);
44
15
    if (!signature_bytes) {
45
2
        return MessageVerificationResult::ERR_MALFORMED_SIGNATURE;
46
2
    }
47
48
13
    CPubKey pubkey;
49
13
    if (!pubkey.RecoverCompact(MessageHash(message), *signature_bytes)) {
50
1
        return MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED;
51
1
    }
52
53
12
    if (!(PKHash(pubkey) == *std::get_if<PKHash>(&destination))) {
54
3
        return MessageVerificationResult::ERR_NOT_SIGNED;
55
3
    }
56
57
9
    return MessageVerificationResult::OK;
58
12
}
59
60
bool MessageSign(
61
    const CKey& privkey,
62
    const std::string& message,
63
    std::string& signature)
64
12
{
65
12
    std::vector<unsigned char> signature_bytes;
66
67
12
    if (!privkey.SignCompact(MessageHash(message), signature_bytes)) {
68
1
        return false;
69
1
    }
70
71
11
    signature = EncodeBase64(signature_bytes);
72
73
11
    return true;
74
12
}
75
76
uint256 MessageHash(const std::string& message)
77
26
{
78
26
    HashWriter hasher{};
79
26
    hasher << MESSAGE_MAGIC << message;
80
81
26
    return hasher.GetHash();
82
26
}
83
84
std::string SigningResultString(const SigningResult res)
85
0
{
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
0
    } // no default case, so the compiler can warn about missing cases
94
0
    assert(false);
95
0
}