* Process named arguments into a vector of positional arguments, based on the * passed-in specification for the RPC call's arguments. */
| 426 | * passed-in specification for the RPC call's arguments. |
| 427 | */ |
| 428 | static inline JSONRPCRequest transformNamedArguments(const JSONRPCRequest& in, const std::vector<std::string>& argNames) |
| 429 | { |
| 430 | JSONRPCRequest out = in; |
| 431 | out.params = UniValue(UniValue::VARR); |
| 432 | // Build a map of parameters, and remove ones that have been processed, so that we can throw a focused error if |
| 433 | // there is an unknown one. |
| 434 | const std::vector<std::string>& keys = in.params.getKeys(); |
| 435 | const std::vector<UniValue>& values = in.params.getValues(); |
| 436 | std::unordered_map<std::string, const UniValue*> argsIn; |
| 437 | for (size_t i=0; i<keys.size(); ++i) { |
| 438 | argsIn[keys[i]] = &values[i]; |
| 439 | } |
| 440 | // Process expected parameters. |
| 441 | int hole = 0; |
| 442 | for (const std::string &argNamePattern: argNames) { |
| 443 | std::vector<std::string> vargNames; |
| 444 | boost::algorithm::split(vargNames, argNamePattern, boost::algorithm::is_any_of("|")); |
| 445 | auto fr = argsIn.end(); |
| 446 | for (const std::string & argName : vargNames) { |
| 447 | fr = argsIn.find(argName); |
| 448 | if (fr != argsIn.end()) { |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | if (fr != argsIn.end()) { |
| 453 | for (int i = 0; i < hole; ++i) { |
| 454 | // Fill hole between specified parameters with JSON nulls, |
| 455 | // but not at the end (for backwards compatibility with calls |
| 456 | // that act based on number of specified parameters). |
| 457 | out.params.push_back(UniValue()); |
| 458 | } |
| 459 | hole = 0; |
| 460 | out.params.push_back(*fr->second); |
| 461 | argsIn.erase(fr); |
| 462 | } else { |
| 463 | hole += 1; |
| 464 | } |
| 465 | } |
| 466 | // If there are still arguments in the argsIn map, this is an error. |
| 467 | if (!argsIn.empty()) { |
| 468 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown named parameter " + argsIn.begin()->first); |
| 469 | } |
| 470 | // Return request with named arguments transformed to positional arguments |
| 471 | return out; |
| 472 | } |
| 473 | |
| 474 | UniValue CRPCTable::execute(const JSONRPCRequest &request) const |
| 475 | { |