Coverage Report

Created: 2025-06-10 13:21

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