Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/wallet/rpc/signmessage.cpp
Line
Count
Source
1
// Copyright (c) 2011-2022 The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <common/signmessage.h>
6
#include <key_io.h>
7
#include <rpc/util.h>
8
#include <wallet/rpc/util.h>
9
#include <wallet/wallet.h>
10
11
#include <univalue.h>
12
13
namespace wallet {
14
RPCHelpMan signmessage()
15
22.1k
{
16
22.1k
    return RPCHelpMan{
17
22.1k
        "signmessage",
18
22.1k
        "Sign a message with the private key of an address" +
19
22.1k
          HELP_REQUIRING_PASSPHRASE,
20
22.1k
        {
21
22.1k
            {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the private key."},
22
22.1k
            {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
23
22.1k
        },
24
22.1k
        RPCResult{
25
22.1k
            RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
26
22.1k
        },
27
22.1k
        RPCExamples{
28
22.1k
            "\nUnlock the wallet for 30 seconds\n"
29
22.1k
            + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
30
22.1k
            "\nCreate the signature\n"
31
22.1k
            + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
32
22.1k
            "\nVerify the signature\n"
33
22.1k
            + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
34
22.1k
            "\nAs a JSON-RPC call\n"
35
22.1k
            + HelpExampleRpc("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")
36
22.1k
        },
37
22.1k
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
38
22.1k
        {
39
0
            const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
40
0
            if (!pwallet) return UniValue::VNULL;
  Branch (40:17): [True: 0, False: 0]
41
42
0
            LOCK(pwallet->cs_wallet);
43
44
0
            EnsureWalletIsUnlocked(*pwallet);
45
46
0
            std::string strAddress = request.params[0].get_str();
47
0
            std::string strMessage = request.params[1].get_str();
48
49
0
            CTxDestination dest = DecodeDestination(strAddress);
50
0
            if (!IsValidDestination(dest)) {
  Branch (50:17): [True: 0, False: 0]
51
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
52
0
            }
53
54
0
            const PKHash* pkhash = std::get_if<PKHash>(&dest);
55
0
            if (!pkhash) {
  Branch (55:17): [True: 0, False: 0]
56
0
                throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
57
0
            }
58
59
0
            std::string signature;
60
0
            SigningResult err = pwallet->SignMessage(strMessage, *pkhash, signature);
61
0
            if (err == SigningResult::SIGNING_FAILED) {
  Branch (61:17): [True: 0, False: 0]
62
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, SigningResultString(err));
63
0
            } else if (err != SigningResult::OK) {
  Branch (63:24): [True: 0, False: 0]
64
0
                throw JSONRPCError(RPC_WALLET_ERROR, SigningResultString(err));
65
0
            }
66
67
0
            return signature;
68
0
        },
69
22.1k
    };
70
22.1k
}
71
} // namespace wallet