Use standard C99 (and Qt) types for 64-bit integers
[novacoin.git] / src / rpcdump.cpp
1 // Copyright (c) 2011 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 <stdint.h>
6
7 #include "headers.h"
8 #include "init.h" // for pwalletMain
9 #include "bitcoinrpc.h"
10
11 // #include <boost/asio.hpp>
12 // #include <boost/iostreams/concepts.hpp>
13 // #include <boost/iostreams/stream.hpp>
14 #include <boost/lexical_cast.hpp>
15 // #ifdef USE_SSL
16 // #include <boost/asio/ssl.hpp> 
17 // typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> SSLStream;
18 // #endif
19 // #include <boost/xpressive/xpressive_dynamic.hpp>
20 #include "json/json_spirit_reader_template.h"
21 #include "json/json_spirit_writer_template.h"
22 #include "json/json_spirit_utils.h"
23
24 #define printf OutputDebugStringF
25
26 // using namespace boost::asio;
27 using namespace json_spirit;
28 using namespace std;
29
30 extern Object JSONRPCError(int code, const string& message);
31
32 class CTxDump
33 {
34 public:
35     CBlockIndex *pindex;
36     int64_t nValue;
37     bool fSpent;
38     CWalletTx* ptx;
39     int nOut;
40     CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
41     {
42         pindex = NULL;
43         nValue = 0;
44         fSpent = false;
45         this->ptx = ptx;
46         this->nOut = nOut;
47     }
48 };
49
50 Value importprivkey(const Array& params, bool fHelp)
51 {
52     if (fHelp || params.size() < 1 || params.size() > 2)
53         throw runtime_error(
54             "importprivkey <bitcoinprivkey> [label]\n"
55             "Adds a private key (as returned by dumpprivkey) to your wallet.");
56
57     string strSecret = params[0].get_str();
58     string strLabel = "";
59     if (params.size() > 1)
60         strLabel = params[1].get_str();
61     CBitcoinSecret vchSecret;
62     bool fGood = vchSecret.SetString(strSecret);
63
64     if (!fGood) throw JSONRPCError(-5,"Invalid private key");
65
66     CKey key;
67     key.SetSecret(vchSecret.GetSecret());
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     if (!pwalletMain->GetSecret(address, vchSecret))
101         throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
102     return CBitcoinSecret(vchSecret).ToString();
103 }