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"
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 keyid = key.GetPubKey().GetID();
66 CBitcoinAddress addr = CBitcoinAddress(keyid);
68 LOCK2(cs_main, pwalletMain->cs_wallet);
70 // Don't throw error in case a key is already there
71 if (pwalletMain->HaveKey(keyid))
74 pwalletMain->mapKeyMetadata[addr].nCreateTime = 1;
75 if (!pwalletMain->AddKey(key))
76 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
78 pwalletMain->MarkDirty();
79 pwalletMain->SetAddressBookName(addr, strLabel);
83 // whenever a key is imported, we need to scan the whole chain
84 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 if (address.IsPair())
105 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "It's senseless to import pubkey pair address.");
106 script.SetAddress(address);
107 } else if (IsHex(params[0].get_str())) {
108 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
109 script = CScript(data.begin(), data.end());
111 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Novacoin address or script");
113 string strLabel = "";
114 if (params.size() > 1)
115 strLabel = params[1].get_str();
117 // Whether to perform rescan after import
119 if (params.size() > 2)
120 fRescan = params[2].get_bool();
123 LOCK2(cs_main, pwalletMain->cs_wallet);
124 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
125 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
127 // Don't throw error in case an address is already there
128 if (pwalletMain->HaveWatchOnly(script))
131 pwalletMain->MarkDirty();
133 if (address.IsValid())
134 pwalletMain->SetAddressBookName(address, strLabel);
136 if (!pwalletMain->AddWatchOnly(script))
137 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
141 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
142 pwalletMain->ReacceptWalletTransactions();
149 Value removeaddress(const Array& params, bool fHelp)
151 if (fHelp || params.size() != 1)
153 "removeaddress 'address'\n"
154 "\nRemoves watch-only address or script (in hex) added by importaddress.\n"
156 "1. 'address' (string, required) The address\n"
158 "\nremoveaddress 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n"
159 "\nRemove watch-only address 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n");
163 CBitcoinAddress address(params[0].get_str());
164 if (address.IsValid()) {
165 if (address.IsPair())
166 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey pair addresses aren't supported.");
167 script.SetAddress(address);
168 } else if (IsHex(params[0].get_str())) {
169 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
170 script = CScript(data.begin(), data.end());
172 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
174 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
175 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet contains the private key for this address or script - can't remove it");
177 if (!pwalletMain->HaveWatchOnly(script))
178 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet does not contain this address or script");
180 LOCK2(cs_main, pwalletMain->cs_wallet);
182 pwalletMain->MarkDirty();
184 if (!pwalletMain->RemoveWatchOnly(script))
185 throw JSONRPCError(RPC_WALLET_ERROR, "Error removing address from wallet");
190 Value importwallet(const Array& params, bool fHelp)
192 if (fHelp || params.size() != 1)
194 "importwallet <filename>\n"
195 "Imports keys from a wallet dump file (see dumpwallet)."
196 + HelpRequiringPassphrase());
198 EnsureWalletIsUnlocked();
200 if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
201 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
206 Value dumpprivkey(const Array& params, bool fHelp)
208 if (fHelp || params.size() != 1)
210 "dumpprivkey <novacoinaddress>\n"
211 "Reveals the private key corresponding to <novacoinaddress>.");
213 EnsureWalletIsUnlocked();
215 string strAddress = params[0].get_str();
216 CBitcoinAddress address;
217 if (!address.SetString(strAddress))
218 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
219 if (fWalletUnlockMintOnly)
220 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
222 if (!address.GetKeyID(keyID))
223 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
226 if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
227 throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
228 return CBitcoinSecret(vchSecret, fCompressed).ToString();
231 Value dumpwallet(const Array& params, bool fHelp)
233 if (fHelp || params.size() != 1)
235 "dumpwallet <filename>\n"
236 "Dumps all wallet keys in a human-readable format."
237 + HelpRequiringPassphrase());
239 EnsureWalletIsUnlocked();
241 if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
242 throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");
247 Value dumpmalleablekey(const Array& params, bool fHelp)
249 if (fHelp || params.size() != 1)
250 throw runtime_error (
251 "dumpmalleablekey <Key view>\n"
252 "Dump the private and public key pairs, which correspond to provided key view.\n");
254 EnsureWalletIsUnlocked();
257 CMalleableKeyView keyView;
258 keyView.SetString(params[0].get_str());
260 if (!pwalletMain->GetMalleableKey(keyView, mKey))
261 throw runtime_error("There is no such item in the wallet");
264 result.push_back(Pair("PrivatePair", mKey.ToString()));
265 result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
270 Value importmalleablekey(const Array& params, bool fHelp)
272 if (fHelp || params.size() != 1)
273 throw runtime_error (
274 "importmalleablekey <Key data>\n"
275 "Imports the private key pair into your wallet.\n");
278 EnsureWalletIsUnlocked();
281 bool fSuccess = mKey.SetString(params[0].get_str());
287 fSuccess = pwalletMain->AddKey(mKey);
288 result.push_back(Pair("Successful", fSuccess));
289 result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
290 result.push_back(Pair("KeyView", CMalleableKeyView(mKey).ToString()));
293 result.push_back(Pair("Successful", false));