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 importwallet(const Array& params, bool fHelp)
76 if (fHelp || params.size() != 1)
78 "importwallet <filename>\n"
79 "Imports keys from a wallet dump file (see dumpwallet)."
80 + HelpRequiringPassphrase());
82 EnsureWalletIsUnlocked();
84 if(!ImportWallet(pwalletMain, params[0].get_str().c_str()))
85 throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys to wallet");
90 Value dumpprivkey(const Array& params, bool fHelp)
92 if (fHelp || params.size() != 1)
94 "dumpprivkey <novacoinaddress>\n"
95 "Reveals the private key corresponding to <novacoinaddress>.");
97 EnsureWalletIsUnlocked();
99 string strAddress = params[0].get_str();
100 CBitcoinAddress address;
101 if (!address.SetString(strAddress))
102 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
103 if (fWalletUnlockMintOnly) // ppcoin: no dumpprivkey in mint-only mode
104 throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is unlocked for minting only.");
106 if (!address.GetKeyID(keyID))
107 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
110 if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
111 throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
112 return CBitcoinSecret(vchSecret, fCompressed).ToString();
115 Value dumpwallet(const Array& params, bool fHelp)
117 if (fHelp || params.size() != 1)
119 "dumpwallet <filename>\n"
120 "Dumps all wallet keys in a human-readable format."
121 + HelpRequiringPassphrase());
123 EnsureWalletIsUnlocked();
125 if(!DumpWallet(pwalletMain, params[0].get_str().c_str() ))
126 throw JSONRPCError(RPC_WALLET_ERROR, "Error dumping wallet keys to file");