| 213 | } |
| 214 | |
| 215 | static bool getScriptFromDescriptor(std::string_view descriptor, CScript& script, std::string& error) |
| 216 | { |
| 217 | FlatSigningProvider key_provider; |
| 218 | const auto descs = Parse(descriptor, key_provider, error, /* require_checksum = */ false); |
| 219 | if (descs.empty()) return false; |
| 220 | if (descs.size() > 1) { |
| 221 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Multipath descriptor not accepted"); |
| 222 | } |
| 223 | const auto& desc = descs.at(0); |
| 224 | if (desc->IsRange()) { |
| 225 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Ranged descriptor not accepted. Maybe pass through deriveaddresses first?"); |
| 226 | } |
| 227 | |
| 228 | FlatSigningProvider provider; |
| 229 | std::vector<CScript> scripts; |
| 230 | if (!desc->Expand(0, key_provider, scripts, provider)) { |
| 231 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot derive script without private keys"); |
| 232 | } |
| 233 | |
| 234 | // Combo descriptors can have 2 or 4 scripts, so we can't just check scripts.size() == 1 |
| 235 | CHECK_NONFATAL(scripts.size() > 0 && scripts.size() <= 4); |
| 236 | |
| 237 | if (scripts.size() == 1) { |
| 238 | script = scripts.at(0); |
| 239 | } else if (scripts.size() == 4) { |
| 240 | // For uncompressed keys, take the 3rd script, since it is p2wpkh |
| 241 | script = scripts.at(2); |
| 242 | } else { |
| 243 | // Else take the 2nd script, since it is p2pkh |
| 244 | script = scripts.at(1); |
| 245 | } |
| 246 | |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | static RPCMethod generatetodescriptor() |
| 251 | { |