rpcclient.cpp 4.3 KB

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