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.
5 #include "init.h" // for pwalletMain
6 #include "bitcoinrpc.h"
7 #include "ui_interface.h"
10 #define printf OutputDebugStringF
12 using namespace json_spirit;
15 void EnsureWalletIsUnlocked();
25 CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
35 Value importprivkey(const Array& params, bool fHelp)
37 if (fHelp || params.size() < 1 || params.size() > 3)
39 "importprivkey <novacoinprivkey> [label] [rescan=true]\n"
40 "Adds a private key (as returned by dumpprivkey) to your wallet.");
42 EnsureWalletIsUnlocked();
44 string strSecret = params[0].get_str();
46 if (params.size() > 1)
47 strLabel = params[1].get_str();
49 // Whether to perform rescan after import
51 if (params.size() > 2)
52 fRescan = params[2].get_bool();
54 CBitcoinSecret vchSecret;
55 bool fGood = vchSecret.SetString(strSecret);
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.");
63 CSecret secret = vchSecret.GetSecret(fCompressed);
64 key.SetSecret(secret, fCompressed);
65 CKeyID vchAddress = key.GetPubKey().GetID();
67 LOCK2(cs_main, pwalletMain->cs_wallet);
69 pwalletMain->MarkDirty();
70 pwalletMain->SetAddressBookName(vchAddress, strLabel);
72 // Don't throw error in case a key is already there
73 if (pwalletMain->HaveKey(vchAddress))
76 pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;
78 if (!pwalletMain->AddKey(key))
79 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
81 // whenever a key is imported, we need to scan the whole chain
82 pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
86 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
87 pwalletMain->ReacceptWalletTransactions();
94 Value importaddress(const Array& params, bool fHelp)
96 if (fHelp || params.size() < 1 || params.size() > 3)
98 "importaddress <address> [label] [rescan=true]\n"
99 "Adds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend.");
102 CBitcoinAddress address(params[0].get_str());
103 if (address.IsValid()) {
104 script.SetDestination(address.Get());
105 } else if (IsHex(params[0].get_str())) {
106 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
107 script = CScript(data.begin(), data.end());
109 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Novacoin address or script");
112 string strLabel = "";
113 if (params.size() > 1)
114 strLabel = params[1].get_str();
116 // Whether to perform rescan after import
118 if (params.size() > 2)
119 fRescan = params[2].get_bool();
122 LOCK2(cs_main, pwalletMain->cs_wallet);
123 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
124 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
126 // Don't throw error in case an address is already there
127 if (pwalletMain->HaveWatchOnly(script))
130 pwalletMain->MarkDirty();
132 if (address.IsValid())
133 pwalletMain->SetAddressBookName(address.Get(), strLabel);
135 if (!pwalletMain->AddWatchOnly(script))
136 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
140 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
141 pwalletMain->ReacceptWalletTransactions();
148 Value removeaddress(const Array& params, bool fHelp)
150 if (fHelp || params.size() != 1)
152 "removeaddress 'address'\n"
153 "\nRemoves watch-only address or script (in hex) added by importaddress.\n"
155 "1. 'address' (string, required) The address\n"
157 "\nremoveaddress 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n"
158 "\nRemove watch-only address 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n");
162 CBitcoinAddress address(params[0].get_str());
163 if (address.IsValid()) {
164 script.SetDestination(address.Get());
165 } else if (IsHex(params[0].get_str())) {
166 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
167 script = CScript(data.begin(), data.end());
169 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
172 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
173 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet contains the private key for this address or script - can't remove it");
175 if (!pwalletMain->HaveWatchOnly(script))
176 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet does not contain this address or script");
178 LOCK2(cs_main, pwalletMain->cs_wallet);
180 pwalletMain->MarkDirty();
182 if (!pwalletMain->RemoveWatchOnly(script))
183 throw JSONRPCError(RPC_WALLET_ERROR, "Error removing address from wallet");
188 Value importwallet(const Array& params, bool fHelp)
190 if (fHelp || params.size() != 1)
192 "importwallet <filename>\n"
193 "Imports keys from a wallet dump file (see dumpwallet)."
194 + HelpRequiringPassphrase());
196 EnsureWalletIsUnlocked();
198 if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
199 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
204 Value dumpprivkey(const Array& params, bool fHelp)
206 if (fHelp || params.size() != 1)
208 "dumpprivkey <novacoinaddress>\n"
209 "Reveals the private key corresponding to <novacoinaddress>.");
211 EnsureWalletIsUnlocked();
213 string strAddress = params[0].get_str();
214 CBitcoinAddress address;
215 if (!address.SetString(strAddress))
216 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
217 if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
218 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
220 if (!address.GetKeyID(keyID))
221 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
224 if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
225 throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
226 return CBitcoinSecret(vchSecret, fCompressed).ToString();
229 Value dumpwallet(const Array& params, bool fHelp)
231 if (fHelp || params.size() != 1)
233 "dumpwallet <filename>\n"
234 "Dumps all wallet keys in a human-readable format."
235 + HelpRequiringPassphrase());
237 EnsureWalletIsUnlocked();
239 if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
240 throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");
245 Value dumpmalleablekey(const Array& params, bool fHelp)
247 if (fHelp || params.size() != 1)
248 throw runtime_error (
249 "dumpmalleablekey <Key view>\n"
250 "Dump the private and public key pairs, which correspond to provided key view.\n");
252 EnsureWalletIsUnlocked();
255 CMalleableKeyView keyView;
256 keyView.SetString(params[0].get_str());
258 if (!pwalletMain->GetMalleableKey(keyView, mKey))
259 throw runtime_error("There is no such item in the wallet");
262 result.push_back(Pair("PrivatePair", mKey.ToString()));
263 result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
268 Value importmalleablekey(const Array& params, bool fHelp)
270 if (fHelp || params.size() != 1)
271 throw runtime_error (
272 "importmalleablekey <Key data>\n"
273 "Imports the private key pair into your wallet.\n");
276 EnsureWalletIsUnlocked();
279 bool fSuccess = mKey.SetString(params[0].get_str());
285 fSuccess = pwalletMain->AddMalleableKey(mKey);
286 result.push_back(Pair("Successful", fSuccess));
287 result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
288 result.push_back(Pair("KeyView", CMalleableKeyView(mKey).ToString()));
291 result.push_back(Pair("Successful", false));