Update all copyrights to 2012
[novacoin.git] / src / rpcdump.cpp
1 // Copyright (c) 2009-2012 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.
4
5 #include "headers.h"
6 #include "init.h" // for pwalletMain
7 #include "bitcoinrpc.h"
8
9 // #include <boost/asio.hpp>
10 // #include <boost/iostreams/concepts.hpp>
11 // #include <boost/iostreams/stream.hpp>
12 #include <boost/lexical_cast.hpp>
13 // #ifdef USE_SSL
14 // #include <boost/asio/ssl.hpp> 
15 // typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> SSLStream;
16 // #endif
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"
21
22 #define printf OutputDebugStringF
23
24 // using namespace boost::asio;
25 using namespace json_spirit;
26 using namespace std;
27
28 extern Object JSONRPCError(int code, const string& message);
29
30 class CTxDump
31 {
32 public:
33     CBlockIndex *pindex;
34     int64 nValue;
35     bool fSpent;
36     CWalletTx* ptx;
37     int nOut;
38     CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
39     {
40         pindex = NULL;
41         nValue = 0;
42         fSpent = false;
43         this->ptx = ptx;
44         this->nOut = nOut;
45     }
46 };
47
48 Value importprivkey(const Array& params, bool fHelp)
49 {
50     if (fHelp || params.size() < 1 || params.size() > 2)
51         throw runtime_error(
52             "importprivkey <bitcoinprivkey> [label]\n"
53             "Adds a private key (as returned by dumpprivkey) to your wallet.");
54
55     string strSecret = params[0].get_str();
56     string strLabel = "";
57     if (params.size() > 1)
58         strLabel = params[1].get_str();
59     CBitcoinSecret vchSecret;
60     bool fGood = vchSecret.SetString(strSecret);
61
62     if (!fGood) throw JSONRPCError(-5,"Invalid private key");
63
64     CKey key;
65     bool fCompressed;
66     CSecret secret = vchSecret.GetSecret(fCompressed);
67     key.SetSecret(secret, fCompressed);
68     CBitcoinAddress vchAddress = CBitcoinAddress(key.GetPubKey());
69
70     CRITICAL_BLOCK(cs_main)
71     CRITICAL_BLOCK(pwalletMain->cs_wallet)
72     {
73         pwalletMain->MarkDirty();
74         pwalletMain->SetAddressBookName(vchAddress, strLabel);
75
76         if (!pwalletMain->AddKey(key))
77             throw JSONRPCError(-4,"Error adding key to wallet");
78
79         pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
80         pwalletMain->ReacceptWalletTransactions();
81     }
82
83     MainFrameRepaint();
84
85     return Value::null;
86 }
87
88 Value dumpprivkey(const Array& params, bool fHelp)
89 {
90     if (fHelp || params.size() != 1)
91         throw runtime_error(
92             "dumpprivkey <bitcoinaddress>\n"
93             "Reveals the private key corresponding to <bitcoinaddress>.");
94
95     string strAddress = params[0].get_str();
96     CBitcoinAddress address;
97     if (!address.SetString(strAddress))
98         throw JSONRPCError(-5, "Invalid bitcoin address");
99     CSecret vchSecret;
100     bool fCompressed;
101     if (!pwalletMain->GetSecret(address, vchSecret, fCompressed))
102         throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
103     return CBitcoinSecret(vchSecret, fCompressed).ToString();
104 }