1 // Copyright (c) 2011 Bitcoin Developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
6 #include "init.h" // for pwalletMain
7 #include "bitcoinrpc.h"
9 // #include <boost/asio.hpp>
10 // #include <boost/iostreams/concepts.hpp>
11 // #include <boost/iostreams/stream.hpp>
12 #include <boost/lexical_cast.hpp>
14 // #include <boost/asio/ssl.hpp>
15 // typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> SSLStream;
17 // #include <boost/xpressive/xpressive_dynamic.hpp>
18 #include "json/json_spirit_reader_template.h"
19 #include "json/json_spirit_writer_template.h"
20 #include "json/json_spirit_utils.h"
22 #define printf OutputDebugStringF
24 // using namespace boost::asio;
25 using namespace json_spirit;
28 extern Object JSONRPCError(int code, const string& message);
38 CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
48 Value importprivkey(const Array& params, bool fHelp)
50 if (fHelp || params.size() < 1 || params.size() > 2)
52 "importprivkey <bitcoinprivkey> [label]\n"
53 "Adds a private key (as returned by dumpprivkey) to your wallet.");
55 string strSecret = params[0].get_str();
57 if (params.size() > 1)
58 strLabel = params[1].get_str();
59 CBitcoinSecret vchSecret;
60 bool fGood = vchSecret.SetString(strSecret);
62 if (!fGood) throw JSONRPCError(-5,"Invalid private key");
65 key.SetSecret(vchSecret.GetSecret());
66 CBitcoinAddress vchAddress = CBitcoinAddress(key.GetPubKey());
68 CRITICAL_BLOCK(cs_main)
69 CRITICAL_BLOCK(pwalletMain->cs_wallet)
71 pwalletMain->MarkDirty();
72 pwalletMain->SetAddressBookName(vchAddress, strLabel);
74 if (!pwalletMain->AddKey(key))
75 throw JSONRPCError(-4,"Error adding key to wallet");
77 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
78 pwalletMain->ReacceptWalletTransactions();
86 Value dumpprivkey(const Array& params, bool fHelp)
88 if (fHelp || params.size() != 1)
90 "dumpprivkey <bitcoinaddress>\n"
91 "Reveals the private key corresponding to <bitcoinaddress>.");
93 string strAddress = params[0].get_str();
94 CBitcoinAddress address;
95 if (!address.SetString(strAddress))
96 throw JSONRPCError(-5, "Invalid bitcoin address");
98 if (!pwalletMain->GetSecret(address, vchSecret))
99 throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
100 return CBitcoinSecret(vchSecret).ToString();