Bugfix: Unspendable inputs handling
[novacoin.git] / src / rpcdump.cpp
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.
4
5 #include "init.h" // for pwalletMain
6 #include "bitcoinrpc.h"
7 #include "ui_interface.h"
8 #include "base58.h"
9
10 #define printf OutputDebugStringF
11
12 using namespace json_spirit;
13 using namespace std;
14
15 void EnsureWalletIsUnlocked();
16
17 class CTxDump
18 {
19 public:
20     CBlockIndex *pindex;
21     int64 nValue;
22     bool fSpent;
23     CWalletTx* ptx;
24     int nOut;
25     CTxDump(CWalletTx* ptx = NULL, int nOut = -1)
26     {
27         pindex = NULL;
28         nValue = 0;
29         fSpent = false;
30         this->ptx = ptx;
31         this->nOut = nOut;
32     }
33 };
34
35 Value importprivkey(const Array& params, bool fHelp)
36 {
37     if (fHelp || params.size() < 1 || params.size() > 2)
38         throw runtime_error(
39             "importprivkey <novacoinprivkey> [label]\n"
40             "Adds a private key (as returned by dumpprivkey) to your wallet.");
41
42     string strSecret = params[0].get_str();
43     string strLabel = "";
44     if (params.size() > 1)
45         strLabel = params[1].get_str();
46     CBitcoinSecret vchSecret;
47     bool fGood = vchSecret.SetString(strSecret);
48
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.");
52
53     CKey key;
54     bool fCompressed;
55     CSecret secret = vchSecret.GetSecret(fCompressed);
56     key.SetSecret(secret, fCompressed);
57     CKeyID vchAddress = key.GetPubKey().GetID();
58     {
59         LOCK2(cs_main, pwalletMain->cs_wallet);
60
61         pwalletMain->MarkDirty();
62         pwalletMain->SetAddressBookName(vchAddress, strLabel);
63
64         if (!pwalletMain->AddKey(key))
65             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
66
67         pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
68         pwalletMain->ReacceptWalletTransactions();
69     }
70
71     return Value::null;
72 }
73
74 Value importaddress(const Array& params, bool fHelp)
75 {
76     if (fHelp || params.size() < 1 || params.size() > 3)
77         throw runtime_error(
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.");
80
81     CScript script;
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());
88     } else {
89         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Novacoin address or script");
90     }
91
92     string strLabel = "";
93     if (params.size() > 1)
94         strLabel = params[1].get_str();
95
96     // Whether to perform rescan after import
97     bool fRescan = true;
98     if (params.size() > 2)
99         fRescan = params[2].get_bool();
100
101     {
102         LOCK2(cs_main, pwalletMain->cs_wallet);
103
104         // Don't throw error in case an address is already there
105         if (pwalletMain->HaveWatchOnly(script))
106             return Value::null;
107
108         pwalletMain->MarkDirty();
109
110         if (address.IsValid())
111             pwalletMain->SetAddressBookName(address.Get(), strLabel);
112
113         if (!pwalletMain->AddWatchOnly(script))
114             throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
115
116         if (fRescan)
117         {
118             pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
119             pwalletMain->ReacceptWalletTransactions();
120         }
121     }
122
123     return Value::null;
124 }
125
126 Value importwallet(const Array& params, bool fHelp)
127 {
128     if (fHelp || params.size() != 1)
129         throw runtime_error(
130             "importwallet <filename>\n"
131             "Imports keys from a wallet dump file (see dumpwallet)."
132             + HelpRequiringPassphrase());
133
134     EnsureWalletIsUnlocked();
135
136     if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
137        throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
138
139     return Value::null;
140 }
141
142 Value dumpprivkey(const Array& params, bool fHelp)
143 {
144     if (fHelp || params.size() != 1)
145         throw runtime_error(
146             "dumpprivkey <novacoinaddress>\n"
147             "Reveals the private key corresponding to <novacoinaddress>.");
148
149     EnsureWalletIsUnlocked();
150
151     string strAddress = params[0].get_str();
152     CBitcoinAddress address;
153     if (!address.SetString(strAddress))
154         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
155     if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
156         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
157     CKeyID keyID;
158     if (!address.GetKeyID(keyID))
159         throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
160     CSecret vchSecret;
161     bool fCompressed;
162     if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
163         throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
164     return CBitcoinSecret(vchSecret, fCompressed).ToString();
165 }
166
167 Value dumpwallet(const Array& params, bool fHelp)
168 {
169     if (fHelp || params.size() != 1)
170         throw runtime_error(
171             "dumpwallet <filename>\n"
172             "Dumps all wallet keys in a human-readable format."
173             + HelpRequiringPassphrase());
174
175     EnsureWalletIsUnlocked();
176
177     if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
178       throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");
179
180     return Value::null;
181 }