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() > 2)
39 "importprivkey <novacoinprivkey> [label]\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();
46 CBitcoinSecret vchSecret;
47 bool fGood = vchSecret.SetString(strSecret);
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.");
55 CSecret secret = vchSecret.GetSecret(fCompressed);
56 key.SetSecret(secret, fCompressed);
57 CKeyID vchAddress = key.GetPubKey().GetID();
59 LOCK2(cs_main, pwalletMain->cs_wallet);
61 pwalletMain->MarkDirty();
62 pwalletMain->SetAddressBookName(vchAddress, strLabel);
64 if (!pwalletMain->AddKey(key))
65 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
67 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
68 pwalletMain->ReacceptWalletTransactions();
74 Value importaddress(const Array& params, bool fHelp)
76 if (fHelp || params.size() < 1 || params.size() > 3)
78 "importaddress <address> [label] [rescan=true]\n"
79 "Adds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend.");
82 CBitcoinAddress address(params[0].get_str());
83 if (address.IsValid()) {
84 script.SetDestination(address.Get());
85 } else if (IsHex(params[0].get_str())) {
86 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
87 script = CScript(data.begin(), data.end());
89 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Novacoin address or script");
93 if (params.size() > 1)
94 strLabel = params[1].get_str();
96 // Whether to perform rescan after import
98 if (params.size() > 2)
99 fRescan = params[2].get_bool();
102 LOCK2(cs_main, pwalletMain->cs_wallet);
103 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
104 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
106 // Don't throw error in case an address is already there
107 if (pwalletMain->HaveWatchOnly(script))
110 pwalletMain->MarkDirty();
112 if (address.IsValid())
113 pwalletMain->SetAddressBookName(address.Get(), strLabel);
115 if (!pwalletMain->AddWatchOnly(script))
116 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
120 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
121 pwalletMain->ReacceptWalletTransactions();
128 Value removeaddress(const Array& params, bool fHelp)
130 if (fHelp || params.size() != 1)
132 "removeaddress 'address'\n"
133 "\nRemoves watch-only address or script (in hex) added by importaddress.\n"
135 "1. 'address' (string, required) The address\n"
137 "\nremoveaddress 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n"
138 "\nRemove watch-only address 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n");
142 CBitcoinAddress address(params[0].get_str());
143 if (address.IsValid()) {
144 script.SetDestination(address.Get());
145 } else if (IsHex(params[0].get_str())) {
146 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
147 script = CScript(data.begin(), data.end());
149 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
152 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
153 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet contains the private key for this address or script - can't remove it");
155 if (!pwalletMain->HaveWatchOnly(script))
156 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet does not contain this address or script");
158 LOCK2(cs_main, pwalletMain->cs_wallet);
160 pwalletMain->MarkDirty();
162 if (!pwalletMain->RemoveWatchOnly(script))
163 throw JSONRPCError(RPC_WALLET_ERROR, "Error removing address from wallet");
168 Value importwallet(const Array& params, bool fHelp)
170 if (fHelp || params.size() != 1)
172 "importwallet <filename>\n"
173 "Imports keys from a wallet dump file (see dumpwallet)."
174 + HelpRequiringPassphrase());
176 EnsureWalletIsUnlocked();
178 if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
179 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
184 Value dumpprivkey(const Array& params, bool fHelp)
186 if (fHelp || params.size() != 1)
188 "dumpprivkey <novacoinaddress>\n"
189 "Reveals the private key corresponding to <novacoinaddress>.");
191 EnsureWalletIsUnlocked();
193 string strAddress = params[0].get_str();
194 CBitcoinAddress address;
195 if (!address.SetString(strAddress))
196 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
197 if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
198 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
200 if (!address.GetKeyID(keyID))
201 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
204 if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
205 throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
206 return CBitcoinSecret(vchSecret, fCompressed).ToString();
209 Value dumpwallet(const Array& params, bool fHelp)
211 if (fHelp || params.size() != 1)
213 "dumpwallet <filename>\n"
214 "Dumps all wallet keys in a human-readable format."
215 + HelpRequiringPassphrase());
217 EnsureWalletIsUnlocked();
219 if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
220 throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");