Update License in File Headers
[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 COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "init.h" // for pwalletMain
6 #include "bitcoinrpc.h"
7 #include "ui_interface.h"
8
9 #include <boost/lexical_cast.hpp>
10
11 #include "json/json_spirit_reader_template.h"
12 #include "json/json_spirit_writer_template.h"
13 #include "json/json_spirit_utils.h"
14
15 #define printf OutputDebugStringF
16
17 // using namespace boost::asio;
18 using namespace json_spirit;
19 using namespace std;
20
21 extern Object JSONRPCError(int code, const string& message);
22
23 class CTxDump
24 {
25 public:
26     CBlockIndex *pindex;
27     int64 nValue;
28     bool fSpent;
29     CWalletTx* ptx;
30     int nOut;
31     CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
32     {
33         pindex = NULL;
34         nValue = 0;
35         fSpent = false;
36         this->ptx = ptx;
37         this->nOut = nOut;
38     }
39 };
40
41 Value importprivkey(const Array& params, bool fHelp)
42 {
43     if (fHelp || params.size() < 1 || params.size() > 2)
44         throw runtime_error(
45             "importprivkey <bitcoinprivkey> [label]\n"
46             "Adds a private key (as returned by dumpprivkey) to your wallet.");
47
48     string strSecret = params[0].get_str();
49     string strLabel = "";
50     if (params.size() > 1)
51         strLabel = params[1].get_str();
52     CBitcoinSecret vchSecret;
53     bool fGood = vchSecret.SetString(strSecret);
54
55     if (!fGood) throw JSONRPCError(-5,"Invalid private key");
56
57     CKey key;
58     bool fCompressed;
59     CSecret secret = vchSecret.GetSecret(fCompressed);
60     key.SetSecret(secret, fCompressed);
61     CBitcoinAddress vchAddress = CBitcoinAddress(key.GetPubKey());
62
63     {
64         LOCK2(cs_main, pwalletMain->cs_wallet);
65
66         pwalletMain->MarkDirty();
67         pwalletMain->SetAddressBookName(vchAddress, strLabel);
68
69         if (!pwalletMain->AddKey(key))
70             throw JSONRPCError(-4,"Error adding key to wallet");
71
72         pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
73         pwalletMain->ReacceptWalletTransactions();
74     }
75
76     MainFrameRepaint();
77
78     return Value::null;
79 }
80
81 Value dumpprivkey(const Array& params, bool fHelp)
82 {
83     if (fHelp || params.size() != 1)
84         throw runtime_error(
85             "dumpprivkey <bitcoinaddress>\n"
86             "Reveals the private key corresponding to <bitcoinaddress>.");
87
88     string strAddress = params[0].get_str();
89     CBitcoinAddress address;
90     if (!address.SetString(strAddress))
91         throw JSONRPCError(-5, "Invalid bitcoin address");
92     CSecret vchSecret;
93     bool fCompressed;
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();
97 }