PPCoin: Change date display format and clean up a merge issue
[novacoin.git] / src / rpcdump.cpp
1 // Copyright (c) 2009-2012 Bitcoin Developers
2 // Copyright (c) 2012 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 <ppcoinprivkey> [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
58     CKey key;
59     bool fCompressed;
60     CSecret secret = vchSecret.GetSecret(fCompressed);
61     key.SetSecret(secret, fCompressed);
62     CBitcoinAddress vchAddress = CBitcoinAddress(key.GetPubKey());
63
64     {
65         LOCK2(cs_main, pwalletMain->cs_wallet);
66
67         pwalletMain->MarkDirty();
68         pwalletMain->SetAddressBookName(vchAddress, strLabel);
69
70         if (!pwalletMain->AddKey(key))
71             throw JSONRPCError(-4,"Error adding key to wallet");
72
73         pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
74         pwalletMain->ReacceptWalletTransactions();
75     }
76
77     MainFrameRepaint();
78
79     return Value::null;
80 }
81
82 Value dumpprivkey(const Array& params, bool fHelp)
83 {
84     if (fHelp || params.size() != 1)
85         throw runtime_error(
86             "dumpprivkey <ppcoinaddress>\n"
87             "Reveals the private key corresponding to <ppcoinaddress>.");
88
89     string strAddress = params[0].get_str();
90     CBitcoinAddress address;
91     if (!address.SetString(strAddress))
92         throw JSONRPCError(-5, "Invalid ppcoin address");
93     CSecret vchSecret;
94     bool fCompressed;
95     if (!pwalletMain->GetSecret(address, vchSecret, fCompressed))
96         throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
97     return CBitcoinSecret(vchSecret, fCompressed).ToString();
98 }