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 string strSecret = params[0].get_str();
44 if (params.size() > 1)
45 strLabel = params[1].get_str();
47 // Whether to perform rescan after import
49 if (params.size() > 2)
50 fRescan = params[2].get_bool();
52 CBitcoinSecret vchSecret;
53 bool fGood = vchSecret.SetString(strSecret);
55 if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
56 if (fWalletUnlockMintOnly) // ppcoin: no importprivkey in mint-only mode
57 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
61 CSecret secret = vchSecret.GetSecret(fCompressed);
62 key.SetSecret(secret, fCompressed);
63 CKeyID vchAddress = key.GetPubKey().GetID();
65 LOCK2(cs_main, pwalletMain->cs_wallet);
67 pwalletMain->MarkDirty();
68 pwalletMain->SetAddressBookName(vchAddress, strLabel);
70 // Don't throw error in case a key is already there
71 if (pwalletMain->HaveKey(vchAddress))
74 pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;
76 if (!pwalletMain->AddKey(key))
77 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
79 // whenever a key is imported, we need to scan the whole chain
80 pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
84 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
85 pwalletMain->ReacceptWalletTransactions();
92 Value importaddress(const Array& params, bool fHelp)
94 if (fHelp || params.size() < 1 || params.size() > 3)
96 "importaddress <address> [label] [rescan=true]\n"
97 "Adds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend.");
100 CBitcoinAddress address(params[0].get_str());
101 if (address.IsValid()) {
102 script.SetDestination(address.Get());
103 } else if (IsHex(params[0].get_str())) {
104 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
105 script = CScript(data.begin(), data.end());
107 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Novacoin address or script");
110 string strLabel = "";
111 if (params.size() > 1)
112 strLabel = params[1].get_str();
114 // Whether to perform rescan after import
116 if (params.size() > 2)
117 fRescan = params[2].get_bool();
120 LOCK2(cs_main, pwalletMain->cs_wallet);
121 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
122 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
124 // Don't throw error in case an address is already there
125 if (pwalletMain->HaveWatchOnly(script))
128 pwalletMain->MarkDirty();
130 if (address.IsValid())
131 pwalletMain->SetAddressBookName(address.Get(), strLabel);
133 if (!pwalletMain->AddWatchOnly(script))
134 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
138 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
139 pwalletMain->ReacceptWalletTransactions();
146 Value removeaddress(const Array& params, bool fHelp)
148 if (fHelp || params.size() != 1)
150 "removeaddress 'address'\n"
151 "\nRemoves watch-only address or script (in hex) added by importaddress.\n"
153 "1. 'address' (string, required) The address\n"
155 "\nremoveaddress 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n"
156 "\nRemove watch-only address 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n");
160 CBitcoinAddress address(params[0].get_str());
161 if (address.IsValid()) {
162 script.SetDestination(address.Get());
163 } else if (IsHex(params[0].get_str())) {
164 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
165 script = CScript(data.begin(), data.end());
167 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
170 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
171 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet contains the private key for this address or script - can't remove it");
173 if (!pwalletMain->HaveWatchOnly(script))
174 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet does not contain this address or script");
176 LOCK2(cs_main, pwalletMain->cs_wallet);
178 pwalletMain->MarkDirty();
180 if (!pwalletMain->RemoveWatchOnly(script))
181 throw JSONRPCError(RPC_WALLET_ERROR, "Error removing address from wallet");
186 Value importwallet(const Array& params, bool fHelp)
188 if (fHelp || params.size() != 1)
190 "importwallet <filename>\n"
191 "Imports keys from a wallet dump file (see dumpwallet)."
192 + HelpRequiringPassphrase());
194 EnsureWalletIsUnlocked();
196 if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
197 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
202 Value dumpprivkey(const Array& params, bool fHelp)
204 if (fHelp || params.size() != 1)
206 "dumpprivkey <novacoinaddress>\n"
207 "Reveals the private key corresponding to <novacoinaddress>.");
209 EnsureWalletIsUnlocked();
211 string strAddress = params[0].get_str();
212 CBitcoinAddress address;
213 if (!address.SetString(strAddress))
214 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
215 if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
216 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
218 if (!address.GetKeyID(keyID))
219 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
222 if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
223 throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
224 return CBitcoinSecret(vchSecret, fCompressed).ToString();
227 Value dumpwallet(const Array& params, bool fHelp)
229 if (fHelp || params.size() != 1)
231 "dumpwallet <filename>\n"
232 "Dumps all wallet keys in a human-readable format."
233 + HelpRequiringPassphrase());
235 EnsureWalletIsUnlocked();
237 if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
238 throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");