1 // Copyright (c) 2009-2012 Bitcoin Developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "init.h" // for pwalletMain
6 #include "bitcoinrpc.h"
7 #include "ui_interface.h"
9 #include <boost/lexical_cast.hpp>
11 #include "json/json_spirit_reader_template.h"
12 #include "json/json_spirit_writer_template.h"
13 #include "json/json_spirit_utils.h"
15 #define printf OutputDebugStringF
17 // using namespace boost::asio;
18 using namespace json_spirit;
21 extern Object JSONRPCError(int code, const string& message);
31 CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
41 Value importprivkey(const Array& params, bool fHelp)
43 if (fHelp || params.size() < 1 || params.size() > 2)
45 "importprivkey <bitcoinprivkey> [label]\n"
46 "Adds a private key (as returned by dumpprivkey) to your wallet.");
48 string strSecret = params[0].get_str();
50 if (params.size() > 1)
51 strLabel = params[1].get_str();
52 CBitcoinSecret vchSecret;
53 bool fGood = vchSecret.SetString(strSecret);
55 if (!fGood) throw JSONRPCError(-5,"Invalid private key");
59 CSecret secret = vchSecret.GetSecret(fCompressed);
60 key.SetSecret(secret, fCompressed);
61 CBitcoinAddress vchAddress = CBitcoinAddress(key.GetPubKey());
64 LOCK2(cs_main, pwalletMain->cs_wallet);
66 pwalletMain->MarkDirty();
67 pwalletMain->SetAddressBookName(vchAddress, strLabel);
69 if (!pwalletMain->AddKey(key))
70 throw JSONRPCError(-4,"Error adding key to wallet");
72 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
73 pwalletMain->ReacceptWalletTransactions();
81 Value dumpprivkey(const Array& params, bool fHelp)
83 if (fHelp || params.size() != 1)
85 "dumpprivkey <bitcoinaddress>\n"
86 "Reveals the private key corresponding to <bitcoinaddress>.");
88 string strAddress = params[0].get_str();
89 CBitcoinAddress address;
90 if (!address.SetString(strAddress))
91 throw JSONRPCError(-5, "Invalid bitcoin address");
94 if (!pwalletMain->GetSecret(address, vchSecret, fCompressed))
95 throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
96 return CBitcoinSecret(vchSecret, fCompressed).ToString();