Add removeaddress RPC call
[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         if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
101             throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
102
103         // Don't throw error in case an address is already there
104         if (pwalletMain->HaveWatchOnly(script))
105             return Value::null;
106
107         pwalletMain->MarkDirty();
108
109         if (address.IsValid())
110             pwalletMain->SetAddressBookName(address.Get(), strLabel);
111
112         if (!pwalletMain->AddWatchOnly(script))
113             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
114
115         if (fRescan)
116         {
117             pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
118             pwalletMain->ReacceptWalletTransactions();
119         }
120     }
121
122     return Value::null;
123 }
124
125 Value removeaddress(const Array& params, bool fHelp)
126 {
127     if (fHelp || params.size() != 1)
128         throw runtime_error(
129             "removeaddress 'address'\n"
130             "\nRemoves watch-only address or script (in hex) added by importaddress.\n"
131             "\nArguments:\n"
132             "1. 'address' (string, required) The address\n"
133             "\nExamples:\n"
134             "\nremoveaddress 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n"
135             "\nRemove watch-only address 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n");
136
137     CScript script;
138
139     CBitcoinAddress address(params[0].get_str());
140     if (address.IsValid()) {
141         script.SetDestination(address.Get());
142     } else if (IsHex(params[0].get_str())) {
143         std::vector<unsigned char> data(ParseHex(params[0].get_str()));
144         script = CScript(data.begin(), data.end());
145     } else {
146         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
147     }
148
149     if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
150         throw JSONRPCError(RPC_WALLET_ERROR, "The wallet contains the private key for this address or script - can't remove it");
151
152     if (!pwalletMain->HaveWatchOnly(script))
153         throw JSONRPCError(RPC_WALLET_ERROR, "The wallet does not contain this address or script");
154
155     pwalletMain->MarkDirty();
156
157     if (!pwalletMain->RemoveWatchOnly(script))
158         throw JSONRPCError(RPC_WALLET_ERROR, "Error removing address from wallet");
159
160     return Value::null;
161 }
162
163 Value importwallet(const Array& params, bool fHelp)
164 {
165     if (fHelp || params.size() != 1)
166         throw runtime_error(
167             "importwallet <filename>\n"
168             "Imports keys from a wallet dump file (see dumpwallet)."
169             + HelpRequiringPassphrase());
170
171     EnsureWalletIsUnlocked();
172
173     if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
174        throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
175
176     return Value::null;
177 }
178
179 Value dumpprivkey(const Array& params, bool fHelp)
180 {
181     if (fHelp || params.size() != 1)
182         throw runtime_error(
183             "dumpprivkey <novacoinaddress>\n"
184             "Reveals the private key corresponding to <novacoinaddress>.");
185
186     EnsureWalletIsUnlocked();
187
188     string strAddress = params[0].get_str();
189     CBitcoinAddress address;
190     if (!address.SetString(strAddress))
191         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
192     if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
193         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
194     CKeyID keyID;
195     if (!address.GetKeyID(keyID))
196         throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
197     CSecret vchSecret;
198     bool fCompressed;
199     if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
200         throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
201     return CBitcoinSecret(vchSecret, fCompressed).ToString();
202 }
203
204 Value dumpwallet(const Array& params, bool fHelp)
205 {
206     if (fHelp || params.size() != 1)
207         throw runtime_error(
208             "dumpwallet <filename>\n"
209             "Dumps all wallet keys in a human-readable format."
210             + HelpRequiringPassphrase());
211
212     EnsureWalletIsUnlocked();
213
214     if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
215       throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");
216
217     return Value::null;
218 }