Update CMakeLists.txt - play with openssl
[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"
6 #include "bitcoinrpc.h"
7 #include "interface.h"
8 #include "base58.h"
9 #include "wallet.h"
10
11 #define printf OutputDebugStringF
12
13 using namespace json_spirit;
14 using namespace std;
15
16 void EnsureWalletIsUnlocked();
17
18 class CTxDump
19 {
20 public:
21     CBlockIndex *pindex;
22     int64_t nValue;
23     bool fSpent;
24     CWalletTx* ptx;
25     int nOut;
26     CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
27     {
28         pindex = NULL;
29         nValue = 0;
30         fSpent = false;
31         this->ptx = ptx;
32         this->nOut = nOut;
33     }
34 };
35
36 Value importprivkey(const Array& params, bool fHelp)
37 {
38     if (fHelp || params.size() < 1 || params.size() > 3)
39         throw runtime_error(
40             "importprivkey <novacoinprivkey> [label] [rescan=true]\n"
41             "Adds a private key (as returned by dumpprivkey) to your wallet.");
42
43     EnsureWalletIsUnlocked();
44
45     string strSecret = params[0].get_str();
46     string strLabel = "";
47     if (params.size() > 1)
48         strLabel = params[1].get_str();
49
50     // Whether to perform rescan after import
51     bool fRescan = true;
52     if (params.size() > 2)
53         fRescan = params[2].get_bool();
54
55     CBitcoinSecret vchSecret;
56     bool fGood = vchSecret.SetString(strSecret);
57
58     if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
59     if (fWalletUnlockMintOnly) // ppcoin: no importprivkey in mint-only mode
60         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
61
62     CKey key;
63     bool fCompressed;
64     CSecret secret = vchSecret.GetSecret(fCompressed);
65     key.SetSecret(secret, fCompressed);
66     CKeyID keyid = key.GetPubKey().GetID();
67     CBitcoinAddress addr = CBitcoinAddress(keyid);
68     {
69         LOCK2(cs_main, pwalletMain->cs_wallet);
70
71         // Don't throw error in case a key is already there
72         if (pwalletMain->HaveKey(keyid))
73             return Value::null;
74
75         pwalletMain->mapKeyMetadata[addr].nCreateTime = 1;
76         if (!pwalletMain->AddKey(key))
77             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
78
79         pwalletMain->MarkDirty();
80         pwalletMain->SetAddressBookName(addr, strLabel);
81
82         if (fRescan)
83         {
84             // whenever a key is imported, we need to scan the whole chain
85             pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
86
87             pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
88             pwalletMain->ReacceptWalletTransactions();
89         }
90     }
91
92     return Value::null;
93 }
94
95 Value importaddress(const Array& params, bool fHelp)
96 {
97     if (fHelp || params.size() < 1 || params.size() > 3)
98         throw runtime_error(
99             "importaddress <address> [label] [rescan=true]\n"
100             "Adds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend.");
101
102     CScript script;
103     CBitcoinAddress address(params[0].get_str());
104     if (address.IsValid()) {
105         if (address.IsPair())
106             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "It's senseless to import pubkey pair address.");
107         script.SetAddress(address);
108     } else if (IsHex(params[0].get_str())) {
109         std::vector<unsigned char> data(ParseHex(params[0].get_str()));
110         script = CScript(data.begin(), data.end());
111     } else
112         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Novacoin address or script");
113
114     string strLabel = "";
115     if (params.size() > 1)
116         strLabel = params[1].get_str();
117
118     // Whether to perform rescan after import
119     bool fRescan = true;
120     if (params.size() > 2)
121         fRescan = params[2].get_bool();
122
123     {
124         LOCK2(cs_main, pwalletMain->cs_wallet);
125         if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
126             throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
127
128         // Don't throw error in case an address is already there
129         if (pwalletMain->HaveWatchOnly(script))
130             return Value::null;
131
132         pwalletMain->MarkDirty();
133
134         if (address.IsValid())
135             pwalletMain->SetAddressBookName(address, strLabel);
136
137         if (!pwalletMain->AddWatchOnly(script))
138             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
139
140         if (fRescan)
141         {
142             pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
143             pwalletMain->ReacceptWalletTransactions();
144         }
145     }
146
147     return Value::null;
148 }
149
150 Value removeaddress(const Array& params, bool fHelp)
151 {
152     if (fHelp || params.size() != 1)
153         throw runtime_error(
154             "removeaddress 'address'\n"
155             "\nRemoves watch-only address or script (in hex) added by importaddress.\n"
156             "\nArguments:\n"
157             "1. 'address' (string, required) The address\n"
158             "\nExamples:\n"
159             "\nremoveaddress 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n"
160             "\nRemove watch-only address 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n");
161
162     CScript script;
163
164     CBitcoinAddress address(params[0].get_str());
165     if (address.IsValid()) {
166         if (address.IsPair())
167             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey pair addresses aren't supported.");
168         script.SetAddress(address);
169     } else if (IsHex(params[0].get_str())) {
170         std::vector<unsigned char> data(ParseHex(params[0].get_str()));
171         script = CScript(data.begin(), data.end());
172     } else
173         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
174
175     if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
176         throw JSONRPCError(RPC_WALLET_ERROR, "The wallet contains the private key for this address or script - can't remove it");
177
178     if (!pwalletMain->HaveWatchOnly(script))
179         throw JSONRPCError(RPC_WALLET_ERROR, "The wallet does not contain this address or script");
180
181     LOCK2(cs_main, pwalletMain->cs_wallet);
182
183     pwalletMain->MarkDirty();
184
185     if (!pwalletMain->RemoveWatchOnly(script))
186         throw JSONRPCError(RPC_WALLET_ERROR, "Error removing address from wallet");
187
188     return Value::null;
189 }
190
191 Value importwallet(const Array& params, bool fHelp)
192 {
193     if (fHelp || params.size() != 1)
194         throw runtime_error(
195             "importwallet <filename>\n"
196             "Imports keys from a wallet dump file (see dumpwallet)."
197             + HelpRequiringPassphrase());
198
199     EnsureWalletIsUnlocked();
200
201     if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
202        throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
203
204     return Value::null;
205 }
206
207 Value dumpprivkey(const Array& params, bool fHelp)
208 {
209     if (fHelp || params.size() != 1)
210         throw runtime_error(
211             "dumpprivkey <novacoinaddress>\n"
212             "Reveals the private key corresponding to <novacoinaddress>.");
213
214     EnsureWalletIsUnlocked();
215
216     string strAddress = params[0].get_str();
217     CBitcoinAddress address;
218     if (!address.SetString(strAddress))
219         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
220     if (fWalletUnlockMintOnly)
221         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
222     CKeyID keyID;
223     if (!address.GetKeyID(keyID))
224         throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
225     CSecret vchSecret;
226     bool fCompressed;
227     if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
228         throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
229     return CBitcoinSecret(vchSecret, fCompressed).ToString();
230 }
231
232 Value dumpwallet(const Array& params, bool fHelp)
233 {
234     if (fHelp || params.size() != 1)
235         throw runtime_error(
236             "dumpwallet <filename>\n"
237             "Dumps all wallet keys in a human-readable format."
238             + HelpRequiringPassphrase());
239
240     EnsureWalletIsUnlocked();
241
242     if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
243       throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");
244
245     return Value::null;
246 }
247
248 Value dumpmalleablekey(const Array& params, bool fHelp)
249 {
250     if (fHelp || params.size() != 1)
251         throw runtime_error (
252             "dumpmalleablekey <Key view>\n"
253             "Dump the private and public key pairs, which correspond to provided key view.\n");
254
255     EnsureWalletIsUnlocked();
256
257     CMalleableKey mKey;
258     CMalleableKeyView keyView;
259     keyView.SetString(params[0].get_str());
260
261     if (!pwalletMain->GetMalleableKey(keyView, mKey))
262         throw runtime_error("There is no such item in the wallet");
263
264     Object result;
265     result.push_back(Pair("PrivatePair", mKey.ToString()));
266     result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
267
268     return result;
269 }
270
271 Value importmalleablekey(const Array& params, bool fHelp)
272 {
273     if (fHelp || params.size() != 1)
274         throw runtime_error (
275             "importmalleablekey <Key data>\n"
276             "Imports the private key pair into your wallet.\n");
277
278
279     EnsureWalletIsUnlocked();
280
281     CMalleableKey mKey;
282     bool fSuccess = mKey.SetString(params[0].get_str());
283
284     Object result;
285
286     if (fSuccess)
287     {
288         fSuccess = pwalletMain->AddKey(mKey);
289         result.push_back(Pair("Successful", fSuccess));
290         result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
291         result.push_back(Pair("KeyView", CMalleableKeyView(mKey).ToString()));
292     }
293     else
294         result.push_back(Pair("Successful", false));
295
296     return result;
297 }