fuzz coverage

Coverage Report

Created: 2025-08-28 15:26

/Users/eugenesiegel/btc/bitcoin/src/rpc/output_script.cpp
Line
Count
Source (jump to first uncovered line)
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 <key_io.h>
7
#include <outputtype.h>
8
#include <pubkey.h>
9
#include <rpc/protocol.h>
10
#include <rpc/request.h>
11
#include <rpc/server.h>
12
#include <rpc/util.h>
13
#include <script/descriptor.h>
14
#include <script/script.h>
15
#include <script/signingprovider.h>
16
#include <tinyformat.h>
17
#include <univalue.h>
18
#include <util/check.h>
19
#include <util/strencodings.h>
20
21
#include <cstdint>
22
#include <memory>
23
#include <optional>
24
#include <string>
25
#include <tuple>
26
#include <vector>
27
28
static RPCHelpMan validateaddress()
29
0
{
30
0
    return RPCHelpMan{
31
0
        "validateaddress",
32
0
        "\nReturn information about the given bitcoin address.\n",
33
0
        {
34
0
            {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to validate"},
35
0
        },
36
0
        RPCResult{
37
0
            RPCResult::Type::OBJ, "", "",
38
0
            {
39
0
                {RPCResult::Type::BOOL, "isvalid", "If the address is valid or not"},
40
0
                {RPCResult::Type::STR, "address", /*optional=*/true, "The bitcoin address validated"},
41
0
                {RPCResult::Type::STR_HEX, "scriptPubKey", /*optional=*/true, "The hex-encoded output script generated by the address"},
42
0
                {RPCResult::Type::BOOL, "isscript", /*optional=*/true, "If the key is a script"},
43
0
                {RPCResult::Type::BOOL, "iswitness", /*optional=*/true, "If the address is a witness address"},
44
0
                {RPCResult::Type::NUM, "witness_version", /*optional=*/true, "The version number of the witness program"},
45
0
                {RPCResult::Type::STR_HEX, "witness_program", /*optional=*/true, "The hex value of the witness program"},
46
0
                {RPCResult::Type::STR, "error", /*optional=*/true, "Error message, if any"},
47
0
                {RPCResult::Type::ARR, "error_locations", /*optional=*/true, "Indices of likely error locations in address, if known (e.g. Bech32 errors)",
48
0
                    {
49
0
                        {RPCResult::Type::NUM, "index", "index of a potential error"},
50
0
                    }},
51
0
            }
52
0
        },
53
0
        RPCExamples{
54
0
            HelpExampleCli("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"") +
55
0
            HelpExampleRpc("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"")
56
0
        },
57
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
58
0
        {
59
0
            std::string error_msg;
60
0
            std::vector<int> error_locations;
61
0
            CTxDestination dest = DecodeDestination(request.params[0].get_str(), error_msg, &error_locations);
62
0
            const bool isValid = IsValidDestination(dest);
63
0
            CHECK_NONFATAL(isValid == error_msg.empty());
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
64
65
0
            UniValue ret(UniValue::VOBJ);
66
0
            ret.pushKV("isvalid", isValid);
67
0
            if (isValid) {
68
0
                std::string currentAddress = EncodeDestination(dest);
69
0
                ret.pushKV("address", currentAddress);
70
71
0
                CScript scriptPubKey = GetScriptForDestination(dest);
72
0
                ret.pushKV("scriptPubKey", HexStr(scriptPubKey));
73
74
0
                UniValue detail = DescribeAddress(dest);
75
0
                ret.pushKVs(std::move(detail));
76
0
            } else {
77
0
                UniValue error_indices(UniValue::VARR);
78
0
                for (int i : error_locations) error_indices.push_back(i);
79
0
                ret.pushKV("error_locations", std::move(error_indices));
80
0
                ret.pushKV("error", error_msg);
81
0
            }
82
83
0
            return ret;
84
0
        },
85
0
    };
86
0
}
87
88
static RPCHelpMan createmultisig()
89
0
{
90
0
    return RPCHelpMan{"createmultisig",
91
0
        "\nCreates a multi-signature address with n signature of m keys required.\n"
92
0
        "It returns a json object with the address and redeemScript.\n",
93
0
        {
94
0
            {"nrequired", RPCArg::Type::NUM, RPCArg::Optional::NO, "The number of required signatures out of the n keys."},
95
0
            {"keys", RPCArg::Type::ARR, RPCArg::Optional::NO, "The hex-encoded public keys.",
96
0
                {
97
0
                    {"key", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "The hex-encoded public key"},
98
0
                }},
99
0
            {"address_type", RPCArg::Type::STR, RPCArg::Default{"legacy"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", and \"bech32\"."},
100
0
        },
101
0
        RPCResult{
102
0
            RPCResult::Type::OBJ, "", "",
103
0
            {
104
0
                {RPCResult::Type::STR, "address", "The value of the new multisig address."},
105
0
                {RPCResult::Type::STR_HEX, "redeemScript", "The string value of the hex-encoded redemption script."},
106
0
                {RPCResult::Type::STR, "descriptor", "The descriptor for this multisig"},
107
0
                {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Any warnings resulting from the creation of this multisig",
108
0
                {
109
0
                    {RPCResult::Type::STR, "", ""},
110
0
                }},
111
0
            }
112
0
        },
113
0
        RPCExamples{
114
0
            "\nCreate a multisig address from 2 public keys\n"
115
0
            + HelpExampleCli("createmultisig", "2 \"[\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\\\",\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\\\"]\"") +
116
0
            "\nAs a JSON-RPC call\n"
117
0
            + HelpExampleRpc("createmultisig", "2, [\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\",\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\"]")
118
0
                },
119
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
120
0
        {
121
0
            int required = request.params[0].getInt<int>();
122
123
            // Get the public keys
124
0
            const UniValue& keys = request.params[1].get_array();
125
0
            std::vector<CPubKey> pubkeys;
126
0
            pubkeys.reserve(keys.size());
127
0
            for (unsigned int i = 0; i < keys.size(); ++i) {
128
0
                pubkeys.push_back(HexToPubKey(keys[i].get_str()));
129
0
            }
130
131
            // Get the output type
132
0
            OutputType output_type = OutputType::LEGACY;
133
0
            if (!request.params[2].isNull()) {
134
0
                std::optional<OutputType> parsed = ParseOutputType(request.params[2].get_str());
135
0
                if (!parsed) {
136
0
                    throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[2].get_str()));
Line
Count
Source
1172
0
#define strprintf tfm::format
137
0
                } else if (parsed.value() == OutputType::BECH32M) {
138
0
                    throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "createmultisig cannot create bech32m multisig addresses");
139
0
                }
140
0
                output_type = parsed.value();
141
0
            }
142
143
0
            FlatSigningProvider keystore;
144
0
            CScript inner;
145
0
            const CTxDestination dest = AddAndGetMultisigDestination(required, pubkeys, output_type, keystore, inner);
146
147
            // Make the descriptor
148
0
            std::unique_ptr<Descriptor> descriptor = InferDescriptor(GetScriptForDestination(dest), keystore);
149
150
0
            UniValue result(UniValue::VOBJ);
151
0
            result.pushKV("address", EncodeDestination(dest));
152
0
            result.pushKV("redeemScript", HexStr(inner));
153
0
            result.pushKV("descriptor", descriptor->ToString());
154
155
0
            UniValue warnings(UniValue::VARR);
156
0
            if (descriptor->GetOutputType() != output_type) {
157
                // Only warns if the user has explicitly chosen an address type we cannot generate
158
0
                warnings.push_back("Unable to make chosen address type, please ensure no uncompressed public keys are present.");
159
0
            }
160
0
            PushWarnings(warnings, result);
161
162
0
            return result;
163
0
        },
164
0
    };
165
0
}
166
167
static RPCHelpMan getdescriptorinfo()
168
0
{
169
0
    const std::string EXAMPLE_DESCRIPTOR = "wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)";
170
171
0
    return RPCHelpMan{"getdescriptorinfo",
172
0
        {"\nAnalyses a descriptor.\n"},
173
0
        {
174
0
            {"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor."},
175
0
        },
176
0
        RPCResult{
177
0
            RPCResult::Type::OBJ, "", "",
178
0
            {
179
0
                {RPCResult::Type::STR, "descriptor", "The descriptor in canonical form, without private keys. For a multipath descriptor, only the first will be returned."},
180
0
                {RPCResult::Type::ARR, "multipath_expansion", /*optional=*/true, "All descriptors produced by expanding multipath derivation elements. Only if the provided descriptor specifies multipath derivation elements.",
181
0
                {
182
0
                    {RPCResult::Type::STR, "", ""},
183
0
                }},
184
0
                {RPCResult::Type::STR, "checksum", "The checksum for the input descriptor"},
185
0
                {RPCResult::Type::BOOL, "isrange", "Whether the descriptor is ranged"},
186
0
                {RPCResult::Type::BOOL, "issolvable", "Whether the descriptor is solvable"},
187
0
                {RPCResult::Type::BOOL, "hasprivatekeys", "Whether the input descriptor contained at least one private key"},
188
0
            }
189
0
        },
190
0
        RPCExamples{
191
0
            "Analyse a descriptor\n" +
192
0
            HelpExampleCli("getdescriptorinfo", "\"" + EXAMPLE_DESCRIPTOR + "\"") +
193
0
            HelpExampleRpc("getdescriptorinfo", "\"" + EXAMPLE_DESCRIPTOR + "\"")
194
0
        },
195
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
196
0
        {
197
0
            FlatSigningProvider provider;
198
0
            std::string error;
199
0
            auto descs = Parse(request.params[0].get_str(), provider, error);
200
0
            if (descs.empty()) {
201
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
202
0
            }
203
204
0
            UniValue result(UniValue::VOBJ);
205
0
            result.pushKV("descriptor", descs.at(0)->ToString());
206
207
0
            if (descs.size() > 1) {
208
0
                UniValue multipath_descs(UniValue::VARR);
209
0
                for (const auto& d : descs) {
210
0
                    multipath_descs.push_back(d->ToString());
211
0
                }
212
0
                result.pushKV("multipath_expansion", multipath_descs);
213
0
            }
214
215
0
            result.pushKV("checksum", GetDescriptorChecksum(request.params[0].get_str()));
216
0
            result.pushKV("isrange", descs.at(0)->IsRange());
217
0
            result.pushKV("issolvable", descs.at(0)->IsSolvable());
218
0
            result.pushKV("hasprivatekeys", provider.keys.size() > 0);
219
0
            return result;
220
0
        },
221
0
    };
222
0
}
223
224
static UniValue DeriveAddresses(const Descriptor* desc, int64_t range_begin, int64_t range_end, FlatSigningProvider& key_provider)
225
0
{
226
0
    UniValue addresses(UniValue::VARR);
227
228
0
    for (int64_t i = range_begin; i <= range_end; ++i) {
229
0
        FlatSigningProvider provider;
230
0
        std::vector<CScript> scripts;
231
0
        if (!desc->Expand(i, key_provider, scripts, provider)) {
232
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot derive script without private keys");
233
0
        }
234
235
0
        for (const CScript& script : scripts) {
236
0
            CTxDestination dest;
237
0
            if (!ExtractDestination(script, dest)) {
238
                // ExtractDestination no longer returns true for P2PK since it doesn't have a corresponding address
239
                // However combo will output P2PK and should just ignore that script
240
0
                if (scripts.size() > 1 && std::get_if<PubKeyDestination>(&dest)) {
241
0
                    continue;
242
0
                }
243
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Descriptor does not have a corresponding address");
244
0
            }
245
246
0
            addresses.push_back(EncodeDestination(dest));
247
0
        }
248
0
    }
249
250
    // This should not be possible, but an assert seems overkill:
251
0
    if (addresses.empty()) {
252
0
        throw JSONRPCError(RPC_MISC_ERROR, "Unexpected empty result");
253
0
    }
254
255
0
    return addresses;
256
0
}
257
258
static RPCHelpMan deriveaddresses()
259
0
{
260
0
    const std::string EXAMPLE_DESCRIPTOR = "wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu";
261
262
0
    return RPCHelpMan{"deriveaddresses",
263
0
        {"\nDerives one or more addresses corresponding to an output descriptor.\n"
264
0
         "Examples of output descriptors are:\n"
265
0
         "    pkh(<pubkey>)                                     P2PKH outputs for the given pubkey\n"
266
0
         "    wpkh(<pubkey>)                                    Native segwit P2PKH outputs for the given pubkey\n"
267
0
         "    sh(multi(<n>,<pubkey>,<pubkey>,...))              P2SH-multisig outputs for the given threshold and pubkeys\n"
268
0
         "    raw(<hex script>)                                 Outputs whose output script equals the specified hex-encoded bytes\n"
269
0
         "    tr(<pubkey>,multi_a(<n>,<pubkey>,<pubkey>,...))   P2TR-multisig outputs for the given threshold and pubkeys\n"
270
0
         "\nIn the above, <pubkey> either refers to a fixed public key in hexadecimal notation, or to an xpub/xprv optionally followed by one\n"
271
0
         "or more path elements separated by \"/\", where \"h\" represents a hardened child key.\n"
272
0
         "For more information on output descriptors, see the documentation in the doc/descriptors.md file.\n"},
273
0
        {
274
0
            {"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor."},
275
0
            {"range", RPCArg::Type::RANGE, RPCArg::Optional::OMITTED, "If a ranged descriptor is used, this specifies the end or the range (in [begin,end] notation) to derive."},
276
0
        },
277
0
        {
278
0
            RPCResult{"for single derivation descriptors",
279
0
                RPCResult::Type::ARR, "", "",
280
0
                {
281
0
                    {RPCResult::Type::STR, "address", "the derived addresses"},
282
0
                }
283
0
            },
284
0
            RPCResult{"for multipath descriptors",
285
0
                RPCResult::Type::ARR, "", "The derived addresses for each of the multipath expansions of the descriptor, in multipath specifier order",
286
0
                {
287
0
                    {
288
0
                        RPCResult::Type::ARR, "", "The derived addresses for a multipath descriptor expansion",
289
0
                        {
290
0
                            {RPCResult::Type::STR, "address", "the derived address"},
291
0
                        },
292
0
                    },
293
0
                },
294
0
            },
295
0
        },
296
0
        RPCExamples{
297
0
            "First three native segwit receive addresses\n" +
298
0
            HelpExampleCli("deriveaddresses", "\"" + EXAMPLE_DESCRIPTOR + "\" \"[0,2]\"") +
299
0
            HelpExampleRpc("deriveaddresses", "\"" + EXAMPLE_DESCRIPTOR + "\", \"[0,2]\"")
300
0
        },
301
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
302
0
        {
303
0
            const std::string desc_str = request.params[0].get_str();
304
305
0
            int64_t range_begin = 0;
306
0
            int64_t range_end = 0;
307
308
0
            if (request.params.size() >= 2 && !request.params[1].isNull()) {
309
0
                std::tie(range_begin, range_end) = ParseDescriptorRange(request.params[1]);
310
0
            }
311
312
0
            FlatSigningProvider key_provider;
313
0
            std::string error;
314
0
            auto descs = Parse(desc_str, key_provider, error, /* require_checksum = */ true);
315
0
            if (descs.empty()) {
316
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
317
0
            }
318
0
            auto& desc = descs.at(0);
319
0
            if (!desc->IsRange() && request.params.size() > 1) {
320
0
                throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
321
0
            }
322
323
0
            if (desc->IsRange() && request.params.size() == 1) {
324
0
                throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified for a ranged descriptor");
325
0
            }
326
327
0
            UniValue addresses = DeriveAddresses(desc.get(), range_begin, range_end, key_provider);
328
329
0
            if (descs.size() == 1) {
330
0
                return addresses;
331
0
            }
332
333
0
            UniValue ret(UniValue::VARR);
334
0
            ret.push_back(addresses);
335
0
            for (size_t i = 1; i < descs.size(); ++i) {
336
0
                ret.push_back(DeriveAddresses(descs.at(i).get(), range_begin, range_end, key_provider));
337
0
            }
338
0
            return ret;
339
0
        },
340
0
    };
341
0
}
342
343
void RegisterOutputScriptRPCCommands(CRPCTable& t)
344
7.28k
{
345
7.28k
    static const CRPCCommand commands[]{
346
7.28k
        {"util", &validateaddress},
347
7.28k
        {"util", &createmultisig},
348
7.28k
        {"util", &deriveaddresses},
349
7.28k
        {"util", &getdescriptorinfo},
350
7.28k
    };
351
29.1k
    for (const auto& c : commands) {
352
29.1k
        t.appendCommand(c.name, &c);
353
29.1k
    }
354
7.28k
}