bb877bf1efa8ceaea01a77bba7bdde54965a6ac0
[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" // for pwalletMain
6 #include "bitcoinrpc.h"
7 #include "ui_interface.h"
8 #include "base58.h"
9
10 #define printf OutputDebugStringF
11
12 using namespace json_spirit;
13 using namespace std;
14
15 void EnsureWalletIsUnlocked();
16
17 class CTxDump
18 {
19 public:
20     CBlockIndex *pindex;
21     int64_t nValue;
22     bool fSpent;
23     CWalletTx* ptx;
24     int nOut;
25     CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
26     {
27         pindex = NULL;
28         nValue = 0;
29         fSpent = false;
30         this->ptx = ptx;
31         this->nOut = nOut;
32     }
33 };
34
35 Value importprivkey(const Array& params, bool fHelp)
36 {
37     if (fHelp || params.size() < 1 || params.size() > 3)
38         throw runtime_error(
39             "importprivkey <novacoinprivkey> [label] [rescan=true]\n"
40             "Adds a private key (as returned by dumpprivkey) to your wallet.");
41
42     EnsureWalletIsUnlocked();
43
44     string strSecret = params[0].get_str();
45     string strLabel = "";
46     if (params.size() > 1)
47         strLabel = params[1].get_str();
48
49     // Whether to perform rescan after import
50     bool fRescan = true;
51     if (params.size() > 2)
52         fRescan = params[2].get_bool();
53
54     CBitcoinSecret vchSecret;
55     bool fGood = vchSecret.SetString(strSecret);
56
57     if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
58     if (fWalletUnlockMintOnly) // ppcoin: no importprivkey in mint-only mode
59         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
60
61     CKey key;
62     bool fCompressed;
63     CSecret secret = vchSecret.GetSecret(fCompressed);
64     key.SetSecret(secret, fCompressed);
65     CKeyID keyid = key.GetPubKey().GetID();
66     CBitcoinAddress addr = CBitcoinAddress(keyid);
67     {
68         LOCK2(cs_main, pwalletMain->cs_wallet);
69
70         pwalletMain->MarkDirty();
71         pwalletMain->SetAddressBookName(addr, strLabel);
72
73         // Don't throw error in case a key is already there
74         if (pwalletMain->HaveKey(keyid))
75             return Value::null;
76
77         pwalletMain->mapKeyMetadata[addr].nCreateTime = 1;
78
79         if (!pwalletMain->AddKey(key))
80             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
81
82         // whenever a key is imported, we need to scan the whole chain
83         pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
84
85         if (fRescan) 
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 dumppem(const Array& params, bool fHelp)
233 {
234     if (fHelp || params.size() != 3)
235         throw runtime_error(
236             "dumppem <novacoinaddress> <filename> <passphrase>\n"
237             "Dump the key pair corresponding to <novacoinaddress> and store it as encrypted PEM file."
238             + HelpRequiringPassphrase());
239
240     EnsureWalletIsUnlocked();
241
242     string strAddress = params[0].get_str();
243     SecureString strPassKey;
244     strPassKey.reserve(100);
245     strPassKey = params[2].get_str().c_str();
246
247     CBitcoinAddress address;
248     if (!address.SetString(strAddress))
249         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
250     if (fWalletUnlockMintOnly)
251         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
252     CKeyID keyID;
253     if (!address.GetKeyID(keyID))
254         throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
255     if (!pwalletMain->GetPEM(keyID, params[1].get_str(), strPassKey))
256         throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping key pair to file");
257
258     return Value::null;
259 }
260
261 Value dumpwallet(const Array& params, bool fHelp)
262 {
263     if (fHelp || params.size() != 1)
264         throw runtime_error(
265             "dumpwallet <filename>\n"
266             "Dumps all wallet keys in a human-readable format."
267             + HelpRequiringPassphrase());
268
269     EnsureWalletIsUnlocked();
270
271     if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
272       throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");
273
274     return Value::null;
275 }
276
277 Value dumpmalleablekey(const Array& params, bool fHelp)
278 {
279     if (fHelp || params.size() != 1)
280         throw runtime_error (
281             "dumpmalleablekey <Key view>\n"
282             "Dump the private and public key pairs, which correspond to provided key view.\n");
283
284     EnsureWalletIsUnlocked();
285
286     CMalleableKey mKey;
287     CMalleableKeyView keyView;
288     keyView.SetString(params[0].get_str());
289
290     if (!pwalletMain->GetMalleableKey(keyView, mKey))
291         throw runtime_error("There is no such item in the wallet");
292
293     Object result;
294     result.push_back(Pair("PrivatePair", mKey.ToString()));
295     result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
296
297     return result;
298 }
299
300 Value importmalleablekey(const Array& params, bool fHelp)
301 {
302     if (fHelp || params.size() != 1)
303         throw runtime_error (
304             "importmalleablekey <Key data>\n"
305             "Imports the private key pair into your wallet.\n");
306
307
308     EnsureWalletIsUnlocked();
309
310     CMalleableKey mKey;
311     bool fSuccess = mKey.SetString(params[0].get_str());
312
313     Object result;
314
315     if (fSuccess)
316     {
317         fSuccess = pwalletMain->AddKey(mKey);
318         result.push_back(Pair("Successful", fSuccess));
319         result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
320         result.push_back(Pair("KeyView", CMalleableKeyView(mKey).ToString()));
321     }
322     else
323         result.push_back(Pair("Successful", false));
324
325     return result;
326 }