Key import and export
[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 "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     key.SetSecret(vchSecret.GetSecret());
66     CBitcoinAddress vchAddress = CBitcoinAddress(key.GetPubKey());
67
68     CRITICAL_BLOCK(cs_main)
69     CRITICAL_BLOCK(pwalletMain->cs_wallet)
70     {
71         pwalletMain->MarkDirty();
72         pwalletMain->SetAddressBookName(vchAddress, strLabel);
73
74         if (!pwalletMain->AddKey(key))
75             throw JSONRPCError(-4,"Error adding key to wallet");
76
77         pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
78         pwalletMain->ReacceptWalletTransactions();
79     }
80
81     MainFrameRepaint();
82
83     return Value::null;
84 }
85
86 Value dumpprivkey(const Array& params, bool fHelp)
87 {
88     if (fHelp || params.size() != 1)
89         throw runtime_error(
90             "dumpprivkey <bitcoinaddress>\n"
91             "Reveals the private key corresponding to <bitcoinaddress>.");
92
93     string strAddress = params[0].get_str();
94     CBitcoinAddress address;
95     if (!address.SetString(strAddress))
96         throw JSONRPCError(-5, "Invalid bitcoin address");
97     CSecret vchSecret;
98     if (!pwalletMain->GetSecret(address, vchSecret))
99         throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
100     return CBitcoinSecret(vchSecret).ToString();
101 }