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