return LOCK
[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() > 2)
38         throw runtime_error(
39             "importprivkey <novacoinprivkey> [label]\n"
40             "Adds a private key (as returned by dumpprivkey) to your wallet.");
41
42     string strSecret = params[0].get_str();
43     string strLabel = "";
44     if (params.size() > 1)
45         strLabel = params[1].get_str();
46     CBitcoinSecret vchSecret;
47     bool fGood = vchSecret.SetString(strSecret);
48
49     if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
50     if (fWalletUnlockMintOnly) // ppcoin: no importprivkey in mint-only mode
51         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
52
53     CKey key;
54     bool fCompressed;
55     CSecret secret = vchSecret.GetSecret(fCompressed);
56     key.SetSecret(secret, fCompressed);
57     CKeyID vchAddress = key.GetPubKey().GetID();
58     {
59         pwalletMain->MarkDirty();
60         pwalletMain->SetAddressBookName(vchAddress, strLabel);
61
62         if (!pwalletMain->AddKey(key))
63             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
64
65         pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
66         pwalletMain->ReacceptWalletTransactions();
67     }
68
69     return Value::null;
70 }
71
72 Value importaddress(const Array& params, bool fHelp)
73 {
74     if (fHelp || params.size() < 1 || params.size() > 3)
75         throw runtime_error(
76             "importaddress <address> [label] [rescan=true]\n"
77             "Adds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend.");
78
79     CScript script;
80     CBitcoinAddress address(params[0].get_str());
81     if (address.IsValid()) {
82         script.SetDestination(address.Get());
83     } else if (IsHex(params[0].get_str())) {
84         std::vector<unsigned char> data(ParseHex(params[0].get_str()));
85         script = CScript(data.begin(), data.end());
86     } else {
87         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Novacoin address or script");
88     }
89
90     string strLabel = "";
91     if (params.size() > 1)
92         strLabel = params[1].get_str();
93
94     // Whether to perform rescan after import
95     bool fRescan = true;
96     if (params.size() > 2)
97         fRescan = params[2].get_bool();
98
99     {
100         LOCK2(cs_main, pwalletMain->cs_wallet);
101         if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
102             throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
103
104         // Don't throw error in case an address is already there
105         if (pwalletMain->HaveWatchOnly(script))
106             return Value::null;
107
108         pwalletMain->MarkDirty();
109
110         if (address.IsValid())
111             pwalletMain->SetAddressBookName(address.Get(), strLabel);
112
113         if (!pwalletMain->AddWatchOnly(script))
114             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
115
116         if (fRescan)
117         {
118             pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
119             pwalletMain->ReacceptWalletTransactions();
120         }
121     }
122
123     return Value::null;
124 }
125
126 Value removeaddress(const Array& params, bool fHelp)
127 {
128     if (fHelp || params.size() != 1)
129         throw runtime_error(
130             "removeaddress 'address'\n"
131             "\nRemoves watch-only address or script (in hex) added by importaddress.\n"
132             "\nArguments:\n"
133             "1. 'address' (string, required) The address\n"
134             "\nExamples:\n"
135             "\nremoveaddress 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n"
136             "\nRemove watch-only address 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n");
137
138     CScript script;
139
140     CBitcoinAddress address(params[0].get_str());
141     if (address.IsValid()) {
142         script.SetDestination(address.Get());
143     } else if (IsHex(params[0].get_str())) {
144         std::vector<unsigned char> data(ParseHex(params[0].get_str()));
145         script = CScript(data.begin(), data.end());
146     } else {
147         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
148     }
149
150     if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
151         throw JSONRPCError(RPC_WALLET_ERROR, "The wallet contains the private key for this address or script - can't remove it");
152
153     if (!pwalletMain->HaveWatchOnly(script))
154         throw JSONRPCError(RPC_WALLET_ERROR, "The wallet does not contain this address or script");
155
156     LOCK2(cs_main, pwalletMain->cs_wallet);
157
158     pwalletMain->MarkDirty();
159
160     if (!pwalletMain->RemoveWatchOnly(script))
161         throw JSONRPCError(RPC_WALLET_ERROR, "Error removing address from wallet");
162
163     return Value::null;
164 }
165
166 Value importwallet(const Array& params, bool fHelp)
167 {
168     if (fHelp || params.size() != 1)
169         throw runtime_error(
170             "importwallet <filename>\n"
171             "Imports keys from a wallet dump file (see dumpwallet)."
172             + HelpRequiringPassphrase());
173
174     EnsureWalletIsUnlocked();
175
176     if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
177        throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
178
179     return Value::null;
180 }
181
182 Value dumpprivkey(const Array& params, bool fHelp)
183 {
184     if (fHelp || params.size() != 1)
185         throw runtime_error(
186             "dumpprivkey <novacoinaddress>\n"
187             "Reveals the private key corresponding to <novacoinaddress>.");
188
189     EnsureWalletIsUnlocked();
190
191     string strAddress = params[0].get_str();
192     CBitcoinAddress address;
193     if (!address.SetString(strAddress))
194         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
195     if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
196         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
197     CKeyID keyID;
198     if (!address.GetKeyID(keyID))
199         throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
200     CSecret vchSecret;
201     bool fCompressed;
202     if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
203         throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
204     return CBitcoinSecret(vchSecret, fCompressed).ToString();
205 }
206
207 Value dumpwallet(const Array& params, bool fHelp)
208 {
209     if (fHelp || params.size() != 1)
210         throw runtime_error(
211             "dumpwallet <filename>\n"
212             "Dumps all wallet keys in a human-readable format."
213             + HelpRequiringPassphrase());
214
215     EnsureWalletIsUnlocked();
216
217     if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
218       throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");
219
220     return Value::null;
221 }