You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpcclient.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) 2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 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. #include "rpcclient.h"
  6. #include "rpcprotocol.h"
  7. #include "util.h"
  8. #include <set>
  9. #include <stdint.h>
  10. #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
  11. #include "univalue/univalue.h"
  12. using namespace std;
  13. class CRPCConvertParam
  14. {
  15. public:
  16. std::string methodName; //! method whose params want conversion
  17. int paramIdx; //! 0-based idx of param to convert
  18. };
  19. static const CRPCConvertParam vRPCConvertParams[] =
  20. {
  21. { "stop", 0 },
  22. { "setmocktime", 0 },
  23. { "getaddednodeinfo", 0 },
  24. { "setgenerate", 0 },
  25. { "setgenerate", 1 },
  26. { "generate", 0 },
  27. { "getnetworkhashps", 0 },
  28. { "getnetworkhashps", 1 },
  29. { "sendtoaddress", 1 },
  30. { "sendtoaddress", 4 },
  31. { "settxfee", 0 },
  32. { "getreceivedbyaddress", 1 },
  33. { "getreceivedbyaccount", 1 },
  34. { "listreceivedbyaddress", 0 },
  35. { "listreceivedbyaddress", 1 },
  36. { "listreceivedbyaddress", 2 },
  37. { "listreceivedbyaccount", 0 },
  38. { "listreceivedbyaccount", 1 },
  39. { "listreceivedbyaccount", 2 },
  40. { "getbalance", 1 },
  41. { "getbalance", 2 },
  42. { "getblockhash", 0 },
  43. { "move", 2 },
  44. { "move", 3 },
  45. { "sendfrom", 2 },
  46. { "sendfrom", 3 },
  47. { "listtransactions", 1 },
  48. { "listtransactions", 2 },
  49. { "listtransactions", 3 },
  50. { "listaccounts", 0 },
  51. { "listaccounts", 1 },
  52. { "walletpassphrase", 1 },
  53. { "getblocktemplate", 0 },
  54. { "listsinceblock", 1 },
  55. { "listsinceblock", 2 },
  56. { "sendmany", 1 },
  57. { "sendmany", 2 },
  58. { "sendmany", 4 },
  59. { "addmultisigaddress", 0 },
  60. { "addmultisigaddress", 1 },
  61. { "createmultisig", 0 },
  62. { "createmultisig", 1 },
  63. { "listunspent", 0 },
  64. { "listunspent", 1 },
  65. { "listunspent", 2 },
  66. { "getblock", 1 },
  67. { "getblockheader", 1 },
  68. { "gettransaction", 1 },
  69. { "getrawtransaction", 1 },
  70. { "createrawtransaction", 0 },
  71. { "createrawtransaction", 1 },
  72. { "signrawtransaction", 1 },
  73. { "signrawtransaction", 2 },
  74. { "sendrawtransaction", 1 },
  75. { "fundrawtransaction", 1 },
  76. { "gettxout", 1 },
  77. { "gettxout", 2 },
  78. { "gettxoutproof", 0 },
  79. { "lockunspent", 0 },
  80. { "lockunspent", 1 },
  81. { "importprivkey", 2 },
  82. { "importaddress", 2 },
  83. { "importaddress", 3 },
  84. { "verifychain", 0 },
  85. { "verifychain", 1 },
  86. { "keypoolrefill", 0 },
  87. { "getrawmempool", 0 },
  88. { "estimatefee", 0 },
  89. { "estimatepriority", 0 },
  90. { "prioritisetransaction", 1 },
  91. { "prioritisetransaction", 2 },
  92. { "setban", 2 },
  93. { "setban", 3 },
  94. };
  95. class CRPCConvertTable
  96. {
  97. private:
  98. std::set<std::pair<std::string, int> > members;
  99. public:
  100. CRPCConvertTable();
  101. bool convert(const std::string& method, int idx) {
  102. return (members.count(std::make_pair(method, idx)) > 0);
  103. }
  104. };
  105. CRPCConvertTable::CRPCConvertTable()
  106. {
  107. const unsigned int n_elem =
  108. (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
  109. for (unsigned int i = 0; i < n_elem; i++) {
  110. members.insert(std::make_pair(vRPCConvertParams[i].methodName,
  111. vRPCConvertParams[i].paramIdx));
  112. }
  113. }
  114. static CRPCConvertTable rpcCvtTable;
  115. /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
  116. * as well as objects and arrays.
  117. */
  118. UniValue ParseNonRFCJSONValue(const std::string& strVal)
  119. {
  120. UniValue jVal;
  121. if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
  122. !jVal.isArray() || jVal.size()!=1)
  123. throw runtime_error(string("Error parsing JSON:")+strVal);
  124. return jVal[0];
  125. }
  126. /** Convert strings to command-specific RPC representation */
  127. UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
  128. {
  129. UniValue params(UniValue::VARR);
  130. for (unsigned int idx = 0; idx < strParams.size(); idx++) {
  131. const std::string& strVal = strParams[idx];
  132. if (!rpcCvtTable.convert(strMethod, idx)) {
  133. // insert string value directly
  134. params.push_back(strVal);
  135. } else {
  136. // parse string as JSON, insert bool/number/object/etc. value
  137. params.push_back(ParseNonRFCJSONValue(strVal));
  138. }
  139. }
  140. return params;
  141. }