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