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.

client.cpp 4.5KB

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