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 <pubkey.h>
8 : #include <rpc/protocol.h>
9 : #include <rpc/request.h>
10 : #include <rpc/server.h>
11 : #include <rpc/util.h>
12 : #include <script/descriptor.h>
13 : #include <script/script.h>
14 : #include <script/signingprovider.h>
15 : #include <script/standard.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 <string>
24 : #include <vector>
25 :
26 6280 : static RPCHelpMan validateaddress()
27 : {
28 6280 : return RPCHelpMan{
29 6280 : "validateaddress",
30 6280 : "\nReturn information about the given Dash address.\n",
31 12560 : {
32 6280 : {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The Dash address to validate"},
33 : },
34 6280 : RPCResult{
35 6280 : RPCResult::Type::OBJ, "", "",
36 37680 : {
37 6280 : {RPCResult::Type::BOOL, "isvalid", "If the address is valid or not"},
38 6280 : {RPCResult::Type::STR, "address", /*optional=*/true, "The Dash address validated"},
39 6280 : {RPCResult::Type::STR_HEX, "scriptPubKey", /*optional=*/true, "The hex-encoded scriptPubKey generated by the address"},
40 6280 : {RPCResult::Type::BOOL, "isscript", /*optional=*/true, "If the key is a script"},
41 6280 : {RPCResult::Type::STR, "error", /*optional=*/true, "Error message, if any"},
42 : }
43 : },
44 6280 : RPCExamples{
45 12560 : HelpExampleCli("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"") +
46 6280 : HelpExampleRpc("validateaddress", "\"" + EXAMPLE_ADDRESS[0] + "\"")
47 : },
48 6404 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
49 : {
50 124 : std::string error_msg;
51 124 : CTxDestination dest = DecodeDestination(request.params[0].get_str(), error_msg);
52 124 : const bool isValid = IsValidDestination(dest);
53 124 : CHECK_NONFATAL(isValid == error_msg.empty());
54 :
55 124 : UniValue ret(UniValue::VOBJ);
56 124 : ret.pushKV("isvalid", isValid);
57 124 : if (isValid) {
58 118 : std::string currentAddress = EncodeDestination(dest);
59 118 : ret.pushKV("address", currentAddress);
60 :
61 118 : CScript scriptPubKey = GetScriptForDestination(dest);
62 118 : ret.pushKV("scriptPubKey", HexStr(scriptPubKey));
63 :
64 118 : UniValue detail = DescribeAddress(dest);
65 118 : ret.pushKVs(detail);
66 118 : } else {
67 6 : ret.pushKV("error", error_msg);
68 : }
69 :
70 124 : return ret;
71 124 : },
72 : };
73 0 : }
74 :
75 6213 : static RPCHelpMan createmultisig()
76 : {
77 12426 : return RPCHelpMan{"createmultisig",
78 6213 : "\nCreates a multi-signature address with n signature of m keys required.\n"
79 : "It returns a json object with the address and redeemScript.\n",
80 18639 : {
81 6213 : {"nrequired", RPCArg::Type::NUM, RPCArg::Optional::NO, "The number of required signatures out of the n keys."},
82 12426 : {"keys", RPCArg::Type::ARR, RPCArg::Optional::NO, "The hex-encoded public keys.",
83 12426 : {
84 6213 : {"key", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "The hex-encoded public key"},
85 : }},
86 : },
87 6213 : RPCResult{
88 6213 : RPCResult::Type::OBJ, "", "",
89 24852 : {
90 6213 : {RPCResult::Type::STR, "address", "The value of the new multisig address."},
91 6213 : {RPCResult::Type::STR_HEX, "redeemScript", "The string value of the hex-encoded redemption script."},
92 6213 : {RPCResult::Type::STR, "descriptor", "The descriptor for this multisig."},
93 : }
94 : },
95 6213 : RPCExamples{
96 : "\nCreate a multisig address from 2 public keys\n"
97 6213 : + HelpExampleCli("createmultisig", "2 \"[\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\\\",\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\\\"]\"") +
98 : "\nAs a JSON-RPC call\n"
99 6213 : + HelpExampleRpc("createmultisig", "2, [\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\",\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\"]")
100 : },
101 6274 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
102 : {
103 61 : int required = request.params[0].getInt<int>();
104 :
105 : // Get the public keys
106 61 : const UniValue& keys = request.params[1].get_array();
107 61 : std::vector<CPubKey> pubkeys;
108 222 : for (unsigned int i = 0; i < keys.size(); ++i) {
109 165 : if (IsHex(keys[i].get_str()) && (keys[i].get_str().length() == 66 || keys[i].get_str().length() == 130)) {
110 163 : pubkeys.push_back(HexToPubKey(keys[i].get_str()));
111 161 : } else {
112 4 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Invalid public key: %s\n.", keys[i].get_str()));
113 : }
114 161 : }
115 :
116 : // Construct using pay-to-script-hash:
117 53 : FillableSigningProvider keystore;
118 53 : CScript inner;
119 53 : const CTxDestination dest = AddAndGetMultisigDestination(required, pubkeys, keystore, inner);
120 :
121 : // Make the descriptor
122 53 : std::unique_ptr<Descriptor> descriptor = InferDescriptor(GetScriptForDestination(dest), keystore);
123 :
124 53 : UniValue result(UniValue::VOBJ);
125 53 : result.pushKV("address", EncodeDestination(dest));
126 53 : result.pushKV("redeemScript", HexStr(inner));
127 53 : result.pushKV("descriptor", descriptor->ToString());
128 :
129 53 : return result;
130 69 : },
131 : };
132 0 : }
133 :
134 6222 : static RPCHelpMan getdescriptorinfo()
135 : {
136 6222 : const std::string EXAMPLE_DESCRIPTOR = "wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)";
137 :
138 12444 : return RPCHelpMan{"getdescriptorinfo",
139 6222 : {"\nAnalyses a descriptor.\n"},
140 12444 : {
141 6222 : {"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor"},
142 : },
143 6222 : RPCResult{
144 6222 : RPCResult::Type::OBJ, "", "",
145 37332 : {
146 6222 : {RPCResult::Type::STR, "descriptor", "The descriptor in canonical form, without private keys"},
147 6222 : {RPCResult::Type::STR, "checksum", "The checksum for the input descriptor"},
148 6222 : {RPCResult::Type::BOOL, "isrange", "Whether the descriptor is ranged"},
149 6222 : {RPCResult::Type::BOOL, "issolvable", "Whether the descriptor is solvable"},
150 6222 : {RPCResult::Type::BOOL, "hasprivatekeys", "Whether the input descriptor contained at least one private key"},
151 : }
152 : },
153 6222 : RPCExamples{
154 6222 : "\nAnalyse a descriptor\n" +
155 12444 : HelpExampleCli("getdescriptorinfo", "\"" + EXAMPLE_DESCRIPTOR + "\"") +
156 6222 : HelpExampleRpc("getdescriptorinfo", "\"" + EXAMPLE_DESCRIPTOR + "\"")
157 :
158 : },
159 6286 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
160 : {
161 :
162 66 : RPCTypeCheck(request.params, {UniValue::VSTR});
163 :
164 62 : FlatSigningProvider provider;
165 62 : std::string error;
166 62 : auto desc = Parse(request.params[0].get_str(), provider, error);
167 62 : if (!desc) {
168 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
169 : }
170 :
171 60 : UniValue result(UniValue::VOBJ);
172 60 : result.pushKV("descriptor", desc->ToString());
173 60 : result.pushKV("checksum", GetDescriptorChecksum(request.params[0].get_str()));
174 60 : result.pushKV("isrange", desc->IsRange());
175 60 : result.pushKV("issolvable", desc->IsSolvable());
176 60 : result.pushKV("hasprivatekeys", provider.keys.size() > 0);
177 60 : return result;
178 64 : },
179 : };
180 6222 : }
181 :
182 6295 : static RPCHelpMan deriveaddresses()
183 : {
184 6295 : const std::string EXAMPLE_DESCRIPTOR = "wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu";
185 :
186 12590 : return RPCHelpMan{"deriveaddresses",
187 6295 : {"\nDerives one or more addresses corresponding to an output descriptor.\n"
188 : "Examples of output descriptors are:\n"
189 : " pkh(<pubkey>) P2PKH outputs for the given pubkey\n"
190 : " sh(multi(<n>,<pubkey>,<pubkey>,...)) P2SH-multisig outputs for the given threshold and pubkeys\n"
191 : " raw(<hex script>) Outputs whose scriptPubKey equals the specified hex scripts\n"
192 : "\nIn the above, <pubkey> either refers to a fixed public key in hexadecimal notation, or to an xpub/xprv optionally followed by one\n"
193 : "or more path elements separated by \"/\", where \"h\" represents a hardened child key.\n"
194 : "For more information on output descriptors, see the documentation in the doc/descriptors.md file.\n"},
195 18885 : {
196 6295 : {"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor"},
197 6295 : {"range", RPCArg::Type::RANGE, RPCArg::Optional::OMITTED_NAMED_ARG, "If a ranged descriptor is used, this specifies the end or the range (in [begin,end] notation) to derive."},
198 : },
199 6295 : RPCResult{
200 6295 : RPCResult::Type::ARR, "", "",
201 12590 : {
202 6295 : {RPCResult::Type::STR, "address", "the derived addresses"},
203 : }
204 : },
205 6295 : RPCExamples{
206 6295 : "First three receive addresses\n" +
207 12590 : HelpExampleCli("deriveaddresses", "\"" + EXAMPLE_DESCRIPTOR + "\" \"[0,2]\"") +
208 6295 : HelpExampleRpc("deriveaddresses", "\"" + EXAMPLE_DESCRIPTOR + "\", \"[0,2]\"")
209 : },
210 6434 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
211 : {
212 :
213 179 : RPCTypeCheck(request.params, {UniValue::VSTR, UniValueType()}); // Range argument is checked later
214 139 : const std::string desc_str = request.params[0].get_str();
215 :
216 139 : int64_t range_begin = 0;
217 139 : int64_t range_end = 0;
218 :
219 139 : if (request.params.size() >= 2 && !request.params[1].isNull()) {
220 70 : std::tie(range_begin, range_end) = ParseDescriptorRange(request.params[1]);
221 54 : }
222 :
223 123 : FlatSigningProvider key_provider;
224 123 : std::string error;
225 123 : auto desc = Parse(desc_str, key_provider, error, /* require_checksum = */ true);
226 123 : if (!desc) {
227 8 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
228 : }
229 :
230 115 : if (!desc->IsRange() && request.params.size() > 1) {
231 4 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
232 : }
233 :
234 111 : if (desc->IsRange() && request.params.size() == 1) {
235 4 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified for a ranged descriptor");
236 : }
237 :
238 107 : UniValue addresses(UniValue::VARR);
239 :
240 222 : for (int64_t i = range_begin; i <= range_end; ++i) {
241 123 : FlatSigningProvider provider;
242 123 : std::vector<CScript> scripts;
243 123 : if (!desc->Expand(i, key_provider, scripts, provider)) {
244 4 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot derive script without private keys");
245 : }
246 :
247 242 : for (const CScript &script : scripts) {
248 127 : CTxDestination dest;
249 127 : if (!ExtractDestination(script, dest)) {
250 4 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Descriptor does not have a corresponding address");
251 : }
252 :
253 123 : addresses.push_back(EncodeDestination(dest));
254 : }
255 123 : }
256 :
257 : // This should not be possible, but an assert seems overkill:
258 99 : if (addresses.empty()) {
259 0 : throw JSONRPCError(RPC_MISC_ERROR, "Unexpected empty result");
260 : }
261 :
262 99 : return addresses;
263 155 : },
264 : };
265 6295 : }
266 :
267 3201 : void RegisterOutputScriptRPCCommands(CRPCTable& t)
268 : {
269 15477 : static const CRPCCommand commands[]{
270 3069 : {"util", &validateaddress},
271 3069 : {"util", &createmultisig},
272 3069 : {"util", &deriveaddresses},
273 3069 : {"util", &getdescriptorinfo},
274 : };
275 16005 : for (const auto& c : commands) {
276 12804 : t.appendCommand(c.name, &c);
277 : }
278 3201 : }
|