Coverage Report

Created: 2026-07-14 18:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/rpc/signmessage.cpp
Line
Count
Source
1
// Copyright (c) 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
#include <key.h>
8
#include <key_io.h>
9
#include <rpc/protocol.h>
10
#include <rpc/request.h>
11
#include <rpc/server.h>
12
#include <rpc/util.h>
13
#include <univalue.h>
14
15
#include <string>
16
17
static RPCMethod verifymessage()
18
54
{
19
54
    return RPCMethod{"verifymessage",
20
54
        "Verify a signed message.",
21
54
        {
22
54
            {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the signature."},
23
54
            {"signature", RPCArg::Type::STR, RPCArg::Optional::NO, "The signature provided by the signer in base 64 encoding (see signmessage)."},
24
54
            {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message that was signed."},
25
54
        },
26
54
        RPCResult{
27
54
            RPCResult::Type::BOOL, "", "If the signature is verified or not."
28
54
        },
29
54
        RPCExamples{
30
54
            "\nUnlock the wallet for 30 seconds\n"
31
54
            + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
32
54
            "\nCreate the signature\n"
33
54
            + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
34
54
            "\nVerify the signature\n"
35
54
            + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
36
54
            "\nAs a JSON-RPC call\n"
37
54
            + HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"")
38
54
        },
39
54
        [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
40
54
        {
41
0
            switch (MessageVerify(std::string{self.Arg<std::string_view>("address")},
  Branch (41:21): [True: 0, False: 0]
42
0
                                  std::string{self.Arg<std::string_view>("signature")},
43
0
                                  std::string{self.Arg<std::string_view>("message")})) {
44
0
            case MessageVerificationResult::ERR_INVALID_ADDRESS:
  Branch (44:13): [True: 0, False: 0]
45
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
46
0
            case MessageVerificationResult::ERR_ADDRESS_NO_KEY:
  Branch (46:13): [True: 0, False: 0]
47
0
                throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
48
0
            case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:
  Branch (48:13): [True: 0, False: 0]
49
0
                throw JSONRPCError(RPC_TYPE_ERROR, "Malformed base64 encoding");
50
0
            case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:
  Branch (50:13): [True: 0, False: 0]
51
0
            case MessageVerificationResult::ERR_NOT_SIGNED:
  Branch (51:13): [True: 0, False: 0]
52
0
                return false;
53
0
            case MessageVerificationResult::OK:
  Branch (53:13): [True: 0, False: 0]
54
0
                return true;
55
0
            }
56
57
0
            return false;
58
0
        },
59
54
    };
60
54
}
61
62
static RPCMethod signmessagewithprivkey()
63
54
{
64
54
    return RPCMethod{
65
54
        "signmessagewithprivkey",
66
54
        "Sign a message with the private key of an address\n",
67
54
        {
68
54
            {"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."},
69
54
            {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
70
54
        },
71
54
        RPCResult{
72
54
            RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
73
54
        },
74
54
        RPCExamples{
75
54
            "\nCreate the signature\n"
76
54
            + HelpExampleCli("signmessagewithprivkey", "\"privkey\" \"my message\"") +
77
54
            "\nVerify the signature\n"
78
54
            + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
79
54
            "\nAs a JSON-RPC call\n"
80
54
            + HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
81
54
        },
82
54
        [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
83
54
        {
84
0
            std::string strPrivkey = request.params[0].get_str();
85
0
            std::string strMessage = request.params[1].get_str();
86
87
0
            CKey key = DecodeSecret(strPrivkey);
88
0
            if (!key.IsValid()) {
  Branch (88:17): [True: 0, False: 0]
89
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
90
0
            }
91
92
0
            std::string signature;
93
94
0
            if (!MessageSign(key, strMessage, signature)) {
  Branch (94:17): [True: 0, False: 0]
95
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
96
0
            }
97
98
0
            return signature;
99
0
        },
100
54
    };
101
54
}
102
103
void RegisterSignMessageRPCCommands(CRPCTable& t)
104
27
{
105
27
    static const CRPCCommand commands[]{
106
27
        {"util", &verifymessage},
107
27
        {"util", &signmessagewithprivkey},
108
27
    };
109
54
    for (const auto& c : commands) {
  Branch (109:24): [True: 54, False: 27]
110
54
        t.appendCommand(c.name, &c);
111
54
    }
112
27
}