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.
8 #include "init.h" // for pwalletMain
9 #include "bitcoinrpc.h"
11 // #include <boost/asio.hpp>
12 // #include <boost/iostreams/concepts.hpp>
13 // #include <boost/iostreams/stream.hpp>
14 #include <boost/lexical_cast.hpp>
16 // #include <boost/asio/ssl.hpp>
17 // typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> SSLStream;
19 // #include <boost/xpressive/xpressive_dynamic.hpp>
20 #include "json/json_spirit_reader_template.h"
21 #include "json/json_spirit_writer_template.h"
22 #include "json/json_spirit_utils.h"
24 #define printf OutputDebugStringF
26 // using namespace boost::asio;
27 using namespace json_spirit;
30 extern Object JSONRPCError(int code, const string& message);
40 CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
50 Value importprivkey(const Array& params, bool fHelp)
52 if (fHelp || params.size() < 1 || params.size() > 2)
54 "importprivkey <bitcoinprivkey> [label]\n"
55 "Adds a private key (as returned by dumpprivkey) to your wallet.");
57 string strSecret = params[0].get_str();
59 if (params.size() > 1)
60 strLabel = params[1].get_str();
61 CBitcoinSecret vchSecret;
62 bool fGood = vchSecret.SetString(strSecret);
64 if (!fGood) throw JSONRPCError(-5,"Invalid private key");
67 key.SetSecret(vchSecret.GetSecret());
68 CBitcoinAddress vchAddress = CBitcoinAddress(key.GetPubKey());
70 CRITICAL_BLOCK(cs_main)
71 CRITICAL_BLOCK(pwalletMain->cs_wallet)
73 pwalletMain->MarkDirty();
74 pwalletMain->SetAddressBookName(vchAddress, strLabel);
76 if (!pwalletMain->AddKey(key))
77 throw JSONRPCError(-4,"Error adding key to wallet");
79 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
80 pwalletMain->ReacceptWalletTransactions();
88 Value dumpprivkey(const Array& params, bool fHelp)
90 if (fHelp || params.size() != 1)
92 "dumpprivkey <bitcoinaddress>\n"
93 "Reveals the private key corresponding to <bitcoinaddress>.");
95 string strAddress = params[0].get_str();
96 CBitcoinAddress address;
97 if (!address.SetString(strAddress))
98 throw JSONRPCError(-5, "Invalid bitcoin address");
100 if (!pwalletMain->GetSecret(address, vchSecret))
101 throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
102 return CBitcoinSecret(vchSecret).ToString();