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 pwalletMain->MarkDirty();
60 pwalletMain->SetAddressBookName(vchAddress, strLabel);
62 if (!pwalletMain->AddKey(key))
63 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
65 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
66 pwalletMain->ReacceptWalletTransactions();
72 Value importaddress(const Array& params, bool fHelp)
74 if (fHelp || params.size() < 1 || params.size() > 3)
76 "importaddress <address> [label] [rescan=true]\n"
77 "Adds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend.");
80 CBitcoinAddress address(params[0].get_str());
81 if (address.IsValid()) {
82 script.SetDestination(address.Get());
83 } else if (IsHex(params[0].get_str())) {
84 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
85 script = CScript(data.begin(), data.end());
87 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Novacoin address or script");
91 if (params.size() > 1)
92 strLabel = params[1].get_str();
94 // Whether to perform rescan after import
96 if (params.size() > 2)
97 fRescan = params[2].get_bool();
100 LOCK2(cs_main, pwalletMain->cs_wallet);
101 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
102 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
104 // Don't throw error in case an address is already there
105 if (pwalletMain->HaveWatchOnly(script))
108 pwalletMain->MarkDirty();
110 if (address.IsValid())
111 pwalletMain->SetAddressBookName(address.Get(), strLabel);
113 if (!pwalletMain->AddWatchOnly(script))
114 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
118 pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
119 pwalletMain->ReacceptWalletTransactions();
126 Value removeaddress(const Array& params, bool fHelp)
128 if (fHelp || params.size() != 1)
130 "removeaddress 'address'\n"
131 "\nRemoves watch-only address or script (in hex) added by importaddress.\n"
133 "1. 'address' (string, required) The address\n"
135 "\nremoveaddress 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n"
136 "\nRemove watch-only address 4EqHMPgEAf56CQmU6ZWS8Ug4d7N3gsQVQA\n");
140 CBitcoinAddress address(params[0].get_str());
141 if (address.IsValid()) {
142 script.SetDestination(address.Get());
143 } else if (IsHex(params[0].get_str())) {
144 std::vector<unsigned char> data(ParseHex(params[0].get_str()));
145 script = CScript(data.begin(), data.end());
147 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
150 if (::IsMine(*pwalletMain, script) == MINE_SPENDABLE)
151 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet contains the private key for this address or script - can't remove it");
153 if (!pwalletMain->HaveWatchOnly(script))
154 throw JSONRPCError(RPC_WALLET_ERROR, "The wallet does not contain this address or script");
156 LOCK2(cs_main, pwalletMain->cs_wallet);
158 pwalletMain->MarkDirty();
160 if (!pwalletMain->RemoveWatchOnly(script))
161 throw JSONRPCError(RPC_WALLET_ERROR, "Error removing address from wallet");
166 Value importwallet(const Array& params, bool fHelp)
168 if (fHelp || params.size() != 1)
170 "importwallet <filename>\n"
171 "Imports keys from a wallet dump file (see dumpwallet)."
172 + HelpRequiringPassphrase());
174 EnsureWalletIsUnlocked();
176 if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
177 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
182 Value dumpprivkey(const Array& params, bool fHelp)
184 if (fHelp || params.size() != 1)
186 "dumpprivkey <novacoinaddress>\n"
187 "Reveals the private key corresponding to <novacoinaddress>.");
189 EnsureWalletIsUnlocked();
191 string strAddress = params[0].get_str();
192 CBitcoinAddress address;
193 if (!address.SetString(strAddress))
194 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
195 if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
196 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
198 if (!address.GetKeyID(keyID))
199 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
202 if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
203 throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
204 return CBitcoinSecret(vchSecret, fCompressed).ToString();
207 Value dumpwallet(const Array& params, bool fHelp)
209 if (fHelp || params.size() != 1)
211 "dumpwallet <filename>\n"
212 "Dumps all wallet keys in a human-readable format."
213 + HelpRequiringPassphrase());
215 EnsureWalletIsUnlocked();
217 if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
218 throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");