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.
6 #include "bitcoinrpc.h"
11 #define printf OutputDebugStringF
13 using namespace json_spirit;
16 void EnsureWalletIsUnlocked();
26 CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
36 Value importprivkey(const Array& params, bool fHelp)
38 if (fHelp || params.size() < 1 || params.size() > 3)
40 "importprivkey <novacoinprivkey> [label] [rescan=true]\n"
41 "Adds a private key (as returned by dumpprivkey) to your wallet.");
43 EnsureWalletIsUnlocked();
45 string strSecret = params[0].get_str();
47 if (params.size() > 1)
48 strLabel = params[1].get_str();
50 // Whether to perform rescan after import
52 if (params.size() > 2)
53 fRescan = params[2].get_bool();
55 CBitcoinSecret vchSecret;
56 bool fGood = vchSecret.SetString(strSecret);
58 if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
59 if (fWalletUnlockMintOnly) // ppcoin: no importprivkey in mint-only mode
60 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
64 CSecret secret = vchSecret.GetSecret(fCompressed);
65 key.SetSecret(secret, fCompressed);
66 CKeyID keyid = key.GetPubKey().GetID();
67 CBitcoinAddress addr = CBitcoinAddress(keyid);
69 LOCK2(cs_main, pwalletMain->cs_wallet);
71 // Don't throw error in case a key is already there
72 if (pwalletMain->HaveKey(keyid))
75 pwalletMain->mapKeyMetadata[addr].nCreateTime = 1;
76 if (!pwalletMain->AddKey(key))
77 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
79 pwalletMain->MarkDirty();
80 pwalletMain->SetAddressBookName(addr, strLabel);
84 // whenever a key is imported, we need to scan the whole chain
85 pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
87 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
88 pwalletMain->ReacceptWalletTransactions();
95 Value importaddress(const Array& params, bool fHelp)
97 if (fHelp || params.size() < 1 || params.size() > 3)
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.");
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());
112 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Novacoin address or script");
114 string strLabel = "";
115 if (params.size() > 1)
116 strLabel = params[1].get_str();
118 // Whether to perform rescan after import
120 if (params.size() > 2)
121 fRescan = params[2].get_bool();
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");
128 // Don't throw error in case an address is already there
129 if (pwalletMain->HaveWatchOnly(script))
132 pwalletMain->MarkDirty();
134 if (address.IsValid())
135 pwalletMain->SetAddressBookName(address, strLabel);
137 if (!pwalletMain->AddWatchOnly(script))
138 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
142 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
143 pwalletMain->ReacceptWalletTransactions();
150 Value removeaddress(const Array& params, bool fHelp)
152 if (fHelp || params.size() != 1)
154 "removeaddress 'address'\n"
155 "\nRemoves watch-only address or script (in hex) added by importaddress.\n"
157 "1. 'address' (string, required) The address\n"
159 "\nremoveaddress 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n"
160 "\nRemove watch-only address 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n");
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());
173 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
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");
178 if (!pwalletMain->HaveWatchOnly(script))
179 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet does not contain this address or script");
181 LOCK2(cs_main, pwalletMain->cs_wallet);
183 pwalletMain->MarkDirty();
185 if (!pwalletMain->RemoveWatchOnly(script))
186 throw JSONRPCError(RPC_WALLET_ERROR, "Error removing address from wallet");
191 Value importwallet(const Array& params, bool fHelp)
193 if (fHelp || params.size() != 1)
195 "importwallet <filename>\n"
196 "Imports keys from a wallet dump file (see dumpwallet)."
197 + HelpRequiringPassphrase());
199 EnsureWalletIsUnlocked();
201 if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
202 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
207 Value dumpprivkey(const Array& params, bool fHelp)
209 if (fHelp || params.size() != 1)
211 "dumpprivkey <novacoinaddress>\n"
212 "Reveals the private key corresponding to <novacoinaddress>.");
214 EnsureWalletIsUnlocked();
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.");
223 if (!address.GetKeyID(keyID))
224 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
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();
232 Value dumpwallet(const Array& params, bool fHelp)
234 if (fHelp || params.size() != 1)
236 "dumpwallet <filename>\n"
237 "Dumps all wallet keys in a human-readable format."
238 + HelpRequiringPassphrase());
240 EnsureWalletIsUnlocked();
242 if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
243 throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");
248 Value dumpmalleablekey(const Array& params, bool fHelp)
250 if (fHelp || params.size() != 1)
251 throw runtime_error (
252 "dumpmalleablekey <Key view>\n"
253 "Dump the private and public key pairs, which correspond to provided key view.\n");
255 EnsureWalletIsUnlocked();
258 CMalleableKeyView keyView;
259 keyView.SetString(params[0].get_str());
261 if (!pwalletMain->GetMalleableKey(keyView, mKey))
262 throw runtime_error("There is no such item in the wallet");
265 result.push_back(Pair("PrivatePair", mKey.ToString()));
266 result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
271 Value importmalleablekey(const Array& params, bool fHelp)
273 if (fHelp || params.size() != 1)
274 throw runtime_error (
275 "importmalleablekey <Key data>\n"
276 "Imports the private key pair into your wallet.\n");
279 EnsureWalletIsUnlocked();
282 bool fSuccess = mKey.SetString(params[0].get_str());
288 fSuccess = pwalletMain->AddKey(mKey);
289 result.push_back(Pair("Successful", fSuccess));
290 result.push_back(Pair("Address", CBitcoinAddress(mKey.GetMalleablePubKey()).ToString()));
291 result.push_back(Pair("KeyView", CMalleableKeyView(mKey).ToString()));
294 result.push_back(Pair("Successful", false));