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.

bitcoinrpc.cpp 104KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104
  1. // Copyright (c) 2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2012 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "main.h"
  6. #include "wallet.h"
  7. #include "db.h"
  8. #include "walletdb.h"
  9. #include "net.h"
  10. #include "init.h"
  11. #include "ui_interface.h"
  12. #include "base58.h"
  13. #include "bitcoinrpc.h"
  14. #undef printf
  15. #include <boost/asio.hpp>
  16. #include <boost/asio/ip/v6_only.hpp>
  17. #include <boost/bind.hpp>
  18. #include <boost/filesystem.hpp>
  19. #include <boost/foreach.hpp>
  20. #include <boost/iostreams/concepts.hpp>
  21. #include <boost/iostreams/stream.hpp>
  22. #include <boost/algorithm/string.hpp>
  23. #include <boost/lexical_cast.hpp>
  24. #include <boost/asio/ssl.hpp>
  25. #include <boost/filesystem/fstream.hpp>
  26. #include <boost/shared_ptr.hpp>
  27. #include <list>
  28. #define printf OutputDebugStringF
  29. using namespace std;
  30. using namespace boost;
  31. using namespace boost::asio;
  32. using namespace json_spirit;
  33. void ThreadRPCServer2(void* parg);
  34. static std::string strRPCUserColonPass;
  35. static int64 nWalletUnlockTime;
  36. static CCriticalSection cs_nWalletUnlockTime;
  37. extern Value getconnectioncount(const Array& params, bool fHelp); // in rpcnet.cpp
  38. extern Value getpeerinfo(const Array& params, bool fHelp);
  39. extern Value dumpprivkey(const Array& params, bool fHelp); // in rpcdump.cpp
  40. extern Value importprivkey(const Array& params, bool fHelp);
  41. extern Value getrawtransaction(const Array& params, bool fHelp); // in rcprawtransaction.cpp
  42. extern Value listunspent(const Array& params, bool fHelp);
  43. extern Value createrawtransaction(const Array& params, bool fHelp);
  44. extern Value decoderawtransaction(const Array& params, bool fHelp);
  45. extern Value signrawtransaction(const Array& params, bool fHelp);
  46. extern Value sendrawtransaction(const Array& params, bool fHelp);
  47. const Object emptyobj;
  48. void ThreadRPCServer3(void* parg);
  49. Object JSONRPCError(int code, const string& message)
  50. {
  51. Object error;
  52. error.push_back(Pair("code", code));
  53. error.push_back(Pair("message", message));
  54. return error;
  55. }
  56. void RPCTypeCheck(const Array& params,
  57. const list<Value_type>& typesExpected)
  58. {
  59. unsigned int i = 0;
  60. BOOST_FOREACH(Value_type t, typesExpected)
  61. {
  62. if (params.size() <= i)
  63. break;
  64. const Value& v = params[i];
  65. if (v.type() != t)
  66. {
  67. string err = strprintf("Expected type %s, got %s",
  68. Value_type_name[t], Value_type_name[v.type()]);
  69. throw JSONRPCError(-3, err);
  70. }
  71. i++;
  72. }
  73. }
  74. void RPCTypeCheck(const Object& o,
  75. const map<string, Value_type>& typesExpected)
  76. {
  77. BOOST_FOREACH(const PAIRTYPE(string, Value_type)& t, typesExpected)
  78. {
  79. const Value& v = find_value(o, t.first);
  80. if (v.type() == null_type)
  81. throw JSONRPCError(-3, strprintf("Missing %s", t.first.c_str()));
  82. if (v.type() != t.second)
  83. {
  84. string err = strprintf("Expected type %s for %s, got %s",
  85. Value_type_name[t.second], t.first.c_str(), Value_type_name[v.type()]);
  86. throw JSONRPCError(-3, err);
  87. }
  88. }
  89. }
  90. double GetDifficulty(const CBlockIndex* blockindex = NULL)
  91. {
  92. // Floating point number that is a multiple of the minimum difficulty,
  93. // minimum difficulty = 1.0.
  94. if (blockindex == NULL)
  95. {
  96. if (pindexBest == NULL)
  97. return 1.0;
  98. else
  99. blockindex = pindexBest;
  100. }
  101. int nShift = (blockindex->nBits >> 24) & 0xff;
  102. double dDiff =
  103. (double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff);
  104. while (nShift < 29)
  105. {
  106. dDiff *= 256.0;
  107. nShift++;
  108. }
  109. while (nShift > 29)
  110. {
  111. dDiff /= 256.0;
  112. nShift--;
  113. }
  114. return dDiff;
  115. }
  116. int64 AmountFromValue(const Value& value)
  117. {
  118. double dAmount = value.get_real();
  119. if (dAmount <= 0.0 || dAmount > 21000000.0)
  120. throw JSONRPCError(-3, "Invalid amount");
  121. int64 nAmount = roundint64(dAmount * COIN);
  122. if (!MoneyRange(nAmount))
  123. throw JSONRPCError(-3, "Invalid amount");
  124. return nAmount;
  125. }
  126. Value ValueFromAmount(int64 amount)
  127. {
  128. return (double)amount / (double)COIN;
  129. }
  130. std::string
  131. HexBits(unsigned int nBits)
  132. {
  133. union {
  134. int32_t nBits;
  135. char cBits[4];
  136. } uBits;
  137. uBits.nBits = htonl((int32_t)nBits);
  138. return HexStr(BEGIN(uBits.cBits), END(uBits.cBits));
  139. }
  140. std::string
  141. HelpRequiringPassphrase()
  142. {
  143. return pwalletMain->IsCrypted()
  144. ? "\nrequires wallet passphrase to be set with walletpassphrase first"
  145. : "";
  146. }
  147. void
  148. EnsureWalletIsUnlocked()
  149. {
  150. if (pwalletMain->IsLocked())
  151. throw JSONRPCError(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.");
  152. }
  153. void WalletTxToJSON(const CWalletTx& wtx, Object& entry)
  154. {
  155. int confirms = wtx.GetDepthInMainChain();
  156. entry.push_back(Pair("confirmations", confirms));
  157. if (confirms)
  158. {
  159. entry.push_back(Pair("blockhash", wtx.hashBlock.GetHex()));
  160. entry.push_back(Pair("blockindex", wtx.nIndex));
  161. }
  162. entry.push_back(Pair("txid", wtx.GetHash().GetHex()));
  163. entry.push_back(Pair("time", (boost::int64_t)wtx.GetTxTime()));
  164. BOOST_FOREACH(const PAIRTYPE(string,string)& item, wtx.mapValue)
  165. entry.push_back(Pair(item.first, item.second));
  166. }
  167. string AccountFromValue(const Value& value)
  168. {
  169. string strAccount = value.get_str();
  170. if (strAccount == "*")
  171. throw JSONRPCError(-11, "Invalid account name");
  172. return strAccount;
  173. }
  174. Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex)
  175. {
  176. Object result;
  177. result.push_back(Pair("hash", block.GetHash().GetHex()));
  178. CMerkleTx txGen(block.vtx[0]);
  179. txGen.SetMerkleBranch(&block);
  180. result.push_back(Pair("confirmations", (int)txGen.GetDepthInMainChain()));
  181. result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION)));
  182. result.push_back(Pair("height", blockindex->nHeight));
  183. result.push_back(Pair("version", block.nVersion));
  184. result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
  185. Array txs;
  186. BOOST_FOREACH(const CTransaction&tx, block.vtx)
  187. txs.push_back(tx.GetHash().GetHex());
  188. result.push_back(Pair("tx", txs));
  189. result.push_back(Pair("time", (boost::int64_t)block.GetBlockTime()));
  190. result.push_back(Pair("nonce", (boost::uint64_t)block.nNonce));
  191. result.push_back(Pair("bits", HexBits(block.nBits)));
  192. result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
  193. if (blockindex->pprev)
  194. result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
  195. if (blockindex->pnext)
  196. result.push_back(Pair("nextblockhash", blockindex->pnext->GetBlockHash().GetHex()));
  197. return result;
  198. }
  199. ///
  200. /// Note: This interface may still be subject to change.
  201. ///
  202. string CRPCTable::help(string strCommand) const
  203. {
  204. string strRet;
  205. set<rpcfn_type> setDone;
  206. for (map<string, const CRPCCommand*>::const_iterator mi = mapCommands.begin(); mi != mapCommands.end(); ++mi)
  207. {
  208. const CRPCCommand *pcmd = mi->second;
  209. string strMethod = mi->first;
  210. // We already filter duplicates, but these deprecated screw up the sort order
  211. if (strMethod.find("label") != string::npos)
  212. continue;
  213. if (strCommand != "" && strMethod != strCommand)
  214. continue;
  215. try
  216. {
  217. Array params;
  218. rpcfn_type pfn = pcmd->actor;
  219. if (setDone.insert(pfn).second)
  220. (*pfn)(params, true);
  221. }
  222. catch (std::exception& e)
  223. {
  224. // Help text is returned in an exception
  225. string strHelp = string(e.what());
  226. if (strCommand == "")
  227. if (strHelp.find('\n') != string::npos)
  228. strHelp = strHelp.substr(0, strHelp.find('\n'));
  229. strRet += strHelp + "\n";
  230. }
  231. }
  232. if (strRet == "")
  233. strRet = strprintf("help: unknown command: %s\n", strCommand.c_str());
  234. strRet = strRet.substr(0,strRet.size()-1);
  235. return strRet;
  236. }
  237. Value help(const Array& params, bool fHelp)
  238. {
  239. if (fHelp || params.size() > 1)
  240. throw runtime_error(
  241. "help [command]\n"
  242. "List commands, or get help for a command.");
  243. string strCommand;
  244. if (params.size() > 0)
  245. strCommand = params[0].get_str();
  246. return tableRPC.help(strCommand);
  247. }
  248. Value stop(const Array& params, bool fHelp)
  249. {
  250. if (fHelp || params.size() != 0)
  251. throw runtime_error(
  252. "stop\n"
  253. "Stop Bitcoin server.");
  254. // Shutdown will take long enough that the response should get back
  255. StartShutdown();
  256. return "Bitcoin server stopping";
  257. }
  258. Value getblockcount(const Array& params, bool fHelp)
  259. {
  260. if (fHelp || params.size() != 0)
  261. throw runtime_error(
  262. "getblockcount\n"
  263. "Returns the number of blocks in the longest block chain.");
  264. return nBestHeight;
  265. }
  266. Value getdifficulty(const Array& params, bool fHelp)
  267. {
  268. if (fHelp || params.size() != 0)
  269. throw runtime_error(
  270. "getdifficulty\n"
  271. "Returns the proof-of-work difficulty as a multiple of the minimum difficulty.");
  272. return GetDifficulty();
  273. }
  274. Value getgenerate(const Array& params, bool fHelp)
  275. {
  276. if (fHelp || params.size() != 0)
  277. throw runtime_error(
  278. "getgenerate\n"
  279. "Returns true or false.");
  280. return GetBoolArg("-gen");
  281. }
  282. Value setgenerate(const Array& params, bool fHelp)
  283. {
  284. if (fHelp || params.size() < 1 || params.size() > 2)
  285. throw runtime_error(
  286. "setgenerate <generate> [genproclimit]\n"
  287. "<generate> is true or false to turn generation on or off.\n"
  288. "Generation is limited to [genproclimit] processors, -1 is unlimited.");
  289. bool fGenerate = true;
  290. if (params.size() > 0)
  291. fGenerate = params[0].get_bool();
  292. if (params.size() > 1)
  293. {
  294. int nGenProcLimit = params[1].get_int();
  295. mapArgs["-genproclimit"] = itostr(nGenProcLimit);
  296. if (nGenProcLimit == 0)
  297. fGenerate = false;
  298. }
  299. mapArgs["-gen"] = (fGenerate ? "1" : "0");
  300. GenerateBitcoins(fGenerate, pwalletMain);
  301. return Value::null;
  302. }
  303. Value gethashespersec(const Array& params, bool fHelp)
  304. {
  305. if (fHelp || params.size() != 0)
  306. throw runtime_error(
  307. "gethashespersec\n"
  308. "Returns a recent hashes per second performance measurement while generating.");
  309. if (GetTimeMillis() - nHPSTimerStart > 8000)
  310. return (boost::int64_t)0;
  311. return (boost::int64_t)dHashesPerSec;
  312. }
  313. Value getinfo(const Array& params, bool fHelp)
  314. {
  315. if (fHelp || params.size() != 0)
  316. throw runtime_error(
  317. "getinfo\n"
  318. "Returns an object containing various state info.");
  319. CService addrProxy;
  320. GetProxy(NET_IPV4, addrProxy);
  321. Object obj;
  322. obj.push_back(Pair("version", (int)CLIENT_VERSION));
  323. obj.push_back(Pair("protocolversion",(int)PROTOCOL_VERSION));
  324. obj.push_back(Pair("walletversion", pwalletMain->GetVersion()));
  325. obj.push_back(Pair("balance", ValueFromAmount(pwalletMain->GetBalance())));
  326. obj.push_back(Pair("blocks", (int)nBestHeight));
  327. obj.push_back(Pair("connections", (int)vNodes.size()));
  328. obj.push_back(Pair("proxy", (addrProxy.IsValid() ? addrProxy.ToStringIPPort() : string())));
  329. obj.push_back(Pair("difficulty", (double)GetDifficulty()));
  330. obj.push_back(Pair("testnet", fTestNet));
  331. obj.push_back(Pair("keypoololdest", (boost::int64_t)pwalletMain->GetOldestKeyPoolTime()));
  332. obj.push_back(Pair("keypoolsize", pwalletMain->GetKeyPoolSize()));
  333. obj.push_back(Pair("paytxfee", ValueFromAmount(nTransactionFee)));
  334. if (pwalletMain->IsCrypted())
  335. obj.push_back(Pair("unlocked_until", (boost::int64_t)nWalletUnlockTime / 1000));
  336. obj.push_back(Pair("errors", GetWarnings("statusbar")));
  337. return obj;
  338. }
  339. Value getmininginfo(const Array& params, bool fHelp)
  340. {
  341. if (fHelp || params.size() != 0)
  342. throw runtime_error(
  343. "getmininginfo\n"
  344. "Returns an object containing mining-related information.");
  345. Object obj;
  346. obj.push_back(Pair("blocks", (int)nBestHeight));
  347. obj.push_back(Pair("currentblocksize",(uint64_t)nLastBlockSize));
  348. obj.push_back(Pair("currentblocktx",(uint64_t)nLastBlockTx));
  349. obj.push_back(Pair("difficulty", (double)GetDifficulty()));
  350. obj.push_back(Pair("errors", GetWarnings("statusbar")));
  351. obj.push_back(Pair("generate", GetBoolArg("-gen")));
  352. obj.push_back(Pair("genproclimit", (int)GetArg("-genproclimit", -1)));
  353. obj.push_back(Pair("hashespersec", gethashespersec(params, false)));
  354. obj.push_back(Pair("pooledtx", (uint64_t)mempool.size()));
  355. obj.push_back(Pair("testnet", fTestNet));
  356. return obj;
  357. }
  358. Value getnewaddress(const Array& params, bool fHelp)
  359. {
  360. if (fHelp || params.size() > 1)
  361. throw runtime_error(
  362. "getnewaddress [account]\n"
  363. "Returns a new Bitcoin address for receiving payments. "
  364. "If [account] is specified (recommended), it is added to the address book "
  365. "so payments received with the address will be credited to [account].");
  366. // Parse the account first so we don't generate a key if there's an error
  367. string strAccount;
  368. if (params.size() > 0)
  369. strAccount = AccountFromValue(params[0]);
  370. if (!pwalletMain->IsLocked())
  371. pwalletMain->TopUpKeyPool();
  372. // Generate a new key that is added to wallet
  373. CPubKey newKey;
  374. if (!pwalletMain->GetKeyFromPool(newKey, false))
  375. throw JSONRPCError(-12, "Error: Keypool ran out, please call keypoolrefill first");
  376. CKeyID keyID = newKey.GetID();
  377. pwalletMain->SetAddressBookName(keyID, strAccount);
  378. return CBitcoinAddress(keyID).ToString();
  379. }
  380. CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false)
  381. {
  382. CWalletDB walletdb(pwalletMain->strWalletFile);
  383. CAccount account;
  384. walletdb.ReadAccount(strAccount, account);
  385. bool bKeyUsed = false;
  386. // Check if the current key has been used
  387. if (account.vchPubKey.IsValid())
  388. {
  389. CScript scriptPubKey;
  390. scriptPubKey.SetDestination(account.vchPubKey.GetID());
  391. for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin();
  392. it != pwalletMain->mapWallet.end() && account.vchPubKey.IsValid();
  393. ++it)
  394. {
  395. const CWalletTx& wtx = (*it).second;
  396. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  397. if (txout.scriptPubKey == scriptPubKey)
  398. bKeyUsed = true;
  399. }
  400. }
  401. // Generate a new key
  402. if (!account.vchPubKey.IsValid() || bForceNew || bKeyUsed)
  403. {
  404. if (!pwalletMain->GetKeyFromPool(account.vchPubKey, false))
  405. throw JSONRPCError(-12, "Error: Keypool ran out, please call keypoolrefill first");
  406. pwalletMain->SetAddressBookName(account.vchPubKey.GetID(), strAccount);
  407. walletdb.WriteAccount(strAccount, account);
  408. }
  409. return CBitcoinAddress(account.vchPubKey.GetID());
  410. }
  411. Value getaccountaddress(const Array& params, bool fHelp)
  412. {
  413. if (fHelp || params.size() != 1)
  414. throw runtime_error(
  415. "getaccountaddress <account>\n"
  416. "Returns the current Bitcoin address for receiving payments to this account.");
  417. // Parse the account first so we don't generate a key if there's an error
  418. string strAccount = AccountFromValue(params[0]);
  419. Value ret;
  420. ret = GetAccountAddress(strAccount).ToString();
  421. return ret;
  422. }
  423. Value setaccount(const Array& params, bool fHelp)
  424. {
  425. if (fHelp || params.size() < 1 || params.size() > 2)
  426. throw runtime_error(
  427. "setaccount <bitcoinaddress> <account>\n"
  428. "Sets the account associated with the given address.");
  429. CBitcoinAddress address(params[0].get_str());
  430. if (!address.IsValid())
  431. throw JSONRPCError(-5, "Invalid Bitcoin address");
  432. string strAccount;
  433. if (params.size() > 1)
  434. strAccount = AccountFromValue(params[1]);
  435. // Detect when changing the account of an address that is the 'unused current key' of another account:
  436. if (pwalletMain->mapAddressBook.count(address.Get()))
  437. {
  438. string strOldAccount = pwalletMain->mapAddressBook[address.Get()];
  439. if (address == GetAccountAddress(strOldAccount))
  440. GetAccountAddress(strOldAccount, true);
  441. }
  442. pwalletMain->SetAddressBookName(address.Get(), strAccount);
  443. return Value::null;
  444. }
  445. Value getaccount(const Array& params, bool fHelp)
  446. {
  447. if (fHelp || params.size() != 1)
  448. throw runtime_error(
  449. "getaccount <bitcoinaddress>\n"
  450. "Returns the account associated with the given address.");
  451. CBitcoinAddress address(params[0].get_str());
  452. if (!address.IsValid())
  453. throw JSONRPCError(-5, "Invalid Bitcoin address");
  454. string strAccount;
  455. map<CTxDestination, string>::iterator mi = pwalletMain->mapAddressBook.find(address.Get());
  456. if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty())
  457. strAccount = (*mi).second;
  458. return strAccount;
  459. }
  460. Value getaddressesbyaccount(const Array& params, bool fHelp)
  461. {
  462. if (fHelp || params.size() != 1)
  463. throw runtime_error(
  464. "getaddressesbyaccount <account>\n"
  465. "Returns the list of addresses for the given account.");
  466. string strAccount = AccountFromValue(params[0]);
  467. // Find all addresses that have the given account
  468. Array ret;
  469. BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
  470. {
  471. const CBitcoinAddress& address = item.first;
  472. const string& strName = item.second;
  473. if (strName == strAccount)
  474. ret.push_back(address.ToString());
  475. }
  476. return ret;
  477. }
  478. Value settxfee(const Array& params, bool fHelp)
  479. {
  480. if (fHelp || params.size() < 1 || params.size() > 1)
  481. throw runtime_error(
  482. "settxfee <amount>\n"
  483. "<amount> is a real and is rounded to the nearest 0.00000001");
  484. // Amount
  485. int64 nAmount = 0;
  486. if (params[0].get_real() != 0.0)
  487. nAmount = AmountFromValue(params[0]); // rejects 0.0 amounts
  488. nTransactionFee = nAmount;
  489. return true;
  490. }
  491. Value sendtoaddress(const Array& params, bool fHelp)
  492. {
  493. if (fHelp || params.size() < 2 || params.size() > 4)
  494. throw runtime_error(
  495. "sendtoaddress <bitcoinaddress> <amount> [comment] [comment-to]\n"
  496. "<amount> is a real and is rounded to the nearest 0.00000001"
  497. + HelpRequiringPassphrase());
  498. CBitcoinAddress address(params[0].get_str());
  499. if (!address.IsValid())
  500. throw JSONRPCError(-5, "Invalid Bitcoin address");
  501. // Amount
  502. int64 nAmount = AmountFromValue(params[1]);
  503. // Wallet comments
  504. CWalletTx wtx;
  505. if (params.size() > 2 && params[2].type() != null_type && !params[2].get_str().empty())
  506. wtx.mapValue["comment"] = params[2].get_str();
  507. if (params.size() > 3 && params[3].type() != null_type && !params[3].get_str().empty())
  508. wtx.mapValue["to"] = params[3].get_str();
  509. if (pwalletMain->IsLocked())
  510. throw JSONRPCError(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.");
  511. string strError = pwalletMain->SendMoneyToDestination(address.Get(), nAmount, wtx);
  512. if (strError != "")
  513. throw JSONRPCError(-4, strError);
  514. return wtx.GetHash().GetHex();
  515. }
  516. Value signmessage(const Array& params, bool fHelp)
  517. {
  518. if (fHelp || params.size() != 2)
  519. throw runtime_error(
  520. "signmessage <bitcoinaddress> <message>\n"
  521. "Sign a message with the private key of an address");
  522. EnsureWalletIsUnlocked();
  523. string strAddress = params[0].get_str();
  524. string strMessage = params[1].get_str();
  525. CBitcoinAddress addr(strAddress);
  526. if (!addr.IsValid())
  527. throw JSONRPCError(-3, "Invalid address");
  528. CKeyID keyID;
  529. if (!addr.GetKeyID(keyID))
  530. throw JSONRPCError(-3, "Address does not refer to key");
  531. CKey key;
  532. if (!pwalletMain->GetKey(keyID, key))
  533. throw JSONRPCError(-4, "Private key not available");
  534. CDataStream ss(SER_GETHASH, 0);
  535. ss << strMessageMagic;
  536. ss << strMessage;
  537. vector<unsigned char> vchSig;
  538. if (!key.SignCompact(Hash(ss.begin(), ss.end()), vchSig))
  539. throw JSONRPCError(-5, "Sign failed");
  540. return EncodeBase64(&vchSig[0], vchSig.size());
  541. }
  542. Value verifymessage(const Array& params, bool fHelp)
  543. {
  544. if (fHelp || params.size() != 3)
  545. throw runtime_error(
  546. "verifymessage <bitcoinaddress> <signature> <message>\n"
  547. "Verify a signed message");
  548. string strAddress = params[0].get_str();
  549. string strSign = params[1].get_str();
  550. string strMessage = params[2].get_str();
  551. CBitcoinAddress addr(strAddress);
  552. if (!addr.IsValid())
  553. throw JSONRPCError(-3, "Invalid address");
  554. CKeyID keyID;
  555. if (!addr.GetKeyID(keyID))
  556. throw JSONRPCError(-3, "Address does not refer to key");
  557. bool fInvalid = false;
  558. vector<unsigned char> vchSig = DecodeBase64(strSign.c_str(), &fInvalid);
  559. if (fInvalid)
  560. throw JSONRPCError(-5, "Malformed base64 encoding");
  561. CDataStream ss(SER_GETHASH, 0);
  562. ss << strMessageMagic;
  563. ss << strMessage;
  564. CKey key;
  565. if (!key.SetCompactSignature(Hash(ss.begin(), ss.end()), vchSig))
  566. return false;
  567. return (key.GetPubKey().GetID() == keyID);
  568. }
  569. Value getreceivedbyaddress(const Array& params, bool fHelp)
  570. {
  571. if (fHelp || params.size() < 1 || params.size() > 2)
  572. throw runtime_error(
  573. "getreceivedbyaddress <bitcoinaddress> [minconf=1]\n"
  574. "Returns the total amount received by <bitcoinaddress> in transactions with at least [minconf] confirmations.");
  575. // Bitcoin address
  576. CBitcoinAddress address = CBitcoinAddress(params[0].get_str());
  577. CScript scriptPubKey;
  578. if (!address.IsValid())
  579. throw JSONRPCError(-5, "Invalid Bitcoin address");
  580. scriptPubKey.SetDestination(address.Get());
  581. if (!IsMine(*pwalletMain,scriptPubKey))
  582. return (double)0.0;
  583. // Minimum confirmations
  584. int nMinDepth = 1;
  585. if (params.size() > 1)
  586. nMinDepth = params[1].get_int();
  587. // Tally
  588. int64 nAmount = 0;
  589. for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
  590. {
  591. const CWalletTx& wtx = (*it).second;
  592. if (wtx.IsCoinBase() || !wtx.IsFinal())
  593. continue;
  594. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  595. if (txout.scriptPubKey == scriptPubKey)
  596. if (wtx.GetDepthInMainChain() >= nMinDepth)
  597. nAmount += txout.nValue;
  598. }
  599. return ValueFromAmount(nAmount);
  600. }
  601. void GetAccountAddresses(string strAccount, set<CTxDestination>& setAddress)
  602. {
  603. BOOST_FOREACH(const PAIRTYPE(CTxDestination, string)& item, pwalletMain->mapAddressBook)
  604. {
  605. const CTxDestination& address = item.first;
  606. const string& strName = item.second;
  607. if (strName == strAccount)
  608. setAddress.insert(address);
  609. }
  610. }
  611. Value getreceivedbyaccount(const Array& params, bool fHelp)
  612. {
  613. if (fHelp || params.size() < 1 || params.size() > 2)
  614. throw runtime_error(
  615. "getreceivedbyaccount <account> [minconf=1]\n"
  616. "Returns the total amount received by addresses with <account> in transactions with at least [minconf] confirmations.");
  617. // Minimum confirmations
  618. int nMinDepth = 1;
  619. if (params.size() > 1)
  620. nMinDepth = params[1].get_int();
  621. // Get the set of pub keys assigned to account
  622. string strAccount = AccountFromValue(params[0]);
  623. set<CTxDestination> setAddress;
  624. GetAccountAddresses(strAccount, setAddress);
  625. // Tally
  626. int64 nAmount = 0;
  627. for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
  628. {
  629. const CWalletTx& wtx = (*it).second;
  630. if (wtx.IsCoinBase() || !wtx.IsFinal())
  631. continue;
  632. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  633. {
  634. CTxDestination address;
  635. if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*pwalletMain, address) && setAddress.count(address))
  636. if (wtx.GetDepthInMainChain() >= nMinDepth)
  637. nAmount += txout.nValue;
  638. }
  639. }
  640. return (double)nAmount / (double)COIN;
  641. }
  642. int64 GetAccountBalance(CWalletDB& walletdb, const string& strAccount, int nMinDepth)
  643. {
  644. int64 nBalance = 0;
  645. // Tally wallet transactions
  646. for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
  647. {
  648. const CWalletTx& wtx = (*it).second;
  649. if (!wtx.IsFinal())
  650. continue;
  651. int64 nGenerated, nReceived, nSent, nFee;
  652. wtx.GetAccountAmounts(strAccount, nGenerated, nReceived, nSent, nFee);
  653. if (nReceived != 0 && wtx.GetDepthInMainChain() >= nMinDepth)
  654. nBalance += nReceived;
  655. nBalance += nGenerated - nSent - nFee;
  656. }
  657. // Tally internal accounting entries
  658. nBalance += walletdb.GetAccountCreditDebit(strAccount);
  659. return nBalance;
  660. }
  661. int64 GetAccountBalance(const string& strAccount, int nMinDepth)
  662. {
  663. CWalletDB walletdb(pwalletMain->strWalletFile);
  664. return GetAccountBalance(walletdb, strAccount, nMinDepth);
  665. }
  666. Value getbalance(const Array& params, bool fHelp)
  667. {
  668. if (fHelp || params.size() > 2)
  669. throw runtime_error(
  670. "getbalance [account] [minconf=1]\n"
  671. "If [account] is not specified, returns the server's total available balance.\n"
  672. "If [account] is specified, returns the balance in the account.");
  673. if (params.size() == 0)
  674. return ValueFromAmount(pwalletMain->GetBalance());
  675. int nMinDepth = 1;
  676. if (params.size() > 1)
  677. nMinDepth = params[1].get_int();
  678. if (params[0].get_str() == "*") {
  679. // Calculate total balance a different way from GetBalance()
  680. // (GetBalance() sums up all unspent TxOuts)
  681. // getbalance and getbalance '*' should always return the same number.
  682. int64 nBalance = 0;
  683. for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
  684. {
  685. const CWalletTx& wtx = (*it).second;
  686. if (!wtx.IsFinal())
  687. continue;
  688. int64 allGeneratedImmature, allGeneratedMature, allFee;
  689. allGeneratedImmature = allGeneratedMature = allFee = 0;
  690. string strSentAccount;
  691. list<pair<CTxDestination, int64> > listReceived;
  692. list<pair<CTxDestination, int64> > listSent;
  693. wtx.GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount);
  694. if (wtx.GetDepthInMainChain() >= nMinDepth)
  695. {
  696. BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& r, listReceived)
  697. nBalance += r.second;
  698. }
  699. BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& r, listSent)
  700. nBalance -= r.second;
  701. nBalance -= allFee;
  702. nBalance += allGeneratedMature;
  703. }
  704. return ValueFromAmount(nBalance);
  705. }
  706. string strAccount = AccountFromValue(params[0]);
  707. int64 nBalance = GetAccountBalance(strAccount, nMinDepth);
  708. return ValueFromAmount(nBalance);
  709. }
  710. Value movecmd(const Array& params, bool fHelp)
  711. {
  712. if (fHelp || params.size() < 3 || params.size() > 5)
  713. throw runtime_error(
  714. "move <fromaccount> <toaccount> <amount> [minconf=1] [comment]\n"
  715. "Move from one account in your wallet to another.");
  716. string strFrom = AccountFromValue(params[0]);
  717. string strTo = AccountFromValue(params[1]);
  718. int64 nAmount = AmountFromValue(params[2]);
  719. if (params.size() > 3)
  720. // unused parameter, used to be nMinDepth, keep type-checking it though
  721. (void)params[3].get_int();
  722. string strComment;
  723. if (params.size() > 4)
  724. strComment = params[4].get_str();
  725. CWalletDB walletdb(pwalletMain->strWalletFile);
  726. if (!walletdb.TxnBegin())
  727. throw JSONRPCError(-20, "database error");
  728. int64 nNow = GetAdjustedTime();
  729. // Debit
  730. CAccountingEntry debit;
  731. debit.strAccount = strFrom;
  732. debit.nCreditDebit = -nAmount;
  733. debit.nTime = nNow;
  734. debit.strOtherAccount = strTo;
  735. debit.strComment = strComment;
  736. walletdb.WriteAccountingEntry(debit);
  737. // Credit
  738. CAccountingEntry credit;
  739. credit.strAccount = strTo;
  740. credit.nCreditDebit = nAmount;
  741. credit.nTime = nNow;
  742. credit.strOtherAccount = strFrom;
  743. credit.strComment = strComment;
  744. walletdb.WriteAccountingEntry(credit);
  745. if (!walletdb.TxnCommit())
  746. throw JSONRPCError(-20, "database error");
  747. return true;
  748. }
  749. Value sendfrom(const Array& params, bool fHelp)
  750. {
  751. if (fHelp || params.size() < 3 || params.size() > 6)
  752. throw runtime_error(
  753. "sendfrom <fromaccount> <tobitcoinaddress> <amount> [minconf=1] [comment] [comment-to]\n"
  754. "<amount> is a real and is rounded to the nearest 0.00000001"
  755. + HelpRequiringPassphrase());
  756. string strAccount = AccountFromValue(params[0]);
  757. CBitcoinAddress address(params[1].get_str());
  758. if (!address.IsValid())
  759. throw JSONRPCError(-5, "Invalid Bitcoin address");
  760. int64 nAmount = AmountFromValue(params[2]);
  761. int nMinDepth = 1;
  762. if (params.size() > 3)
  763. nMinDepth = params[3].get_int();
  764. CWalletTx wtx;
  765. wtx.strFromAccount = strAccount;
  766. if (params.size() > 4 && params[4].type() != null_type && !params[4].get_str().empty())
  767. wtx.mapValue["comment"] = params[4].get_str();
  768. if (params.size() > 5 && params[5].type() != null_type && !params[5].get_str().empty())
  769. wtx.mapValue["to"] = params[5].get_str();
  770. EnsureWalletIsUnlocked();
  771. // Check funds
  772. int64 nBalance = GetAccountBalance(strAccount, nMinDepth);
  773. if (nAmount > nBalance)
  774. throw JSONRPCError(-6, "Account has insufficient funds");
  775. // Send
  776. string strError = pwalletMain->SendMoneyToDestination(address.Get(), nAmount, wtx);
  777. if (strError != "")
  778. throw JSONRPCError(-4, strError);
  779. return wtx.GetHash().GetHex();
  780. }
  781. Value sendmany(const Array& params, bool fHelp)
  782. {
  783. if (fHelp || params.size() < 2 || params.size() > 4)
  784. throw runtime_error(
  785. "sendmany <fromaccount> {address:amount,...} [minconf=1] [comment]\n"
  786. "amounts are double-precision floating point numbers"
  787. + HelpRequiringPassphrase());
  788. string strAccount = AccountFromValue(params[0]);
  789. Object sendTo = params[1].get_obj();
  790. int nMinDepth = 1;
  791. if (params.size() > 2)
  792. nMinDepth = params[2].get_int();
  793. CWalletTx wtx;
  794. wtx.strFromAccount = strAccount;
  795. if (params.size() > 3 && params[3].type() != null_type && !params[3].get_str().empty())
  796. wtx.mapValue["comment"] = params[3].get_str();
  797. set<CBitcoinAddress> setAddress;
  798. vector<pair<CScript, int64> > vecSend;
  799. int64 totalAmount = 0;
  800. BOOST_FOREACH(const Pair& s, sendTo)
  801. {
  802. CBitcoinAddress address(s.name_);
  803. if (!address.IsValid())
  804. throw JSONRPCError(-5, string("Invalid Bitcoin address:")+s.name_);
  805. if (setAddress.count(address))
  806. throw JSONRPCError(-8, string("Invalid parameter, duplicated address: ")+s.name_);
  807. setAddress.insert(address);
  808. CScript scriptPubKey;
  809. scriptPubKey.SetDestination(address.Get());
  810. int64 nAmount = AmountFromValue(s.value_);
  811. totalAmount += nAmount;
  812. vecSend.push_back(make_pair(scriptPubKey, nAmount));
  813. }
  814. EnsureWalletIsUnlocked();
  815. // Check funds
  816. int64 nBalance = GetAccountBalance(strAccount, nMinDepth);
  817. if (totalAmount > nBalance)
  818. throw JSONRPCError(-6, "Account has insufficient funds");
  819. // Send
  820. CReserveKey keyChange(pwalletMain);
  821. int64 nFeeRequired = 0;
  822. bool fCreated = pwalletMain->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
  823. if (!fCreated)
  824. {
  825. if (totalAmount + nFeeRequired > pwalletMain->GetBalance())
  826. throw JSONRPCError(-6, "Insufficient funds");
  827. throw JSONRPCError(-4, "Transaction creation failed");
  828. }
  829. if (!pwalletMain->CommitTransaction(wtx, keyChange))
  830. throw JSONRPCError(-4, "Transaction commit failed");
  831. return wtx.GetHash().GetHex();
  832. }
  833. Value addmultisigaddress(const Array& params, bool fHelp)
  834. {
  835. if (fHelp || params.size() < 2 || params.size() > 3)
  836. {
  837. string msg = "addmultisigaddress <nrequired> <'[\"key\",\"key\"]'> [account]\n"
  838. "Add a nrequired-to-sign multisignature address to the wallet\"\n"
  839. "each key is a Bitcoin address or hex-encoded public key\n"
  840. "If [account] is specified, assign address to [account].";
  841. throw runtime_error(msg);
  842. }
  843. int nRequired = params[0].get_int();
  844. const Array& keys = params[1].get_array();
  845. string strAccount;
  846. if (params.size() > 2)
  847. strAccount = AccountFromValue(params[2]);
  848. // Gather public keys
  849. if (nRequired < 1)
  850. throw runtime_error("a multisignature address must require at least one key to redeem");
  851. if ((int)keys.size() < nRequired)
  852. throw runtime_error(
  853. strprintf("not enough keys supplied "
  854. "(got %d keys, but need at least %d to redeem)", keys.size(), nRequired));
  855. std::vector<CKey> pubkeys;
  856. pubkeys.resize(keys.size());
  857. for (unsigned int i = 0; i < keys.size(); i++)
  858. {
  859. const std::string& ks = keys[i].get_str();
  860. // Case 1: Bitcoin address and we have full public key:
  861. CBitcoinAddress address(ks);
  862. if (address.IsValid())
  863. {
  864. CKeyID keyID;
  865. if (!address.GetKeyID(keyID))
  866. throw runtime_error(
  867. strprintf("%s does not refer to a key",ks.c_str()));
  868. CPubKey vchPubKey;
  869. if (!pwalletMain->GetPubKey(keyID, vchPubKey))
  870. throw runtime_error(
  871. strprintf("no full public key for address %s",ks.c_str()));
  872. if (!vchPubKey.IsValid() || !pubkeys[i].SetPubKey(vchPubKey))
  873. throw runtime_error(" Invalid public key: "+ks);
  874. }
  875. // Case 2: hex public key
  876. else if (IsHex(ks))
  877. {
  878. CPubKey vchPubKey(ParseHex(ks));
  879. if (!vchPubKey.IsValid() || !pubkeys[i].SetPubKey(vchPubKey))
  880. throw runtime_error(" Invalid public key: "+ks);
  881. }
  882. else
  883. {
  884. throw runtime_error(" Invalid public key: "+ks);
  885. }
  886. }
  887. // Construct using pay-to-script-hash:
  888. CScript inner;
  889. inner.SetMultisig(nRequired, pubkeys);
  890. CScriptID innerID = inner.GetID();
  891. pwalletMain->AddCScript(inner);
  892. pwalletMain->SetAddressBookName(innerID, strAccount);
  893. return CBitcoinAddress(innerID).ToString();
  894. }
  895. struct tallyitem
  896. {
  897. int64 nAmount;
  898. int nConf;
  899. tallyitem()
  900. {
  901. nAmount = 0;
  902. nConf = std::numeric_limits<int>::max();
  903. }
  904. };
  905. Value ListReceived(const Array& params, bool fByAccounts)
  906. {
  907. // Minimum confirmations
  908. int nMinDepth = 1;
  909. if (params.size() > 0)
  910. nMinDepth = params[0].get_int();
  911. // Whether to include empty accounts
  912. bool fIncludeEmpty = false;
  913. if (params.size() > 1)
  914. fIncludeEmpty = params[1].get_bool();
  915. // Tally
  916. map<CBitcoinAddress, tallyitem> mapTally;
  917. for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
  918. {
  919. const CWalletTx& wtx = (*it).second;
  920. if (wtx.IsCoinBase() || !wtx.IsFinal())
  921. continue;
  922. int nDepth = wtx.GetDepthInMainChain();
  923. if (nDepth < nMinDepth)
  924. continue;
  925. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  926. {
  927. CTxDestination address;
  928. if (!ExtractDestination(txout.scriptPubKey, address) || !IsMine(*pwalletMain, address))
  929. continue;
  930. tallyitem& item = mapTally[address];
  931. item.nAmount += txout.nValue;
  932. item.nConf = min(item.nConf, nDepth);
  933. }
  934. }
  935. // Reply
  936. Array ret;
  937. map<string, tallyitem> mapAccountTally;
  938. BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
  939. {
  940. const CBitcoinAddress& address = item.first;
  941. const string& strAccount = item.second;
  942. map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address);
  943. if (it == mapTally.end() && !fIncludeEmpty)
  944. continue;
  945. int64 nAmount = 0;
  946. int nConf = std::numeric_limits<int>::max();
  947. if (it != mapTally.end())
  948. {
  949. nAmount = (*it).second.nAmount;
  950. nConf = (*it).second.nConf;
  951. }
  952. if (fByAccounts)
  953. {
  954. tallyitem& item = mapAccountTally[strAccount];
  955. item.nAmount += nAmount;
  956. item.nConf = min(item.nConf, nConf);
  957. }
  958. else
  959. {
  960. Object obj;
  961. obj.push_back(Pair("address", address.ToString()));
  962. obj.push_back(Pair("account", strAccount));
  963. obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
  964. obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
  965. ret.push_back(obj);
  966. }
  967. }
  968. if (fByAccounts)
  969. {
  970. for (map<string, tallyitem>::iterator it = mapAccountTally.begin(); it != mapAccountTally.end(); ++it)
  971. {
  972. int64 nAmount = (*it).second.nAmount;
  973. int nConf = (*it).second.nConf;
  974. Object obj;
  975. obj.push_back(Pair("account", (*it).first));
  976. obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
  977. obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
  978. ret.push_back(obj);
  979. }
  980. }
  981. return ret;
  982. }
  983. Value listreceivedbyaddress(const Array& params, bool fHelp)
  984. {
  985. if (fHelp || params.size() > 2)
  986. throw runtime_error(
  987. "listreceivedbyaddress [minconf=1] [includeempty=false]\n"
  988. "[minconf] is the minimum number of confirmations before payments are included.\n"
  989. "[includeempty] whether to include addresses that haven't received any payments.\n"
  990. "Returns an array of objects containing:\n"
  991. " \"address\" : receiving address\n"
  992. " \"account\" : the account of the receiving address\n"
  993. " \"amount\" : total amount received by the address\n"
  994. " \"confirmations\" : number of confirmations of the most recent transaction included");
  995. return ListReceived(params, false);
  996. }
  997. Value listreceivedbyaccount(const Array& params, bool fHelp)
  998. {
  999. if (fHelp || params.size() > 2)
  1000. throw runtime_error(
  1001. "listreceivedbyaccount [minconf=1] [includeempty=false]\n"
  1002. "[minconf] is the minimum number of confirmations before payments are included.\n"
  1003. "[includeempty] whether to include accounts that haven't received any payments.\n"
  1004. "Returns an array of objects containing:\n"
  1005. " \"account\" : the account of the receiving addresses\n"
  1006. " \"amount\" : total amount received by addresses with this account\n"
  1007. " \"confirmations\" : number of confirmations of the most recent transaction included");
  1008. return ListReceived(params, true);
  1009. }
  1010. void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDepth, bool fLong, Array& ret)
  1011. {
  1012. int64 nGeneratedImmature, nGeneratedMature, nFee;
  1013. string strSentAccount;
  1014. list<pair<CTxDestination, int64> > listReceived;
  1015. list<pair<CTxDestination, int64> > listSent;
  1016. wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount);
  1017. bool fAllAccounts = (strAccount == string("*"));
  1018. // Generated blocks assigned to account ""
  1019. if ((nGeneratedMature+nGeneratedImmature) != 0 && (fAllAccounts || strAccount == ""))
  1020. {
  1021. Object entry;
  1022. entry.push_back(Pair("account", string("")));
  1023. if (nGeneratedImmature)
  1024. {
  1025. entry.push_back(Pair("category", wtx.GetDepthInMainChain() ? "immature" : "orphan"));
  1026. entry.push_back(Pair("amount", ValueFromAmount(nGeneratedImmature)));
  1027. }
  1028. else
  1029. {
  1030. entry.push_back(Pair("category", "generate"));
  1031. entry.push_back(Pair("amount", ValueFromAmount(nGeneratedMature)));
  1032. }
  1033. if (fLong)
  1034. WalletTxToJSON(wtx, entry);
  1035. ret.push_back(entry);
  1036. }
  1037. // Sent
  1038. if ((!listSent.empty() || nFee != 0) && (fAllAccounts || strAccount == strSentAccount))
  1039. {
  1040. BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64)& s, listSent)
  1041. {
  1042. Object entry;
  1043. entry.push_back(Pair("account", strSentAccount));
  1044. entry.push_back(Pair("address", CBitcoinAddress(s.first).ToString()));
  1045. entry.push_back(Pair("category", "send"));
  1046. entry.push_back(Pair("amount", ValueFromAmount(-s.second)));
  1047. entry.push_back(Pair("fee", ValueFromAmount(-nFee)));
  1048. if (fLong)
  1049. WalletTxToJSON(wtx, entry);
  1050. ret.push_back(entry);
  1051. }
  1052. }
  1053. // Received
  1054. if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth)
  1055. {
  1056. BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64)& r, listReceived)
  1057. {
  1058. string account;
  1059. if (pwalletMain->mapAddressBook.count(r.first))
  1060. account = pwalletMain->mapAddressBook[r.first];
  1061. if (fAllAccounts || (account == strAccount))
  1062. {
  1063. Object entry;
  1064. entry.push_back(Pair("account", account));
  1065. entry.push_back(Pair("address", CBitcoinAddress(r.first).ToString()));
  1066. entry.push_back(Pair("category", "receive"));
  1067. entry.push_back(Pair("amount", ValueFromAmount(r.second)));
  1068. if (fLong)
  1069. WalletTxToJSON(wtx, entry);
  1070. ret.push_back(entry);
  1071. }
  1072. }
  1073. }
  1074. }
  1075. void AcentryToJSON(const CAccountingEntry& acentry, const string& strAccount, Array& ret)
  1076. {
  1077. bool fAllAccounts = (strAccount == string("*"));
  1078. if (fAllAccounts || acentry.strAccount == strAccount)
  1079. {
  1080. Object entry;
  1081. entry.push_back(Pair("account", acentry.strAccount));
  1082. entry.push_back(Pair("category", "move"));
  1083. entry.push_back(Pair("time", (boost::int64_t)acentry.nTime));
  1084. entry.push_back(Pair("amount", ValueFromAmount(acentry.nCreditDebit)));
  1085. entry.push_back(Pair("otheraccount", acentry.strOtherAccount));
  1086. entry.push_back(Pair("comment", acentry.strComment));
  1087. ret.push_back(entry);
  1088. }
  1089. }
  1090. Value listtransactions(const Array& params, bool fHelp)
  1091. {
  1092. if (fHelp || params.size() > 3)
  1093. throw runtime_error(
  1094. "listtransactions [account] [count=10] [from=0]\n"
  1095. "Returns up to [count] most recent transactions skipping the first [from] transactions for account [account].");
  1096. string strAccount = "*";
  1097. if (params.size() > 0)
  1098. strAccount = params[0].get_str();
  1099. int nCount = 10;
  1100. if (params.size() > 1)
  1101. nCount = params[1].get_int();
  1102. int nFrom = 0;
  1103. if (params.size() > 2)
  1104. nFrom = params[2].get_int();
  1105. if (nCount < 0)
  1106. throw JSONRPCError(-8, "Negative count");
  1107. if (nFrom < 0)
  1108. throw JSONRPCError(-8, "Negative from");
  1109. Array ret;
  1110. CWalletDB walletdb(pwalletMain->strWalletFile);
  1111. // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
  1112. typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
  1113. typedef multimap<int64, TxPair > TxItems;
  1114. TxItems txByTime;
  1115. // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry
  1116. // would make this much faster for applications that do this a lot.
  1117. for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
  1118. {
  1119. CWalletTx* wtx = &((*it).second);
  1120. txByTime.insert(make_pair(wtx->GetTxTime(), TxPair(wtx, (CAccountingEntry*)0)));
  1121. }
  1122. list<CAccountingEntry> acentries;
  1123. walletdb.ListAccountCreditDebit(strAccount, acentries);
  1124. BOOST_FOREACH(CAccountingEntry& entry, acentries)
  1125. {
  1126. txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
  1127. }
  1128. // iterate backwards until we have nCount items to return:
  1129. for (TxItems::reverse_iterator it = txByTime.rbegin(); it != txByTime.rend(); ++it)
  1130. {
  1131. CWalletTx *const pwtx = (*it).second.first;
  1132. if (pwtx != 0)
  1133. ListTransactions(*pwtx, strAccount, 0, true, ret);
  1134. CAccountingEntry *const pacentry = (*it).second.second;
  1135. if (pacentry != 0)
  1136. AcentryToJSON(*pacentry, strAccount, ret);
  1137. if ((int)ret.size() >= (nCount+nFrom)) break;
  1138. }
  1139. // ret is newest to oldest
  1140. if (nFrom > (int)ret.size())
  1141. nFrom = ret.size();
  1142. if ((nFrom + nCount) > (int)ret.size())
  1143. nCount = ret.size() - nFrom;
  1144. Array::iterator first = ret.begin();
  1145. std::advance(first, nFrom);
  1146. Array::iterator last = ret.begin();
  1147. std::advance(last, nFrom+nCount);
  1148. if (last != ret.end()) ret.erase(last, ret.end());
  1149. if (first != ret.begin()) ret.erase(ret.begin(), first);
  1150. std::reverse(ret.begin(), ret.end()); // Return oldest to newest
  1151. return ret;
  1152. }
  1153. Value listaccounts(const Array& params, bool fHelp)
  1154. {
  1155. if (fHelp || params.size() > 1)
  1156. throw runtime_error(
  1157. "listaccounts [minconf=1]\n"
  1158. "Returns Object that has account names as keys, account balances as values.");
  1159. int nMinDepth = 1;
  1160. if (params.size() > 0)
  1161. nMinDepth = params[0].get_int();
  1162. map<string, int64> mapAccountBalances;
  1163. BOOST_FOREACH(const PAIRTYPE(CTxDestination, string)& entry, pwalletMain->mapAddressBook) {
  1164. if (IsMine(*pwalletMain, entry.first)) // This address belongs to me
  1165. mapAccountBalances[entry.second] = 0;
  1166. }
  1167. for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
  1168. {
  1169. const CWalletTx& wtx = (*it).second;
  1170. int64 nGeneratedImmature, nGeneratedMature, nFee;
  1171. string strSentAccount;
  1172. list<pair<CTxDestination, int64> > listReceived;
  1173. list<pair<CTxDestination, int64> > listSent;
  1174. wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount);
  1175. mapAccountBalances[strSentAccount] -= nFee;
  1176. BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64)& s, listSent)
  1177. mapAccountBalances[strSentAccount] -= s.second;
  1178. if (wtx.GetDepthInMainChain() >= nMinDepth)
  1179. {
  1180. mapAccountBalances[""] += nGeneratedMature;
  1181. BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64)& r, listReceived)
  1182. if (pwalletMain->mapAddressBook.count(r.first))
  1183. mapAccountBalances[pwalletMain->mapAddressBook[r.first]] += r.second;
  1184. else
  1185. mapAccountBalances[""] += r.second;
  1186. }
  1187. }
  1188. list<CAccountingEntry> acentries;
  1189. CWalletDB(pwalletMain->strWalletFile).ListAccountCreditDebit("*", acentries);
  1190. BOOST_FOREACH(const CAccountingEntry& entry, acentries)
  1191. mapAccountBalances[entry.strAccount] += entry.nCreditDebit;
  1192. Object ret;
  1193. BOOST_FOREACH(const PAIRTYPE(string, int64)& accountBalance, mapAccountBalances) {
  1194. ret.push_back(Pair(accountBalance.first, ValueFromAmount(accountBalance.second)));
  1195. }
  1196. return ret;
  1197. }
  1198. Value listsinceblock(const Array& params, bool fHelp)
  1199. {
  1200. if (fHelp)
  1201. throw runtime_error(
  1202. "listsinceblock [blockhash] [target-confirmations]\n"
  1203. "Get all transactions in blocks since block [blockhash], or all transactions if omitted");
  1204. CBlockIndex *pindex = NULL;
  1205. int target_confirms = 1;
  1206. if (params.size() > 0)
  1207. {
  1208. uint256 blockId = 0;
  1209. blockId.SetHex(params[0].get_str());
  1210. pindex = CBlockLocator(blockId).GetBlockIndex();
  1211. }
  1212. if (params.size() > 1)
  1213. {
  1214. target_confirms = params[1].get_int();
  1215. if (target_confirms < 1)
  1216. throw JSONRPCError(-8, "Invalid parameter");
  1217. }
  1218. int depth = pindex ? (1 + nBestHeight - pindex->nHeight) : -1;
  1219. Array transactions;
  1220. for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); it++)
  1221. {
  1222. CWalletTx tx = (*it).second;
  1223. if (depth == -1 || tx.GetDepthInMainChain() < depth)
  1224. ListTransactions(tx, "*", 0, true, transactions);
  1225. }
  1226. uint256 lastblock;
  1227. if (target_confirms == 1)
  1228. {
  1229. lastblock = hashBestChain;
  1230. }
  1231. else
  1232. {
  1233. int target_height = pindexBest->nHeight + 1 - target_confirms;
  1234. CBlockIndex *block;
  1235. for (block = pindexBest;
  1236. block && block->nHeight > target_height;
  1237. block = block->pprev) { }
  1238. lastblock = block ? block->GetBlockHash() : 0;
  1239. }
  1240. Object ret;
  1241. ret.push_back(Pair("transactions", transactions));
  1242. ret.push_back(Pair("lastblock", lastblock.GetHex()));
  1243. return ret;
  1244. }
  1245. Value gettransaction(const Array& params, bool fHelp)
  1246. {
  1247. if (fHelp || params.size() != 1)
  1248. throw runtime_error(
  1249. "gettransaction <txid>\n"
  1250. "Get detailed information about in-wallet transaction <txid>");
  1251. uint256 hash;
  1252. hash.SetHex(params[0].get_str());
  1253. Object entry;
  1254. if (!pwalletMain->mapWallet.count(hash))
  1255. throw JSONRPCError(-5, "Invalid or non-wallet transaction id");
  1256. const CWalletTx& wtx = pwalletMain->mapWallet[hash];
  1257. int64 nCredit = wtx.GetCredit();
  1258. int64 nDebit = wtx.GetDebit();
  1259. int64 nNet = nCredit - nDebit;
  1260. int64 nFee = (wtx.IsFromMe() ? wtx.GetValueOut() - nDebit : 0);
  1261. entry.push_back(Pair("amount", ValueFromAmount(nNet - nFee)));
  1262. if (wtx.IsFromMe())
  1263. entry.push_back(Pair("fee", ValueFromAmount(nFee)));
  1264. WalletTxToJSON(wtx, entry);
  1265. Array details;
  1266. ListTransactions(wtx, "*", 0, false, details);
  1267. entry.push_back(Pair("details", details));
  1268. return entry;
  1269. }
  1270. Value backupwallet(const Array& params, bool fHelp)
  1271. {
  1272. if (fHelp || params.size() != 1)
  1273. throw runtime_error(
  1274. "backupwallet <destination>\n"
  1275. "Safely copies wallet.dat to destination, which can be a directory or a path with filename.");
  1276. string strDest = params[0].get_str();
  1277. BackupWallet(*pwalletMain, strDest);
  1278. return Value::null;
  1279. }
  1280. Value keypoolrefill(const Array& params, bool fHelp)
  1281. {
  1282. if (fHelp || params.size() > 0)
  1283. throw runtime_error(
  1284. "keypoolrefill\n"
  1285. "Fills the keypool."
  1286. + HelpRequiringPassphrase());
  1287. EnsureWalletIsUnlocked();
  1288. pwalletMain->TopUpKeyPool();
  1289. if (pwalletMain->GetKeyPoolSize() < GetArg("-keypool", 100))
  1290. throw JSONRPCError(-4, "Error refreshing keypool.");
  1291. return Value::null;
  1292. }
  1293. void ThreadTopUpKeyPool(void* parg)
  1294. {
  1295. pwalletMain->TopUpKeyPool();
  1296. }
  1297. void ThreadCleanWalletPassphrase(void* parg)
  1298. {
  1299. int64 nMyWakeTime = GetTimeMillis() + *((int64*)parg) * 1000;
  1300. ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime);
  1301. if (nWalletUnlockTime == 0)
  1302. {
  1303. nWalletUnlockTime = nMyWakeTime;
  1304. do
  1305. {
  1306. if (nWalletUnlockTime==0)
  1307. break;
  1308. int64 nToSleep = nWalletUnlockTime - GetTimeMillis();
  1309. if (nToSleep <= 0)
  1310. break;
  1311. LEAVE_CRITICAL_SECTION(cs_nWalletUnlockTime);
  1312. Sleep(nToSleep);
  1313. ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime);
  1314. } while(1);
  1315. if (nWalletUnlockTime)
  1316. {
  1317. nWalletUnlockTime = 0;
  1318. pwalletMain->Lock();
  1319. }
  1320. }
  1321. else
  1322. {
  1323. if (nWalletUnlockTime < nMyWakeTime)
  1324. nWalletUnlockTime = nMyWakeTime;
  1325. }
  1326. LEAVE_CRITICAL_SECTION(cs_nWalletUnlockTime);
  1327. delete (int64*)parg;
  1328. }
  1329. Value walletpassphrase(const Array& params, bool fHelp)
  1330. {
  1331. if (pwalletMain->IsCrypted() && (fHelp || params.size() != 2))
  1332. throw runtime_error(
  1333. "walletpassphrase <passphrase> <timeout>\n"
  1334. "Stores the wallet decryption key in memory for <timeout> seconds.");
  1335. if (fHelp)
  1336. return true;
  1337. if (!pwalletMain->IsCrypted())
  1338. throw JSONRPCError(-15, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
  1339. if (!pwalletMain->IsLocked())
  1340. throw JSONRPCError(-17, "Error: Wallet is already unlocked.");
  1341. // Note that the walletpassphrase is stored in params[0] which is not mlock()ed
  1342. SecureString strWalletPass;
  1343. strWalletPass.reserve(100);
  1344. // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
  1345. // Alternately, find a way to make params[0] mlock()'d to begin with.
  1346. strWalletPass = params[0].get_str().c_str();
  1347. if (strWalletPass.length() > 0)
  1348. {
  1349. if (!pwalletMain->Unlock(strWalletPass))
  1350. throw JSONRPCError(-14, "Error: The wallet passphrase entered was incorrect.");
  1351. }
  1352. else
  1353. throw runtime_error(
  1354. "walletpassphrase <passphrase> <timeout>\n"
  1355. "Stores the wallet decryption key in memory for <timeout> seconds.");
  1356. CreateThread(ThreadTopUpKeyPool, NULL);
  1357. int64* pnSleepTime = new int64(params[1].get_int64());
  1358. CreateThread(ThreadCleanWalletPassphrase, pnSleepTime);
  1359. return Value::null;
  1360. }
  1361. Value walletpassphrasechange(const Array& params, bool fHelp)
  1362. {
  1363. if (pwalletMain->IsCrypted() && (fHelp || params.size() != 2))
  1364. throw runtime_error(
  1365. "walletpassphrasechange <oldpassphrase> <newpassphrase>\n"
  1366. "Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.");
  1367. if (fHelp)
  1368. return true;
  1369. if (!pwalletMain->IsCrypted())
  1370. throw JSONRPCError(-15, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.");
  1371. // TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
  1372. // Alternately, find a way to make params[0] mlock()'d to begin with.
  1373. SecureString strOldWalletPass;
  1374. strOldWalletPass.reserve(100);
  1375. strOldWalletPass = params[0].get_str().c_str();
  1376. SecureString strNewWalletPass;
  1377. strNewWalletPass.reserve(100);
  1378. strNewWalletPass = params[1].get_str().c_str();
  1379. if (strOldWalletPass.length() < 1 || strNewWalletPass.length() < 1)
  1380. throw runtime_error(
  1381. "walletpassphrasechange <oldpassphrase> <newpassphrase>\n"
  1382. "Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>.");
  1383. if (!pwalletMain->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass))
  1384. throw JSONRPCError(-14, "Error: The wallet passphrase entered was incorrect.");
  1385. return Value::null;
  1386. }
  1387. Value walletlock(const Array& params, bool fHelp)
  1388. {
  1389. if (pwalletMain->IsCrypted() && (fHelp || params.size() != 0))
  1390. throw runtime_error(
  1391. "walletlock\n"
  1392. "Removes the wallet encryption key from memory, locking the wallet.\n"
  1393. "After calling this method, you will need to call walletpassphrase again\n"
  1394. "before being able to call any methods which require the wallet to be unlocked.");
  1395. if (fHelp)
  1396. return true;
  1397. if (!pwalletMain->IsCrypted())
  1398. throw JSONRPCError(-15, "Error: running with an unencrypted wallet, but walletlock was called.");
  1399. {
  1400. LOCK(cs_nWalletUnlockTime);
  1401. pwalletMain->Lock();
  1402. nWalletUnlockTime = 0;
  1403. }
  1404. return Value::null;
  1405. }
  1406. Value encryptwallet(const Array& params, bool fHelp)
  1407. {
  1408. if (!pwalletMain->IsCrypted() && (fHelp || params.size() != 1))
  1409. throw runtime_error(
  1410. "encryptwallet <passphrase>\n"
  1411. "Encrypts the wallet with <passphrase>.");
  1412. if (fHelp)
  1413. return true;
  1414. if (pwalletMain->IsCrypted())
  1415. throw JSONRPCError(-15, "Error: running with an encrypted wallet, but encryptwallet was called.");
  1416. // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
  1417. // Alternately, find a way to make params[0] mlock()'d to begin with.
  1418. SecureString strWalletPass;
  1419. strWalletPass.reserve(100);
  1420. strWalletPass = params[0].get_str().c_str();
  1421. if (strWalletPass.length() < 1)
  1422. throw runtime_error(
  1423. "encryptwallet <passphrase>\n"
  1424. "Encrypts the wallet with <passphrase>.");
  1425. if (!pwalletMain->EncryptWallet(strWalletPass))
  1426. throw JSONRPCError(-16, "Error: Failed to encrypt the wallet.");
  1427. // BDB seems to have a bad habit of writing old data into
  1428. // slack space in .dat files; that is bad if the old data is
  1429. // unencrypted private keys. So:
  1430. StartShutdown();
  1431. return "wallet encrypted; Bitcoin server stopping, restart to run with encrypted wallet";
  1432. }
  1433. class DescribeAddressVisitor : public boost::static_visitor<Object>
  1434. {
  1435. public:
  1436. Object operator()(const CNoDestination &dest) const { return Object(); }
  1437. Object operator()(const CKeyID &keyID) const {
  1438. Object obj;
  1439. CPubKey vchPubKey;
  1440. pwalletMain->GetPubKey(keyID, vchPubKey);
  1441. obj.push_back(Pair("isscript", false));
  1442. obj.push_back(Pair("pubkey", HexStr(vchPubKey.Raw())));
  1443. obj.push_back(Pair("iscompressed", vchPubKey.IsCompressed()));
  1444. return obj;
  1445. }
  1446. Object operator()(const CScriptID &scriptID) const {
  1447. Object obj;
  1448. obj.push_back(Pair("isscript", true));
  1449. CScript subscript;
  1450. pwalletMain->GetCScript(scriptID, subscript);
  1451. std::vector<CTxDestination> addresses;
  1452. txnouttype whichType;
  1453. int nRequired;
  1454. ExtractDestinations(subscript, whichType, addresses, nRequired);
  1455. obj.push_back(Pair("script", GetTxnOutputType(whichType)));
  1456. Array a;
  1457. BOOST_FOREACH(const CTxDestination& addr, addresses)
  1458. a.push_back(CBitcoinAddress(addr).ToString());
  1459. obj.push_back(Pair("addresses", a));
  1460. if (whichType == TX_MULTISIG)
  1461. obj.push_back(Pair("sigsrequired", nRequired));
  1462. return obj;
  1463. }
  1464. };
  1465. Value validateaddress(const Array& params, bool fHelp)
  1466. {
  1467. if (fHelp || params.size() != 1)
  1468. throw runtime_error(
  1469. "validateaddress <bitcoinaddress>\n"
  1470. "Return information about <bitcoinaddress>.");
  1471. CBitcoinAddress address(params[0].get_str());
  1472. bool isValid = address.IsValid();
  1473. Object ret;
  1474. ret.push_back(Pair("isvalid", isValid));
  1475. if (isValid)
  1476. {
  1477. CTxDestination dest = address.Get();
  1478. string currentAddress = address.ToString();
  1479. ret.push_back(Pair("address", currentAddress));
  1480. bool fMine = IsMine(*pwalletMain, dest);
  1481. ret.push_back(Pair("ismine", fMine));
  1482. if (fMine) {
  1483. Object detail = boost::apply_visitor(DescribeAddressVisitor(), dest);
  1484. ret.insert(ret.end(), detail.begin(), detail.end());
  1485. }
  1486. if (pwalletMain->mapAddressBook.count(dest))
  1487. ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest]));
  1488. }
  1489. return ret;
  1490. }
  1491. Value getwork(const Array& params, bool fHelp)
  1492. {
  1493. if (fHelp || params.size() > 1)
  1494. throw runtime_error(
  1495. "getwork [data]\n"
  1496. "If [data] is not specified, returns formatted hash data to work on:\n"
  1497. " \"midstate\" : precomputed hash state after hashing the first half of the data (DEPRECATED)\n" // deprecated
  1498. " \"data\" : block data\n"
  1499. " \"hash1\" : formatted hash buffer for second hash (DEPRECATED)\n" // deprecated
  1500. " \"target\" : little endian hash target\n"
  1501. "If [data] is specified, tries to solve the block and returns true if it was successful.");
  1502. if (vNodes.empty())
  1503. throw JSONRPCError(-9, "Bitcoin is not connected!");
  1504. if (IsInitialBlockDownload())
  1505. throw JSONRPCError(-10, "Bitcoin is downloading blocks...");
  1506. typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
  1507. static mapNewBlock_t mapNewBlock; // FIXME: thread safety
  1508. static vector<CBlock*> vNewBlock;
  1509. static CReserveKey reservekey(pwalletMain);
  1510. if (params.size() == 0)
  1511. {
  1512. // Update block
  1513. static unsigned int nTransactionsUpdatedLast;
  1514. static CBlockIndex* pindexPrev;
  1515. static int64 nStart;
  1516. static CBlock* pblock;
  1517. if (pindexPrev != pindexBest ||
  1518. (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60))
  1519. {
  1520. if (pindexPrev != pindexBest)
  1521. {
  1522. // Deallocate old blocks since they're obsolete now
  1523. mapNewBlock.clear();
  1524. BOOST_FOREACH(CBlock* pblock, vNewBlock)
  1525. delete pblock;
  1526. vNewBlock.clear();
  1527. }
  1528. nTransactionsUpdatedLast = nTransactionsUpdated;
  1529. pindexPrev = pindexBest;
  1530. nStart = GetTime();
  1531. // Create new block
  1532. pblock = CreateNewBlock(reservekey);
  1533. if (!pblock)
  1534. throw JSONRPCError(-7, "Out of memory");
  1535. vNewBlock.push_back(pblock);
  1536. }
  1537. // Update nTime
  1538. pblock->UpdateTime(pindexPrev);
  1539. pblock->nNonce = 0;
  1540. // Update nExtraNonce
  1541. static unsigned int nExtraNonce = 0;
  1542. IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
  1543. // Save
  1544. mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
  1545. // Prebuild hash buffers
  1546. char pmidstate[32];
  1547. char pdata[128];
  1548. char phash1[64];
  1549. FormatHashBuffers(pblock, pmidstate, pdata, phash1);
  1550. uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
  1551. Object result;
  1552. result.push_back(Pair("midstate", HexStr(BEGIN(pmidstate), END(pmidstate)))); // deprecated
  1553. result.push_back(Pair("data", HexStr(BEGIN(pdata), END(pdata))));
  1554. result.push_back(Pair("hash1", HexStr(BEGIN(phash1), END(phash1)))); // deprecated
  1555. result.push_back(Pair("target", HexStr(BEGIN(hashTarget), END(hashTarget))));
  1556. return result;
  1557. }
  1558. else
  1559. {
  1560. // Parse parameters
  1561. vector<unsigned char> vchData = ParseHex(params[0].get_str());
  1562. if (vchData.size() != 128)
  1563. throw JSONRPCError(-8, "Invalid parameter");
  1564. CBlock* pdata = (CBlock*)&vchData[0];
  1565. // Byte reverse
  1566. for (int i = 0; i < 128/4; i++)
  1567. ((unsigned int*)pdata)[i] = ByteReverse(((unsigned int*)pdata)[i]);
  1568. // Get saved block
  1569. if (!mapNewBlock.count(pdata->hashMerkleRoot))
  1570. return false;
  1571. CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first;
  1572. pblock->nTime = pdata->nTime;
  1573. pblock->nNonce = pdata->nNonce;
  1574. pblock->vtx[0].vin[0].scriptSig = mapNewBlock[pdata->hashMerkleRoot].second;
  1575. pblock->hashMerkleRoot = pblock->BuildMerkleTree();
  1576. return CheckWork(pblock, *pwalletMain, reservekey);
  1577. }
  1578. }
  1579. Value getmemorypool(const Array& params, bool fHelp)
  1580. {
  1581. if (fHelp || params.size() > 1)
  1582. throw runtime_error(
  1583. "getmemorypool [data]\n"
  1584. "If [data] is not specified, returns data needed to construct a block to work on:\n"
  1585. " \"version\" : block version\n"
  1586. " \"previousblockhash\" : hash of current highest block\n"
  1587. " \"transactions\" : contents of non-coinbase transactions that should be included in the next block\n"
  1588. " \"coinbasevalue\" : maximum allowable input to coinbase transaction, including the generation award and transaction fees\n"
  1589. " \"coinbaseflags\" : data that should be included in coinbase so support for new features can be judged\n"
  1590. " \"time\" : timestamp appropriate for next block\n"
  1591. " \"mintime\" : minimum timestamp appropriate for next block\n"
  1592. " \"curtime\" : current timestamp\n"
  1593. " \"bits\" : compressed target of next block\n"
  1594. "If [data] is specified, tries to solve the block and returns true if it was successful.");
  1595. if (params.size() == 0)
  1596. {
  1597. if (vNodes.empty())
  1598. throw JSONRPCError(-9, "Bitcoin is not connected!");
  1599. if (IsInitialBlockDownload())
  1600. throw JSONRPCError(-10, "Bitcoin is downloading blocks...");
  1601. static CReserveKey reservekey(pwalletMain);
  1602. // Update block
  1603. static unsigned int nTransactionsUpdatedLast;
  1604. static CBlockIndex* pindexPrev;
  1605. static int64 nStart;
  1606. static CBlock* pblock;
  1607. if (pindexPrev != pindexBest ||
  1608. (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 5))
  1609. {
  1610. nTransactionsUpdatedLast = nTransactionsUpdated;
  1611. pindexPrev = pindexBest;
  1612. nStart = GetTime();
  1613. // Create new block
  1614. if(pblock)
  1615. delete pblock;
  1616. pblock = CreateNewBlock(reservekey);
  1617. if (!pblock)
  1618. throw JSONRPCError(-7, "Out of memory");
  1619. }
  1620. // Update nTime
  1621. pblock->UpdateTime(pindexPrev);
  1622. pblock->nNonce = 0;
  1623. Array transactions;
  1624. BOOST_FOREACH(CTransaction tx, pblock->vtx) {
  1625. if(tx.IsCoinBase())
  1626. continue;
  1627. CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
  1628. ssTx << tx;
  1629. transactions.push_back(HexStr(ssTx.begin(), ssTx.end()));
  1630. }
  1631. Object result;
  1632. result.push_back(Pair("version", pblock->nVersion));
  1633. result.push_back(Pair("previousblockhash", pblock->hashPrevBlock.GetHex()));
  1634. result.push_back(Pair("transactions", transactions));
  1635. result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue));
  1636. result.push_back(Pair("coinbaseflags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));
  1637. result.push_back(Pair("time", (int64_t)pblock->nTime));
  1638. result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
  1639. result.push_back(Pair("curtime", (int64_t)GetAdjustedTime()));
  1640. result.push_back(Pair("bits", HexBits(pblock->nBits)));
  1641. return result;
  1642. }
  1643. else
  1644. {
  1645. // Parse parameters
  1646. CDataStream ssBlock(ParseHex(params[0].get_str()), SER_NETWORK, PROTOCOL_VERSION);
  1647. CBlock pblock;
  1648. ssBlock >> pblock;
  1649. return ProcessBlock(NULL, &pblock);
  1650. }
  1651. }
  1652. Value getrawmempool(const Array& params, bool fHelp)
  1653. {
  1654. if (fHelp || params.size() != 0)
  1655. throw runtime_error(
  1656. "getrawmempool\n"
  1657. "Returns all transaction ids in memory pool.");
  1658. vector<uint256> vtxid;
  1659. mempool.queryHashes(vtxid);
  1660. Array a;
  1661. BOOST_FOREACH(const uint256& hash, vtxid)
  1662. a.push_back(hash.ToString());
  1663. return a;
  1664. }
  1665. Value getblockhash(const Array& params, bool fHelp)
  1666. {
  1667. if (fHelp || params.size() != 1)
  1668. throw runtime_error(
  1669. "getblockhash <index>\n"
  1670. "Returns hash of block in best-block-chain at <index>.");
  1671. int nHeight = params[0].get_int();
  1672. if (nHeight < 0 || nHeight > nBestHeight)
  1673. throw runtime_error("Block number out of range.");
  1674. CBlock block;
  1675. CBlockIndex* pblockindex = mapBlockIndex[hashBestChain];
  1676. while (pblockindex->nHeight > nHeight)
  1677. pblockindex = pblockindex->pprev;
  1678. return pblockindex->phashBlock->GetHex();
  1679. }
  1680. Value getblock(const Array& params, bool fHelp)
  1681. {
  1682. if (fHelp || params.size() != 1)
  1683. throw runtime_error(
  1684. "getblock <hash>\n"
  1685. "Returns details of a block with given block-hash.");
  1686. std::string strHash = params[0].get_str();
  1687. uint256 hash(strHash);
  1688. if (mapBlockIndex.count(hash) == 0)
  1689. throw JSONRPCError(-5, "Block not found");
  1690. CBlock block;
  1691. CBlockIndex* pblockindex = mapBlockIndex[hash];
  1692. block.ReadFromDisk(pblockindex, true);
  1693. return blockToJSON(block, pblockindex);
  1694. }
  1695. //
  1696. // Call Table
  1697. //
  1698. static const CRPCCommand vRPCCommands[] =
  1699. { // name function safe mode?
  1700. // ------------------------ ----------------------- ----------
  1701. { "help", &help, true },
  1702. { "stop", &stop, true },
  1703. { "getblockcount", &getblockcount, true },
  1704. { "getconnectioncount", &getconnectioncount, true },
  1705. { "getpeerinfo", &getpeerinfo, true },
  1706. { "getdifficulty", &getdifficulty, true },
  1707. { "getgenerate", &getgenerate, true },
  1708. { "setgenerate", &setgenerate, true },
  1709. { "gethashespersec", &gethashespersec, true },
  1710. { "getinfo", &getinfo, true },
  1711. { "getmininginfo", &getmininginfo, true },
  1712. { "getnewaddress", &getnewaddress, true },
  1713. { "getaccountaddress", &getaccountaddress, true },
  1714. { "setaccount", &setaccount, true },
  1715. { "getaccount", &getaccount, false },
  1716. { "getaddressesbyaccount", &getaddressesbyaccount, true },
  1717. { "sendtoaddress", &sendtoaddress, false },
  1718. { "getreceivedbyaddress", &getreceivedbyaddress, false },
  1719. { "getreceivedbyaccount", &getreceivedbyaccount, false },
  1720. { "listreceivedbyaddress", &listreceivedbyaddress, false },
  1721. { "listreceivedbyaccount", &listreceivedbyaccount, false },
  1722. { "backupwallet", &backupwallet, true },
  1723. { "keypoolrefill", &keypoolrefill, true },
  1724. { "walletpassphrase", &walletpassphrase, true },
  1725. { "walletpassphrasechange", &walletpassphrasechange, false },
  1726. { "walletlock", &walletlock, true },
  1727. { "encryptwallet", &encryptwallet, false },
  1728. { "validateaddress", &validateaddress, true },
  1729. { "getbalance", &getbalance, false },
  1730. { "move", &movecmd, false },
  1731. { "sendfrom", &sendfrom, false },
  1732. { "sendmany", &sendmany, false },
  1733. { "addmultisigaddress", &addmultisigaddress, false },
  1734. { "getrawmempool", &getrawmempool, true },
  1735. { "getblock", &getblock, false },
  1736. { "getblockhash", &getblockhash, false },
  1737. { "gettransaction", &gettransaction, false },
  1738. { "listtransactions", &listtransactions, false },
  1739. { "signmessage", &signmessage, false },
  1740. { "verifymessage", &verifymessage, false },
  1741. { "getwork", &getwork, true },
  1742. { "listaccounts", &listaccounts, false },
  1743. { "settxfee", &settxfee, false },
  1744. { "getmemorypool", &getmemorypool, true },
  1745. { "listsinceblock", &listsinceblock, false },
  1746. { "dumpprivkey", &dumpprivkey, false },
  1747. { "importprivkey", &importprivkey, false },
  1748. { "listunspent", &listunspent, false },
  1749. { "getrawtransaction", &getrawtransaction, false },
  1750. { "createrawtransaction", &createrawtransaction, false },
  1751. { "decoderawtransaction", &decoderawtransaction, false },
  1752. { "signrawtransaction", &signrawtransaction, false },
  1753. { "sendrawtransaction", &sendrawtransaction, false },
  1754. };
  1755. CRPCTable::CRPCTable()
  1756. {
  1757. unsigned int vcidx;
  1758. for (vcidx = 0; vcidx < (sizeof(vRPCCommands) / sizeof(vRPCCommands[0])); vcidx++)
  1759. {
  1760. const CRPCCommand *pcmd;
  1761. pcmd = &vRPCCommands[vcidx];
  1762. mapCommands[pcmd->name] = pcmd;
  1763. }
  1764. }
  1765. const CRPCCommand *CRPCTable::operator[](string name) const
  1766. {
  1767. map<string, const CRPCCommand*>::const_iterator it = mapCommands.find(name);
  1768. if (it == mapCommands.end())
  1769. return NULL;
  1770. return (*it).second;
  1771. }
  1772. //
  1773. // HTTP protocol
  1774. //
  1775. // This ain't Apache. We're just using HTTP header for the length field
  1776. // and to be compatible with other JSON-RPC implementations.
  1777. //
  1778. string HTTPPost(const string& strMsg, const map<string,string>& mapRequestHeaders)
  1779. {
  1780. ostringstream s;
  1781. s << "POST / HTTP/1.1\r\n"
  1782. << "User-Agent: bitcoin-json-rpc/" << FormatFullVersion() << "\r\n"
  1783. << "Host: 127.0.0.1\r\n"
  1784. << "Content-Type: application/json\r\n"
  1785. << "Content-Length: " << strMsg.size() << "\r\n"
  1786. << "Connection: close\r\n"
  1787. << "Accept: application/json\r\n";
  1788. BOOST_FOREACH(const PAIRTYPE(string, string)& item, mapRequestHeaders)
  1789. s << item.first << ": " << item.second << "\r\n";
  1790. s << "\r\n" << strMsg;
  1791. return s.str();
  1792. }
  1793. string rfc1123Time()
  1794. {
  1795. char buffer[64];
  1796. time_t now;
  1797. time(&now);
  1798. struct tm* now_gmt = gmtime(&now);
  1799. string locale(setlocale(LC_TIME, NULL));
  1800. setlocale(LC_TIME, "C"); // we want posix (aka "C") weekday/month strings
  1801. strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S +0000", now_gmt);
  1802. setlocale(LC_TIME, locale.c_str());
  1803. return string(buffer);
  1804. }
  1805. static string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
  1806. {
  1807. if (nStatus == 401)
  1808. return strprintf("HTTP/1.0 401 Authorization Required\r\n"
  1809. "Date: %s\r\n"
  1810. "Server: bitcoin-json-rpc/%s\r\n"
  1811. "WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
  1812. "Content-Type: text/html\r\n"
  1813. "Content-Length: 296\r\n"
  1814. "\r\n"
  1815. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\r\n"
  1816. "\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">\r\n"
  1817. "<HTML>\r\n"
  1818. "<HEAD>\r\n"
  1819. "<TITLE>Error</TITLE>\r\n"
  1820. "<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>\r\n"
  1821. "</HEAD>\r\n"
  1822. "<BODY><H1>401 Unauthorized.</H1></BODY>\r\n"
  1823. "</HTML>\r\n", rfc1123Time().c_str(), FormatFullVersion().c_str());
  1824. const char *cStatus;
  1825. if (nStatus == 200) cStatus = "OK";
  1826. else if (nStatus == 400) cStatus = "Bad Request";
  1827. else if (nStatus == 403) cStatus = "Forbidden";
  1828. else if (nStatus == 404) cStatus = "Not Found";
  1829. else if (nStatus == 500) cStatus = "Internal Server Error";
  1830. else cStatus = "";
  1831. return strprintf(
  1832. "HTTP/1.1 %d %s\r\n"
  1833. "Date: %s\r\n"
  1834. "Connection: %s\r\n"
  1835. "Content-Length: %d\r\n"
  1836. "Content-Type: application/json\r\n"
  1837. "Server: bitcoin-json-rpc/%s\r\n"
  1838. "\r\n"
  1839. "%s",
  1840. nStatus,
  1841. cStatus,
  1842. rfc1123Time().c_str(),
  1843. keepalive ? "keep-alive" : "close",
  1844. strMsg.size(),
  1845. FormatFullVersion().c_str(),
  1846. strMsg.c_str());
  1847. }
  1848. int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto)
  1849. {
  1850. string str;
  1851. getline(stream, str);
  1852. vector<string> vWords;
  1853. boost::split(vWords, str, boost::is_any_of(" "));
  1854. if (vWords.size() < 2)
  1855. return 500;
  1856. proto = 0;
  1857. const char *ver = strstr(str.c_str(), "HTTP/1.");
  1858. if (ver != NULL)
  1859. proto = atoi(ver+7);
  1860. return atoi(vWords[1].c_str());
  1861. }
  1862. int ReadHTTPHeader(std::basic_istream<char>& stream, map<string, string>& mapHeadersRet)
  1863. {
  1864. int nLen = 0;
  1865. loop
  1866. {
  1867. string str;
  1868. std::getline(stream, str);
  1869. if (str.empty() || str == "\r")
  1870. break;
  1871. string::size_type nColon = str.find(":");
  1872. if (nColon != string::npos)
  1873. {
  1874. string strHeader = str.substr(0, nColon);
  1875. boost::trim(strHeader);
  1876. boost::to_lower(strHeader);
  1877. string strValue = str.substr(nColon+1);
  1878. boost::trim(strValue);
  1879. mapHeadersRet[strHeader] = strValue;
  1880. if (strHeader == "content-length")
  1881. nLen = atoi(strValue.c_str());
  1882. }
  1883. }
  1884. return nLen;
  1885. }
  1886. int ReadHTTP(std::basic_istream<char>& stream, map<string, string>& mapHeadersRet, string& strMessageRet)
  1887. {
  1888. mapHeadersRet.clear();
  1889. strMessageRet = "";
  1890. // Read status
  1891. int nProto = 0;
  1892. int nStatus = ReadHTTPStatus(stream, nProto);
  1893. // Read header
  1894. int nLen = ReadHTTPHeader(stream, mapHeadersRet);
  1895. if (nLen < 0 || nLen > (int)MAX_SIZE)
  1896. return 500;
  1897. // Read message
  1898. if (nLen > 0)
  1899. {
  1900. vector<char> vch(nLen);
  1901. stream.read(&vch[0], nLen);
  1902. strMessageRet = string(vch.begin(), vch.end());
  1903. }
  1904. string sConHdr = mapHeadersRet["connection"];
  1905. if ((sConHdr != "close") && (sConHdr != "keep-alive"))
  1906. {
  1907. if (nProto >= 1)
  1908. mapHeadersRet["connection"] = "keep-alive";
  1909. else
  1910. mapHeadersRet["connection"] = "close";
  1911. }
  1912. return nStatus;
  1913. }
  1914. bool HTTPAuthorized(map<string, string>& mapHeaders)
  1915. {
  1916. string strAuth = mapHeaders["authorization"];
  1917. if (strAuth.substr(0,6) != "Basic ")
  1918. return false;
  1919. string strUserPass64 = strAuth.substr(6); boost::trim(strUserPass64);
  1920. string strUserPass = DecodeBase64(strUserPass64);
  1921. return strUserPass == strRPCUserColonPass;
  1922. }
  1923. //
  1924. // JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility,
  1925. // but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
  1926. // unspecified (HTTP errors and contents of 'error').
  1927. //
  1928. // 1.0 spec: http://json-rpc.org/wiki/specification
  1929. // 1.2 spec: http://groups.google.com/group/json-rpc/web/json-rpc-over-http
  1930. // http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx
  1931. //
  1932. string JSONRPCRequest(const string& strMethod, const Array& params, const Value& id)
  1933. {
  1934. Object request;
  1935. request.push_back(Pair("method", strMethod));
  1936. request.push_back(Pair("params", params));
  1937. request.push_back(Pair("id", id));
  1938. return write_string(Value(request), false) + "\n";
  1939. }
  1940. Object JSONRPCReplyObj(const Value& result, const Value& error, const Value& id)
  1941. {
  1942. Object reply;
  1943. if (error.type() != null_type)
  1944. reply.push_back(Pair("result", Value::null));
  1945. else
  1946. reply.push_back(Pair("result", result));
  1947. reply.push_back(Pair("error", error));
  1948. reply.push_back(Pair("id", id));
  1949. return reply;
  1950. }
  1951. string JSONRPCReply(const Value& result, const Value& error, const Value& id)
  1952. {
  1953. Object reply = JSONRPCReplyObj(result, error, id);
  1954. return write_string(Value(reply), false) + "\n";
  1955. }
  1956. void ErrorReply(std::ostream& stream, const Object& objError, const Value& id)
  1957. {
  1958. // Send error reply from json-rpc error object
  1959. int nStatus = 500;
  1960. int code = find_value(objError, "code").get_int();
  1961. if (code == -32600) nStatus = 400;
  1962. else if (code == -32601) nStatus = 404;
  1963. string strReply = JSONRPCReply(Value::null, objError, id);
  1964. stream << HTTPReply(nStatus, strReply, false) << std::flush;
  1965. }
  1966. bool ClientAllowed(const boost::asio::ip::address& address)
  1967. {
  1968. // Make sure that IPv4-compatible and IPv4-mapped IPv6 addresses are treated as IPv4 addresses
  1969. if (address.is_v6()
  1970. && (address.to_v6().is_v4_compatible()
  1971. || address.to_v6().is_v4_mapped()))
  1972. return ClientAllowed(address.to_v6().to_v4());
  1973. if (address == asio::ip::address_v4::loopback()
  1974. || address == asio::ip::address_v6::loopback()
  1975. || (address.is_v4()
  1976. // Chech whether IPv4 addresses match 127.0.0.0/8 (loopback subnet)
  1977. && (address.to_v4().to_ulong() & 0xff000000) == 0x7f000000))
  1978. return true;
  1979. const string strAddress = address.to_string();
  1980. const vector<string>& vAllow = mapMultiArgs["-rpcallowip"];
  1981. BOOST_FOREACH(string strAllow, vAllow)
  1982. if (WildcardMatch(strAddress, strAllow))
  1983. return true;
  1984. return false;
  1985. }
  1986. //
  1987. // IOStream device that speaks SSL but can also speak non-SSL
  1988. //
  1989. template <typename Protocol>
  1990. class SSLIOStreamDevice : public iostreams::device<iostreams::bidirectional> {
  1991. public:
  1992. SSLIOStreamDevice(asio::ssl::stream<typename Protocol::socket> &streamIn, bool fUseSSLIn) : stream(streamIn)
  1993. {
  1994. fUseSSL = fUseSSLIn;
  1995. fNeedHandshake = fUseSSLIn;
  1996. }
  1997. void handshake(ssl::stream_base::handshake_type role)
  1998. {
  1999. if (!fNeedHandshake) return;
  2000. fNeedHandshake = false;
  2001. stream.handshake(role);
  2002. }
  2003. std::streamsize read(char* s, std::streamsize n)
  2004. {
  2005. handshake(ssl::stream_base::server); // HTTPS servers read first
  2006. if (fUseSSL) return stream.read_some(asio::buffer(s, n));
  2007. return stream.next_layer().read_some(asio::buffer(s, n));
  2008. }
  2009. std::streamsize write(const char* s, std::streamsize n)
  2010. {
  2011. handshake(ssl::stream_base::client); // HTTPS clients write first
  2012. if (fUseSSL) return asio::write(stream, asio::buffer(s, n));
  2013. return asio::write(stream.next_layer(), asio::buffer(s, n));
  2014. }
  2015. bool connect(const std::string& server, const std::string& port)
  2016. {
  2017. ip::tcp::resolver resolver(stream.get_io_service());
  2018. ip::tcp::resolver::query query(server.c_str(), port.c_str());
  2019. ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  2020. ip::tcp::resolver::iterator end;
  2021. boost::system::error_code error = asio::error::host_not_found;
  2022. while (error && endpoint_iterator != end)
  2023. {
  2024. stream.lowest_layer().close();
  2025. stream.lowest_layer().connect(*endpoint_iterator++, error);
  2026. }
  2027. if (error)
  2028. return false;
  2029. return true;
  2030. }
  2031. private:
  2032. bool fNeedHandshake;
  2033. bool fUseSSL;
  2034. asio::ssl::stream<typename Protocol::socket>& stream;
  2035. };
  2036. class AcceptedConnection
  2037. {
  2038. public:
  2039. virtual ~AcceptedConnection() {}
  2040. virtual std::iostream& stream() = 0;
  2041. virtual std::string peer_address_to_string() const = 0;
  2042. virtual void close() = 0;
  2043. };
  2044. template <typename Protocol>
  2045. class AcceptedConnectionImpl : public AcceptedConnection
  2046. {
  2047. public:
  2048. AcceptedConnectionImpl(
  2049. asio::io_service& io_service,
  2050. ssl::context &context,
  2051. bool fUseSSL) :
  2052. sslStream(io_service, context),
  2053. _d(sslStream, fUseSSL),
  2054. _stream(_d)
  2055. {
  2056. }
  2057. virtual std::iostream& stream()
  2058. {
  2059. return _stream;
  2060. }
  2061. virtual std::string peer_address_to_string() const
  2062. {
  2063. return peer.address().to_string();
  2064. }
  2065. virtual void close()
  2066. {
  2067. _stream.close();
  2068. }
  2069. typename Protocol::endpoint peer;
  2070. asio::ssl::stream<typename Protocol::socket> sslStream;
  2071. private:
  2072. SSLIOStreamDevice<Protocol> _d;
  2073. iostreams::stream< SSLIOStreamDevice<Protocol> > _stream;
  2074. };
  2075. void ThreadRPCServer(void* parg)
  2076. {
  2077. IMPLEMENT_RANDOMIZE_STACK(ThreadRPCServer(parg));
  2078. try
  2079. {
  2080. vnThreadsRunning[THREAD_RPCLISTENER]++;
  2081. ThreadRPCServer2(parg);
  2082. vnThreadsRunning[THREAD_RPCLISTENER]--;
  2083. }
  2084. catch (std::exception& e) {
  2085. vnThreadsRunning[THREAD_RPCLISTENER]--;
  2086. PrintException(&e, "ThreadRPCServer()");
  2087. } catch (...) {
  2088. vnThreadsRunning[THREAD_RPCLISTENER]--;
  2089. PrintException(NULL, "ThreadRPCServer()");
  2090. }
  2091. printf("ThreadRPCServer exited\n");
  2092. }
  2093. // Forward declaration required for RPCListen
  2094. template <typename Protocol, typename SocketAcceptorService>
  2095. static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol, SocketAcceptorService> > acceptor,
  2096. ssl::context& context,
  2097. bool fUseSSL,
  2098. AcceptedConnection* conn,
  2099. const boost::system::error_code& error);
  2100. /**
  2101. * Sets up I/O resources to accept and handle a new connection.
  2102. */
  2103. template <typename Protocol, typename SocketAcceptorService>
  2104. static void RPCListen(boost::shared_ptr< basic_socket_acceptor<Protocol, SocketAcceptorService> > acceptor,
  2105. ssl::context& context,
  2106. const bool fUseSSL)
  2107. {
  2108. // Accept connection
  2109. AcceptedConnectionImpl<Protocol>* conn = new AcceptedConnectionImpl<Protocol>(acceptor->get_io_service(), context, fUseSSL);
  2110. acceptor->async_accept(
  2111. conn->sslStream.lowest_layer(),
  2112. conn->peer,
  2113. boost::bind(&RPCAcceptHandler<Protocol, SocketAcceptorService>,
  2114. acceptor,
  2115. boost::ref(context),
  2116. fUseSSL,
  2117. conn,
  2118. boost::asio::placeholders::error));
  2119. }
  2120. /**
  2121. * Accept and handle incoming connection.
  2122. */
  2123. template <typename Protocol, typename SocketAcceptorService>
  2124. static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol, SocketAcceptorService> > acceptor,
  2125. ssl::context& context,
  2126. const bool fUseSSL,
  2127. AcceptedConnection* conn,
  2128. const boost::system::error_code& error)
  2129. {
  2130. vnThreadsRunning[THREAD_RPCLISTENER]++;
  2131. // Immediately start accepting new connections, except when we're canceled or our socket is closed.
  2132. if (error != asio::error::operation_aborted
  2133. && acceptor->is_open())
  2134. RPCListen(acceptor, context, fUseSSL);
  2135. AcceptedConnectionImpl<ip::tcp>* tcp_conn = dynamic_cast< AcceptedConnectionImpl<ip::tcp>* >(conn);
  2136. // TODO: Actually handle errors
  2137. if (error)
  2138. {
  2139. delete conn;
  2140. }
  2141. // Restrict callers by IP. It is important to
  2142. // do this before starting client thread, to filter out
  2143. // certain DoS and misbehaving clients.
  2144. else if (tcp_conn
  2145. && !ClientAllowed(tcp_conn->peer.address()))
  2146. {
  2147. // Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake.
  2148. if (!fUseSSL)
  2149. conn->stream() << HTTPReply(403, "", false) << std::flush;
  2150. delete conn;
  2151. }
  2152. // start HTTP client thread
  2153. else if (!CreateThread(ThreadRPCServer3, conn)) {
  2154. printf("Failed to create RPC server client thread\n");
  2155. delete conn;
  2156. }
  2157. vnThreadsRunning[THREAD_RPCLISTENER]--;
  2158. }
  2159. void ThreadRPCServer2(void* parg)
  2160. {
  2161. printf("ThreadRPCServer started\n");
  2162. strRPCUserColonPass = mapArgs["-rpcuser"] + ":" + mapArgs["-rpcpassword"];
  2163. if (mapArgs["-rpcpassword"] == "")
  2164. {
  2165. unsigned char rand_pwd[32];
  2166. RAND_bytes(rand_pwd, 32);
  2167. string strWhatAmI = "To use bitcoind";
  2168. if (mapArgs.count("-server"))
  2169. strWhatAmI = strprintf(_("To use the %s option"), "\"-server\"");
  2170. else if (mapArgs.count("-daemon"))
  2171. strWhatAmI = strprintf(_("To use the %s option"), "\"-daemon\"");
  2172. uiInterface.ThreadSafeMessageBox(strprintf(
  2173. _("%s, you must set a rpcpassword in the configuration file:\n %s\n"
  2174. "It is recommended you use the following random password:\n"
  2175. "rpcuser=bitcoinrpc\n"
  2176. "rpcpassword=%s\n"
  2177. "(you do not need to remember this password)\n"
  2178. "If the file does not exist, create it with owner-readable-only file permissions.\n"),
  2179. strWhatAmI.c_str(),
  2180. GetConfigFile().string().c_str(),
  2181. EncodeBase58(&rand_pwd[0],&rand_pwd[0]+32).c_str()),
  2182. _("Error"), CClientUIInterface::OK | CClientUIInterface::MODAL);
  2183. StartShutdown();
  2184. return;
  2185. }
  2186. const bool fUseSSL = GetBoolArg("-rpcssl");
  2187. asio::io_service io_service;
  2188. ssl::context context(io_service, ssl::context::sslv23);
  2189. if (fUseSSL)
  2190. {
  2191. context.set_options(ssl::context::no_sslv2);
  2192. filesystem::path pathCertFile(GetArg("-rpcsslcertificatechainfile", "server.cert"));
  2193. if (!pathCertFile.is_complete()) pathCertFile = filesystem::path(GetDataDir()) / pathCertFile;
  2194. if (filesystem::exists(pathCertFile)) context.use_certificate_chain_file(pathCertFile.string());
  2195. else printf("ThreadRPCServer ERROR: missing server certificate file %s\n", pathCertFile.string().c_str());
  2196. filesystem::path pathPKFile(GetArg("-rpcsslprivatekeyfile", "server.pem"));
  2197. if (!pathPKFile.is_complete()) pathPKFile = filesystem::path(GetDataDir()) / pathPKFile;
  2198. if (filesystem::exists(pathPKFile)) context.use_private_key_file(pathPKFile.string(), ssl::context::pem);
  2199. else printf("ThreadRPCServer ERROR: missing server private key file %s\n", pathPKFile.string().c_str());
  2200. string strCiphers = GetArg("-rpcsslciphers", "TLSv1+HIGH:!SSLv2:!aNULL:!eNULL:!AH:!3DES:@STRENGTH");
  2201. SSL_CTX_set_cipher_list(context.impl(), strCiphers.c_str());
  2202. }
  2203. // Try a dual IPv6/IPv4 socket, falling back to separate IPv4 and IPv6 sockets
  2204. const bool loopback = !mapArgs.count("-rpcallowip");
  2205. asio::ip::address bindAddress = loopback ? asio::ip::address_v6::loopback() : asio::ip::address_v6::any();
  2206. ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", 8332));
  2207. boost::signals2::signal<void ()> StopRequests;
  2208. try
  2209. {
  2210. boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(io_service));
  2211. acceptor->open(endpoint.protocol());
  2212. acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  2213. // Try making the socket dual IPv6/IPv4 (if listening on the "any" address)
  2214. boost::system::error_code v6_only_error;
  2215. acceptor->set_option(boost::asio::ip::v6_only(loopback), v6_only_error);
  2216. acceptor->bind(endpoint);
  2217. acceptor->listen(socket_base::max_connections);
  2218. RPCListen(acceptor, context, fUseSSL);
  2219. // Cancel outstanding listen-requests for this acceptor when shutting down
  2220. StopRequests.connect(signals2::slot<void ()>(
  2221. static_cast<void (ip::tcp::acceptor::*)()>(&ip::tcp::acceptor::close), acceptor.get())
  2222. .track(acceptor));
  2223. // If dual IPv6/IPv4 failed (or we're opening loopback interfaces only), open IPv4 separately
  2224. if (loopback || v6_only_error)
  2225. {
  2226. bindAddress = loopback ? asio::ip::address_v4::loopback() : asio::ip::address_v4::any();
  2227. endpoint.address(bindAddress);
  2228. acceptor.reset(new ip::tcp::acceptor(io_service));
  2229. acceptor->open(endpoint.protocol());
  2230. acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  2231. acceptor->bind(endpoint);
  2232. acceptor->listen(socket_base::max_connections);
  2233. RPCListen(acceptor, context, fUseSSL);
  2234. // Cancel outstanding listen-requests for this acceptor when shutting down
  2235. StopRequests.connect(signals2::slot<void ()>(
  2236. static_cast<void (ip::tcp::acceptor::*)()>(&ip::tcp::acceptor::close), acceptor.get())
  2237. .track(acceptor));
  2238. }
  2239. }
  2240. catch(boost::system::system_error &e)
  2241. {
  2242. uiInterface.ThreadSafeMessageBox(strprintf(_("An error occured while setting up the RPC port %i for listening: %s"), endpoint.port(), e.what()),
  2243. _("Error"), CClientUIInterface::OK | CClientUIInterface::MODAL);
  2244. StartShutdown();
  2245. return;
  2246. }
  2247. vnThreadsRunning[THREAD_RPCLISTENER]--;
  2248. while (!fShutdown)
  2249. io_service.run_one();
  2250. vnThreadsRunning[THREAD_RPCLISTENER]++;
  2251. StopRequests();
  2252. }
  2253. class JSONRequest
  2254. {
  2255. public:
  2256. Value id;
  2257. string strMethod;
  2258. Array params;
  2259. JSONRequest() { id = Value::null; }
  2260. void parse(const Value& valRequest);
  2261. };
  2262. void JSONRequest::parse(const Value& valRequest)
  2263. {
  2264. // Parse request
  2265. if (valRequest.type() != obj_type)
  2266. throw JSONRPCError(-32600, "Invalid Request object");
  2267. const Object& request = valRequest.get_obj();
  2268. // Parse id now so errors from here on will have the id
  2269. id = find_value(request, "id");
  2270. // Parse method
  2271. Value valMethod = find_value(request, "method");
  2272. if (valMethod.type() == null_type)
  2273. throw JSONRPCError(-32600, "Missing method");
  2274. if (valMethod.type() != str_type)
  2275. throw JSONRPCError(-32600, "Method must be a string");
  2276. strMethod = valMethod.get_str();
  2277. if (strMethod != "getwork" && strMethod != "getmemorypool")
  2278. printf("ThreadRPCServer method=%s\n", strMethod.c_str());
  2279. // Parse params
  2280. Value valParams = find_value(request, "params");
  2281. if (valParams.type() == array_type)
  2282. params = valParams.get_array();
  2283. else if (valParams.type() == null_type)
  2284. params = Array();
  2285. else
  2286. throw JSONRPCError(-32600, "Params must be an array");
  2287. }
  2288. static Object JSONRPCExecOne(const Value& req)
  2289. {
  2290. Object rpc_result;
  2291. JSONRequest jreq;
  2292. try {
  2293. jreq.parse(req);
  2294. Value result = tableRPC.execute(jreq.strMethod, jreq.params);
  2295. rpc_result = JSONRPCReplyObj(result, Value::null, jreq.id);
  2296. }
  2297. catch (Object& objError)
  2298. {
  2299. rpc_result = JSONRPCReplyObj(Value::null, objError, jreq.id);
  2300. }
  2301. catch (std::exception& e)
  2302. {
  2303. rpc_result = JSONRPCReplyObj(Value::null,
  2304. JSONRPCError(-32700, e.what()), jreq.id);
  2305. }
  2306. return rpc_result;
  2307. }
  2308. static string JSONRPCExecBatch(const Array& vReq)
  2309. {
  2310. Array ret;
  2311. for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++)
  2312. ret.push_back(JSONRPCExecOne(vReq[reqIdx]));
  2313. return write_string(Value(ret), false) + "\n";
  2314. }
  2315. static CCriticalSection cs_THREAD_RPCHANDLER;
  2316. void ThreadRPCServer3(void* parg)
  2317. {
  2318. IMPLEMENT_RANDOMIZE_STACK(ThreadRPCServer3(parg));
  2319. {
  2320. LOCK(cs_THREAD_RPCHANDLER);
  2321. vnThreadsRunning[THREAD_RPCHANDLER]++;
  2322. }
  2323. AcceptedConnection *conn = (AcceptedConnection *) parg;
  2324. bool fRun = true;
  2325. loop {
  2326. if (fShutdown || !fRun)
  2327. {
  2328. conn->close();
  2329. delete conn;
  2330. {
  2331. LOCK(cs_THREAD_RPCHANDLER);
  2332. --vnThreadsRunning[THREAD_RPCHANDLER];
  2333. }
  2334. return;
  2335. }
  2336. map<string, string> mapHeaders;
  2337. string strRequest;
  2338. ReadHTTP(conn->stream(), mapHeaders, strRequest);
  2339. // Check authorization
  2340. if (mapHeaders.count("authorization") == 0)
  2341. {
  2342. conn->stream() << HTTPReply(401, "", false) << std::flush;
  2343. break;
  2344. }
  2345. if (!HTTPAuthorized(mapHeaders))
  2346. {
  2347. printf("ThreadRPCServer incorrect password attempt from %s\n", conn->peer_address_to_string().c_str());
  2348. /* Deter brute-forcing short passwords.
  2349. If this results in a DOS the user really
  2350. shouldn't have their RPC port exposed.*/
  2351. if (mapArgs["-rpcpassword"].size() < 20)
  2352. Sleep(250);
  2353. conn->stream() << HTTPReply(401, "", false) << std::flush;
  2354. break;
  2355. }
  2356. if (mapHeaders["connection"] == "close")
  2357. fRun = false;
  2358. JSONRequest jreq;
  2359. try
  2360. {
  2361. // Parse request
  2362. Value valRequest;
  2363. if (!read_string(strRequest, valRequest))
  2364. throw JSONRPCError(-32700, "Parse error");
  2365. string strReply;
  2366. // singleton request
  2367. if (valRequest.type() == obj_type) {
  2368. jreq.parse(valRequest);
  2369. Value result = tableRPC.execute(jreq.strMethod, jreq.params);
  2370. // Send reply
  2371. strReply = JSONRPCReply(result, Value::null, jreq.id);
  2372. // array of requests
  2373. } else if (valRequest.type() == array_type)
  2374. strReply = JSONRPCExecBatch(valRequest.get_array());
  2375. else
  2376. throw JSONRPCError(-32700, "Top-level object parse error");
  2377. conn->stream() << HTTPReply(200, strReply, fRun) << std::flush;
  2378. }
  2379. catch (Object& objError)
  2380. {
  2381. ErrorReply(conn->stream(), objError, jreq.id);
  2382. break;
  2383. }
  2384. catch (std::exception& e)
  2385. {
  2386. ErrorReply(conn->stream(), JSONRPCError(-32700, e.what()), jreq.id);
  2387. break;
  2388. }
  2389. }
  2390. delete conn;
  2391. {
  2392. LOCK(cs_THREAD_RPCHANDLER);
  2393. vnThreadsRunning[THREAD_RPCHANDLER]--;
  2394. }
  2395. }
  2396. json_spirit::Value CRPCTable::execute(const std::string &strMethod, const json_spirit::Array &params) const
  2397. {
  2398. // Find method
  2399. const CRPCCommand *pcmd = tableRPC[strMethod];
  2400. if (!pcmd)
  2401. throw JSONRPCError(-32601, "Method not found");
  2402. // Observe safe mode
  2403. string strWarning = GetWarnings("rpc");
  2404. if (strWarning != "" && !GetBoolArg("-disablesafemode") &&
  2405. !pcmd->okSafeMode)
  2406. throw JSONRPCError(-2, string("Safe mode: ") + strWarning);
  2407. try
  2408. {
  2409. // Execute
  2410. Value result;
  2411. {
  2412. LOCK2(cs_main, pwalletMain->cs_wallet);
  2413. result = pcmd->actor(params, false);
  2414. }
  2415. return result;
  2416. }
  2417. catch (std::exception& e)
  2418. {
  2419. throw JSONRPCError(-1, e.what());
  2420. }
  2421. }
  2422. Object CallRPC(const string& strMethod, const Array& params)
  2423. {
  2424. if (mapArgs["-rpcuser"] == "" && mapArgs["-rpcpassword"] == "")
  2425. throw runtime_error(strprintf(
  2426. _("You must set rpcpassword=<password> in the configuration file:\n%s\n"
  2427. "If the file does not exist, create it with owner-readable-only file permissions."),
  2428. GetConfigFile().string().c_str()));
  2429. // Connect to localhost
  2430. bool fUseSSL = GetBoolArg("-rpcssl");
  2431. asio::io_service io_service;
  2432. ssl::context context(io_service, ssl::context::sslv23);
  2433. context.set_options(ssl::context::no_sslv2);
  2434. asio::ssl::stream<asio::ip::tcp::socket> sslStream(io_service, context);
  2435. SSLIOStreamDevice<asio::ip::tcp> d(sslStream, fUseSSL);
  2436. iostreams::stream< SSLIOStreamDevice<asio::ip::tcp> > stream(d);
  2437. if (!d.connect(GetArg("-rpcconnect", "127.0.0.1"), GetArg("-rpcport", "8332")))
  2438. throw runtime_error("couldn't connect to server");
  2439. // HTTP basic authentication
  2440. string strUserPass64 = EncodeBase64(mapArgs["-rpcuser"] + ":" + mapArgs["-rpcpassword"]);
  2441. map<string, string> mapRequestHeaders;
  2442. mapRequestHeaders["Authorization"] = string("Basic ") + strUserPass64;
  2443. // Send request
  2444. string strRequest = JSONRPCRequest(strMethod, params, 1);
  2445. string strPost = HTTPPost(strRequest, mapRequestHeaders);
  2446. stream << strPost << std::flush;
  2447. // Receive reply
  2448. map<string, string> mapHeaders;
  2449. string strReply;
  2450. int nStatus = ReadHTTP(stream, mapHeaders, strReply);
  2451. if (nStatus == 401)
  2452. throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
  2453. else if (nStatus >= 400 && nStatus != 400 && nStatus != 404 && nStatus != 500)
  2454. throw runtime_error(strprintf("server returned HTTP error %d", nStatus));
  2455. else if (strReply.empty())
  2456. throw runtime_error("no response from server");
  2457. // Parse reply
  2458. Value valReply;
  2459. if (!read_string(strReply, valReply))
  2460. throw runtime_error("couldn't parse reply from server");
  2461. const Object& reply = valReply.get_obj();
  2462. if (reply.empty())
  2463. throw runtime_error("expected reply to have result, error and id properties");
  2464. return reply;
  2465. }
  2466. template<typename T>
  2467. void ConvertTo(Value& value)
  2468. {
  2469. if (value.type() == str_type)
  2470. {
  2471. // reinterpret string as unquoted json value
  2472. Value value2;
  2473. if (!read_string(value.get_str(), value2))
  2474. throw runtime_error("type mismatch");
  2475. value = value2.get_value<T>();
  2476. }
  2477. else
  2478. {
  2479. value = value.get_value<T>();
  2480. }
  2481. }
  2482. // Convert strings to command-specific RPC representation
  2483. Array RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
  2484. {
  2485. Array params;
  2486. BOOST_FOREACH(const std::string &param, strParams)
  2487. params.push_back(param);
  2488. int n = params.size();
  2489. //
  2490. // Special case non-string parameter types
  2491. //
  2492. if (strMethod == "setgenerate" && n > 0) ConvertTo<bool>(params[0]);
  2493. if (strMethod == "setgenerate" && n > 1) ConvertTo<boost::int64_t>(params[1]);
  2494. if (strMethod == "sendtoaddress" && n > 1) ConvertTo<double>(params[1]);
  2495. if (strMethod == "settxfee" && n > 0) ConvertTo<double>(params[0]);
  2496. if (strMethod == "getreceivedbyaddress" && n > 1) ConvertTo<boost::int64_t>(params[1]);
  2497. if (strMethod == "getreceivedbyaccount" && n > 1) ConvertTo<boost::int64_t>(params[1]);
  2498. if (strMethod == "listreceivedbyaddress" && n > 0) ConvertTo<boost::int64_t>(params[0]);
  2499. if (strMethod == "listreceivedbyaddress" && n > 1) ConvertTo<bool>(params[1]);
  2500. if (strMethod == "listreceivedbyaccount" && n > 0) ConvertTo<boost::int64_t>(params[0]);
  2501. if (strMethod == "listreceivedbyaccount" && n > 1) ConvertTo<bool>(params[1]);
  2502. if (strMethod == "getbalance" && n > 1) ConvertTo<boost::int64_t>(params[1]);
  2503. if (strMethod == "getblockhash" && n > 0) ConvertTo<boost::int64_t>(params[0]);
  2504. if (strMethod == "move" && n > 2) ConvertTo<double>(params[2]);
  2505. if (strMethod == "move" && n > 3) ConvertTo<boost::int64_t>(params[3]);
  2506. if (strMethod == "sendfrom" && n > 2) ConvertTo<double>(params[2]);
  2507. if (strMethod == "sendfrom" && n > 3) ConvertTo<boost::int64_t>(params[3]);
  2508. if (strMethod == "listtransactions" && n > 1) ConvertTo<boost::int64_t>(params[1]);
  2509. if (strMethod == "listtransactions" && n > 2) ConvertTo<boost::int64_t>(params[2]);
  2510. if (strMethod == "listaccounts" && n > 0) ConvertTo<boost::int64_t>(params[0]);
  2511. if (strMethod == "walletpassphrase" && n > 1) ConvertTo<boost::int64_t>(params[1]);
  2512. if (strMethod == "listsinceblock" && n > 1) ConvertTo<boost::int64_t>(params[1]);
  2513. if (strMethod == "sendmany" && n > 1) ConvertTo<Object>(params[1]);
  2514. if (strMethod == "sendmany" && n > 2) ConvertTo<boost::int64_t>(params[2]);
  2515. if (strMethod == "addmultisigaddress" && n > 0) ConvertTo<boost::int64_t>(params[0]);
  2516. if (strMethod == "addmultisigaddress" && n > 1) ConvertTo<Array>(params[1]);
  2517. if (strMethod == "listunspent" && n > 0) ConvertTo<boost::int64_t>(params[0]);
  2518. if (strMethod == "listunspent" && n > 1) ConvertTo<boost::int64_t>(params[1]);
  2519. if (strMethod == "getrawtransaction" && n > 1) ConvertTo<boost::int64_t>(params[1]);
  2520. if (strMethod == "createrawtransaction" && n > 0) ConvertTo<Array>(params[0]);
  2521. if (strMethod == "createrawtransaction" && n > 1) ConvertTo<Object>(params[1]);
  2522. if (strMethod == "signrawtransaction" && n > 1) ConvertTo<Array>(params[1]);
  2523. if (strMethod == "signrawtransaction" && n > 2) ConvertTo<Array>(params[2]);
  2524. return params;
  2525. }
  2526. int CommandLineRPC(int argc, char *argv[])
  2527. {
  2528. string strPrint;
  2529. int nRet = 0;
  2530. try
  2531. {
  2532. // Skip switches
  2533. while (argc > 1 && IsSwitchChar(argv[1][0]))
  2534. {
  2535. argc--;
  2536. argv++;
  2537. }
  2538. // Method
  2539. if (argc < 2)
  2540. throw runtime_error("too few parameters");
  2541. string strMethod = argv[1];
  2542. // Parameters default to strings
  2543. std::vector<std::string> strParams(&argv[2], &argv[argc]);
  2544. Array params = RPCConvertValues(strMethod, strParams);
  2545. // Execute
  2546. Object reply = CallRPC(strMethod, params);
  2547. // Parse reply
  2548. const Value& result = find_value(reply, "result");
  2549. const Value& error = find_value(reply, "error");
  2550. if (error.type() != null_type)
  2551. {
  2552. // Error
  2553. strPrint = "error: " + write_string(error, false);
  2554. int code = find_value(error.get_obj(), "code").get_int();
  2555. nRet = abs(code);
  2556. }
  2557. else
  2558. {
  2559. // Result
  2560. if (result.type() == null_type)
  2561. strPrint = "";
  2562. else if (result.type() == str_type)
  2563. strPrint = result.get_str();
  2564. else
  2565. strPrint = write_string(result, true);
  2566. }
  2567. }
  2568. catch (std::exception& e)
  2569. {
  2570. strPrint = string("error: ") + e.what();
  2571. nRet = 87;
  2572. }
  2573. catch (...)
  2574. {
  2575. PrintException(NULL, "CommandLineRPC()");
  2576. }
  2577. if (strPrint != "")
  2578. {
  2579. fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
  2580. }
  2581. return nRet;
  2582. }
  2583. #ifdef TEST
  2584. int main(int argc, char *argv[])
  2585. {
  2586. #ifdef _MSC_VER
  2587. // Turn off microsoft heap dump noise
  2588. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  2589. _CrtSetReportFile(_CRT_WARN, CreateFile("NUL", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0));
  2590. #endif
  2591. setbuf(stdin, NULL);
  2592. setbuf(stdout, NULL);
  2593. setbuf(stderr, NULL);
  2594. try
  2595. {
  2596. if (argc >= 2 && string(argv[1]) == "-server")
  2597. {
  2598. printf("server ready\n");
  2599. ThreadRPCServer(NULL);
  2600. }
  2601. else
  2602. {
  2603. return CommandLineRPC(argc, argv);
  2604. }
  2605. }
  2606. catch (std::exception& e) {
  2607. PrintException(&e, "main()");
  2608. } catch (...) {
  2609. PrintException(NULL, "main()");
  2610. }
  2611. return 0;
  2612. }
  2613. #endif
  2614. const CRPCTable tableRPC;