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