Update to 0.3.0 (New upstream + new RPC calls)
[novacoin.git] / src / rpcdump.cpp
1 // Copyright (c) 2009-2012 Bitcoin Developers
2 // Copyright (c) 2012-2013 The PPCoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include "init.h" // for pwalletMain
7 #include "bitcoinrpc.h"
8 #include "ui_interface.h"
9
10 #include <boost/lexical_cast.hpp>
11
12 #include "json/json_spirit_reader_template.h"
13 #include "json/json_spirit_writer_template.h"
14 #include "json/json_spirit_utils.h"
15
16 #define printf OutputDebugStringF
17
18 // using namespace boost::asio;
19 using namespace json_spirit;
20 using namespace std;
21
22 extern Object JSONRPCError(int code, const string& message);
23
24 class CTxDump
25 {
26 public:
27     CBlockIndex *pindex;
28     int64 nValue;
29     bool fSpent;
30     CWalletTx* ptx;
31     int nOut;
32     CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
33     {
34         pindex = NULL;
35         nValue = 0;
36         fSpent = false;
37         this->ptx = ptx;
38         this->nOut = nOut;
39     }
40 };
41
42 Value importprivkey(const Array& params, bool fHelp)
43 {
44     if (fHelp || params.size() < 1 || params.size() > 2)
45         throw runtime_error(
46             "importprivkey <novacoinprivkey> [label]\n"
47             "Adds a private key (as returned by dumpprivkey) to your wallet.");
48
49     string strSecret = params[0].get_str();
50     string strLabel = "";
51     if (params.size() > 1)
52         strLabel = params[1].get_str();
53     CBitcoinSecret vchSecret;
54     bool fGood = vchSecret.SetString(strSecret);
55
56     if (!fGood) throw JSONRPCError(-5,"Invalid private key");
57     if (fWalletUnlockMintOnly) // ppcoin: no importprivkey in mint-only mode
58         throw JSONRPCError(-102, "Wallet is unlocked for minting only.");
59
60     CKey key;
61     bool fCompressed;
62     CSecret secret = vchSecret.GetSecret(fCompressed);
63     key.SetSecret(secret, fCompressed);
64     CBitcoinAddress vchAddress = CBitcoinAddress(key.GetPubKey());
65
66     {
67         LOCK2(cs_main, pwalletMain->cs_wallet);
68
69         pwalletMain->MarkDirty();
70         pwalletMain->SetAddressBookName(vchAddress, strLabel);
71
72         if (!pwalletMain->AddKey(key))
73             throw JSONRPCError(-4,"Error adding key to wallet");
74
75         pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
76         pwalletMain->ReacceptWalletTransactions();
77     }
78
79     MainFrameRepaint();
80
81     return Value::null;
82 }
83
84 Value dumpprivkey(const Array& params, bool fHelp)
85 {
86     if (fHelp || params.size() != 1)
87         throw runtime_error(
88             "dumpprivkey <novacoinaddress>\n"
89             "Reveals the private key corresponding to <novacoinaddress>.");
90
91     string strAddress = params[0].get_str();
92     CBitcoinAddress address;
93     if (!address.SetString(strAddress))
94         throw JSONRPCError(-5, "Invalid novacoin address");
95     if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
96         throw JSONRPCError(-102, "Wallet is unlocked for minting only.");
97     CSecret vchSecret;
98     bool fCompressed;
99     if (!pwalletMain->GetSecret(address, vchSecret, fCompressed))
100         throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
101     return CBitcoinSecret(vchSecret, fCompressed).ToString();
102 }