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